uqfoundation/dill

`dill.load_session()` raises `NameError: name 'UnpicklingError' is not defined`

Closed this issue · 5 comments

dill.load_session() raises NameError: name 'UnpicklingError' is not defined

Error occurred: NameError: name 'UnpicklingError' is not defined, traceback:
---------------------------------------------------------------------------
ValueError                                
Traceback (most recent call last)
File /usr/local/lib/python3.11/site-packages/dill/session.py:312, in _identify_module(file, main)
    311 try:
--> 312     for opcode, arg, pos in genops(file.peek(256)):
    313         if not found_import:
File /usr/local/lib/python3.11/pickletools.py:2285, in _genops(data, yield_end_pos)
   2284     else:
-> 2285         raise ValueError("at position %s, opcode %r unknown" % (
   2286                          "<unknown>" if pos is None else pos,
   2287                          code))
   2288 if opcode.arg is None:
ValueError: at position 5, opcode b' ' unknown

During handling of the above exception, another exception occurred:
NameError                                 
Traceback (most recent call last)
Cell In[1], line 4
      2 dump_dir = '/mnt/dump'
      3 dump_files = glob.glob(os.path.join(dump_dir, '*')) if os.path.exists(dump_dir) and os.path.isdir(dump_dir) else []
----> 4 dill.load_session(dump_files[0]) if len(dump_files) == 1 else None
File /usr/local/lib/python3.11/site-packages/dill/session.py:512, in load_session(filename, main, **kwds)
    510 def load_session(filename=None, main=None, **kwds):
    511     warnings.warn("load_session() has been renamed load_module().", PendingDeprecationWarning)
--> 512     load_module(filename, module=main, **kwds)
File /usr/local/lib/python3.11/site-packages/dill/session.py:453, in load_module(filename, module, **kwds)
    450 unpickler._session = True
    452 # Resolve unpickler._main
--> 453 pickle_main = _identify_module(file, main)
    454 if main is None and pickle_main is not None:
    455     main = pickle_main
File /usr/local/lib/python3.11/site-packages/dill/session.py:327, in _identify_module(file, main)
    324 if isinstance(error, NotImplementedError) and main is not None:
    325     # file is not peekable, but we have main.
    326     return None
--> 327 raise UnpicklingError("unable to identify main module") from error
NameError: name 'UnpicklingError' is not defined

The file from which I'm loading is indeed invalid, the exception ValueError: at position 5, opcode b' ' unknown makes sense. But why this exception is not raised properly and I got NameError: name 'UnpicklingError' is not defined instead?

Hm, ok. Can you provide a minimal session that produces this error? Doing so would help us reproduce, confirm, and fix the issue.

I've updated my description above. Sorry for any confusion. The file I was loading is NOT a valid dump file.

Minimum repro:

import tempfile

import dill

with tempfile.TemporaryFile() as f:
    f.write("some content that is not a valid dump".encode())
    f.flush()
    try:
        dill.load_session(f.name)
    except Exception as e:
        print(type(e), str(e))

This will print: <class 'NameError'> name 'UnpicklingError' is not defined

I've created a simple PR to fix this: #649

With this PR, the above code will print <class '_pickle.UnpicklingError'> unable to identify main module instead. I suppose this behavior makes more sense.

@mmckerns Could you please review it?

thanks