This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: datetime.time issue with pickling in PyPy
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: belopolsky, ddddaaaa, miss-islington, p-ganssle, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2020-10-07 10:08 by ddddaaaa, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 22731 merged ddddaaaa, 2020-10-16 20:42
PR 22747 merged miss-islington, 2020-10-18 14:50
PR 22748 merged miss-islington, 2020-10-18 14:50
Messages (5)
msg378155 - (view) Author: Dean (ddddaaaa) * Date: 2020-10-07 10:08
I've run into an issue pickling a datetime.time subclass in PyPy3. I believe it arises because PyPy uses the pure Python implementation of time and time.__reduce_ex__() returns (time, ...) on this line (https://github.com/python/cpython/blob/044a1048ca93d466965afc027b91a5a9eb9ce23c/Lib/datetime.py#L1551) rather than (self.__class__, ...) as it does for datetime.datetime (and in datetime.date.__reduce__()). So when pickle creates the dump it uses the time class rather than my subclass.
msg378214 - (view) Author: Dean (ddddaaaa) * Date: 2020-10-08 06:34
Code demonstrating the issue; the CTimeFoo class is pickled correctly, but TimeFoo isn't.


import builtins
from _datetime import time as ctime

original_importer = builtins.__import__

def my_importer(name, globals, locals, fromlist, level):
    if name == '_datetime':
        raise ImportError

    return original_importer(name, globals, locals, fromlist, level)

builtins.__import__ = my_importer

import datetime

builtins.__import__ = original_importer

import pickle


class CTimeFoo(ctime): pass
class TimeFoo(datetime.time): pass
class DateFoo(datetime.date): pass


if __name__ == "__main__":
    t = DateFoo(2001, 2, 3) 
    d = pickle.dumps(t)  # OK
    print(d)
    # b'\x80\x04\x95#\x00\x00\x00\x00\x00\x00\x00\x8c\x08__main__\x94\x8c\x07DateFoo\x94\x93\x94C\x04\x07\xd1\x02\x03\x94\x85\x94R\x94.'
    t = pickle.loads(d)

    t = CTimeFoo(1, 2, 3)  
    d = pickle.dumps(t)  # OK
    print(d)
    # b'\x80\x04\x95&\x00\x00\x00\x00\x00\x00\x00\x8c\x08__main__\x94\x8c\x08CTimeFoo\x94\x93\x94C\x06\x01\x02\x03\x00\x00\x00\x94\x85\x94R\x94.'
    t = pickle.loads(d)

    t = TimeFoo(1, 2, 3)
    d = pickle.dumps(t)  # :(
    print(d)
    # b'\x80\x04\x95"\x00\x00\x00\x00\x00\x00\x00\x8c\x08datetime\x94\x8c\x04time\x94\x93\x94C\x06\x01\x02\x03\x00\x00\x00\x94\x85\x94R\x94.'
    t = pickle.loads(d)
msg378871 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-10-18 14:49
New changeset c304c9a7efa8751b5bc7526fa95cd5f30aac2b92 by scaramallion in branch 'master':
bpo-41966: Fix pickling pure datetime.time subclasses (GH-22731)
https://github.com/python/cpython/commit/c304c9a7efa8751b5bc7526fa95cd5f30aac2b92
msg378874 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-10-18 15:33
New changeset a055ced9d43630cadc3c1d5edab0884f2c5131ea by Miss Skeleton (bot) in branch '3.9':
bpo-41966: Fix pickling pure datetime.time subclasses (GH-22731) (GH-22747)
https://github.com/python/cpython/commit/a055ced9d43630cadc3c1d5edab0884f2c5131ea
msg378875 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-10-18 15:33
New changeset 1040299e9283609f3de0f6e32a0d43458fe7f4f6 by Miss Skeleton (bot) in branch '3.8':
bpo-41966: Fix pickling pure datetime.time subclasses (GH-22731) (GH-22748)
https://github.com/python/cpython/commit/1040299e9283609f3de0f6e32a0d43458fe7f4f6
History
Date User Action Args
2022-04-11 14:59:36adminsetgithub: 86132
2020-10-18 16:00:16serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-10-18 15:33:31serhiy.storchakasetmessages: + msg378875
2020-10-18 15:33:04serhiy.storchakasetmessages: + msg378874
2020-10-18 14:50:21miss-islingtonsetpull_requests: + pull_request21711
2020-10-18 14:50:13miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request21710
2020-10-18 14:49:57serhiy.storchakasetmessages: + msg378871
2020-10-16 20:42:20ddddaaaasetkeywords: + patch
stage: patch review
pull_requests: + pull_request21695
2020-10-08 07:43:53serhiy.storchakasetnosy: + serhiy.storchaka

versions: - Python 3.5, Python 3.6, Python 3.7
2020-10-08 06:34:22ddddaaaasetmessages: + msg378214
2020-10-07 10:08:31ddddaaaacreate