Skip to content

Fix recursion error when copying Record with deepcopy() #257

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

Merged
merged 1 commit into from
Jan 20, 2024
Merged

Fix recursion error when copying Record with deepcopy() #257

merged 1 commit into from
Jan 20, 2024

Conversation

midichef
Copy link
Contributor

This commit fixes the following error that happens when I try to do copy.deepcopy() on a Record:

Traceback (most recent call last):
  File "/home/midichef/pyshp/test.py", line 12, in <module>
    records_deepcopy = copy.deepcopy(records)
...
  File "/usr/lib/python3.8/copy.py", line 271, in _reconstruct
    if hasattr(y, '__setstate__'):
  File "/home/midichef/.local/lib/python3.8/site-packages/shapefile.py", line 735, in __getattr__
    index = self.__field_positions[item]
...
  [Previous line repeated 991 more times]
RecursionError: maximum recursion depth exceeded

The behavior can be tested with this script:

#!/usr/bin/python3

import shapefile
import copy
import pickle

shapefile_path = "shapefiles/blockgroups"
sf = shapefile.Reader(shapefile_path)
records = sf.records()

records_deepcopy = copy.deepcopy(records)
assert records_deepcopy == records

records_pickle = pickle.dumps(records)
records_unpickled = pickle.loads(records_pickle)
assert records_unpickled == records

For a good explanation of how this fix works, see this Stack Overflow comment.
This commit also fixes the error previously reported when trying to unpickle records (#238).

@GeospatialPython GeospatialPython merged commit 66ae197 into GeospatialPython:master Jan 20, 2024
@midichef midichef deleted the deepcopy branch January 23, 2024 08:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants