From 2cf3cd8df69b8b258d687731d5587cfafc3f2c63 Mon Sep 17 00:00:00 2001 From: AN Long Date: Thu, 19 Sep 2024 23:49:38 +0800 Subject: [PATCH 1/5] Improve datetime.fromtimestamp's error message --- Lib/_pydatetime.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py index f8e121eb79a04d..edf6c1bc316cda 100644 --- a/Lib/_pydatetime.py +++ b/Lib/_pydatetime.py @@ -999,7 +999,8 @@ def __new__(cls, year, month=None, day=None): def fromtimestamp(cls, t): "Construct a date from a POSIX timestamp (like time.time())." if t is None: - raise TypeError("'NoneType' object cannot be interpreted as an integer") + raise TypeError("'NoneType' object cannot be interpreted " + "as an integer or a float") y, m, d, hh, mm, ss, weekday, jday, dst = _time.localtime(t) return cls(y, m, d) From 3e9404e2a01eecb8b17ae14cb0f8a0b174fc1756 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 19 Sep 2024 16:00:29 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-09-19-16-00-22.gh-issue-111513.6jHm02.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-09-19-16-00-22.gh-issue-111513.6jHm02.rst diff --git a/Misc/NEWS.d/next/Library/2024-09-19-16-00-22.gh-issue-111513.6jHm02.rst b/Misc/NEWS.d/next/Library/2024-09-19-16-00-22.gh-issue-111513.6jHm02.rst new file mode 100644 index 00000000000000..58d35a14bc6465 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-09-19-16-00-22.gh-issue-111513.6jHm02.rst @@ -0,0 +1 @@ +Improving the error message that may be raised by :meth:`datetime.date.fromtimestamp`. From 70b69ac587d189c44f6da369f2da99dff06a996f Mon Sep 17 00:00:00 2001 From: AN Long Date: Fri, 20 Sep 2024 00:02:28 +0800 Subject: [PATCH 3/5] Update news entry --- .../next/Library/2024-09-19-16-00-22.gh-issue-111513.6jHm02.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-09-19-16-00-22.gh-issue-111513.6jHm02.rst b/Misc/NEWS.d/next/Library/2024-09-19-16-00-22.gh-issue-111513.6jHm02.rst index 58d35a14bc6465..c6b85f9cd72255 100644 --- a/Misc/NEWS.d/next/Library/2024-09-19-16-00-22.gh-issue-111513.6jHm02.rst +++ b/Misc/NEWS.d/next/Library/2024-09-19-16-00-22.gh-issue-111513.6jHm02.rst @@ -1 +1 @@ -Improving the error message that may be raised by :meth:`datetime.date.fromtimestamp`. +Improve the error message that may be raised by :meth:`datetime.date.fromtimestamp`. From 076e29f125104b85271aeab109c0f1007ec168f8 Mon Sep 17 00:00:00 2001 From: AN Long Date: Fri, 20 Sep 2024 00:41:21 +0800 Subject: [PATCH 4/5] Fix the right function --- Lib/_pydatetime.py | 3 +-- Python/pytime.c | 5 +++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py index edf6c1bc316cda..f8e121eb79a04d 100644 --- a/Lib/_pydatetime.py +++ b/Lib/_pydatetime.py @@ -999,8 +999,7 @@ def __new__(cls, year, month=None, day=None): def fromtimestamp(cls, t): "Construct a date from a POSIX timestamp (like time.time())." if t is None: - raise TypeError("'NoneType' object cannot be interpreted " - "as an integer or a float") + raise TypeError("'NoneType' object cannot be interpreted as an integer") y, m, d, hh, mm, ss, weekday, jday, dst = _time.localtime(t) return cls(y, m, d) diff --git a/Python/pytime.c b/Python/pytime.c index cd76970718622f..147ca1dc57fdae 100644 --- a/Python/pytime.c +++ b/Python/pytime.c @@ -387,6 +387,11 @@ pytime_object_to_denominator(PyObject *obj, time_t *sec, long *numerator, *sec = _PyLong_AsTime_t(obj); *numerator = 0; if (*sec == (time_t)-1 && PyErr_Occurred()) { + if (PyErr_ExceptionMatches(PyExc_TypeError)) { + PyErr_Format(PyExc_TypeError, + "argument must be int or float, not %.200s", + Py_TYPE(obj)->tp_name); + } return -1; } return 0; From 37790c84dfa171bd1b168c48d08130d1e2a58457 Mon Sep 17 00:00:00 2001 From: AN Long Date: Fri, 20 Sep 2024 19:36:57 +0800 Subject: [PATCH 5/5] Utilize PEP737 formatter Co-authored-by: Victor Stinner --- Python/pytime.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Python/pytime.c b/Python/pytime.c index 147ca1dc57fdae..2b37cd991ef4e4 100644 --- a/Python/pytime.c +++ b/Python/pytime.c @@ -389,8 +389,7 @@ pytime_object_to_denominator(PyObject *obj, time_t *sec, long *numerator, if (*sec == (time_t)-1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_TypeError)) { PyErr_Format(PyExc_TypeError, - "argument must be int or float, not %.200s", - Py_TYPE(obj)->tp_name); + "argument must be int or float, not %T", obj); } return -1; }