Page MenuHomePhabricator

Representation for Timestamp object is different between CPython and Pypy
Closed, ResolvedPublicBUG REPORT

Description

Steps to replicate the issue (include links if applicable):

>>> from pywikibot import Timestamp
>>> ts = Timestamp.now()
>>> ts

What happens?:
Timestamp(2022, 12, 24, 10, 49, 6, 958707) is given for CPython but pywikibot.time.Timestamp(2022, 12, 24, 10, 49, 6, 958707) for Pypy.
https://github.com/wikimedia/pywikibot/actions/runs/3768283603/jobs/6406590317

Timestamp inherits from datetime.datetime. The repr of datetime is definded as:

def __repr__(self):
    """Convert to formal string, for repr()."""
    L = [self._year, self._month, self._day,  # These are never zero
         self._hour, self._minute, self._second, self._microsecond]
    if L[-1] == 0:
        del L[-1]
    if L[-1] == 0:
        del L[-1]
    s = "%s.%s(%s)" % (self.__class__.__module__,
                       self.__class__.__qualname__,
                       ", ".join(map(str, L)))
    if self._tzinfo is not None:
        assert s[-1:] == ")"
        s = s[:-1] + ", tzinfo=%r" % self._tzinfo + ")"
    if self._fold:
        assert s[-1:] == ")"
        s = s[:-1] + ", fold=1)"
    return s

but obviously only pypy takes this into account. Neither datetime.repr nor Timestamp.repr calls it because the datetime implementation is overridden by _datetime:

try:
    from _datetime import *
except ImportError:
    pass

I guess this is a C-implementation.

What should have happened instead?:
The result should be unique.

Software version (skip for WMF-hosted wikis like Wikipedia):
Tested with CPython 3.6-3.12 and Pypy 3.7

Event Timeline

Xqt changed the task status from Open to In Progress.Dec 24 2022, 10:13 AM
Xqt claimed this task.
Xqt triaged this task as High priority.
Xqt updated the task description. (Show Details)

Change 871263 had a related patch set uploaded (by Xqt; author: Xqt):

[pywikibot/core@master] [bugfix] Add Timestamp.__repr__() method to unify the result

https://gerrit.wikimedia.org/r/871263

Change 871263 merged by jenkins-bot:

[pywikibot/core@master] [bugfix] Add Timestamp.__repr__() method to unify the result

https://gerrit.wikimedia.org/r/871263