Skip to content

Add pickling support for textparser.ParseError #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ jobs:
strategy:
max-parallel: 4
matrix:
python-version: [3.6, 3.9]
python-version: [3.8, 3.13]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Test
Expand All @@ -28,11 +28,11 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v1
- name: Set up Python 3.9
uses: actions/setup-python@v1
uses: actions/checkout@v4
- name: Set up Python 3.13
uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: 3.13
- name: Install pypa/build
run: |
python -m pip install build --user
Expand Down
16 changes: 16 additions & 0 deletions tests/test_textparser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pickle
import unittest
from collections import namedtuple

Expand Down Expand Up @@ -1115,6 +1116,21 @@ def grammar(self):
'}',
';'])

def test_error_picklable(self):
class Parser(textparser.Parser):

def grammar(self):
return Sequence('__EOF__')

try:
Parser().parse('123', match_sof=True)
except Exception as exc:
self.assertIsInstance(exc, textparser.ParseError)
pickled = pickle.dumps(exc)
unpickled = pickle.loads(pickled)
self.assertIsInstance(unpickled, textparser.ParseError)
self.assertEqual(repr(unpickled), repr(exc))


if __name__ == '__main__':
unittest.main()
4 changes: 4 additions & 0 deletions textparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ def column(self):

return self._column

def __reduce__(self):
"""Adds pickling support."""
return type(self), (self._text, self._offset), {}


Token = namedtuple('Token', ['kind', 'value', 'offset'])

Expand Down