Skip to content

Commit f8abdf2

Browse files
committed
Catch Type and ValueErrors only when forming date
1 parent 87d97eb commit f8abdf2

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/shapefile.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ class GeoJsonShapeT(TypedDict):
119119
MISSING = [None, ""]
120120
NODATA = -10e38 # as per the ESRI shapefile spec, only used for m-values.
121121

122+
unpack_2_int32_be = Struct(">2i").unpack
123+
122124

123125
def b(
124126
v: Union[str, bytes], encoding: str = "utf-8", encodingErrors: str = "strict"
@@ -1078,7 +1080,7 @@ def __init__(
10781080
# Close and delete the temporary zipfile
10791081
try:
10801082
zipfileobj.close()
1081-
except: # pylint disable=broad-exception-caught
1083+
except: # pylint: disable=broad-exception-caught
10821084
pass
10831085
# Try to load shapefile
10841086
if self.shp or self.dbf:
@@ -1223,7 +1225,6 @@ def __len__(self):
12231225
shpLength = shp.tell()
12241226
shp.seek(100)
12251227
# Do a fast shape iteration until end of file.
1226-
unpack_2_int32_be = Struct(">2i").unpack
12271228
offsets = []
12281229
pos = shp.tell()
12291230
while pos < shpLength:
@@ -1414,7 +1415,7 @@ def __shape(self, oid=None, bbox=None):
14141415
nParts = nPoints = zmin = zmax = mmin = mmax = None
14151416
(recNum, recLength) = unpack(">2i", f.read(8))
14161417
# Determine the start of the next record
1417-
next = f.tell() + (2 * recLength)
1418+
next_shape = f.tell() + (2 * recLength)
14181419
shapeType = unpack("<i", f.read(4))[0]
14191420
record.shapeType = shapeType
14201421
# For Null shapes create an empty points list for consistency
@@ -1427,7 +1428,7 @@ def __shape(self, oid=None, bbox=None):
14271428
if bbox is not None and not bbox_overlap(bbox, record.bbox):
14281429
# because we stop parsing this shape, skip to beginning of
14291430
# next shape before we return
1430-
f.seek(next)
1431+
f.seek(next_shape)
14311432
return None
14321433
# Shape types with parts
14331434
if shapeType in (3, 5, 13, 15, 23, 25, 31):
@@ -1451,10 +1452,10 @@ def __shape(self, oid=None, bbox=None):
14511452
record.z = _Array("d", unpack(f"<{nPoints}d", f.read(nPoints * 8)))
14521453
# Read m extremes and values
14531454
if shapeType in (13, 15, 18, 23, 25, 28, 31):
1454-
if next - f.tell() >= 16:
1455+
if next_shape - f.tell() >= 16:
14551456
(mmin, mmax) = unpack("<2d", f.read(16))
14561457
# Measure values less than -10e38 are nodata values according to the spec
1457-
if next - f.tell() >= nPoints * 8:
1458+
if next_shape - f.tell() >= nPoints * 8:
14581459
record.m = []
14591460
for m in _Array("d", unpack(f"<{nPoints}d", f.read(nPoints * 8))):
14601461
if m > NODATA:
@@ -1471,14 +1472,14 @@ def __shape(self, oid=None, bbox=None):
14711472
point_bbox = list(record.points[0] + record.points[0])
14721473
# skip shape if no overlap with bounding box
14731474
if not bbox_overlap(bbox, point_bbox):
1474-
f.seek(next)
1475+
f.seek(next_shape)
14751476
return None
14761477
# Read a single Z value
14771478
if shapeType == 11:
14781479
record.z = list(unpack("<d", f.read(8)))
14791480
# Read a single M value
14801481
if shapeType in (21, 11):
1481-
if next - f.tell() >= 8:
1482+
if next_shape - f.tell() >= 8:
14821483
(m,) = unpack("<d", f.read(8))
14831484
else:
14841485
m = NODATA
@@ -1491,7 +1492,7 @@ def __shape(self, oid=None, bbox=None):
14911492
# Seek to the end of this record as defined by the record header because
14921493
# the shapefile spec doesn't require the actual content to meet the header
14931494
# definition. Probably allowed for lazy feature deletion.
1494-
f.seek(next)
1495+
f.seek(next_shape)
14951496
return record
14961497

14971498
def __shxHeader(self):
@@ -1549,15 +1550,14 @@ def shape(self, i=0, bbox=None):
15491550
shpLength = shp.tell()
15501551
shp.seek(100)
15511552
# Do a fast shape iteration until the requested index or end of file.
1552-
unpack = Struct(">2i").unpack
15531553
_i = 0
15541554
offset = shp.tell()
15551555
while offset < shpLength:
15561556
if _i == i:
15571557
# Reached the requested index, exit loop with the offset value
15581558
break
15591559
# Unpack the shape header only
1560-
(recNum, recLength) = unpack(shp.read(8))
1560+
(recNum, recLength) = unpack_2_int32_be(shp.read(8))
15611561
# Jump to next shape position
15621562
offset += 8 + (2 * recLength)
15631563
shp.seek(offset)
@@ -1804,7 +1804,7 @@ def __record(
18041804
# return as python date object
18051805
y, m, d = int(value[:4]), int(value[4:6]), int(value[6:8])
18061806
value = date(y, m, d)
1807-
except:
1807+
except (TypeError, ValueError):
18081808
# if invalid date, just return as unicode string so user can decide
18091809
value = u(value.strip())
18101810
elif typ == "L":

0 commit comments

Comments
 (0)