Skip to content

Commit c5b5b4f

Browse files
authored
Merge pull request #96 from ulikoehler/ulikoehler-patch-3
Fix file like objects without seek support not being readable (without allow_copy)
2 parents b580449 + 2e33398 commit c5b5b4f

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
@@ -16,6 +16,7 @@
1616
import array
1717
import tempfile
1818
import itertools
19+
import io
1920
from datetime import date
2021

2122
#
@@ -238,23 +239,35 @@ def __init__(self, *args, **kwargs):
238239
if "shp" in kwargs.keys():
239240
if hasattr(kwargs["shp"], "read"):
240241
self.shp = kwargs["shp"]
241-
if hasattr(self.shp, "seek"):
242+
# Copy if required
243+
try:
242244
self.shp.seek(0)
245+
except (NameError, io.UnsupportedOperation):
246+
self.shp = io.BytesIO(self.shp.read())
243247
if "shx" in kwargs.keys():
244248
if hasattr(kwargs["shx"], "read"):
245249
self.shx = kwargs["shx"]
246-
if hasattr(self.shx, "seek"):
250+
# Copy if required
251+
try:
247252
self.shx.seek(0)
253+
except (NameError, io.UnsupportedOperation):
254+
self.shx = io.BytesIO(self.shx.read())
248255
if "dbf" in kwargs.keys():
249256
if hasattr(kwargs["dbf"], "read"):
250257
self.dbf = kwargs["dbf"]
251-
if hasattr(self.dbf, "seek"):
258+
# Copy if required
259+
try:
252260
self.dbf.seek(0)
261+
except (NameError, io.UnsupportedOperation):
262+
self.dbf = io.BytesIO(self.dbf.read())
253263
if self.shp or self.dbf:
254264
self.load()
255265
else:
256266
raise ShapefileException("Shapefile Reader requires a shapefile or file-like object.")
257267

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

0 commit comments

Comments
 (0)