[Python-Dev] Python2.5 _sre deepcopy regression?

Andrew McNamara andrewm at object-craft.com.au
Thu Oct 23 04:24:32 CEST 2008

In version of Python prior to 2.5, it would appear that deepcopying
compiled regular expressions worked by accident:


    >>> copy.deepcopy(re.compile(''))
    <_sre.SRE_Pattern object at 0xb7d53ef0>


    >>> copy.deepcopy(re.compile(''))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python2.5/copy.py", line 173, in deepcopy
        y = copier(memo)
    TypeError: cannot deepcopy this pattern object

I say "by accident", since the SRE_Pattern object in 2.4 has
a __deepcopy__ method which raises the "cannot deepcopy this pattern
object" TypeError, however this method isn't found by copy.deepcopy()
in 2.4, and copy.deepcopy() falls back to using the pickle logic.

The _sre source has #ifdef-out support for __deepcopy__, issue 416670
has the gory details:


Changeset 38430 on the release24-maint branch introduced the changes
that stopped SRE_Pattern.__deepcopy__ being found. r38430 was a patch
forward ported from 2.3, but never ported to the trunk (probably a good
thing, too).


Andrew McNamara, Senior Developer, Object Craft

More information about the Python-Dev mailing list