Skip to content

Commit 8d55faf

Browse files
authored
Remove internal _PyTime_AsLong() function (#141053)
* Replace _PyTime_AsLong() with PyLong_FromInt64() * Replace _PyTime_FromLong() with PyLong_AsInt64().
1 parent 30ab627 commit 8d55faf

File tree

5 files changed

+20
-62
lines changed

5 files changed

+20
-62
lines changed

Include/internal/pycore_time.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,6 @@ extern int _PyTime_FromSecondsDouble(
147147
// Clamp to [PyTime_MIN; PyTime_MAX] on overflow.
148148
extern PyTime_t _PyTime_FromMicrosecondsClamp(PyTime_t us);
149149

150-
// Create a timestamp from a Python int object (number of nanoseconds).
151-
// Export for '_lsprof' shared extension.
152-
PyAPI_FUNC(int) _PyTime_FromLong(PyTime_t *t,
153-
PyObject *obj);
154-
155150
// Convert a number of seconds (Python float or int) to a timestamp.
156151
// Raise an exception and return -1 on error, return 0 on success.
157152
// Export for '_socket' shared extension.
@@ -182,10 +177,6 @@ extern PyTime_t _PyTime_As100Nanoseconds(PyTime_t t,
182177
_PyTime_round_t round);
183178
#endif
184179

185-
// Convert a timestamp (number of nanoseconds) as a Python int object.
186-
// Export for '_testinternalcapi' shared extension.
187-
PyAPI_FUNC(PyObject*) _PyTime_AsLong(PyTime_t t);
188-
189180
#ifndef MS_WINDOWS
190181
// Create a timestamp from a timeval structure.
191182
// Raise an exception and return -1 on overflow, return 0 on success.

Modules/_lsprof.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "pycore_call.h" // _PyObject_CallNoArgs()
77
#include "pycore_ceval.h" // _PyEval_SetProfile()
88
#include "pycore_pystate.h" // _PyThreadState_GET()
9-
#include "pycore_time.h" // _PyTime_FromLong()
9+
#include "pycore_time.h" // _PyTime_FromSecondsObject()
1010
#include "pycore_typeobject.h" // _PyType_GetModuleState()
1111
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
1212

@@ -111,7 +111,7 @@ static PyTime_t CallExternalTimer(ProfilerObject *pObj)
111111
if (pObj->externalTimerUnit > 0.0) {
112112
/* interpret the result as an integer that will be scaled
113113
in profiler_getstats() */
114-
err = _PyTime_FromLong(&result, o);
114+
err = PyLong_AsInt64(o, &result);
115115
}
116116
else {
117117
/* interpret the result as a double measured in seconds.

Modules/_testinternalcapi/pytime.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test_pytime_fromseconds(PyObject *self, PyObject *args)
1717
return NULL;
1818
}
1919
PyTime_t ts = _PyTime_FromSeconds(seconds);
20-
return _PyTime_AsLong(ts);
20+
return PyLong_FromInt64(ts);
2121
}
2222

2323
static int
@@ -49,7 +49,7 @@ test_pytime_fromsecondsobject(PyObject *self, PyObject *args)
4949
if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) {
5050
return NULL;
5151
}
52-
return _PyTime_AsLong(ts);
52+
return PyLong_FromInt64(ts);
5353
}
5454

5555
static PyObject *
@@ -64,7 +64,7 @@ test_PyTime_AsTimeval(PyObject *self, PyObject *args)
6464
return NULL;
6565
}
6666
PyTime_t t;
67-
if (_PyTime_FromLong(&t, obj) < 0) {
67+
if (PyLong_AsInt64(obj, &t) < 0) {
6868
return NULL;
6969
}
7070
struct timeval tv;
@@ -91,7 +91,7 @@ test_PyTime_AsTimeval_clamp(PyObject *self, PyObject *args)
9191
return NULL;
9292
}
9393
PyTime_t t;
94-
if (_PyTime_FromLong(&t, obj) < 0) {
94+
if (PyLong_AsInt64(obj, &t) < 0) {
9595
return NULL;
9696
}
9797
struct timeval tv;
@@ -113,7 +113,7 @@ test_PyTime_AsTimespec(PyObject *self, PyObject *args)
113113
return NULL;
114114
}
115115
PyTime_t t;
116-
if (_PyTime_FromLong(&t, obj) < 0) {
116+
if (PyLong_AsInt64(obj, &t) < 0) {
117117
return NULL;
118118
}
119119
struct timespec ts;
@@ -131,7 +131,7 @@ test_PyTime_AsTimespec_clamp(PyObject *self, PyObject *args)
131131
return NULL;
132132
}
133133
PyTime_t t;
134-
if (_PyTime_FromLong(&t, obj) < 0) {
134+
if (PyLong_AsInt64(obj, &t) < 0) {
135135
return NULL;
136136
}
137137
struct timespec ts;
@@ -149,14 +149,14 @@ test_PyTime_AsMilliseconds(PyObject *self, PyObject *args)
149149
return NULL;
150150
}
151151
PyTime_t t;
152-
if (_PyTime_FromLong(&t, obj) < 0) {
152+
if (PyLong_AsInt64(obj, &t) < 0) {
153153
return NULL;
154154
}
155155
if (check_time_rounding(round) < 0) {
156156
return NULL;
157157
}
158158
PyTime_t ms = _PyTime_AsMilliseconds(t, round);
159-
return _PyTime_AsLong(ms);
159+
return PyLong_FromInt64(ms);
160160
}
161161

162162
static PyObject *
@@ -168,14 +168,14 @@ test_PyTime_AsMicroseconds(PyObject *self, PyObject *args)
168168
return NULL;
169169
}
170170
PyTime_t t;
171-
if (_PyTime_FromLong(&t, obj) < 0) {
171+
if (PyLong_AsInt64(obj, &t) < 0) {
172172
return NULL;
173173
}
174174
if (check_time_rounding(round) < 0) {
175175
return NULL;
176176
}
177177
PyTime_t us = _PyTime_AsMicroseconds(t, round);
178-
return _PyTime_AsLong(us);
178+
return PyLong_FromInt64(us);
179179
}
180180

181181
static PyObject *

Modules/timemodule.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ time_time_ns(PyObject *self, PyObject *unused)
128128
if (PyTime_Time(&t) < 0) {
129129
return NULL;
130130
}
131-
return _PyTime_AsLong(t);
131+
return PyLong_FromInt64(t);
132132
}
133133

134134
PyDoc_STRVAR(time_ns_doc,
@@ -261,7 +261,7 @@ time_clock_gettime_ns_impl(PyObject *module, clockid_t clk_id)
261261
if (_PyTime_FromTimespec(&t, &ts) < 0) {
262262
return NULL;
263263
}
264-
return _PyTime_AsLong(t);
264+
return PyLong_FromInt64(t);
265265
}
266266
#endif /* HAVE_CLOCK_GETTIME */
267267

@@ -310,7 +310,7 @@ time_clock_settime_ns(PyObject *self, PyObject *args)
310310
return NULL;
311311
}
312312

313-
if (_PyTime_FromLong(&t, obj) < 0) {
313+
if (PyLong_AsInt64(obj, &t) < 0) {
314314
return NULL;
315315
}
316316
if (_PyTime_AsTimespec(t, &ts) == -1) {
@@ -1216,7 +1216,7 @@ time_monotonic_ns(PyObject *self, PyObject *unused)
12161216
if (PyTime_Monotonic(&t) < 0) {
12171217
return NULL;
12181218
}
1219-
return _PyTime_AsLong(t);
1219+
return PyLong_FromInt64(t);
12201220
}
12211221

12221222
PyDoc_STRVAR(monotonic_ns_doc,
@@ -1248,7 +1248,7 @@ time_perf_counter_ns(PyObject *self, PyObject *unused)
12481248
if (PyTime_PerfCounter(&t) < 0) {
12491249
return NULL;
12501250
}
1251-
return _PyTime_AsLong(t);
1251+
return PyLong_FromInt64(t);
12521252
}
12531253

12541254
PyDoc_STRVAR(perf_counter_ns_doc,
@@ -1437,7 +1437,7 @@ time_process_time_ns(PyObject *module, PyObject *unused)
14371437
if (py_process_time(state, &t, NULL) < 0) {
14381438
return NULL;
14391439
}
1440-
return _PyTime_AsLong(t);
1440+
return PyLong_FromInt64(t);
14411441
}
14421442

14431443
PyDoc_STRVAR(process_time_ns_doc,
@@ -1610,7 +1610,7 @@ time_thread_time_ns(PyObject *self, PyObject *unused)
16101610
if (_PyTime_GetThreadTimeWithInfo(&t, NULL) < 0) {
16111611
return NULL;
16121612
}
1613-
return _PyTime_AsLong(t);
1613+
return PyLong_FromInt64(t);
16141614
}
16151615

16161616
PyDoc_STRVAR(thread_time_ns_doc,

Python/pytime.c

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "pycore_initconfig.h" // _PyStatus_ERR
33
#include "pycore_pystate.h" // _Py_AssertHoldsTstate()
44
#include "pycore_runtime.h" // _PyRuntime
5-
#include "pycore_time.h" // PyTime_t
5+
#include "pycore_time.h" // export _PyLong_FromTime_t()
66

77
#include <time.h> // gmtime_r()
88
#ifdef HAVE_SYS_TIME_H
@@ -472,31 +472,6 @@ _PyTime_FromMicrosecondsClamp(PyTime_t us)
472472
}
473473

474474

475-
int
476-
_PyTime_FromLong(PyTime_t *tp, PyObject *obj)
477-
{
478-
if (!PyLong_Check(obj)) {
479-
PyErr_Format(PyExc_TypeError, "expect int, got %s",
480-
Py_TYPE(obj)->tp_name);
481-
return -1;
482-
}
483-
484-
static_assert(sizeof(long long) == sizeof(PyTime_t),
485-
"PyTime_t is not long long");
486-
long long nsec = PyLong_AsLongLong(obj);
487-
if (nsec == -1 && PyErr_Occurred()) {
488-
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
489-
pytime_overflow();
490-
}
491-
return -1;
492-
}
493-
494-
PyTime_t t = (PyTime_t)nsec;
495-
*tp = t;
496-
return 0;
497-
}
498-
499-
500475
#ifdef HAVE_CLOCK_GETTIME
501476
static int
502477
pytime_fromtimespec(PyTime_t *tp, const struct timespec *ts, int raise_exc)
@@ -658,14 +633,6 @@ PyTime_AsSecondsDouble(PyTime_t ns)
658633
}
659634

660635

661-
PyObject *
662-
_PyTime_AsLong(PyTime_t ns)
663-
{
664-
static_assert(sizeof(long long) >= sizeof(PyTime_t),
665-
"PyTime_t is larger than long long");
666-
return PyLong_FromLongLong((long long)ns);
667-
}
668-
669636
int
670637
_PyTime_FromSecondsDouble(double seconds, _PyTime_round_t round, PyTime_t *result)
671638
{

0 commit comments

Comments
 (0)