From 46535b25a642b596c1f43217fe00f6205bcc0ecb Mon Sep 17 00:00:00 2001 From: Jason Kirtland Date: Wed, 15 Aug 2007 23:11:47 +0000 Subject: [PATCH] use threading.local if available speed up ThreadLocal for python 2.3 [ticket:743] clean in topo (in patch from [ticket:743]) --- lib/sqlalchemy/topological.py | 2 +- lib/sqlalchemy/util.py | 46 ++++++++++++++++++++--------------- test/testlib/config.py | 2 +- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/lib/sqlalchemy/topological.py b/lib/sqlalchemy/topological.py index b744edaa57..a13923885f 100644 --- a/lib/sqlalchemy/topological.py +++ b/lib/sqlalchemy/topological.py @@ -196,7 +196,7 @@ class QueueDependencySorter(object): for n in nodes.values(): if not edges.has_parents(n): queue.append(n) - cycles = {} + output = [] while nodes: if not queue: diff --git a/lib/sqlalchemy/util.py b/lib/sqlalchemy/util.py index 82815f1015..37dfeb2114 100644 --- a/lib/sqlalchemy/util.py +++ b/lib/sqlalchemy/util.py @@ -366,26 +366,32 @@ class OrderedDict(dict): self._list.remove(item[0]) return item -class ThreadLocal(object): - """An object in which attribute access occurs only within the context of the current thread.""" - - def __init__(self): - self.__dict__['_tdict'] = {} - - def __delattr__(self, key): - try: - del self._tdict["%d_%s" % (thread.get_ident(), key)] - except KeyError: - raise AttributeError(key) - - def __getattr__(self, key): - try: - return self._tdict["%d_%s" % (thread.get_ident(), key)] - except KeyError: - raise AttributeError(key) - - def __setattr__(self, key, value): - self._tdict["%d_%s" % (thread.get_ident(), key)] = value +try: + from threading import local as ThreadLocal +except ImportError: + try: + from dummy_threading import local as ThreadLocal + except ImportError: + class ThreadLocal(object): + """An object in which attribute access occurs only within the context of the current thread.""" + + def __init__(self): + self.__dict__['_tdict'] = {} + + def __delattr__(self, key): + try: + del self._tdict[(thread.get_ident(), key)] + except KeyError: + raise AttributeError(key) + + def __getattr__(self, key): + try: + return self._tdict[(thread.get_ident(), key)] + except KeyError: + raise AttributeError(key) + + def __setattr__(self, key, value): + self._tdict[(thread.get_ident(), key)] = value class DictDecorator(dict): """A Dictionary that delegates items not found to a second wrapped dictionary.""" diff --git a/test/testlib/config.py b/test/testlib/config.py index 719dcf6c0e..8f5a39d866 100644 --- a/test/testlib/config.py +++ b/test/testlib/config.py @@ -109,7 +109,7 @@ opt("--mockpool", action="store_true", dest="mockpool", help="Use mock pool (asserts only one connection used)") opt("--enginestrategy", action="callback", type="string", callback=_engine_strategy, - help="Engine strategy (plain or threadlocal, defaults toplain)") + help="Engine strategy (plain or threadlocal, defaults to plain)") opt("--reversetop", action="store_true", dest="reversetop", default=False, help="Reverse the collection ordering for topological sorts (helps " "reveal dependency issues)") -- 2.47.3