Skip to content

Commit ae8b10a

Browse files
committed
reduce for _thread._local
1 parent a2e2a59 commit ae8b10a

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

dill/_dill.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ def _trace(boolean):
4747
from _thread import RLock as RLockType
4848
else:
4949
from threading import _RLock as RLockType
50+
# monkey patch for _thread._local
51+
try:
52+
from _thread import _local as LocalType
53+
except ImportError:
54+
from _threading_local import local as LocalType
5055
#from io import IOBase
5156
from types import CodeType, FunctionType, MethodType, GeneratorType, \
5257
TracebackType, FrameType, ModuleType, BuiltinMethodType
@@ -85,7 +90,7 @@ def _trace(boolean):
8590
FileNotFoundError = IOError
8691
if PY3 and sys.hexversion < 0x03040000:
8792
GENERATOR_FAIL = True
88-
else: GENERATOR_FAIL = False
93+
else: GENERATOR_FAIL = False
8994
try:
9095
import ctypes
9196
HAS_CTYPES = True
@@ -596,6 +601,13 @@ def _create_ftype(ftypeobj, func, args, kwds):
596601
args = ()
597602
return ftypeobj(func, *args, **kwds)
598603

604+
def _create_local(impl, *args, **kwargs): #XXX: ignores 'blocking'
605+
local = LocalType()
606+
if local:
607+
if impl:
608+
local.__setattr__('_local__impl', impl)
609+
return local
610+
599611
def _create_lock(locked, *args): #XXX: ignores 'blocking'
600612
from threading import Lock
601613
lock = Lock()
@@ -927,6 +939,23 @@ def save_classobj(pickler, obj): #FIXME: enable pickler._byref
927939
log.info("# C2")
928940
return
929941

942+
@register(LocalType)
943+
def save_local(pickler, obj):
944+
log.info("Loc: %s" % obj)
945+
impl = obj.__getattribute__('_local__impl')
946+
if impl:
947+
try:
948+
dct = impl.get_dict()
949+
except KeyError:
950+
dct = impl.create_dict()
951+
args, kwargs = impl.localargs
952+
obj.__init__(*args, **kwargs)
953+
with impl.locallock:
954+
obj.__setattr__('__dict__', dct)
955+
pickler.save_reduce(_create_local, (impl,), obj=obj)
956+
log.info("# Loc")
957+
return
958+
930959
@register(LockType)
931960
def save_lock(pickler, obj):
932961
log.info("Lo: %s" % obj)

0 commit comments

Comments
 (0)