Skip to content

Commit af1dee9

Browse files
committed
Fix errors when writing to individual file-handles, fix corresponding tests
Previous tests failed to detect the errors because the errors only happen when trying to add a record or shape; fixed. Files were not closed and thus did not flush the updated header data; fixed. Made sure shx records were only written if shx file was available.
1 parent ff51716 commit af1dee9

18 files changed

+111
-38
lines changed

shapefile.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,14 +1861,13 @@ def close(self):
18611861
if self.dbf and dbf_open:
18621862
self.__dbfHeader()
18631863

1864-
# Close files, if target is a filepath
1865-
if self.target:
1866-
for attribute in (self.shp, self.shx, self.dbf):
1867-
if hasattr(attribute, 'close'):
1868-
try:
1869-
attribute.close()
1870-
except IOError:
1871-
pass
1864+
# Close files
1865+
for attribute in (self.shp, self.shx, self.dbf):
1866+
if hasattr(attribute, 'close'):
1867+
try:
1868+
attribute.close()
1869+
except IOError:
1870+
pass
18721871

18731872
def __getFileObj(self, f):
18741873
"""Safety handler to verify file-like objects"""
@@ -2088,7 +2087,8 @@ def shape(self, s):
20882087
"not: %r" % s)
20892088
# Write to file
20902089
offset,length = self.__shpRecord(s)
2091-
self.__shxRecord(offset, length)
2090+
if self.shx:
2091+
self.__shxRecord(offset, length)
20922092

20932093
def __shpRecord(self, s):
20942094
f = self.__getFileObj(self.shp)

shapefiles/test/balancing.dbf

0 Bytes
Binary file not shown.

shapefiles/test/contextwriter.dbf

0 Bytes
Binary file not shown.

shapefiles/test/corrupt_too_long.dbf

0 Bytes
Binary file not shown.

shapefiles/test/dtype.dbf

0 Bytes
Binary file not shown.

shapefiles/test/edit.dbf

0 Bytes
Binary file not shown.

shapefiles/test/line.dbf

0 Bytes
Binary file not shown.

shapefiles/test/linem.dbf

0 Bytes
Binary file not shown.

shapefiles/test/linez.dbf

0 Bytes
Binary file not shown.

shapefiles/test/merge.dbf

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)