Commit 33637ce1 authored by Jim Fulton's avatar Jim Fulton

Fixes a serious bug in compile and syscomp. They were not mixing

in Sync.Synchronized, because Sync.Synchronized doesn't want to play
with __getattr__.

Also changed to allow correct execution on systems without threads.
parent 9d717ae3
......@@ -82,78 +82,85 @@
# attributions are listed in the accompanying credits file.
#
##############################################################################
import regex, regsub
"""Provide a thread-safe interface to regex
"""
import regex, regsub, Sync
from regex import *
from regsub import split, sub, gsub, splitx, capwords
try:
import thread, Sync
class compile:
_r=None
def __init__(self, *args):
self._r=r=apply(regex.compile,args)
self.search=r.search
self.match=r.match
def search_group(self, str, group, pos=0):
"""Search a string for a pattern.
If the pattern was not found, then None is returned,
otherwise, the location where the pattern was found,
as well as any specified group are returned.
"""
r=self._r
l=r.search(str, pos)
if l < 0: return None
return l, apply(r.group, group)
def match_group(self, str, group, pos=0):
"""Match a pattern against a string
If the string does not match the pattern, then None is
returned, otherwise, the length of the match, as well
as any specified group are returned.
"""
r=self._r
l=r.match(str, pos)
if l < 0: return None
return l, apply(r.group, group)
def search_regs(self, str, pos=0):
"""Search a string for a pattern.
If the pattern was not found, then None is returned,
otherwise, the 'regs' attribute of the expression is
returned.
"""
r=self._r
r.search(str, pos)
return r.regs
def match_regs(self, str, pos=0):
"""Match a pattern against a string
If the string does not match the pattern, then None is
returned, otherwise, the 'regs' attribute of the expression is
returned.
"""
r=self._r
r.match(str, pos)
return r.regs
def __getattr__(self, name): return getattr(self._r, name)
class symcomp(compile):
def __init__(self, *args):
self._r=r=apply(regex.symcomp,args)
self.search=r.search
self.match=r.match
class compile(Sync.Synchronized):
_r=None
groupindex=None
def __init__(self, *args):
self._r=r=apply(regex.compile,args)
self._init(r)
def _init(self, r):
self.search=r.search
self.match=r.match
self.translate=r.translate
self.givenpat=r.givenpat
self.realpat=r.realpat
def search_group(self, str, group, pos=0):
"""Search a string for a pattern.
If the pattern was not found, then None is returned,
otherwise, the location where the pattern was found,
as well as any specified group are returned.
"""
r=self._r
l=r.search(str, pos)
if l < 0: return None
return l, apply(r.group, group)
def match_group(self, str, group, pos=0):
"""Match a pattern against a string
If the string does not match the pattern, then None is
returned, otherwise, the length of the match, as well
as any specified group are returned.
"""
r=self._r
l=r.match(str, pos)
if l < 0: return None
return l, apply(r.group, group)
def search_regs(self, str, pos=0):
"""Search a string for a pattern.
If the pattern was not found, then None is returned,
otherwise, the 'regs' attribute of the expression is
returned.
"""
r=self._r
r.search(str, pos)
return r.regs
def match_regs(self, str, pos=0):
"""Match a pattern against a string
If the string does not match the pattern, then None is
returned, otherwise, the 'regs' attribute of the expression is
returned.
"""
r=self._r
r.match(str, pos)
return r.regs
class symcomp(compile):
def __init__(self, *args):
self._r=r=apply(regex.symcomp,args)
self._init(r)
self.groupindex=r.groupindex
try:
import thread
except: pass
else:
class SafeFunction:
_l=thread.allocate_lock()
_a=_l.acquire
......@@ -173,7 +180,6 @@ try:
splitx=SafeFunction(splitx)
capwords=SafeFunction(capwords)
except: pass
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment