Skip to content

Commit 6d1e4ba

Browse files
committed
Fix file like objects without seek support not being readable
1 parent 616bf26 commit 6d1e4ba

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

shapefile.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import array
1919
import tempfile
2020
import itertools
21+
import io
2122
from datetime import date
2223

2324
#
@@ -239,23 +240,35 @@ def __init__(self, *args, **kwargs):
239240
if "shp" in kwargs.keys():
240241
if hasattr(kwargs["shp"], "read"):
241242
self.shp = kwargs["shp"]
242-
if hasattr(self.shp, "seek"):
243+
# Copy if required
244+
try:
243245
self.shp.seek(0)
246+
except (NameError, io.UnsupportedOperation):
247+
self.shp = io.BytesIO(self.shp.read())
244248
if "shx" in kwargs.keys():
245249
if hasattr(kwargs["shx"], "read"):
246250
self.shx = kwargs["shx"]
247-
if hasattr(self.shx, "seek"):
251+
# Copy if required
252+
try:
248253
self.shx.seek(0)
254+
except (NameError, io.UnsupportedOperation):
255+
self.shx = io.BytesIO(self.shx.read())
249256
if "dbf" in kwargs.keys():
250257
if hasattr(kwargs["dbf"], "read"):
251258
self.dbf = kwargs["dbf"]
252-
if hasattr(self.dbf, "seek"):
259+
# Copy if required
260+
try:
253261
self.dbf.seek(0)
262+
except (NameError, io.UnsupportedOperation):
263+
self.dbf = io.BytesIO(self.dbf.read())
254264
if self.shp or self.dbf:
255265
self.load()
256266
else:
257267
raise ShapefileException("Shapefile Reader requires a shapefile or file-like object.")
258268

269+
270+
271+
259272
def load(self, shapefile=None):
260273
"""Opens a shapefile from a filename or file-like
261274
object. Normally this method would be called by the

0 commit comments

Comments
 (0)