[Python-Dev] Re: [ python-Bugs-416670 ] MatchObjects not deepcopy()able

Martin v. Loewis martin@loewis.home.cs.tu-berlin.de
Thu, 26 Apr 2001 23:12:45 +0200


> what makes you think a match object is immutable?

Because you cannot modify their state.

> import array, sre
> 
> a = array.array("c", "abcde")
> m = sre.search("bcd", a)
> print m.group(0)

a1 = m.group(0)
a1[0]='z'
print m.group(0)

So you'd have to modify a, to modify m.group(0). To catch this case,
you might want to do

def copy_match(m):
  g = m.group(0)
  if copy(g) is not g:
    raise KeyError    # will be re-raised as copy.Error
  return g

That will restore backwards compatibility with pre, which is what the
submitter of the bug requested.

Regards,
Martin