Skip to content

Commit eb109cc

Browse files
authored
Pass parse offsets via constructors (#268)
1 parent 78e29ab commit eb109cc

2 files changed

Lines changed: 17 additions & 40 deletions

File tree

python_multipart/exceptions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class ParseError(FormParserError):
77
parsing something.
88
"""
99

10-
#: This is the offset in the input data chunk (*NOT* the overall stream) in
11-
#: which the parse error occurred. It will be -1 if not specified.
12-
offset = -1
10+
def __init__(self, message: str, *, offset: int = -1) -> None:
11+
super().__init__(message)
12+
self.offset = offset
1313

1414

1515
class MultipartParseError(ParseError):

python_multipart/multipart.py

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -820,9 +820,7 @@ def _internal_write(self, data: bytes, length: int) -> int:
820820
if found_sep:
821821
# If we're parsing strictly, we disallow blank chunks.
822822
if strict_parsing:
823-
e = QuerystringParseError("Skipping duplicate ampersand/semicolon at %d" % i)
824-
e.offset = i
825-
raise e
823+
raise QuerystringParseError("Skipping duplicate ampersand/semicolon at %d" % i, offset=i)
826824
else:
827825
self.logger.debug("Skipping duplicate ampersand/semicolon at %d", i)
828826
else:
@@ -886,13 +884,12 @@ def _internal_write(self, data: bytes, length: int) -> int:
886884
# We're parsing strictly. If we find a separator,
887885
# this is an error - we require an equals sign.
888886
if sep_pos != -1:
889-
e = QuerystringParseError(
887+
raise QuerystringParseError(
890888
"When strict_parsing is True, we require an "
891889
"equals sign in all field chunks. Did not "
892-
"find one in the chunk that starts at %d" % (i,)
890+
"find one in the chunk that starts at %d" % (i,),
891+
offset=i,
893892
)
894-
e.offset = i
895-
raise e
896893

897894
# No separator in the rest of this chunk, so it's just
898895
# a field name.
@@ -927,9 +924,7 @@ def _internal_write(self, data: bytes, length: int) -> int:
927924
else: # pragma: no cover (error case)
928925
msg = "Reached an unknown state %d at %d" % (state, i)
929926
self.logger.warning(msg)
930-
e = QuerystringParseError(msg)
931-
e.offset = i
932-
raise e
927+
raise QuerystringParseError(msg, offset=i)
933928

934929
i += 1
935930

@@ -1131,19 +1126,15 @@ def data_callback(name: CallbackName, end_i: int, remaining: bool = False) -> No
11311126
# Error!
11321127
msg = "Did not find CR at end of boundary (%d)" % (i,)
11331128
self.logger.warning(msg)
1134-
e = MultipartParseError(msg)
1135-
e.offset = i
1136-
raise e
1129+
raise MultipartParseError(msg, offset=i)
11371130

11381131
index += 1
11391132

11401133
elif index == len(boundary) - 2 + 1:
11411134
if c != LF:
11421135
msg = "Did not find LF at end of boundary (%d)" % (i,)
11431136
self.logger.warning(msg)
1144-
e = MultipartParseError(msg)
1145-
e.offset = i
1146-
raise e
1137+
raise MultipartParseError(msg, offset=i)
11471138

11481139
# The index is now used for indexing into our boundary.
11491140
index = 0
@@ -1159,9 +1150,7 @@ def data_callback(name: CallbackName, end_i: int, remaining: bool = False) -> No
11591150
if c != boundary[index + 2]:
11601151
msg = "Expected boundary character %r, got %r at index %d" % (boundary[index + 2], c, index + 2)
11611152
self.logger.warning(msg)
1162-
e = MultipartParseError(msg)
1163-
e.offset = i
1164-
raise e
1153+
raise MultipartParseError(msg, offset=i)
11651154

11661155
# Increment index into boundary and continue.
11671156
index += 1
@@ -1204,9 +1193,7 @@ def data_callback(name: CallbackName, end_i: int, remaining: bool = False) -> No
12041193
if index == 1:
12051194
msg = "Found 0-length header at %d" % (i,)
12061195
self.logger.warning(msg)
1207-
e = MultipartParseError(msg)
1208-
e.offset = i
1209-
raise e
1196+
raise MultipartParseError(msg, offset=i)
12101197

12111198
# Call our callback with the header field.
12121199
data_callback("header_field", i)
@@ -1217,9 +1204,7 @@ def data_callback(name: CallbackName, end_i: int, remaining: bool = False) -> No
12171204
elif c not in TOKEN_CHARS_SET:
12181205
msg = "Found invalid character %r in header at %d" % (c, i)
12191206
self.logger.warning(msg)
1220-
e = MultipartParseError(msg)
1221-
e.offset = i
1222-
raise e
1207+
raise MultipartParseError(msg, offset=i)
12231208

12241209
elif state == MultipartState.HEADER_VALUE_START:
12251210
# Skip leading spaces.
@@ -1247,9 +1232,7 @@ def data_callback(name: CallbackName, end_i: int, remaining: bool = False) -> No
12471232
if c != LF:
12481233
msg = f"Did not find LF character at end of header (found {c!r})"
12491234
self.logger.warning(msg)
1250-
e = MultipartParseError(msg)
1251-
e.offset = i
1252-
raise e
1235+
raise MultipartParseError(msg, offset=i)
12531236

12541237
# Move back to the start of another header. Note that if that
12551238
# state detects ANOTHER newline, it'll trigger the end of our
@@ -1263,9 +1246,7 @@ def data_callback(name: CallbackName, end_i: int, remaining: bool = False) -> No
12631246
if c != LF:
12641247
msg = f"Did not find LF at end of headers (found {c!r})"
12651248
self.logger.warning(msg)
1266-
e = MultipartParseError(msg)
1267-
e.offset = i
1268-
raise e
1249+
raise MultipartParseError(msg, offset=i)
12691250

12701251
self.callback("headers_finished")
12711252
state = MultipartState.PART_DATA_START
@@ -1409,9 +1390,7 @@ def data_callback(name: CallbackName, end_i: int, remaining: bool = False) -> No
14091390
if c != HYPHEN:
14101391
msg = "Did not find - at end of boundary (%d)" % (i,)
14111392
self.logger.warning(msg)
1412-
e = MultipartParseError(msg)
1413-
e.offset = i
1414-
raise e
1393+
raise MultipartParseError(msg, offset=i)
14151394
index += 1
14161395
self.callback("end")
14171396
state = MultipartState.END
@@ -1426,9 +1405,7 @@ def data_callback(name: CallbackName, end_i: int, remaining: bool = False) -> No
14261405
# We got into a strange state somehow! Just stop processing.
14271406
msg = "Reached an unknown state %d at %d" % (state, i)
14281407
self.logger.warning(msg)
1429-
e = MultipartParseError(msg)
1430-
e.offset = i
1431-
raise e
1408+
raise MultipartParseError(msg, offset=i)
14321409

14331410
# Move to the next byte.
14341411
i += 1

0 commit comments

Comments
 (0)