3737headerRE = re .compile (r'^(From |[\041-\071\073-\176]*:|[\t ])' )
3838EMPTYSTRING = ''
3939NL = '\n '
40+ boundaryendRE = re .compile (
41+ r'(?P<end>--)?(?P<ws>[ \t]*)(?P<linesep>\r\n|\r|\n)?$' )
4042
4143NeedMoreData = object ()
4244
@@ -327,9 +329,10 @@ def _parsegen(self):
327329 # this onto the input stream until we've scanned past the
328330 # preamble.
329331 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 ))
333336 capturing_preamble = True
334337 preamble = []
335338 linesep = False
@@ -341,7 +344,7 @@ def _parsegen(self):
341344 continue
342345 if line == '' :
343346 break
344- mo = boundaryre . match (line )
347+ mo = boundarymatch (line )
345348 if mo :
346349 # If we're looking at the end boundary, we're done with
347350 # this multipart. If there was a newline at the end of
@@ -373,13 +376,13 @@ def _parsegen(self):
373376 if line is NeedMoreData :
374377 yield NeedMoreData
375378 continue
376- mo = boundaryre . match (line )
379+ mo = boundarymatch (line )
377380 if not mo :
378381 self ._input .unreadline (line )
379382 break
380383 # Recurse to parse this subpart; the input stream points
381384 # at the subpart's first line.
382- self ._input .push_eof_matcher (boundaryre . match )
385+ self ._input .push_eof_matcher (boundarymatch )
383386 for retval in self ._parsegen ():
384387 if retval is NeedMoreData :
385388 yield NeedMoreData
0 commit comments