This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Regex objects became uncopyable in 2.5
Type: enhancement Stage: resolved
Components: Library (Lib), Regular Expressions Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder: MatchObjects not deepcopy()able
View: 416670
Assigned To: Nosy List: Michael.Shields, ezio.melotti, mrabarnett, orsenthil, pitrou, serhiy.storchaka, shields, Анхбаяр.Лхагвадорж
Priority: normal Keywords: patch

Created on 2010-10-12 20:45 by Michael.Shields, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
copy-pattern.diff Michael.Shields, 2010-10-12 20:48 review
Pull Requests
URL Status Linked Edit
PR 1000 merged serhiy.storchaka, 2017-04-05 08:16
Messages (8)
msg118469 - (view) Author: Michael Shields (Michael.Shields) Date: 2010-10-12 20:45
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.
msg118470 - (view) Author: Michael Shields (Michael.Shields) Date: 2010-10-12 20:48
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.
msg118741 - (view) Author: Senthil Kumaran (orsenthil) * (Python committer) Date: 2010-10-15 06:28
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.
msg168988 - (view) Author: Анхбаяр Лхагвадорж (Анхбаяр.Лхагвадорж) Date: 2012-08-24 09:00
Ding.
msg169167 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2012-08-26 16:46
Is it necessary to actually copy it? Isn't the pattern object immutable?
msg169214 - (view) Author: Michael Shields (shields) Date: 2012-08-27 20:20
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.
msg291166 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-04-05 08:16
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.
msg291744 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-04-16 07:16
New changeset fdbd01151dbd5feea3e4c0316d102db3d2a2a412 by Serhiy Storchaka in branch 'master':
bpo-10076: Compiled regular expression and match objects now are copyable. (#1000)
https://github.com/python/cpython/commit/fdbd01151dbd5feea3e4c0316d102db3d2a2a412
History
Date User Action Args
2022-04-11 14:57:07adminsetgithub: 54285
2017-04-16 07:17:05serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-04-16 07:16:05serhiy.storchakasetmessages: + msg291744
2017-04-05 08:16:56serhiy.storchakasetpull_requests: + pull_request1173
2017-04-05 08:16:24serhiy.storchakasetversions: + Python 3.7, - Python 2.7, Python 3.4, Python 3.5
nosy: + serhiy.storchaka

messages: + msg291166

type: behavior -> enhancement
2016-12-21 18:33:47lukasz.langaunlinkissue16058 dependencies
2016-12-13 20:52:23serhiy.storchakalinkissue16058 dependencies
2014-10-23 21:01:48serhiy.storchakasetcomponents: + Regular Expressions
versions: + Python 3.4, Python 3.5, - Python 3.1
2012-08-27 20:20:24shieldssetnosy: + shields
messages: + msg169214
2012-08-26 16:46:03mrabarnettsetnosy: + mrabarnett
messages: + msg169167
2012-08-24 09:00:25Анхбаяр.Лхагвадоржsetnosy: + Анхбаяр.Лхагвадорж
messages: + msg168988
2010-10-21 22:05:43eric.araujosetnosy: + ezio.melotti
2010-10-15 06:28:13orsenthilsetversions: + Python 3.1, - Python 2.6, Python 2.5
nosy: + orsenthil, pitrou

messages: + msg118741

superseder: MatchObjects not deepcopy()able
stage: patch review
2010-10-12 20:48:48Michael.Shieldssetfiles: + copy-pattern.diff
keywords: + patch
messages: + msg118470
2010-10-12 20:45:22Michael.Shieldscreate