[Zope-dev] ts_regex.gsub() doesn't work with compiled expressions
Steve Alexander
steve@cat-box.net
Mon, 24 Jul 2000 16:36:51 +0100
I'm trying to do the following:
>>> import ts_regex
>>> Z=ts_regex.compile('\(^\|[^%]\)%Z')
>>> ts_regex.gsub(Z, '\\1BAR', '%Z foo %Z')
Traceback (innermost last):
File "<stdin>", line 1, in ?
File "/usr/local/zope/SiteBox/lib/python/ts_regex.py", line 109, in
__call__
try: return apply(self._f, args, kw)
File "/usr//lib/python1.5/regsub.py", line 38, in gsub
regs = prog.regs
AttributeError: regs
This works just fine if you don't compile the regex first:
>>> import ts_regex
>>> ts_regex.gsub('\(^\|[^%]\)%Z', '\\1BAR', '%Z foo %Z')
'BAR foo BAR'
The problem is that a ts_regex.compile instances do not expose the
attribute "regs" that the method gsub of lib/python1.5/regsub.py
requires.
I don't know the details of what is thread-safe and what is not in the
regsub module, so I won't hazard a patch here. Perhaps overriding
__getattr__ to return "regs" safely would help? I don't know.
I can also get it to work if I pass in the unwrapped compiled regular
expression:
>>> ts_regex.gsub(Z._r, '\\1BAR', '%Z foo %Z')
'BAR foo BAR'
Is doing this in Zope threadsafe?
--
Steve Alexander
Software Engineer
Cat-Box limited
http://www.cat-box.net