Commit 2a8f4f52 authored by Jim Fulton's avatar Jim Fulton

Changed to not use Synchronized because there is a refcounting

problem with the Synchronized class. :(
parent 6f33860d
...@@ -84,11 +84,40 @@ ...@@ -84,11 +84,40 @@
############################################################################## ##############################################################################
"""Provide a thread-safe interface to regex """Provide a thread-safe interface to regex
""" """
import regex, regsub, Sync import regex, regsub #, Sync
from regex import * from regex import *
from regsub import split, sub, gsub, splitx, capwords from regsub import split, sub, gsub, splitx, capwords
class compile(Sync.Synchronized): try:
import thread
except:
class allocate_lock:
def acquire(*args): pass
def release(*args): pass
else:
class SafeFunction:
_l=thread.allocate_lock()
_a=_l.acquire
_r=_l.release
def __init__(self, f):
self._f=f
def __call__(self, *args, **kw):
self._a()
try: return apply(self._f, args, kw)
finally: self._r()
split=SafeFunction(split)
sub=SafeFunction(sub)
gsub=SafeFunction(gsub)
splitx=SafeFunction(splitx)
capwords=SafeFunction(capwords)
allocate_lock=thread.allocate_lock
class compile:
_r=None _r=None
groupindex=None groupindex=None
...@@ -96,14 +125,25 @@ class compile(Sync.Synchronized): ...@@ -96,14 +125,25 @@ class compile(Sync.Synchronized):
def __init__(self, *args): def __init__(self, *args):
self._r=r=apply(regex.compile,args) self._r=r=apply(regex.compile,args)
self._init(r) self._init(r)
lock=allocate_lock()
self.__a=lock.acquire
self.__r=lock.release
def _init(self, r): def _init(self, r):
self.search=r.search
self.match=r.match
self.translate=r.translate self.translate=r.translate
self.givenpat=r.givenpat self.givenpat=r.givenpat
self.realpat=r.realpat self.realpat=r.realpat
def match(self, string, pos=0):
self.__a()
try: return self._r.match(string, pos)
finally: self.__r()
def search(self, string, pos=0):
self.__a()
try: return self._r.search(string, pos)
finally: self.__r()
def search_group(self, str, group, pos=0): def search_group(self, str, group, pos=0):
"""Search a string for a pattern. """Search a string for a pattern.
...@@ -111,10 +151,13 @@ class compile(Sync.Synchronized): ...@@ -111,10 +151,13 @@ class compile(Sync.Synchronized):
otherwise, the location where the pattern was found, otherwise, the location where the pattern was found,
as well as any specified group are returned. as well as any specified group are returned.
""" """
self.__a()
try:
r=self._r r=self._r
l=r.search(str, pos) l=r.search(str, pos)
if l < 0: return None if l < 0: return None
return l, apply(r.group, group) return l, apply(r.group, group)
finally: self.__r()
def match_group(self, str, group, pos=0): def match_group(self, str, group, pos=0):
"""Match a pattern against a string """Match a pattern against a string
...@@ -123,10 +166,13 @@ class compile(Sync.Synchronized): ...@@ -123,10 +166,13 @@ class compile(Sync.Synchronized):
returned, otherwise, the length of the match, as well returned, otherwise, the length of the match, as well
as any specified group are returned. as any specified group are returned.
""" """
self.__a()
try:
r=self._r r=self._r
l=r.match(str, pos) l=r.match(str, pos)
if l < 0: return None if l < 0: return None
return l, apply(r.group, group) return l, apply(r.group, group)
finally: self.__r()
def search_regs(self, str, pos=0): def search_regs(self, str, pos=0):
"""Search a string for a pattern. """Search a string for a pattern.
...@@ -135,9 +181,12 @@ class compile(Sync.Synchronized): ...@@ -135,9 +181,12 @@ class compile(Sync.Synchronized):
otherwise, the 'regs' attribute of the expression is otherwise, the 'regs' attribute of the expression is
returned. returned.
""" """
self.__a()
try:
r=self._r r=self._r
r.search(str, pos) r.search(str, pos)
return r.regs return r.regs
finally: self.__r()
def match_regs(self, str, pos=0): def match_regs(self, str, pos=0):
"""Match a pattern against a string """Match a pattern against a string
...@@ -146,9 +195,12 @@ class compile(Sync.Synchronized): ...@@ -146,9 +195,12 @@ class compile(Sync.Synchronized):
returned, otherwise, the 'regs' attribute of the expression is returned, otherwise, the 'regs' attribute of the expression is
returned. returned.
""" """
self.__a()
try:
r=self._r r=self._r
r.match(str, pos) r.match(str, pos)
return r.regs return r.regs
finally: self.__r()
class symcomp(compile): class symcomp(compile):
...@@ -157,28 +209,6 @@ class symcomp(compile): ...@@ -157,28 +209,6 @@ class symcomp(compile):
self._init(r) self._init(r)
self.groupindex=r.groupindex self.groupindex=r.groupindex
try:
import thread
except: pass
else:
class SafeFunction:
_l=thread.allocate_lock()
_a=_l.acquire
_r=_l.release
def __init__(self, f):
self._f=f
def __call__(self, *args, **kw):
self._a()
try: return apply(self._f, args, kw)
finally: self._r()
split=SafeFunction(split)
sub=SafeFunction(sub)
gsub=SafeFunction(gsub)
splitx=SafeFunction(splitx)
capwords=SafeFunction(capwords)
......
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