@@ -364,31 +364,35 @@ def dumps(obj, protocol=None, byref=None, fmode=None, recurse=None, portable=Non
364
364
"""
365
365
if os .environ .get ('CI' ) and \
366
366
getattr (obj , '__qualname__' , None ) != 'test_getstate.<locals>.<lambda>' and \
367
- not (os .environ .get ('TRAVIS' ) and \
368
- obj .__module__ == 'numpy' if isinstance (obj , type ) else any (base .__module__ == 'numpy' for base in type (obj ).mro ())) and \
369
367
__file__ != 'test_session.py' :
370
368
_test_portable (obj , protocol , byref , fmode , recurse , ** kwds )
371
369
file = StringIO ()
372
370
dump (obj , file , protocol , byref , fmode , recurse , portable , ** kwds )#, strictio)
373
371
return file .getvalue ()
374
372
373
+ _tempd = None
375
374
def _test_portable (obj , protocol , byref , fmode , recurse , ** kwds ):
376
- import dill , subprocess , tempfile , textwrap
375
+ import atexit , dill , subprocess , tempfile , textwrap
377
376
try :
378
377
import pox
379
378
python = pox .which_python (version = True , fullpath = False ) or 'python'
380
379
except ImportError :
381
380
python = 'python'
382
381
382
+ global _tempd
383
+ if _tempd is None :
384
+ import shutil
385
+ from importlib .util import find_spec
386
+ _tempd = tempfile .mkdtemp ()
387
+ atexit .register (shutil .rmtree , _tempd )
388
+ os .symlink (os .path .dirname (find_spec ('numpy' ).origin ), os .path .join (_tempd , 'numpy' ), True )
389
+ os .symlink (find_spec ('dill._objects' ).origin , os .path .join (_tempd , '_objects.py' ))
383
390
temp = tempfile .NamedTemporaryFile ()
391
+
384
392
script = textwrap .dedent (f"""\
385
393
import importlib, os, pickle, sys, types
386
394
dill_spec = importlib.util.find_spec('dill')
387
- if os.path.exists(os.path.join('dill', '__init__.py')):
388
- sys.path.insert(0, 'dill') # for _objects
389
- sys.path.remove('')
390
395
while (spec := importlib.util.find_spec("dill")) is not None:
391
- sys.path.insert(0, os.path.dirname(spec.origin))
392
396
sys.path.remove(os.path.dirname(os.path.dirname(spec.origin)))
393
397
try:
394
398
with open("{ temp .name } ", "rb") as f: pickle.load(f)
@@ -417,7 +421,7 @@ def _test_portable(obj, protocol, byref, fmode, recurse, **kwds):
417
421
if all ('Cannot locate reference' not in str (warn .message ) for warn in warns ):
418
422
temp .file .flush ()
419
423
dill_path = os .path .dirname (dill .__path__ [0 ])
420
- subprocess .run (python , input = script , check = True )
424
+ subprocess .run (python , input = script , cwd = _tempd , check = True )
421
425
finally :
422
426
warnings .simplefilter ('error' )
423
427
temp .file .close ()
0 commit comments