]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Remove internal _PyTime_AsLong() function (#141053)
authorVictor Stinner <vstinner@python.org>
Wed, 5 Nov 2025 17:37:06 +0000 (18:37 +0100)
committerGitHub <noreply@github.com>
Wed, 5 Nov 2025 17:37:06 +0000 (18:37 +0100)
* Replace _PyTime_AsLong() with PyLong_FromInt64()
* Replace _PyTime_FromLong() with PyLong_AsInt64().

Include/internal/pycore_time.h
Modules/_lsprof.c
Modules/_testinternalcapi/pytime.c
Modules/timemodule.c
Python/pytime.c

index 23312471c6584efee837e0b5c51cf6401391afa5..b671225ca6ea44d642a48030846dca4240be12fd 100644 (file)
@@ -147,11 +147,6 @@ extern int _PyTime_FromSecondsDouble(
 // Clamp to [PyTime_MIN; PyTime_MAX] on overflow.
 extern PyTime_t _PyTime_FromMicrosecondsClamp(PyTime_t us);
 
-// Create a timestamp from a Python int object (number of nanoseconds).
-// Export for '_lsprof' shared extension.
-PyAPI_FUNC(int) _PyTime_FromLong(PyTime_t *t,
-    PyObject *obj);
-
 // Convert a number of seconds (Python float or int) to a timestamp.
 // Raise an exception and return -1 on error, return 0 on success.
 // Export for '_socket' shared extension.
@@ -182,10 +177,6 @@ extern PyTime_t _PyTime_As100Nanoseconds(PyTime_t t,
     _PyTime_round_t round);
 #endif
 
-// Convert a timestamp (number of nanoseconds) as a Python int object.
-// Export for '_testinternalcapi' shared extension.
-PyAPI_FUNC(PyObject*) _PyTime_AsLong(PyTime_t t);
-
 #ifndef MS_WINDOWS
 // Create a timestamp from a timeval structure.
 // Raise an exception and return -1 on overflow, return 0 on success.
index c20dbc3f4f4bfbda168df3e51c88ac10d151c648..025a3fac46e59babbabc59d5fdb9ede2c17e8b10 100644 (file)
@@ -6,7 +6,7 @@
 #include "pycore_call.h"          // _PyObject_CallNoArgs()
 #include "pycore_ceval.h"         // _PyEval_SetProfile()
 #include "pycore_pystate.h"       // _PyThreadState_GET()
-#include "pycore_time.h"          // _PyTime_FromLong()
+#include "pycore_time.h"          // _PyTime_FromSecondsObject()
 #include "pycore_typeobject.h"    // _PyType_GetModuleState()
 #include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
 
@@ -111,7 +111,7 @@ static PyTime_t CallExternalTimer(ProfilerObject *pObj)
     if (pObj->externalTimerUnit > 0.0) {
         /* interpret the result as an integer that will be scaled
            in profiler_getstats() */
-        err = _PyTime_FromLong(&result, o);
+        err = PyLong_AsInt64(o, &result);
     }
     else {
         /* interpret the result as a double measured in seconds.
index 2b0a205d158a9638fc3915699861796c5e8900ad..7fb100c41a1b2b421ed28091442e78223b5d2dec 100644 (file)
@@ -17,7 +17,7 @@ test_pytime_fromseconds(PyObject *self, PyObject *args)
         return NULL;
     }
     PyTime_t ts = _PyTime_FromSeconds(seconds);
-    return _PyTime_AsLong(ts);
+    return PyLong_FromInt64(ts);
 }
 
 static int
@@ -49,7 +49,7 @@ test_pytime_fromsecondsobject(PyObject *self, PyObject *args)
     if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) {
         return NULL;
     }
-    return _PyTime_AsLong(ts);
+    return PyLong_FromInt64(ts);
 }
 
 static PyObject *
@@ -64,7 +64,7 @@ test_PyTime_AsTimeval(PyObject *self, PyObject *args)
         return NULL;
     }
     PyTime_t t;
-    if (_PyTime_FromLong(&t, obj) < 0) {
+    if (PyLong_AsInt64(obj, &t) < 0) {
         return NULL;
     }
     struct timeval tv;
@@ -91,7 +91,7 @@ test_PyTime_AsTimeval_clamp(PyObject *self, PyObject *args)
         return NULL;
     }
     PyTime_t t;
-    if (_PyTime_FromLong(&t, obj) < 0) {
+    if (PyLong_AsInt64(obj, &t) < 0) {
         return NULL;
     }
     struct timeval tv;
@@ -113,7 +113,7 @@ test_PyTime_AsTimespec(PyObject *self, PyObject *args)
         return NULL;
     }
     PyTime_t t;
-    if (_PyTime_FromLong(&t, obj) < 0) {
+    if (PyLong_AsInt64(obj, &t) < 0) {
         return NULL;
     }
     struct timespec ts;
@@ -131,7 +131,7 @@ test_PyTime_AsTimespec_clamp(PyObject *self, PyObject *args)
         return NULL;
     }
     PyTime_t t;
-    if (_PyTime_FromLong(&t, obj) < 0) {
+    if (PyLong_AsInt64(obj, &t) < 0) {
         return NULL;
     }
     struct timespec ts;
@@ -149,14 +149,14 @@ test_PyTime_AsMilliseconds(PyObject *self, PyObject *args)
         return NULL;
     }
     PyTime_t t;
-    if (_PyTime_FromLong(&t, obj) < 0) {
+    if (PyLong_AsInt64(obj, &t) < 0) {
         return NULL;
     }
     if (check_time_rounding(round) < 0) {
         return NULL;
     }
     PyTime_t ms = _PyTime_AsMilliseconds(t, round);
-    return _PyTime_AsLong(ms);
+    return PyLong_FromInt64(ms);
 }
 
 static PyObject *
@@ -168,14 +168,14 @@ test_PyTime_AsMicroseconds(PyObject *self, PyObject *args)
         return NULL;
     }
     PyTime_t t;
-    if (_PyTime_FromLong(&t, obj) < 0) {
+    if (PyLong_AsInt64(obj, &t) < 0) {
         return NULL;
     }
     if (check_time_rounding(round) < 0) {
         return NULL;
     }
     PyTime_t us = _PyTime_AsMicroseconds(t, round);
-    return _PyTime_AsLong(us);
+    return PyLong_FromInt64(us);
 }
 
 static PyObject *
index 3271d87ddc27f2b3914b0127b440e45f8aad96f0..3946d18479e253bfd4e75c061d63f0fc5bc96f8b 100644 (file)
@@ -128,7 +128,7 @@ time_time_ns(PyObject *self, PyObject *unused)
     if (PyTime_Time(&t) < 0) {
         return NULL;
     }
-    return _PyTime_AsLong(t);
+    return PyLong_FromInt64(t);
 }
 
 PyDoc_STRVAR(time_ns_doc,
@@ -261,7 +261,7 @@ time_clock_gettime_ns_impl(PyObject *module, clockid_t clk_id)
     if (_PyTime_FromTimespec(&t, &ts) < 0) {
         return NULL;
     }
-    return _PyTime_AsLong(t);
+    return PyLong_FromInt64(t);
 }
 #endif   /* HAVE_CLOCK_GETTIME */
 
@@ -310,7 +310,7 @@ time_clock_settime_ns(PyObject *self, PyObject *args)
         return NULL;
     }
 
-    if (_PyTime_FromLong(&t, obj) < 0) {
+    if (PyLong_AsInt64(obj, &t) < 0) {
         return NULL;
     }
     if (_PyTime_AsTimespec(t, &ts) == -1) {
@@ -1216,7 +1216,7 @@ time_monotonic_ns(PyObject *self, PyObject *unused)
     if (PyTime_Monotonic(&t) < 0) {
         return NULL;
     }
-    return _PyTime_AsLong(t);
+    return PyLong_FromInt64(t);
 }
 
 PyDoc_STRVAR(monotonic_ns_doc,
@@ -1248,7 +1248,7 @@ time_perf_counter_ns(PyObject *self, PyObject *unused)
     if (PyTime_PerfCounter(&t) < 0) {
         return NULL;
     }
-    return _PyTime_AsLong(t);
+    return PyLong_FromInt64(t);
 }
 
 PyDoc_STRVAR(perf_counter_ns_doc,
@@ -1437,7 +1437,7 @@ time_process_time_ns(PyObject *module, PyObject *unused)
     if (py_process_time(state, &t, NULL) < 0) {
         return NULL;
     }
-    return _PyTime_AsLong(t);
+    return PyLong_FromInt64(t);
 }
 
 PyDoc_STRVAR(process_time_ns_doc,
@@ -1610,7 +1610,7 @@ time_thread_time_ns(PyObject *self, PyObject *unused)
     if (_PyTime_GetThreadTimeWithInfo(&t, NULL) < 0) {
         return NULL;
     }
-    return _PyTime_AsLong(t);
+    return PyLong_FromInt64(t);
 }
 
 PyDoc_STRVAR(thread_time_ns_doc,
index 0206467364f8940ec83a1c53b281f8c3ea04150b..2f3d854428b4bfb09e3abfc1c0a2547bb5291ebc 100644 (file)
@@ -2,7 +2,7 @@
 #include "pycore_initconfig.h"    // _PyStatus_ERR
 #include "pycore_pystate.h"       // _Py_AssertHoldsTstate()
 #include "pycore_runtime.h"       // _PyRuntime
-#include "pycore_time.h"          // PyTime_t
+#include "pycore_time.h"          // export _PyLong_FromTime_t()
 
 #include <time.h>                 // gmtime_r()
 #ifdef HAVE_SYS_TIME_H
@@ -472,31 +472,6 @@ _PyTime_FromMicrosecondsClamp(PyTime_t us)
 }
 
 
-int
-_PyTime_FromLong(PyTime_t *tp, PyObject *obj)
-{
-    if (!PyLong_Check(obj)) {
-        PyErr_Format(PyExc_TypeError, "expect int, got %s",
-                     Py_TYPE(obj)->tp_name);
-        return -1;
-    }
-
-    static_assert(sizeof(long long) == sizeof(PyTime_t),
-                  "PyTime_t is not long long");
-    long long nsec = PyLong_AsLongLong(obj);
-    if (nsec == -1 && PyErr_Occurred()) {
-        if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
-            pytime_overflow();
-        }
-        return -1;
-    }
-
-    PyTime_t t = (PyTime_t)nsec;
-    *tp = t;
-    return 0;
-}
-
-
 #ifdef HAVE_CLOCK_GETTIME
 static int
 pytime_fromtimespec(PyTime_t *tp, const struct timespec *ts, int raise_exc)
@@ -658,14 +633,6 @@ PyTime_AsSecondsDouble(PyTime_t ns)
 }
 
 
-PyObject *
-_PyTime_AsLong(PyTime_t ns)
-{
-    static_assert(sizeof(long long) >= sizeof(PyTime_t),
-                  "PyTime_t is larger than long long");
-    return PyLong_FromLongLong((long long)ns);
-}
-
 int
 _PyTime_FromSecondsDouble(double seconds, _PyTime_round_t round, PyTime_t *result)
 {