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

TypeError: a bytes-like object is required, not 'str' when loading pickle in text mode #3330

Open
L3ster1337 opened this issue Dec 15, 2024 · 0 comments

Comments

@L3ster1337
Copy link

I am encountering a TypeError when attempting to load a pickle file using lsf_runner.py. The code in lsf_runner.py opens the job-instance.pickle file in text mode ("r") and then calls pickle.load(). In Python 3, pickle.load() expects a binary stream, and opening the file in text mode results in a TypeError: a bytes-like object is required, not 'str'.

Steps to Reproduce:

Run lsf_runner.py with a job-instance.pickle file that has been generated in any manner (binary or ASCII protocol).
Observe that lsf_runner.py opens job-instance.pickle using open("job-instance.pickle", "r").
pickle.load() (or dill.load()) fails with TypeError: a bytes-like object is required, not 'str'.

Expected Behavior:
pickle.load() should successfully unpickle the object.

Actual Behavior:
The unpickling fails with a TypeError due to the file being opened in text mode.

Environment:

Python 3.x
dill / pickle module
lsf_runner.py script as provided by the repository.

Possible Solution:
Change the file opening mode in lsf_runner.py from "r" to "rb". For example:

with open("job-instance.pickle", "rb") as pickle_file_handle:
    job = pickle.load(pickle_file_handle)

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

No branches or pull requests

1 participant