0

I'm trying to loads a pickle string in python3.8,but pickle.loads get error _pickle.UnpicklingError: slot state is not a dictionary,this string is read from postgresql,In python2 it is working, I found python3's version pickle only support loads bytes type data,but python2 support str type. I used six library to loads string, but get a another error UnpicklingError: slot state is not a dictionary,I tried to print slot, it is a list type object,thats a confused problem. my python2 code:

a = ""  # the dumps string
result = pickle.loads(a)  # its working
print "result type", type(result)  # <type 'dict'>

python3 code:

a = ""
result = pickle.loads(a.encode('utf-8'), fix_imports=True, encoding='utf-8')
# error: _pickle.UnpicklingError: slot state is not a dictionary
Auraxc
  • 51
  • 5

1 Answers1

0

The encoding of an empty string does not produce the equivalent of a pickled empty string therefore pickle.loads() can't make sense of the input bytes.

Consider this:

import pickle

STRING = ''

p = pickle.dumps(STRING)
print(len(p))
print('>', pickle.loads(p), '<', sep='')

Output:

15
><

Note:

Note that the number of bytes needed to represent a pickled empty string is 15

DarkKnight
  • 19,739
  • 3
  • 6
  • 22
  • thank you your answer my question,finally I found this mistake because the old python2 code generated result have some mistake,thats very stupid – Auraxc Oct 18 '22 at 01:28