File Lib/sre_parse.py changed (mode: 100644) (index ab37fd3fe2..d8d1bd552f) |
... |
... |
def _parse_sub(source, state, verbose, nested=True): |
412 |
412 |
sourcematch = source.match |
sourcematch = source.match |
413 |
413 |
start = source.tell() |
start = source.tell() |
414 |
414 |
while True: |
while True: |
415 |
|
itemsappend(_parse(source, state, verbose)) |
|
|
415 |
|
itemsappend(_parse(source, state, verbose, not nested and not items)) |
416 |
416 |
if not sourcematch("|"): |
if not sourcematch("|"): |
417 |
417 |
break |
break |
418 |
418 |
|
|
|
... |
... |
def _parse_sub_cond(source, state, condgroup, verbose): |
466 |
466 |
subpattern.append((GROUPREF_EXISTS, (condgroup, item_yes, item_no))) |
subpattern.append((GROUPREF_EXISTS, (condgroup, item_yes, item_no))) |
467 |
467 |
return subpattern |
return subpattern |
468 |
468 |
|
|
469 |
|
def _parse(source, state, verbose): |
|
|
469 |
|
def _parse(source, state, verbose, first=False): |
470 |
470 |
# parse a simple pattern |
# parse a simple pattern |
471 |
471 |
subpattern = SubPattern(state) |
subpattern = SubPattern(state) |
472 |
472 |
|
|
|
... |
... |
def _parse(source, state, verbose): |
730 |
730 |
state.checklookbehindgroup(condgroup, source) |
state.checklookbehindgroup(condgroup, source) |
731 |
731 |
elif char in FLAGS or char == "-": |
elif char in FLAGS or char == "-": |
732 |
732 |
# flags |
# flags |
733 |
|
pos = source.pos |
|
734 |
733 |
flags = _parse_flags(source, state, char) |
flags = _parse_flags(source, state, char) |
735 |
734 |
if flags is None: # global flags |
if flags is None: # global flags |
736 |
|
if pos != 3: # "(?x" |
|
|
735 |
|
if not first or subpattern: |
737 |
736 |
import warnings |
import warnings |
738 |
737 |
warnings.warn( |
warnings.warn( |
739 |
738 |
'Flags not at the start of the expression %s%s' % ( |
'Flags not at the start of the expression %s%s' % ( |
|
... |
... |
def _parse(source, state, verbose): |
742 |
741 |
), |
), |
743 |
742 |
DeprecationWarning, stacklevel=7 |
DeprecationWarning, stacklevel=7 |
744 |
743 |
) |
) |
|
744 |
|
if (state.flags & SRE_FLAG_VERBOSE) and not verbose: |
|
745 |
|
raise Verbose |
745 |
746 |
continue |
continue |
746 |
747 |
add_flags, del_flags = flags |
add_flags, del_flags = flags |
747 |
748 |
group = None |
group = None |
|
... |
... |
def _parse_flags(source, state, char): |
795 |
796 |
msg = "unknown flag" if char.isalpha() else "missing -, : or )" |
msg = "unknown flag" if char.isalpha() else "missing -, : or )" |
796 |
797 |
raise source.error(msg, len(char)) |
raise source.error(msg, len(char)) |
797 |
798 |
if char == ")": |
if char == ")": |
798 |
|
if ((add_flags & SRE_FLAG_VERBOSE) and |
|
799 |
|
not (state.flags & SRE_FLAG_VERBOSE)): |
|
800 |
|
raise Verbose |
|
801 |
799 |
state.flags |= add_flags |
state.flags |= add_flags |
802 |
800 |
return None |
return None |
803 |
801 |
if add_flags & GLOBAL_FLAGS: |
if add_flags & GLOBAL_FLAGS: |
File Lib/test/test_re.py changed (mode: 100644) (index 3129f7e988..4d71eea517) |
... |
... |
class ReTests(unittest.TestCase): |
1325 |
1325 |
upper_char = '\u1ea0' # Latin Capital Letter A with Dot Below |
upper_char = '\u1ea0' # Latin Capital Letter A with Dot Below |
1326 |
1326 |
lower_char = '\u1ea1' # Latin Small Letter A with Dot Below |
lower_char = '\u1ea1' # Latin Small Letter A with Dot Below |
1327 |
1327 |
|
|
1328 |
|
p = re.compile(upper_char, re.I | re.U) |
|
1329 |
|
q = p.match(lower_char) |
|
|
1328 |
|
p = re.compile('.' + upper_char, re.I | re.S) |
|
1329 |
|
q = p.match('\n' + lower_char) |
1330 |
1330 |
self.assertTrue(q) |
self.assertTrue(q) |
1331 |
1331 |
|
|
1332 |
|
p = re.compile(lower_char, re.I | re.U) |
|
1333 |
|
q = p.match(upper_char) |
|
|
1332 |
|
p = re.compile('.' + lower_char, re.I | re.S) |
|
1333 |
|
q = p.match('\n' + upper_char) |
1334 |
1334 |
self.assertTrue(q) |
self.assertTrue(q) |
1335 |
1335 |
|
|
1336 |
|
p = re.compile('(?i)' + upper_char, re.U) |
|
1337 |
|
q = p.match(lower_char) |
|
|
1336 |
|
p = re.compile('(?i).' + upper_char, re.S) |
|
1337 |
|
q = p.match('\n' + lower_char) |
1338 |
1338 |
self.assertTrue(q) |
self.assertTrue(q) |
1339 |
1339 |
|
|
1340 |
|
p = re.compile('(?i)' + lower_char, re.U) |
|
1341 |
|
q = p.match(upper_char) |
|
|
1340 |
|
p = re.compile('(?i).' + lower_char, re.S) |
|
1341 |
|
q = p.match('\n' + upper_char) |
1342 |
1342 |
self.assertTrue(q) |
self.assertTrue(q) |
1343 |
1343 |
|
|
1344 |
|
p = re.compile('(?iu)' + upper_char) |
|
1345 |
|
q = p.match(lower_char) |
|
|
1344 |
|
p = re.compile('(?is).' + upper_char) |
|
1345 |
|
q = p.match('\n' + lower_char) |
1346 |
1346 |
self.assertTrue(q) |
self.assertTrue(q) |
1347 |
1347 |
|
|
1348 |
|
p = re.compile('(?iu)' + lower_char) |
|
1349 |
|
q = p.match(upper_char) |
|
|
1348 |
|
p = re.compile('(?is).' + lower_char) |
|
1349 |
|
q = p.match('\n' + upper_char) |
1350 |
1350 |
self.assertTrue(q) |
self.assertTrue(q) |
1351 |
1351 |
|
|
1352 |
|
self.assertTrue(re.match('(?ixu) ' + upper_char, lower_char)) |
|
1353 |
|
self.assertTrue(re.match('(?ixu) ' + lower_char, upper_char)) |
|
|
1352 |
|
p = re.compile('(?s)(?i).' + upper_char) |
|
1353 |
|
q = p.match('\n' + lower_char) |
|
1354 |
|
self.assertTrue(q) |
|
1355 |
|
|
|
1356 |
|
p = re.compile('(?s)(?i).' + lower_char) |
|
1357 |
|
q = p.match('\n' + upper_char) |
|
1358 |
|
self.assertTrue(q) |
|
1359 |
|
|
|
1360 |
|
self.assertTrue(re.match('(?ix) ' + upper_char, lower_char)) |
|
1361 |
|
self.assertTrue(re.match('(?ix) ' + lower_char, upper_char)) |
|
1362 |
|
self.assertTrue(re.match(' (?i) ' + upper_char, lower_char, re.X)) |
|
1363 |
|
self.assertTrue(re.match('(?x) (?i) ' + upper_char, lower_char)) |
|
1364 |
|
self.assertTrue(re.match(' (?x) (?i) ' + upper_char, lower_char, re.X)) |
1354 |
1365 |
|
|
1355 |
1366 |
p = upper_char + '(?i)' |
p = upper_char + '(?i)' |
1356 |
1367 |
with self.assertWarns(DeprecationWarning) as warns: |
with self.assertWarns(DeprecationWarning) as warns: |
|
... |
... |
class ReTests(unittest.TestCase): |
1368 |
1379 |
'Flags not at the start of the expression %s (truncated)' % p[:20] |
'Flags not at the start of the expression %s (truncated)' % p[:20] |
1369 |
1380 |
) |
) |
1370 |
1381 |
|
|
|
1382 |
|
with self.assertWarns(DeprecationWarning): |
|
1383 |
|
self.assertTrue(re.match('(?s).(?i)' + upper_char, '\n' + lower_char)) |
|
1384 |
|
with self.assertWarns(DeprecationWarning): |
|
1385 |
|
self.assertTrue(re.match('(?i) ' + upper_char + ' (?x)', lower_char)) |
|
1386 |
|
with self.assertWarns(DeprecationWarning): |
|
1387 |
|
self.assertTrue(re.match(' (?x) (?i) ' + upper_char, lower_char)) |
|
1388 |
|
with self.assertWarns(DeprecationWarning): |
|
1389 |
|
self.assertTrue(re.match('^(?i)' + upper_char, lower_char)) |
|
1390 |
|
with self.assertWarns(DeprecationWarning): |
|
1391 |
|
self.assertTrue(re.match('$|(?i)' + upper_char, lower_char)) |
|
1392 |
|
with self.assertWarns(DeprecationWarning): |
|
1393 |
|
self.assertTrue(re.match('(?:(?i)' + upper_char + ')', lower_char)) |
|
1394 |
|
with self.assertWarns(DeprecationWarning): |
|
1395 |
|
self.assertTrue(re.fullmatch('(^)?(?(1)(?i)' + upper_char + ')', |
|
1396 |
|
lower_char)) |
|
1397 |
|
with self.assertWarns(DeprecationWarning): |
|
1398 |
|
self.assertTrue(re.fullmatch('($)?(?(1)|(?i)' + upper_char + ')', |
|
1399 |
|
lower_char)) |
|
1400 |
|
|
|
1401 |
|
|
1371 |
1402 |
def test_dollar_matches_twice(self): |
def test_dollar_matches_twice(self): |
1372 |
1403 |
"$ matches the end of string, and just before the terminating \n" |
"$ matches the end of string, and just before the terminating \n" |
1373 |
1404 |
pattern = re.compile('$') |
pattern = re.compile('$') |