Skip to content

Commit

Permalink
fix #597: better warning filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
mmckerns committed May 29, 2023
1 parent 4e420e3 commit 272f057
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
23 changes: 14 additions & 9 deletions dill/_dill.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
OLD38 = (sys.hexversion < 0x3080000)
OLD39 = (sys.hexversion < 0x3090000)
OLD310 = (sys.hexversion < 0x30a0000)
OLD312a7 = (sys.hexversion < 0x30c00a7)
#XXX: get types from .objtypes ?
import builtins as __builtin__
from pickle import _Pickler as StockPickler, Unpickler as StockUnpickler
Expand Down Expand Up @@ -989,7 +990,7 @@ def _create_capsule(pointer, name, context, destructor):
return capsule
raise UnpicklingError("%s object exists at %s but a PyCapsule object was expected." % (type(capsule), name))
else:
warnings.warn('Creating a new PyCapsule %s for a C data structure that may not be present in memory. Segmentation faults or other memory errors are possible.' % (name,), UnpicklingWarning)
#warnings.warn('Creating a new PyCapsule %s for a C data structure that may not be present in memory. Segmentation faults or other memory errors are possible.' % (name,), UnpicklingWarning)
capsule = _PyCapsule_New(pointer, name, destructor)
_PyCapsule_SetContext(capsule, context)
return capsule
Expand Down Expand Up @@ -1136,8 +1137,10 @@ def save_code(pickler, obj):
obj.co_firstlineno, obj.co_linetable, obj.co_endlinetable,
obj.co_columntable, obj.co_exceptiontable, obj.co_freevars,
obj.co_cellvars
)
)
elif hasattr(obj, "co_exceptiontable"): # python 3.11 (18 args)
if not OLD312a7: # issue 597
warnings.filterwarnings('ignore', category=DeprecationWarning)
args = (
obj.co_lnotab, # for < python 3.10 [not counted in args]
obj.co_argcount, obj.co_posonlyargcount,
Expand All @@ -1146,7 +1149,9 @@ def save_code(pickler, obj):
obj.co_varnames, obj.co_filename, obj.co_name, obj.co_qualname,
obj.co_firstlineno, obj.co_linetable, obj.co_exceptiontable,
obj.co_freevars, obj.co_cellvars
)
)
if not OLD312a7 and warnings.filters:
del warnings.filters[0] #[::2] == ('ignore', DeprecationWarning, 0)
elif hasattr(obj, "co_linetable"): # python 3.10 (16 args)
args = (
obj.co_lnotab, # for < python 3.10 [not counted in args]
Expand All @@ -1156,7 +1161,7 @@ def save_code(pickler, obj):
obj.co_varnames, obj.co_filename, obj.co_name,
obj.co_firstlineno, obj.co_linetable, obj.co_freevars,
obj.co_cellvars
)
)
elif hasattr(obj, "co_posonlyargcount"): # python 3.8 (16 args)
args = (
obj.co_argcount, obj.co_posonlyargcount,
Expand All @@ -1165,15 +1170,15 @@ def save_code(pickler, obj):
obj.co_varnames, obj.co_filename, obj.co_name,
obj.co_firstlineno, obj.co_lnotab, obj.co_freevars,
obj.co_cellvars
)
)
else: # python 3.7 (15 args)
args = (
obj.co_argcount, obj.co_kwonlyargcount, obj.co_nlocals,
obj.co_stacksize, obj.co_flags, obj.co_code, obj.co_consts,
obj.co_names, obj.co_varnames, obj.co_filename,
obj.co_name, obj.co_firstlineno, obj.co_lnotab,
obj.co_freevars, obj.co_cellvars
)
)

pickler.save_reduce(_create_code, args, obj=obj)
logger.trace(pickler, "# Co")
Expand Down Expand Up @@ -2023,7 +2028,7 @@ def save_function(pickler, obj):
def save_capsule(pickler, obj):
logger.trace(pickler, "Cap: %s", obj)
name = _PyCapsule_GetName(obj)
warnings.warn('Pickling a PyCapsule (%s) does not pickle any C data structures and could cause segmentation faults or other memory errors when unpickling.' % (name,), PicklingWarning)
#warnings.warn('Pickling a PyCapsule (%s) does not pickle any C data structures and could cause segmentation faults or other memory errors when unpickling.' % (name,), PicklingWarning)
pointer = _PyCapsule_GetPointer(obj, name)
context = _PyCapsule_GetContext(obj)
destructor = _PyCapsule_GetDestructor(obj)
Expand Down Expand Up @@ -2093,9 +2098,9 @@ def pickles(obj,exact=False,safe=False,**kwds):
#FIXME: should be "(pik == obj).all()" for numpy comparison, though that'll fail if shapes differ
result = bool(pik.all() == obj.all())
except (AttributeError, TypeError):
warnings.filterwarnings('ignore')
warnings.filterwarnings('ignore') #FIXME: be specific
result = pik == obj
warnings.resetwarnings()
if warnings.filters: del warnings.filters[0]
if hasattr(result, 'toarray'): # for unusual types like sparse matrix
result = result.toarray().all()
if result: return True
Expand Down
2 changes: 1 addition & 1 deletion dill/tests/test_fglobals.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def fun_with_strftime():

def get_fun_with_strftime2():
import datetime
return datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')


def test_doc_dill_issue_219():
Expand Down
3 changes: 3 additions & 0 deletions dill/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,12 @@ def test_functions():
assert dill.loads(dumped_func_e)(1, 2, 3, e2=4, e3=5) == 15''')

def test_code_object():
import warnings
from dill._dill import ALL_CODE_PARAMS, CODE_PARAMS, CODE_VERSION, _create_code
code = function_c.__code__
warnings.filterwarnings('ignore', category=DeprecationWarning) # issue 597
LNOTAB = getattr(code, 'co_lnotab', b'')
if warnings.filters: del warnings.filters[0]
fields = {f: getattr(code, 'co_'+f) for f in CODE_PARAMS}
fields.setdefault('posonlyargcount', 0) # python >= 3.8
fields.setdefault('lnotab', LNOTAB) # python <= 3.9
Expand Down

0 comments on commit 272f057

Please sign in to comment.