From 438927ed7b7740a2daa10517aa9d67bd20046173 Mon Sep 17 00:00:00 2001 From: Nicolas Mussat Date: Wed, 8 Jul 2015 14:52:20 +0200 Subject: [PATCH] Fix `thread` module import on Python 2.7 The Python 2.7 `thread` module has been renamed to `_thread` in Python3. This package was importing the `_thread` anyway, and was thus fallbacking to the `dummy_thread32` module everytime, which is far from failproof (eg. `lru_cache` decorator used in a WSGI app started on uWSGI with threads enabled and high concurrency raises exceptions). This fix loads the `thread` module accordingly, without trying to load Python3 `_thread` module since setup.py prevents installation on Python 3 explicitly. This package targets Python 2.7 only, as documented --- functools32/_dummy_thread32.py | 5 ++++- functools32/functools32.py | 4 ++-- functools32/reprlib32.py | 2 +- setup.py | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/functools32/_dummy_thread32.py b/functools32/_dummy_thread32.py index ed50520..8503b0e 100644 --- a/functools32/_dummy_thread32.py +++ b/functools32/_dummy_thread32.py @@ -6,7 +6,10 @@ not need to be rewritten for when the thread module is not present. Suggested usage is:: try: - import _thread + try: + import _thread # Python >= 3 + except: + import thread as _thread # Python < 3 except ImportError: import _dummy_thread as _thread diff --git a/functools32/functools32.py b/functools32/functools32.py index e472c22..c44551f 100644 --- a/functools32/functools32.py +++ b/functools32/functools32.py @@ -17,8 +17,8 @@ from .reprlib32 import recursive_repr as _recursive_repr from weakref import proxy as _proxy import sys as _sys try: - from _thread import allocate_lock as Lock -except: + from thread import allocate_lock as Lock +except ImportError: from ._dummy_thread32 import allocate_lock as Lock ################################################################################ diff --git a/functools32/reprlib32.py b/functools32/reprlib32.py index bf895c7..af91975 100644 --- a/functools32/reprlib32.py +++ b/functools32/reprlib32.py @@ -5,7 +5,7 @@ __all__ = ["Repr", "repr", "recursive_repr"] import __builtin__ as builtins from itertools import islice try: - from _thread import get_ident + from thread import get_ident except ImportError: from _dummy_thread32 import get_ident diff --git a/setup.py b/setup.py index f8dbe5a..a04c5d9 100755 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ def main(): setup( name='functools32', - version='3.2.3-1', + version='3.2.3-2', description='Backport of the functools module from Python 3.2.3 for use on 2.7 and PyPy.', long_description=""" This is a backport of the functools standard library module from