Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e4c34f0

Browse filesBrowse files
authored
gh-110850: Cleanup PyTime API: PyTime_t are nanoseconds (#115753)
PyTime_t no longer uses an arbitrary unit, it's always a number of nanoseconds (64-bit signed integer). * Rename _PyTime_FromNanosecondsObject() to _PyTime_FromLong(). * Rename _PyTime_AsNanosecondsObject() to _PyTime_AsLong(). * Remove pytime_from_nanoseconds(). * Remove pytime_as_nanoseconds(). * Remove _PyTime_FromNanoseconds().
1 parent 69ab930 commit e4c34f0
Copy full SHA for e4c34f0

File tree

Expand file treeCollapse file tree

8 files changed

+57
-100
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+57
-100
lines changed

‎Include/internal/pycore_time.h

Copy file name to clipboardExpand all lines: Include/internal/pycore_time.h
+4-9Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,13 @@ PyAPI_FUNC(PyTime_t) _PyTime_FromSecondsDouble(double seconds, _PyTime_round_t r
140140
#define _PYTIME_FROMSECONDS(seconds) \
141141
((PyTime_t)(seconds) * (1000 * 1000 * 1000))
142142

143-
// Create a timestamp from a number of nanoseconds.
144-
// Export for '_testinternalcapi' shared extension.
145-
PyAPI_FUNC(PyTime_t) _PyTime_FromNanoseconds(PyTime_t ns);
146-
147143
// Create a timestamp from a number of microseconds.
148144
// Clamp to [PyTime_MIN; PyTime_MAX] on overflow.
149145
extern PyTime_t _PyTime_FromMicrosecondsClamp(PyTime_t us);
150146

151-
// Create a timestamp from nanoseconds (Python int).
147+
// Create a timestamp from a Python int object (number of nanoseconds).
152148
// Export for '_lsprof' shared extension.
153-
PyAPI_FUNC(int) _PyTime_FromNanosecondsObject(PyTime_t *t,
149+
PyAPI_FUNC(int) _PyTime_FromLong(PyTime_t *t,
154150
PyObject *obj);
155151

156152
// Convert a number of seconds (Python float or int) to a timestamp.
@@ -183,10 +179,9 @@ extern PyTime_t _PyTime_As100Nanoseconds(PyTime_t t,
183179
_PyTime_round_t round);
184180
#endif
185181

186-
// Convert timestamp to a number of nanoseconds (10^-9 seconds) as a Python int
187-
// object.
182+
// Convert a timestamp (number of nanoseconds) as a Python int object.
188183
// Export for '_testinternalcapi' shared extension.
189-
PyAPI_FUNC(PyObject*) _PyTime_AsNanosecondsObject(PyTime_t t);
184+
PyAPI_FUNC(PyObject*) _PyTime_AsLong(PyTime_t t);
190185

191186
#ifndef MS_WINDOWS
192187
// Create a timestamp from a timeval structure.

‎Modules/_lsprof.c

Copy file name to clipboardExpand all lines: Modules/_lsprof.c
+2-2Lines 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_FromNanosecondsObject()
9+
#include "pycore_time.h" // _PyTime_FromLong()
1010

1111
#include "rotatingtree.h"
1212

@@ -98,7 +98,7 @@ static PyTime_t CallExternalTimer(ProfilerObject *pObj)
9898
if (pObj->externalTimerUnit > 0.0) {
9999
/* interpret the result as an integer that will be scaled
100100
in profiler_getstats() */
101-
err = _PyTime_FromNanosecondsObject(&result, o);
101+
err = _PyTime_FromLong(&result, o);
102102
}
103103
else {
104104
/* interpret the result as a double measured in seconds.

‎Modules/_testinternalcapi/pytime.c

Copy file name to clipboardExpand all lines: Modules/_testinternalcapi/pytime.c
+10-12Lines changed: 10 additions & 12 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_AsNanosecondsObject(ts);
20+
return _PyTime_AsLong(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_AsNanosecondsObject(ts);
52+
return _PyTime_AsLong(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_FromNanosecondsObject(&t, obj) < 0) {
67+
if (_PyTime_FromLong(&t, obj) < 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_FromNanosecondsObject(&t, obj) < 0) {
94+
if (_PyTime_FromLong(&t, obj) < 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_FromNanosecondsObject(&t, obj) < 0) {
116+
if (_PyTime_FromLong(&t, obj) < 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_FromNanosecondsObject(&t, obj) < 0) {
134+
if (_PyTime_FromLong(&t, obj) < 0) {
135135
return NULL;
136136
}
137137
struct timespec ts;
@@ -149,15 +149,14 @@ test_PyTime_AsMilliseconds(PyObject *self, PyObject *args)
149149
return NULL;
150150
}
151151
PyTime_t t;
152-
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
152+
if (_PyTime_FromLong(&t, obj) < 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-
PyTime_t ns = _PyTime_FromNanoseconds(ms);
160-
return _PyTime_AsNanosecondsObject(ns);
159+
return _PyTime_AsLong(ms);
161160
}
162161

163162
static PyObject *
@@ -169,15 +168,14 @@ test_PyTime_AsMicroseconds(PyObject *self, PyObject *args)
169168
return NULL;
170169
}
171170
PyTime_t t;
172-
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
171+
if (_PyTime_FromLong(&t, obj) < 0) {
173172
return NULL;
174173
}
175174
if (check_time_rounding(round) < 0) {
176175
return NULL;
177176
}
178177
PyTime_t us = _PyTime_AsMicroseconds(t, round);
179-
PyTime_t ns = _PyTime_FromNanoseconds(us);
180-
return _PyTime_AsNanosecondsObject(ns);
178+
return _PyTime_AsLong(us);
181179
}
182180

183181
static PyObject *

‎Modules/timemodule.c

Copy file name to clipboardExpand all lines: Modules/timemodule.c
+15-18Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ time_time_ns(PyObject *self, PyObject *unused)
127127
if (PyTime_Time(&t) < 0) {
128128
return NULL;
129129
}
130-
return _PyTime_AsNanosecondsObject(t);
130+
return _PyTime_AsLong(t);
131131
}
132132

133133
PyDoc_STRVAR(time_ns_doc,
@@ -164,8 +164,7 @@ py_clock(time_module_state *state, PyTime_t *tp, _Py_clock_info_t *info)
164164
"or its value cannot be represented");
165165
return -1;
166166
}
167-
PyTime_t ns = _PyTimeFraction_Mul(ticks, base);
168-
*tp = _PyTime_FromNanoseconds(ns);
167+
*tp = _PyTimeFraction_Mul(ticks, base);
169168
return 0;
170169
}
171170
#endif /* HAVE_CLOCK */
@@ -259,7 +258,7 @@ time_clock_gettime_ns_impl(PyObject *module, clockid_t clk_id)
259258
if (_PyTime_FromTimespec(&t, &ts) < 0) {
260259
return NULL;
261260
}
262-
return _PyTime_AsNanosecondsObject(t);
261+
return _PyTime_AsLong(t);
263262
}
264263
#endif /* HAVE_CLOCK_GETTIME */
265264

@@ -308,7 +307,7 @@ time_clock_settime_ns(PyObject *self, PyObject *args)
308307
return NULL;
309308
}
310309

311-
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
310+
if (_PyTime_FromLong(&t, obj) < 0) {
312311
return NULL;
313312
}
314313
if (_PyTime_AsTimespec(t, &ts) == -1) {
@@ -1170,7 +1169,7 @@ time_monotonic_ns(PyObject *self, PyObject *unused)
11701169
if (PyTime_Monotonic(&t) < 0) {
11711170
return NULL;
11721171
}
1173-
return _PyTime_AsNanosecondsObject(t);
1172+
return _PyTime_AsLong(t);
11741173
}
11751174

11761175
PyDoc_STRVAR(monotonic_ns_doc,
@@ -1202,7 +1201,7 @@ time_perf_counter_ns(PyObject *self, PyObject *unused)
12021201
if (PyTime_PerfCounter(&t) < 0) {
12031202
return NULL;
12041203
}
1205-
return _PyTime_AsNanosecondsObject(t);
1204+
return _PyTime_AsLong(t);
12061205
}
12071206

12081207
PyDoc_STRVAR(perf_counter_ns_doc,
@@ -1233,7 +1232,7 @@ process_time_times(time_module_state *state, PyTime_t *tp,
12331232
PyTime_t ns;
12341233
ns = _PyTimeFraction_Mul(process.tms_utime, base);
12351234
ns += _PyTimeFraction_Mul(process.tms_stime, base);
1236-
*tp = _PyTime_FromNanoseconds(ns);
1235+
*tp = ns;
12371236
return 1;
12381237
}
12391238
#endif
@@ -1247,7 +1246,7 @@ py_process_time(time_module_state *state, PyTime_t *tp,
12471246
HANDLE process;
12481247
FILETIME creation_time, exit_time, kernel_time, user_time;
12491248
ULARGE_INTEGER large;
1250-
PyTime_t ktime, utime, t;
1249+
PyTime_t ktime, utime;
12511250
BOOL ok;
12521251

12531252
process = GetCurrentProcess();
@@ -1274,8 +1273,7 @@ py_process_time(time_module_state *state, PyTime_t *tp,
12741273
utime = large.QuadPart;
12751274

12761275
/* ktime and utime have a resolution of 100 nanoseconds */
1277-
t = _PyTime_FromNanoseconds((ktime + utime) * 100);
1278-
*tp = t;
1276+
*tp = (ktime + utime) * 100;
12791277
return 0;
12801278
#else
12811279

@@ -1383,7 +1381,7 @@ time_process_time_ns(PyObject *module, PyObject *unused)
13831381
if (py_process_time(state, &t, NULL) < 0) {
13841382
return NULL;
13851383
}
1386-
return _PyTime_AsNanosecondsObject(t);
1384+
return _PyTime_AsLong(t);
13871385
}
13881386

13891387
PyDoc_STRVAR(process_time_ns_doc,
@@ -1401,7 +1399,7 @@ _PyTime_GetThreadTimeWithInfo(PyTime_t *tp, _Py_clock_info_t *info)
14011399
HANDLE thread;
14021400
FILETIME creation_time, exit_time, kernel_time, user_time;
14031401
ULARGE_INTEGER large;
1404-
PyTime_t ktime, utime, t;
1402+
PyTime_t ktime, utime;
14051403
BOOL ok;
14061404

14071405
thread = GetCurrentThread();
@@ -1428,8 +1426,7 @@ _PyTime_GetThreadTimeWithInfo(PyTime_t *tp, _Py_clock_info_t *info)
14281426
utime = large.QuadPart;
14291427

14301428
/* ktime and utime have a resolution of 100 nanoseconds */
1431-
t = _PyTime_FromNanoseconds((ktime + utime) * 100);
1432-
*tp = t;
1429+
*tp = (ktime + utime) * 100;
14331430
return 0;
14341431
}
14351432

@@ -1453,7 +1450,7 @@ _PyTime_GetThreadTimeWithInfo(PyTime_t *tp, _Py_clock_info_t *info)
14531450
info->adjustable = 0;
14541451
info->resolution = 1e-9;
14551452
}
1456-
*tp = _PyTime_FromNanoseconds(tc.stime + tc.utime);
1453+
*tp = (tc.stime + tc.utime);
14571454
return 0;
14581455
}
14591456

@@ -1470,7 +1467,7 @@ _PyTime_GetThreadTimeWithInfo(PyTime_t *tp, _Py_clock_info_t *info)
14701467
info->monotonic = 1;
14711468
info->adjustable = 0;
14721469
}
1473-
*tp = _PyTime_FromNanoseconds(gethrvtime());
1470+
*tp = gethrvtime();
14741471
return 0;
14751472
}
14761473

@@ -1550,7 +1547,7 @@ time_thread_time_ns(PyObject *self, PyObject *unused)
15501547
if (_PyTime_GetThreadTimeWithInfo(&t, NULL) < 0) {
15511548
return NULL;
15521549
}
1553-
return _PyTime_AsNanosecondsObject(t);
1550+
return _PyTime_AsLong(t);
15541551
}
15551552

15561553
PyDoc_STRVAR(thread_time_ns_doc,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.