Skip to content

Instantly share code, notes, and snippets.

@bdbaddog
Created November 27, 2022 03:38
Show Gist options
  • Save bdbaddog/ef6d5fdd1d6b2ede49d9bea86b6ac045 to your computer and use it in GitHub Desktop.
Save bdbaddog/ef6d5fdd1d6b2ede49d9bea86b6ac045 to your computer and use it in GitHub Desktop.
import re
x1 = """\
Traceback (most recent call last):
File "<stdin>", line 9, in <module>
File "/home/mats/github/scons/testing/framework/TestCommon.py", line 753, in run
super().run(**kw)
File "/home/mats/github/scons/testing/framework/TestCmd.py", line 1653, in run
p = self.start(program=program,
File "/home/mats/github/scons/testing/framework/TestCommon.py", line 696, in start
raise e
File "/home/mats/github/scons/testing/framework/TestCommon.py", line 679, in start
return super().start(program, interpreter, arguments,
File "<stdin>", line 4, in raise_exception
TypeError: forced TypeError
"""
x2 = """\
Traceback (most recent call last):
File "<stdin>", line 9, in <module>
File "/home/mats/github/scons/testing/framework/TestCommon.py", line 753, in run
super().run(**kw)
File "/home/mats/github/scons/testing/framework/TestCmd.py", line 1653, in run
p = self.start(program=program,
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/mats/github/scons/testing/framework/TestCommon.py", line 696, in start
raise e
File "/home/mats/github/scons/testing/framework/TestCommon.py", line 679, in start
return super().start(program, interpreter, arguments,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<stdin>", line 4, in raise_exception
TypeError: forced TypeError
"""
# try to add an optional group to pick up the "underlining" (^^^) in x2,
# but still matches x1
expect_old = \
fr"""Traceback \(most recent call last\):
File "<stdin>", line \d+, in (\?|<module>)
File "[^"]+TestCommon.py", line \d+, in run
super\(\).run\(\*\*kw\)
File "[^"]+TestCmd.py", line \d+, in run
p = self.start\(program=program,
(?:.*)
File "[^"]+TestCommon.py", line \d+, in start
raise e
File "[^"]+TestCommon.py", line \d+, in start
return super\(\).start\(program, interpreter, arguments,
(?:.*) File "<stdin>", line \d+, in raise_exception
TypeError: forced TypeError
"""
expect = \
fr"""Traceback \(most recent call last\):
File \"<stdin>\", line \d+, in (\?|<module>)
File \"[^\"]+TestCommon.py\", line \d+, in run
super\(\).run\(\*\*kw\)
File \"[^\"]+TestCmd.py\", line \d+, in run
p = self.start\(program=program,
(?:\s*\^*\s)? File \"[^\"]+TestCommon.py\", line \d+, in start
raise e
File \"[^\"]+TestCommon.py\", line \d+, in start
return super\(\).start\(program, interpreter, arguments,
(?:\s*\^*\s)? File \"<stdin>\", line \d+, in raise_exception
TypeError: forced TypeError"""
rv1 = re.match(expect, x1)
if rv1:
print("pattern 1 matches")
else:
print("pattern 1 does not match")
rv2 = re.match(expect, x2)
if rv2:
print("pattern 2 matches")
else:
print("pattern 2 does not match")
@bdbaddog
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment