python3 exited without any error when use pickle.load()

Viewed 355

I used pickle.dump(dic, open(a_dic,'wb')) to save a Python3 dic in another script, it's 15 MB. When I used pickle.load(open(a_dic,'rb')) to load this dic, my script just stops without any error.

print('start to load')
pickle.load(open(a_dic,'rb'))
print('finished')

The terminal only output this and exit:

start to load
2 Answers

So there's no way to know if the file has been successfully loaded.

You should change your code to

print('start')
with open(a_dic, 'rb') as file:
    try:
        var = pickle.load(file)
    except pickle.PicklingError as exc:
        print('Got pickling error: {0}'.format(exc))
print('finished')

You may then add other prints, or just run that code in a debugger to know what actually happens.

What's the content of your dictionary? Pickle is a format that deliberately allows nearly arbitrary code execution. If, for example, the pickle contains an instance of an object of a class defined in module foo, and that module foo executes code like exit() when imported, you may just told the interpreter to exit when the pickle is loaded.

Related