Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
lizy14 opened this issue Feb 21, 2024 · 5 comments · Fixed by #649
Closed

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

lizy14 opened this issue Feb 21, 2024 · 5 comments · Fixed by #649
Labels
Milestone

Comments

@lizy14
Copy link
Contributor

lizy14 commented Feb 21, 2024

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?

@mmckerns
Copy link
Member

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

@lizy14
Copy link
Contributor Author

lizy14 commented Feb 22, 2024

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

@lizy14
Copy link
Contributor Author

lizy14 commented Feb 22, 2024

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

@lizy14
Copy link
Contributor Author

lizy14 commented Feb 22, 2024

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?

@mmckerns mmckerns added the bug label Feb 24, 2024
@mmckerns mmckerns added this to the dill-0.3.9 milestone Feb 24, 2024
@mmckerns
Copy link
Member

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants