![](https://secure.gravatar.com/avatar/cc7737cd64a84f1b5c61a160798e97ee.jpg?s=120&d=mm&r=g)
https://github.com/python/cpython/commit/c57e6e2e52d5d8b4005753bed789d99ebe4... commit: c57e6e2e52d5d8b4005753bed789d99ebe407fb6 branch: master author: Anthony Sottile <asottile@umich.edu> committer: Serhiy Storchaka <storchaka@gmail.com> date: 2018-11-27T20:39:49+02:00 summary: bpo-35312: Make lib2to3.pgen2.parse.ParseError round-trip pickle-able. (GH-10710) files: A Misc/NEWS.d/next/Library/2018-11-25-20-05-33.bpo-35312.wbw0zO.rst M Lib/lib2to3/pgen2/parse.py M Lib/lib2to3/tests/test_parser.py diff --git a/Lib/lib2to3/pgen2/parse.py b/Lib/lib2to3/pgen2/parse.py index 6bebdbba7e52..cf3fcf7e99fd 100644 --- a/Lib/lib2to3/pgen2/parse.py +++ b/Lib/lib2to3/pgen2/parse.py @@ -24,6 +24,9 @@ def __init__(self, msg, type, value, context): self.value = value self.context = context + def __reduce__(self): + return type(self), (self.msg, self.type, self.value, self.context) + class Parser(object): """Parser engine. diff --git a/Lib/lib2to3/tests/test_parser.py b/Lib/lib2to3/tests/test_parser.py index 829e5a72924a..01b2b51e4ab6 100644 --- a/Lib/lib2to3/tests/test_parser.py +++ b/Lib/lib2to3/tests/test_parser.py @@ -622,6 +622,18 @@ def test_multiline_str_literals(self): self.validate(s) +class TestPickleableException(unittest.TestCase): + def test_ParseError(self): + err = ParseError('msg', 2, None, (1, 'context')) + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + err2 = pickle.loads(pickle.dumps(err, protocol=proto)) + self.assertEqual(err.args, err2.args) + self.assertEqual(err.msg, err2.msg) + self.assertEqual(err.type, err2.type) + self.assertEqual(err.value, err2.value) + self.assertEqual(err.context, err2.context) + + def diff_texts(a, b, filename): a = a.splitlines() b = b.splitlines() diff --git a/Misc/NEWS.d/next/Library/2018-11-25-20-05-33.bpo-35312.wbw0zO.rst b/Misc/NEWS.d/next/Library/2018-11-25-20-05-33.bpo-35312.wbw0zO.rst new file mode 100644 index 000000000000..c195468b9e27 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-11-25-20-05-33.bpo-35312.wbw0zO.rst @@ -0,0 +1 @@ +Make ``lib2to3.pgen2.parse.ParseError`` round-trip pickle-able. Patch by Anthony Sottile.