This seems to have been fixed with 8452ca15f41061c8a6297d7956df22ab476d4df4. I checked out your example locally and renamed pickle.py to another name to avoid module collision. Ran the below commands to pickle using Python 2 and to unpickle from master before and after the relevant commit. The fix is present in 3.7 and 3.6.
➜ bpo36499 python2 pickledatetime.py
# with 8452ca15f41061c8a6297d7956df22ab476d4df4
➜ bpo36499 ../cpython/python.exe unpickledatetime.py
Pickle: {'timenode': datetime.datetime(2019, 4, 2, 15, 13, 2, 675110)}
# with 8452ca15f41061c8a6297d7956df22ab476d4df4~1
➜ bpo36499 ../cpython/python.exe unpickledatetime.py
Traceback (most recent call last):
File "unpickledatetime.py", line 22, in <module>
main()
File "unpickledatetime.py", line 18, in main
pkl = unpickler.load()
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/pickle.py", line 1081, in load
dispatch[key[0]](self)
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/pickle.py", line 1429, in load_reduce
stack[-1] = func(*args)
TypeError: an integer is required (got type str)
# pickledatetime.py
import io
import pickle
from datetime import datetime
def main():
"""
Uses python 2.7 to create a pickle file with a datetime object inside a dictionary.
"""
data = {}
data['timenode'] = datetime.now()
with io.open('written_by_py27.pickle', 'wb') as handle:
pickle.dump(data, handle, protocol=pickle.HIGHEST_PROTOCOL)
if __name__ == "__main__":
main()
# unpickledatetime.py
import io
import pickle
def main():
"""
Attempts to unpickle a dictionary with a datatype object inside
"""
with io.open('written_by_py27.pickle', 'rb') as handle:
unpickler = pickle._Unpickler(handle)
unpickler.encoding = 'latin1'
pkl = unpickler.load()
print("Pickle: ", pkl)
if __name__ == "__main__":
main()
|