From d82d37e098efd41dc094d54995c21b3cd7adc06d Mon Sep 17 00:00:00 2001 From: JamesParrott <80779630+JamesParrott@users.noreply.github.com> Date: Fri, 23 Sep 2022 16:50:34 +0100 Subject: [PATCH] call .close() on shapefile.Writer objects properly. Don't just rely on del ing them. Fixes https://github.com/fiftysevendegreesofrad/sdna_open/issues/11 File writer objects were being relied on to close themselves. In Iron Python relying on del to call .close() or some other error meant Shapefile headers were not being written properly - this should happen in the __exit__ of the context manager but can be called in .close() if preferred. del is known to result in wierd behaviour in cPython, and it's certainly no better in Iron Python, so ought not be relied on to call .close() The curious number 960051513 that made its way into the shapefile type in the header, is 39393939 in base 16. 39 happens to be b'9'.hex(). In shapefile.Writer.__init__ long before the header is constructed properly (as the length of the file is not yet known) is the following line of code: if self.shp: self.shp.write(b'9'*100) This just reserves the 100 bytes of space required at the start of the file according to the ESRI shapefile spec. --- arcscripts/sdna_environment.py | 1 + 1 file changed, 1 insertion(+) diff --git a/arcscripts/sdna_environment.py b/arcscripts/sdna_environment.py index 04d572c..2694ee0 100644 --- a/arcscripts/sdna_environment.py +++ b/arcscripts/sdna_environment.py @@ -545,6 +545,7 @@ def AddRowGeomItem(self,geomitem): def Close(self): self.env.SetProgressorPosition(self.numitems) + self.writer.close() del self.writer class SdnaArcpyEnvironment(SdnaEnvironment):