Skip to content
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

"TypeError: cannot deepcopy this pattern object" when there is a Widget with a compiled regular expression attribute #791

Closed
int-ua opened this issue Jun 19, 2018 · 3 comments

Comments

@int-ua
Copy link
Contributor

int-ua commented Jun 19, 2018

Version: 1.0.1
Cause: #550

Widget:

class SpreadsheetPhoneFixWidget(Widget):
    def __init__(self):
        self.regexp = re.compile("^(\d{2})(\d{3})(\d{2})(\d{2})$")

    def clean(self, value, row=None):
        try:
            return "0" + " ".join(self.regexp.match(value).groups())
        except AttributeError:
            return value

Traceback:

Traceback (most recent call last):
  File ".../venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 132, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File ".../venv/lib/python3.5/site-packages/django/utils/decorators.py", line 110, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File ".../venv/lib/python3.5/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File ".../venv/lib/python3.5/site-packages/django/contrib/admin/sites.py", line 233, in inner
    return view(request, *args, **kwargs)
  File ".../venv/lib/python3.5/site-packages/import_export/admin.py", line 241, in import_action
    resource = self.get_import_resource_class()(**self.get_import_resource_kwargs(request, *args, **kwargs))
  File ".../venv/lib/python3.5/site-packages/import_export/resources.py", line 201, in __init__
    self.fields = deepcopy(self.fields)
  File "/usr/lib/python3.5/copy.py", line 182, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib/python3.5/copy.py", line 320, in _reconstruct
    value = deepcopy(value, memo)
  File "/usr/lib/python3.5/copy.py", line 182, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib/python3.5/copy.py", line 297, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python3.5/copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.5/copy.py", line 243, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python3.5/copy.py", line 182, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib/python3.5/copy.py", line 297, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python3.5/copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.5/copy.py", line 243, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python3.5/copy.py", line 166, in deepcopy
    y = copier(memo)

Workaround: do not compile regex patterns or move them out of Widgets.

@int-ua int-ua changed the title "TypeError: cannot deepcopy this pattern object" after upgrading to 1.0.1 "TypeError: cannot deepcopy this pattern object" when there is a Widget with a compiled regular expression attribute Jun 19, 2018
@int-ua
Copy link
Contributor Author

int-ua commented Jun 19, 2018

Should I add a test or will it be added to docs as a limitation?

@bmihelac
Copy link
Member

Hi @int-ua, thanks for opening this issue.
I think that it cannot be fixed as deepcopy does not know how to copy compiled regexp. Django for example override deepcopy to avoid this on field validators. Having a note in Widget docstring should probably be enough?

@int-ua
Copy link
Contributor Author

int-ua commented Nov 29, 2018

Yes, looks like a note would do, thank you. I'll close this if you don't mind, as this discussion must be relatively easy to find for those few who encounter this issue.

@int-ua int-ua closed this as completed Nov 29, 2018
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

No branches or pull requests

2 participants