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 @@
##############################################################################
"""Provide a thread-safe interface to regex
"""
import regex, regsub, Sync
import regex, regsub #, Sync
from regex import *
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
groupindex=None
......@@ -96,13 +125,24 @@ class compile(Sync.Synchronized):
def __init__(self, *args):
self._r=r=apply(regex.compile,args)
self._init(r)
lock=allocate_lock()
self.__a=lock.acquire
self.__r=lock.release
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 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):
"""Search a string for a pattern.
......@@ -111,10 +151,13 @@ class compile(Sync.Synchronized):
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)
self.__a()
try:
r=self._r
l=r.search(str, pos)
if l < 0: return None
return l, apply(r.group, group)
finally: self.__r()
def match_group(self, str, group, pos=0):
"""Match a pattern against a string
......@@ -123,10 +166,13 @@ class compile(Sync.Synchronized):
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)
self.__a()
try:
r=self._r
l=r.match(str, pos)
if l < 0: return None
return l, apply(r.group, group)
finally: self.__r()
def search_regs(self, str, pos=0):
"""Search a string for a pattern.
......@@ -135,9 +181,12 @@ class compile(Sync.Synchronized):
otherwise, the 'regs' attribute of the expression is
returned.
"""
r=self._r
r.search(str, pos)
return r.regs
self.__a()
try:
r=self._r
r.search(str, pos)
return r.regs
finally: self.__r()
def match_regs(self, str, pos=0):
"""Match a pattern against a string
......@@ -146,9 +195,12 @@ class compile(Sync.Synchronized):
returned, otherwise, the 'regs' attribute of the expression is
returned.
"""
r=self._r
r.match(str, pos)
return r.regs
self.__a()
try:
r=self._r
r.match(str, pos)
return r.regs
finally: self.__r()
class symcomp(compile):
......@@ -157,28 +209,6 @@ class symcomp(compile):
self._init(r)
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