-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
Regex objects became uncopyable in 2.5 #54285
Comments
For many years now, the sre module has provided __copy__ and __deepcopy__ modules that raise an exception ("cannot copy this pattern object") by default, with an #ifdef to enable their implementations. Until Python 2.5, these were simply unused. Since then, deepcopying these objects fails, instead of falling back to the default implementation. Python 2.4.6 (#1, Nov 23 2009, 03:28:22)
[GCC 4.2.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> x = [re.compile('.*')]
>>> import copy
>>> copy.deepcopy(x)
[<_sre.SRE_Pattern object at 0x7f3e9411e168>]
Python 2.6.2 (r262:71600, Jul 24 2009, 17:29:21)
[GCC 4.2.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> x = [re.compile('.*')]
>>> import copy
>>> copy.deepcopy(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/grte/v1/k8-linux/lib/python2.6/copy.py", line 162, in deepcopy
y = copier(x, memo)
File "/usr/grte/v1/k8-linux/lib/python2.6/copy.py", line 228, in _deepcopy_list
y.append(deepcopy(a, memo))
File "/usr/grte/v1/k8-linux/lib/python2.6/copy.py", line 173, in deepcopy
y = copier(memo)
TypeError: cannot deepcopy this pattern object I'll attach a patch against 2.7 to correct this. |
Here's the patch. I updated the test case and release notes also. I'm a Google employee, so this patch is covered by whatever usual copyright arrangement we have with the PSF. |
The patch looks reasonable to me. I was trying to find out why they got disabled after 2.5, but I don't see any reason. (There was an issue open and it was closed for no reason). So, I think, this should move forward, unless there is any technical objection to the patch. |
Ding. |
Is it necessary to actually copy it? Isn't the pattern object immutable? |
It's not strictly necessary that re objects be copyable, but there's no reason to break it either. It's not strictly necessary that str or int be copyable either. This came up in code that had objects with a number of members, one of which was a regexp pattern object. deepcopy of this object broke in 2.5. |
Since the pattern and the match objects can be considered as immutable, it is worth to implement __copy__ and __deepcopy__ returning the object itself. Proposed PR does this. It also fixes signatures of __deepcopy__ methods. Since copying didn't work in all maintained version for long time, this is rather a new feature than a bug fix. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: