37
37
headerRE = re .compile (r'^(From |[\041-\071\073-\176]*:|[\t ])' )
38
38
EMPTYSTRING = ''
39
39
NL = '\n '
40
+ boundaryendRE = re .compile (
41
+ r'(?P<end>--)?(?P<ws>[ \t]*)(?P<linesep>\r\n|\r|\n)?$' )
40
42
41
43
NeedMoreData = object ()
42
44
@@ -327,9 +329,10 @@ def _parsegen(self):
327
329
# this onto the input stream until we've scanned past the
328
330
# preamble.
329
331
separator = '--' + boundary
330
- boundaryre = re .compile (
331
- '(?P<sep>' + re .escape (separator ) +
332
- r')(?P<end>--)?(?P<ws>[ \t]*)(?P<linesep>\r\n|\r|\n)?$' )
332
+ def boundarymatch (line ):
333
+ if not line .startswith (separator ):
334
+ return None
335
+ return boundaryendRE .match (line , len (separator ))
333
336
capturing_preamble = True
334
337
preamble = []
335
338
linesep = False
@@ -341,7 +344,7 @@ def _parsegen(self):
341
344
continue
342
345
if line == '' :
343
346
break
344
- mo = boundaryre . match (line )
347
+ mo = boundarymatch (line )
345
348
if mo :
346
349
# If we're looking at the end boundary, we're done with
347
350
# this multipart. If there was a newline at the end of
@@ -373,13 +376,13 @@ def _parsegen(self):
373
376
if line is NeedMoreData :
374
377
yield NeedMoreData
375
378
continue
376
- mo = boundaryre . match (line )
379
+ mo = boundarymatch (line )
377
380
if not mo :
378
381
self ._input .unreadline (line )
379
382
break
380
383
# Recurse to parse this subpart; the input stream points
381
384
# at the subpart's first line.
382
- self ._input .push_eof_matcher (boundaryre . match )
385
+ self ._input .push_eof_matcher (boundarymatch )
383
386
for retval in self ._parsegen ():
384
387
if retval is NeedMoreData :
385
388
yield NeedMoreData
0 commit comments