ValueError: insecure string pickle

后端 未结 10 1513
轻奢々
轻奢々 2020-12-03 04:00

When I am trying to load something I dumped using cPickle, I get the error message:

ValueError: insecure string pickle

Both the dumping and

10条回答
  •  自闭症患者
    2020-12-03 04:54

    I ran into this earlier, found this thread, and assumed that I was immune to the file closing issue mentioned in a couple of these answers since I was using a with statement:

    with tempfile.NamedTemporaryFile(mode='wb') as temp_file:
        pickle.dump(foo, temp_file)
    
        # Push file to another machine
        _send_file(temp_file.name)
    

    However, since I was pushing the temp file from inside the with, the file still wasn't closed, so the file I was pushing was truncated. This resulted in the same insecure string pickle error in the script that read the file on the remote machine.

    Two potential fixes to this: Keep the file open and force a flush:

    with tempfile.NamedTemporaryFile(mode='wb') as temp_file:
        pickle.dump(foo, temp_file)
        temp_file.flush()
    
        # Push file to another machine
        _send_file(temp_file.name)
    

    Or make sure the file is closed before doing anything with it:

    file_name = ''
    with tempfile.NamedTemporaryFile(mode='wb', delete=False) as temp_file:
        file_name = temp_file.name
        pickle.dump(foo, temp_file)
    
    # Push file to another machine
    _send_file(file_name)
    

提交回复
热议问题