mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 05:31:20 +00:00 
			
		
		
		
	gh-106316: Remove pytime.h header file (#106317)
Remove the "cpython/pytime.h" header file: it only contained private functions. Move functions to the internal pycore_time.h header file. Move tests from _testcapi to _testinternalcapi. Rename also test methods to have the same name than tested C functions. No longer export these functions: * _PyTime_Add() * _PyTime_As100Nanoseconds() * _PyTime_FromMicrosecondsClamp() * _PyTime_FromTimespec() * _PyTime_FromTimeval() * _PyTime_GetPerfCounterWithInfo() * _PyTime_MulDiv()
This commit is contained in:
		
							parent
							
								
									822db860ea
								
							
						
					
					
						commit
						46d77610fc
					
				
					 25 changed files with 650 additions and 667 deletions
				
			
		
							
								
								
									
										2
									
								
								.github/CODEOWNERS
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/CODEOWNERS
									
										
									
									
										vendored
									
									
								
							|  | @ -79,7 +79,7 @@ Doc/library/time.rst          @pganssle @abalkin | |||
| Lib/test/test_time.py         @pganssle @abalkin | ||||
| Modules/timemodule.c          @pganssle @abalkin | ||||
| Python/pytime.c               @pganssle @abalkin | ||||
| Include/pytime.h              @pganssle @abalkin | ||||
| Include/internal/pycore_time.h  @pganssle @abalkin | ||||
| 
 | ||||
| # Email and related | ||||
| **/*mail*                     @python/email-team | ||||
|  |  | |||
|  | @ -602,3 +602,6 @@ Removed | |||
|   use ``PyObject_Vectorcall()`` which is available since Python 3.8 | ||||
|   (:pep:`590`). | ||||
|   (Contributed by Victor Stinner in :gh:`106023`.) | ||||
| 
 | ||||
| * Remove ``cpython/pytime.h`` header file: it only contained private functions. | ||||
|   (Contributed by Victor Stinner in :gh:`106316`.) | ||||
|  |  | |||
|  | @ -83,7 +83,6 @@ | |||
| #include "weakrefobject.h" | ||||
| #include "structseq.h" | ||||
| #include "cpython/picklebufobject.h" | ||||
| #include "cpython/pytime.h" | ||||
| #include "codecs.h" | ||||
| #include "pyerrors.h" | ||||
| #include "pythread.h" | ||||
|  |  | |||
|  | @ -1,331 +0,0 @@ | |||
| // The _PyTime_t API is written to use timestamp and timeout values stored in
 | ||||
| // various formats and to read clocks.
 | ||||
| //
 | ||||
| // The _PyTime_t type is an integer to support directly common arithmetic
 | ||||
| // operations like t1 + t2.
 | ||||
| //
 | ||||
| // The _PyTime_t API supports a resolution of 1 nanosecond. The _PyTime_t type
 | ||||
| // is signed to support negative timestamps. The supported range is around
 | ||||
| // [-292.3 years; +292.3 years]. Using the Unix epoch (January 1st, 1970), the
 | ||||
| // supported date range is around [1677-09-21; 2262-04-11].
 | ||||
| //
 | ||||
| // Formats:
 | ||||
| //
 | ||||
| // * seconds
 | ||||
| // * seconds as a floating pointer number (C double)
 | ||||
| // * milliseconds (10^-3 seconds)
 | ||||
| // * microseconds (10^-6 seconds)
 | ||||
| // * 100 nanoseconds (10^-7 seconds)
 | ||||
| // * nanoseconds (10^-9 seconds)
 | ||||
| // * timeval structure, 1 microsecond resolution (10^-6 seconds)
 | ||||
| // * timespec structure, 1 nanosecond resolution (10^-9 seconds)
 | ||||
| //
 | ||||
| // Integer overflows are detected and raise OverflowError. Conversion to a
 | ||||
| // resolution worse than 1 nanosecond is rounded correctly with the requested
 | ||||
| // rounding mode. There are 4 rounding modes: floor (towards -inf), ceiling
 | ||||
| // (towards +inf), half even and up (away from zero).
 | ||||
| //
 | ||||
| // Some functions clamp the result in the range [_PyTime_MIN; _PyTime_MAX], so
 | ||||
| // the caller doesn't have to handle errors and doesn't need to hold the GIL.
 | ||||
| // For example, _PyTime_Add(t1, t2) computes t1+t2 and clamp the result on
 | ||||
| // overflow.
 | ||||
| //
 | ||||
| // Clocks:
 | ||||
| //
 | ||||
| // * System clock
 | ||||
| // * Monotonic clock
 | ||||
| // * Performance counter
 | ||||
| //
 | ||||
| // Operations like (t * k / q) with integers are implemented in a way to reduce
 | ||||
| // the risk of integer overflow. Such operation is used to convert a clock
 | ||||
| // value expressed in ticks with a frequency to _PyTime_t, like
 | ||||
| // QueryPerformanceCounter() with QueryPerformanceFrequency().
 | ||||
| 
 | ||||
| #ifndef Py_LIMITED_API | ||||
| #ifndef Py_PYTIME_H | ||||
| #define Py_PYTIME_H | ||||
| 
 | ||||
| /**************************************************************************
 | ||||
| Symbols and macros to supply platform-independent interfaces to time related | ||||
| functions and constants | ||||
| **************************************************************************/ | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
| #endif | ||||
| 
 | ||||
| #ifdef __clang__ | ||||
| struct timeval; | ||||
| #endif | ||||
| 
 | ||||
| /* _PyTime_t: Python timestamp with subsecond precision. It can be used to
 | ||||
|    store a duration, and so indirectly a date (related to another date, like | ||||
|    UNIX epoch). */ | ||||
| typedef int64_t _PyTime_t; | ||||
| // _PyTime_MIN nanoseconds is around -292.3 years
 | ||||
| #define _PyTime_MIN INT64_MIN | ||||
| // _PyTime_MAX nanoseconds is around +292.3 years
 | ||||
| #define _PyTime_MAX INT64_MAX | ||||
| #define _SIZEOF_PYTIME_T 8 | ||||
| 
 | ||||
| typedef enum { | ||||
|     /* Round towards minus infinity (-inf).
 | ||||
|        For example, used to read a clock. */ | ||||
|     _PyTime_ROUND_FLOOR=0, | ||||
|     /* Round towards infinity (+inf).
 | ||||
|        For example, used for timeout to wait "at least" N seconds. */ | ||||
|     _PyTime_ROUND_CEILING=1, | ||||
|     /* Round to nearest with ties going to nearest even integer.
 | ||||
|        For example, used to round from a Python float. */ | ||||
|     _PyTime_ROUND_HALF_EVEN=2, | ||||
|     /* Round away from zero
 | ||||
|        For example, used for timeout. _PyTime_ROUND_CEILING rounds | ||||
|        -1e-9 to 0 milliseconds which causes bpo-31786 issue. | ||||
|        _PyTime_ROUND_UP rounds -1e-9 to -1 millisecond which keeps | ||||
|        the timeout sign as expected. select.poll(timeout) must block | ||||
|        for negative values." */ | ||||
|     _PyTime_ROUND_UP=3, | ||||
|     /* _PyTime_ROUND_TIMEOUT (an alias for _PyTime_ROUND_UP) should be
 | ||||
|        used for timeouts. */ | ||||
|     _PyTime_ROUND_TIMEOUT = _PyTime_ROUND_UP | ||||
| } _PyTime_round_t; | ||||
| 
 | ||||
| 
 | ||||
| /* Convert a time_t to a PyLong. */ | ||||
| PyAPI_FUNC(PyObject *) _PyLong_FromTime_t( | ||||
|     time_t sec); | ||||
| 
 | ||||
| /* Convert a PyLong to a time_t. */ | ||||
| PyAPI_FUNC(time_t) _PyLong_AsTime_t( | ||||
|     PyObject *obj); | ||||
| 
 | ||||
| /* Convert a number of seconds, int or float, to time_t. */ | ||||
| PyAPI_FUNC(int) _PyTime_ObjectToTime_t( | ||||
|     PyObject *obj, | ||||
|     time_t *sec, | ||||
|     _PyTime_round_t); | ||||
| 
 | ||||
| /* Convert a number of seconds, int or float, to a timeval structure.
 | ||||
|    usec is in the range [0; 999999] and rounded towards zero. | ||||
|    For example, -1.2 is converted to (-2, 800000). */ | ||||
| PyAPI_FUNC(int) _PyTime_ObjectToTimeval( | ||||
|     PyObject *obj, | ||||
|     time_t *sec, | ||||
|     long *usec, | ||||
|     _PyTime_round_t); | ||||
| 
 | ||||
| /* Convert a number of seconds, int or float, to a timespec structure.
 | ||||
|    nsec is in the range [0; 999999999] and rounded towards zero. | ||||
|    For example, -1.2 is converted to (-2, 800000000). */ | ||||
| PyAPI_FUNC(int) _PyTime_ObjectToTimespec( | ||||
|     PyObject *obj, | ||||
|     time_t *sec, | ||||
|     long *nsec, | ||||
|     _PyTime_round_t); | ||||
| 
 | ||||
| 
 | ||||
| /* Create a timestamp from a number of seconds. */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_FromSeconds(int seconds); | ||||
| 
 | ||||
| /* Macro to create a timestamp from a number of seconds, no integer overflow.
 | ||||
|    Only use the macro for small values, prefer _PyTime_FromSeconds(). */ | ||||
| #define _PYTIME_FROMSECONDS(seconds) \ | ||||
|             ((_PyTime_t)(seconds) * (1000 * 1000 * 1000)) | ||||
| 
 | ||||
| /* Create a timestamp from a number of nanoseconds. */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(_PyTime_t ns); | ||||
| 
 | ||||
| /* Create a timestamp from a number of microseconds.
 | ||||
|  * Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow. */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_FromMicrosecondsClamp(_PyTime_t us); | ||||
| 
 | ||||
| /* Create a timestamp from nanoseconds (Python int). */ | ||||
| PyAPI_FUNC(int) _PyTime_FromNanosecondsObject(_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. */ | ||||
| PyAPI_FUNC(int) _PyTime_FromSecondsObject(_PyTime_t *t, | ||||
|     PyObject *obj, | ||||
|     _PyTime_round_t round); | ||||
| 
 | ||||
| /* Convert a number of milliseconds (Python float or int, 10^-3) to a timestamp.
 | ||||
|    Raise an exception and return -1 on error, return 0 on success. */ | ||||
| PyAPI_FUNC(int) _PyTime_FromMillisecondsObject(_PyTime_t *t, | ||||
|     PyObject *obj, | ||||
|     _PyTime_round_t round); | ||||
| 
 | ||||
| /* Convert a timestamp to a number of seconds as a C double. */ | ||||
| PyAPI_FUNC(double) _PyTime_AsSecondsDouble(_PyTime_t t); | ||||
| 
 | ||||
| /* Convert timestamp to a number of milliseconds (10^-3 seconds). */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_AsMilliseconds(_PyTime_t t, | ||||
|     _PyTime_round_t round); | ||||
| 
 | ||||
| /* Convert timestamp to a number of microseconds (10^-6 seconds). */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_AsMicroseconds(_PyTime_t t, | ||||
|     _PyTime_round_t round); | ||||
| 
 | ||||
| /* Convert timestamp to a number of nanoseconds (10^-9 seconds). */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_AsNanoseconds(_PyTime_t t); | ||||
| 
 | ||||
| #ifdef MS_WINDOWS | ||||
| // Convert timestamp to a number of 100 nanoseconds (10^-7 seconds).
 | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_As100Nanoseconds(_PyTime_t t, | ||||
|     _PyTime_round_t round); | ||||
| #endif | ||||
| 
 | ||||
| /* Convert timestamp to a number of nanoseconds (10^-9 seconds) as a Python int
 | ||||
|    object. */ | ||||
| PyAPI_FUNC(PyObject *) _PyTime_AsNanosecondsObject(_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. */ | ||||
| PyAPI_FUNC(int) _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv); | ||||
| #endif | ||||
| 
 | ||||
| /* Convert a timestamp to a timeval structure (microsecond resolution).
 | ||||
|    tv_usec is always positive. | ||||
|    Raise an exception and return -1 if the conversion overflowed, | ||||
|    return 0 on success. */ | ||||
| PyAPI_FUNC(int) _PyTime_AsTimeval(_PyTime_t t, | ||||
|     struct timeval *tv, | ||||
|     _PyTime_round_t round); | ||||
| 
 | ||||
| /* Similar to _PyTime_AsTimeval() but don't raise an exception on overflow.
 | ||||
|    On overflow, clamp tv_sec to _PyTime_t min/max. */ | ||||
| PyAPI_FUNC(void) _PyTime_AsTimeval_clamp(_PyTime_t t, | ||||
|     struct timeval *tv, | ||||
|     _PyTime_round_t round); | ||||
| 
 | ||||
| /* Convert a timestamp to a number of seconds (secs) and microseconds (us).
 | ||||
|    us is always positive. This function is similar to _PyTime_AsTimeval() | ||||
|    except that secs is always a time_t type, whereas the timeval structure | ||||
|    uses a C long for tv_sec on Windows. | ||||
|    Raise an exception and return -1 if the conversion overflowed, | ||||
|    return 0 on success. */ | ||||
| PyAPI_FUNC(int) _PyTime_AsTimevalTime_t( | ||||
|     _PyTime_t t, | ||||
|     time_t *secs, | ||||
|     int *us, | ||||
|     _PyTime_round_t round); | ||||
| 
 | ||||
| #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE) | ||||
| /* Create a timestamp from a timespec structure.
 | ||||
|    Raise an exception and return -1 on overflow, return 0 on success. */ | ||||
| PyAPI_FUNC(int) _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts); | ||||
| 
 | ||||
| /* Convert a timestamp to a timespec structure (nanosecond resolution).
 | ||||
|    tv_nsec is always positive. | ||||
|    Raise an exception and return -1 on error, return 0 on success. */ | ||||
| PyAPI_FUNC(int) _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts); | ||||
| 
 | ||||
| /* Similar to _PyTime_AsTimespec() but don't raise an exception on overflow.
 | ||||
|    On overflow, clamp tv_sec to _PyTime_t min/max. */ | ||||
| PyAPI_FUNC(void) _PyTime_AsTimespec_clamp(_PyTime_t t, struct timespec *ts); | ||||
| #endif | ||||
| 
 | ||||
| 
 | ||||
| // Compute t1 + t2. Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow.
 | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_Add(_PyTime_t t1, _PyTime_t t2); | ||||
| 
 | ||||
| /* Compute ticks * mul / div.
 | ||||
|    Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow. | ||||
|    The caller must ensure that ((div - 1) * mul) cannot overflow. */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_MulDiv(_PyTime_t ticks, | ||||
|     _PyTime_t mul, | ||||
|     _PyTime_t div); | ||||
| 
 | ||||
| /* Structure used by time.get_clock_info() */ | ||||
| typedef struct { | ||||
|     const char *implementation; | ||||
|     int monotonic; | ||||
|     int adjustable; | ||||
|     double resolution; | ||||
| } _Py_clock_info_t; | ||||
| 
 | ||||
| /* Get the current time from the system clock.
 | ||||
| 
 | ||||
|    If the internal clock fails, silently ignore the error and return 0. | ||||
|    On integer overflow, silently ignore the overflow and clamp the clock to | ||||
|    [_PyTime_MIN; _PyTime_MAX]. | ||||
| 
 | ||||
|    Use _PyTime_GetSystemClockWithInfo() to check for failure. */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_GetSystemClock(void); | ||||
| 
 | ||||
| /* Get the current time from the system clock.
 | ||||
|  * On success, set *t and *info (if not NULL), and return 0. | ||||
|  * On error, raise an exception and return -1. | ||||
|  */ | ||||
| PyAPI_FUNC(int) _PyTime_GetSystemClockWithInfo( | ||||
|     _PyTime_t *t, | ||||
|     _Py_clock_info_t *info); | ||||
| 
 | ||||
| /* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
 | ||||
|    The clock is not affected by system clock updates. The reference point of | ||||
|    the returned value is undefined, so that only the difference between the | ||||
|    results of consecutive calls is valid. | ||||
| 
 | ||||
|    If the internal clock fails, silently ignore the error and return 0. | ||||
|    On integer overflow, silently ignore the overflow and clamp the clock to | ||||
|    [_PyTime_MIN; _PyTime_MAX]. | ||||
| 
 | ||||
|    Use _PyTime_GetMonotonicClockWithInfo() to check for failure. */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_GetMonotonicClock(void); | ||||
| 
 | ||||
| /* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
 | ||||
|    The clock is not affected by system clock updates. The reference point of | ||||
|    the returned value is undefined, so that only the difference between the | ||||
|    results of consecutive calls is valid. | ||||
| 
 | ||||
|    Fill info (if set) with information of the function used to get the time. | ||||
| 
 | ||||
|    Return 0 on success, raise an exception and return -1 on error. */ | ||||
| PyAPI_FUNC(int) _PyTime_GetMonotonicClockWithInfo( | ||||
|     _PyTime_t *t, | ||||
|     _Py_clock_info_t *info); | ||||
| 
 | ||||
| 
 | ||||
| /* Converts a timestamp to the Gregorian time, using the local time zone.
 | ||||
|    Return 0 on success, raise an exception and return -1 on error. */ | ||||
| PyAPI_FUNC(int) _PyTime_localtime(time_t t, struct tm *tm); | ||||
| 
 | ||||
| /* Converts a timestamp to the Gregorian time, assuming UTC.
 | ||||
|    Return 0 on success, raise an exception and return -1 on error. */ | ||||
| PyAPI_FUNC(int) _PyTime_gmtime(time_t t, struct tm *tm); | ||||
| 
 | ||||
| /* Get the performance counter: clock with the highest available resolution to
 | ||||
|    measure a short duration. | ||||
| 
 | ||||
|    If the internal clock fails, silently ignore the error and return 0. | ||||
|    On integer overflow, silently ignore the overflow and clamp the clock to | ||||
|    [_PyTime_MIN; _PyTime_MAX]. | ||||
| 
 | ||||
|    Use _PyTime_GetPerfCounterWithInfo() to check for failure. */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_GetPerfCounter(void); | ||||
| 
 | ||||
| /* Get the performance counter: clock with the highest available resolution to
 | ||||
|    measure a short duration. | ||||
| 
 | ||||
|    Fill info (if set) with information of the function used to get the time. | ||||
| 
 | ||||
|    Return 0 on success, raise an exception and return -1 on error. */ | ||||
| PyAPI_FUNC(int) _PyTime_GetPerfCounterWithInfo( | ||||
|     _PyTime_t *t, | ||||
|     _Py_clock_info_t *info); | ||||
| 
 | ||||
| 
 | ||||
| // Create a deadline.
 | ||||
| // Pseudo code: _PyTime_GetMonotonicClock() + timeout.
 | ||||
| PyAPI_FUNC(_PyTime_t) _PyDeadline_Init(_PyTime_t timeout); | ||||
| 
 | ||||
| // Get remaining time from a deadline.
 | ||||
| // Pseudo code: deadline - _PyTime_GetMonotonicClock().
 | ||||
| PyAPI_FUNC(_PyTime_t) _PyDeadline_Get(_PyTime_t deadline); | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
| #endif /* Py_PYTIME_H */ | ||||
| #endif /* Py_LIMITED_API */ | ||||
|  | @ -5,6 +5,8 @@ | |||
| extern "C" { | ||||
| #endif | ||||
| 
 | ||||
| #include "pycore_time.h"          // _PyTime_t | ||||
| 
 | ||||
| 
 | ||||
| struct _import_runtime_state { | ||||
|     /* The builtin modules (defined in config.c). */ | ||||
|  |  | |||
|  | @ -1,3 +1,46 @@ | |||
| // The _PyTime_t API is written to use timestamp and timeout values stored in
 | ||||
| // various formats and to read clocks.
 | ||||
| //
 | ||||
| // The _PyTime_t type is an integer to support directly common arithmetic
 | ||||
| // operations like t1 + t2.
 | ||||
| //
 | ||||
| // The _PyTime_t API supports a resolution of 1 nanosecond. The _PyTime_t type
 | ||||
| // is signed to support negative timestamps. The supported range is around
 | ||||
| // [-292.3 years; +292.3 years]. Using the Unix epoch (January 1st, 1970), the
 | ||||
| // supported date range is around [1677-09-21; 2262-04-11].
 | ||||
| //
 | ||||
| // Formats:
 | ||||
| //
 | ||||
| // * seconds
 | ||||
| // * seconds as a floating pointer number (C double)
 | ||||
| // * milliseconds (10^-3 seconds)
 | ||||
| // * microseconds (10^-6 seconds)
 | ||||
| // * 100 nanoseconds (10^-7 seconds)
 | ||||
| // * nanoseconds (10^-9 seconds)
 | ||||
| // * timeval structure, 1 microsecond resolution (10^-6 seconds)
 | ||||
| // * timespec structure, 1 nanosecond resolution (10^-9 seconds)
 | ||||
| //
 | ||||
| // Integer overflows are detected and raise OverflowError. Conversion to a
 | ||||
| // resolution worse than 1 nanosecond is rounded correctly with the requested
 | ||||
| // rounding mode. There are 4 rounding modes: floor (towards -inf), ceiling
 | ||||
| // (towards +inf), half even and up (away from zero).
 | ||||
| //
 | ||||
| // Some functions clamp the result in the range [_PyTime_MIN; _PyTime_MAX], so
 | ||||
| // the caller doesn't have to handle errors and doesn't need to hold the GIL.
 | ||||
| // For example, _PyTime_Add(t1, t2) computes t1+t2 and clamp the result on
 | ||||
| // overflow.
 | ||||
| //
 | ||||
| // Clocks:
 | ||||
| //
 | ||||
| // * System clock
 | ||||
| // * Monotonic clock
 | ||||
| // * Performance counter
 | ||||
| //
 | ||||
| // Operations like (t * k / q) with integers are implemented in a way to reduce
 | ||||
| // the risk of integer overflow. Such operation is used to convert a clock
 | ||||
| // value expressed in ticks with a frequency to _PyTime_t, like
 | ||||
| // QueryPerformanceCounter() with QueryPerformanceFrequency().
 | ||||
| 
 | ||||
| #ifndef Py_INTERNAL_TIME_H | ||||
| #define Py_INTERNAL_TIME_H | ||||
| #ifdef __cplusplus | ||||
|  | @ -19,6 +62,275 @@ struct _time_runtime_state { | |||
| }; | ||||
| 
 | ||||
| 
 | ||||
| #ifdef __clang__ | ||||
| struct timeval; | ||||
| #endif | ||||
| 
 | ||||
| /* _PyTime_t: Python timestamp with subsecond precision. It can be used to
 | ||||
|    store a duration, and so indirectly a date (related to another date, like | ||||
|    UNIX epoch). */ | ||||
| typedef int64_t _PyTime_t; | ||||
| // _PyTime_MIN nanoseconds is around -292.3 years
 | ||||
| #define _PyTime_MIN INT64_MIN | ||||
| // _PyTime_MAX nanoseconds is around +292.3 years
 | ||||
| #define _PyTime_MAX INT64_MAX | ||||
| #define _SIZEOF_PYTIME_T 8 | ||||
| 
 | ||||
| typedef enum { | ||||
|     /* Round towards minus infinity (-inf).
 | ||||
|        For example, used to read a clock. */ | ||||
|     _PyTime_ROUND_FLOOR=0, | ||||
|     /* Round towards infinity (+inf).
 | ||||
|        For example, used for timeout to wait "at least" N seconds. */ | ||||
|     _PyTime_ROUND_CEILING=1, | ||||
|     /* Round to nearest with ties going to nearest even integer.
 | ||||
|        For example, used to round from a Python float. */ | ||||
|     _PyTime_ROUND_HALF_EVEN=2, | ||||
|     /* Round away from zero
 | ||||
|        For example, used for timeout. _PyTime_ROUND_CEILING rounds | ||||
|        -1e-9 to 0 milliseconds which causes bpo-31786 issue. | ||||
|        _PyTime_ROUND_UP rounds -1e-9 to -1 millisecond which keeps | ||||
|        the timeout sign as expected. select.poll(timeout) must block | ||||
|        for negative values." */ | ||||
|     _PyTime_ROUND_UP=3, | ||||
|     /* _PyTime_ROUND_TIMEOUT (an alias for _PyTime_ROUND_UP) should be
 | ||||
|        used for timeouts. */ | ||||
|     _PyTime_ROUND_TIMEOUT = _PyTime_ROUND_UP | ||||
| } _PyTime_round_t; | ||||
| 
 | ||||
| 
 | ||||
| /* Convert a time_t to a PyLong. */ | ||||
| PyAPI_FUNC(PyObject*) _PyLong_FromTime_t(time_t sec); | ||||
| 
 | ||||
| /* Convert a PyLong to a time_t. */ | ||||
| PyAPI_FUNC(time_t) _PyLong_AsTime_t(PyObject *obj); | ||||
| 
 | ||||
| /* Convert a number of seconds, int or float, to time_t. */ | ||||
| PyAPI_FUNC(int) _PyTime_ObjectToTime_t( | ||||
|     PyObject *obj, | ||||
|     time_t *sec, | ||||
|     _PyTime_round_t); | ||||
| 
 | ||||
| /* Convert a number of seconds, int or float, to a timeval structure.
 | ||||
|    usec is in the range [0; 999999] and rounded towards zero. | ||||
|    For example, -1.2 is converted to (-2, 800000). */ | ||||
| PyAPI_FUNC(int) _PyTime_ObjectToTimeval( | ||||
|     PyObject *obj, | ||||
|     time_t *sec, | ||||
|     long *usec, | ||||
|     _PyTime_round_t); | ||||
| 
 | ||||
| /* Convert a number of seconds, int or float, to a timespec structure.
 | ||||
|    nsec is in the range [0; 999999999] and rounded towards zero. | ||||
|    For example, -1.2 is converted to (-2, 800000000). */ | ||||
| PyAPI_FUNC(int) _PyTime_ObjectToTimespec( | ||||
|     PyObject *obj, | ||||
|     time_t *sec, | ||||
|     long *nsec, | ||||
|     _PyTime_round_t); | ||||
| 
 | ||||
| 
 | ||||
| /* Create a timestamp from a number of seconds. */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_FromSeconds(int seconds); | ||||
| 
 | ||||
| /* Macro to create a timestamp from a number of seconds, no integer overflow.
 | ||||
|    Only use the macro for small values, prefer _PyTime_FromSeconds(). */ | ||||
| #define _PYTIME_FROMSECONDS(seconds) \ | ||||
|             ((_PyTime_t)(seconds) * (1000 * 1000 * 1000)) | ||||
| 
 | ||||
| /* Create a timestamp from a number of nanoseconds. */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(_PyTime_t ns); | ||||
| 
 | ||||
| /* Create a timestamp from a number of microseconds.
 | ||||
|  * Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow. */ | ||||
| extern _PyTime_t _PyTime_FromMicrosecondsClamp(_PyTime_t us); | ||||
| 
 | ||||
| /* Create a timestamp from nanoseconds (Python int). */ | ||||
| PyAPI_FUNC(int) _PyTime_FromNanosecondsObject(_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. */ | ||||
| PyAPI_FUNC(int) _PyTime_FromSecondsObject(_PyTime_t *t, | ||||
|     PyObject *obj, | ||||
|     _PyTime_round_t round); | ||||
| 
 | ||||
| /* Convert a number of milliseconds (Python float or int, 10^-3) to a timestamp.
 | ||||
|    Raise an exception and return -1 on error, return 0 on success. */ | ||||
| PyAPI_FUNC(int) _PyTime_FromMillisecondsObject(_PyTime_t *t, | ||||
|     PyObject *obj, | ||||
|     _PyTime_round_t round); | ||||
| 
 | ||||
| /* Convert a timestamp to a number of seconds as a C double. */ | ||||
| PyAPI_FUNC(double) _PyTime_AsSecondsDouble(_PyTime_t t); | ||||
| 
 | ||||
| /* Convert timestamp to a number of milliseconds (10^-3 seconds). */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_AsMilliseconds(_PyTime_t t, | ||||
|     _PyTime_round_t round); | ||||
| 
 | ||||
| /* Convert timestamp to a number of microseconds (10^-6 seconds). */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_AsMicroseconds(_PyTime_t t, | ||||
|     _PyTime_round_t round); | ||||
| 
 | ||||
| /* Convert timestamp to a number of nanoseconds (10^-9 seconds). */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_AsNanoseconds(_PyTime_t t); | ||||
| 
 | ||||
| #ifdef MS_WINDOWS | ||||
| // Convert timestamp to a number of 100 nanoseconds (10^-7 seconds).
 | ||||
| extern _PyTime_t _PyTime_As100Nanoseconds(_PyTime_t t, | ||||
|     _PyTime_round_t round); | ||||
| #endif | ||||
| 
 | ||||
| /* Convert timestamp to a number of nanoseconds (10^-9 seconds) as a Python int
 | ||||
|    object. */ | ||||
| PyAPI_FUNC(PyObject*) _PyTime_AsNanosecondsObject(_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. */ | ||||
| extern int _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv); | ||||
| #endif | ||||
| 
 | ||||
| /* Convert a timestamp to a timeval structure (microsecond resolution).
 | ||||
|    tv_usec is always positive. | ||||
|    Raise an exception and return -1 if the conversion overflowed, | ||||
|    return 0 on success. */ | ||||
| PyAPI_FUNC(int) _PyTime_AsTimeval(_PyTime_t t, | ||||
|     struct timeval *tv, | ||||
|     _PyTime_round_t round); | ||||
| 
 | ||||
| /* Similar to _PyTime_AsTimeval() but don't raise an exception on overflow.
 | ||||
|    On overflow, clamp tv_sec to _PyTime_t min/max. */ | ||||
| PyAPI_FUNC(void) _PyTime_AsTimeval_clamp(_PyTime_t t, | ||||
|     struct timeval *tv, | ||||
|     _PyTime_round_t round); | ||||
| 
 | ||||
| /* Convert a timestamp to a number of seconds (secs) and microseconds (us).
 | ||||
|    us is always positive. This function is similar to _PyTime_AsTimeval() | ||||
|    except that secs is always a time_t type, whereas the timeval structure | ||||
|    uses a C long for tv_sec on Windows. | ||||
|    Raise an exception and return -1 if the conversion overflowed, | ||||
|    return 0 on success. */ | ||||
| PyAPI_FUNC(int) _PyTime_AsTimevalTime_t( | ||||
|     _PyTime_t t, | ||||
|     time_t *secs, | ||||
|     int *us, | ||||
|     _PyTime_round_t round); | ||||
| 
 | ||||
| #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE) | ||||
| /* Create a timestamp from a timespec structure.
 | ||||
|    Raise an exception and return -1 on overflow, return 0 on success. */ | ||||
| extern int _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts); | ||||
| 
 | ||||
| /* Convert a timestamp to a timespec structure (nanosecond resolution).
 | ||||
|    tv_nsec is always positive. | ||||
|    Raise an exception and return -1 on error, return 0 on success. */ | ||||
| PyAPI_FUNC(int) _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts); | ||||
| 
 | ||||
| /* Similar to _PyTime_AsTimespec() but don't raise an exception on overflow.
 | ||||
|    On overflow, clamp tv_sec to _PyTime_t min/max. */ | ||||
| PyAPI_FUNC(void) _PyTime_AsTimespec_clamp(_PyTime_t t, struct timespec *ts); | ||||
| #endif | ||||
| 
 | ||||
| 
 | ||||
| // Compute t1 + t2. Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow.
 | ||||
| extern _PyTime_t _PyTime_Add(_PyTime_t t1, _PyTime_t t2); | ||||
| 
 | ||||
| /* Compute ticks * mul / div.
 | ||||
|    Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow. | ||||
|    The caller must ensure that ((div - 1) * mul) cannot overflow. */ | ||||
| extern _PyTime_t _PyTime_MulDiv(_PyTime_t ticks, | ||||
|     _PyTime_t mul, | ||||
|     _PyTime_t div); | ||||
| 
 | ||||
| /* Structure used by time.get_clock_info() */ | ||||
| typedef struct { | ||||
|     const char *implementation; | ||||
|     int monotonic; | ||||
|     int adjustable; | ||||
|     double resolution; | ||||
| } _Py_clock_info_t; | ||||
| 
 | ||||
| /* Get the current time from the system clock.
 | ||||
| 
 | ||||
|    If the internal clock fails, silently ignore the error and return 0. | ||||
|    On integer overflow, silently ignore the overflow and clamp the clock to | ||||
|    [_PyTime_MIN; _PyTime_MAX]. | ||||
| 
 | ||||
|    Use _PyTime_GetSystemClockWithInfo() to check for failure. */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_GetSystemClock(void); | ||||
| 
 | ||||
| /* Get the current time from the system clock.
 | ||||
|  * On success, set *t and *info (if not NULL), and return 0. | ||||
|  * On error, raise an exception and return -1. | ||||
|  */ | ||||
| PyAPI_FUNC(int) _PyTime_GetSystemClockWithInfo( | ||||
|     _PyTime_t *t, | ||||
|     _Py_clock_info_t *info); | ||||
| 
 | ||||
| /* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
 | ||||
|    The clock is not affected by system clock updates. The reference point of | ||||
|    the returned value is undefined, so that only the difference between the | ||||
|    results of consecutive calls is valid. | ||||
| 
 | ||||
|    If the internal clock fails, silently ignore the error and return 0. | ||||
|    On integer overflow, silently ignore the overflow and clamp the clock to | ||||
|    [_PyTime_MIN; _PyTime_MAX]. | ||||
| 
 | ||||
|    Use _PyTime_GetMonotonicClockWithInfo() to check for failure. */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_GetMonotonicClock(void); | ||||
| 
 | ||||
| /* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
 | ||||
|    The clock is not affected by system clock updates. The reference point of | ||||
|    the returned value is undefined, so that only the difference between the | ||||
|    results of consecutive calls is valid. | ||||
| 
 | ||||
|    Fill info (if set) with information of the function used to get the time. | ||||
| 
 | ||||
|    Return 0 on success, raise an exception and return -1 on error. */ | ||||
| PyAPI_FUNC(int) _PyTime_GetMonotonicClockWithInfo( | ||||
|     _PyTime_t *t, | ||||
|     _Py_clock_info_t *info); | ||||
| 
 | ||||
| 
 | ||||
| /* Converts a timestamp to the Gregorian time, using the local time zone.
 | ||||
|    Return 0 on success, raise an exception and return -1 on error. */ | ||||
| PyAPI_FUNC(int) _PyTime_localtime(time_t t, struct tm *tm); | ||||
| 
 | ||||
| /* Converts a timestamp to the Gregorian time, assuming UTC.
 | ||||
|    Return 0 on success, raise an exception and return -1 on error. */ | ||||
| PyAPI_FUNC(int) _PyTime_gmtime(time_t t, struct tm *tm); | ||||
| 
 | ||||
| /* Get the performance counter: clock with the highest available resolution to
 | ||||
|    measure a short duration. | ||||
| 
 | ||||
|    If the internal clock fails, silently ignore the error and return 0. | ||||
|    On integer overflow, silently ignore the overflow and clamp the clock to | ||||
|    [_PyTime_MIN; _PyTime_MAX]. | ||||
| 
 | ||||
|    Use _PyTime_GetPerfCounterWithInfo() to check for failure. */ | ||||
| PyAPI_FUNC(_PyTime_t) _PyTime_GetPerfCounter(void); | ||||
| 
 | ||||
| /* Get the performance counter: clock with the highest available resolution to
 | ||||
|    measure a short duration. | ||||
| 
 | ||||
|    Fill info (if set) with information of the function used to get the time. | ||||
| 
 | ||||
|    Return 0 on success, raise an exception and return -1 on error. */ | ||||
| extern int _PyTime_GetPerfCounterWithInfo( | ||||
|     _PyTime_t *t, | ||||
|     _Py_clock_info_t *info); | ||||
| 
 | ||||
| 
 | ||||
| // Create a deadline.
 | ||||
| // Pseudo code: _PyTime_GetMonotonicClock() + timeout.
 | ||||
| PyAPI_FUNC(_PyTime_t) _PyDeadline_Init(_PyTime_t timeout); | ||||
| 
 | ||||
| // Get remaining time from a deadline.
 | ||||
| // Pseudo code: deadline - _PyTime_GetMonotonicClock().
 | ||||
| PyAPI_FUNC(_PyTime_t) _PyDeadline_Get(_PyTime_t deadline); | ||||
| 
 | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  |  | |||
|  | @ -14,6 +14,10 @@ | |||
|     import _testcapi | ||||
| except ImportError: | ||||
|     _testcapi = None | ||||
| try: | ||||
|     import _testinternalcapi | ||||
| except ImportError: | ||||
|     _testinternalcapi = None | ||||
| 
 | ||||
| from test.support import skip_if_buggy_ucrt_strfptime | ||||
| 
 | ||||
|  | @ -761,7 +765,8 @@ def test_short_times(self): | |||
|         self.assertIs(lt.tm_zone, None) | ||||
| 
 | ||||
| 
 | ||||
| @unittest.skipIf(_testcapi is None, 'need the _testcapi module') | ||||
| @unittest.skipIf(_testcapi is None, 'need the _testinternalcapi module') | ||||
| @unittest.skipIf(_testinternalcapi is None, 'need the _testinternalcapi module') | ||||
| class CPyTimeTestCase: | ||||
|     """ | ||||
|     Base class to test the C _PyTime_t API. | ||||
|  | @ -769,7 +774,7 @@ class CPyTimeTestCase: | |||
|     OVERFLOW_SECONDS = None | ||||
| 
 | ||||
|     def setUp(self): | ||||
|         from _testcapi import SIZEOF_TIME_T | ||||
|         from _testinternalcapi import SIZEOF_TIME_T | ||||
|         bits = SIZEOF_TIME_T * 8 - 1 | ||||
|         self.time_t_min = -2 ** bits | ||||
|         self.time_t_max = 2 ** bits - 1 | ||||
|  | @ -897,39 +902,39 @@ class TestCPyTime(CPyTimeTestCase, unittest.TestCase): | |||
|     OVERFLOW_SECONDS = math.ceil((2**63 + 1) / SEC_TO_NS) | ||||
| 
 | ||||
|     def test_FromSeconds(self): | ||||
|         from _testcapi import PyTime_FromSeconds | ||||
|         from _testinternalcapi import _PyTime_FromSeconds | ||||
| 
 | ||||
|         # PyTime_FromSeconds() expects a C int, reject values out of range | ||||
|         # _PyTime_FromSeconds() expects a C int, reject values out of range | ||||
|         def c_int_filter(secs): | ||||
|             return (_testcapi.INT_MIN <= secs <= _testcapi.INT_MAX) | ||||
| 
 | ||||
|         self.check_int_rounding(lambda secs, rnd: PyTime_FromSeconds(secs), | ||||
|         self.check_int_rounding(lambda secs, rnd: _PyTime_FromSeconds(secs), | ||||
|                                 lambda secs: secs * SEC_TO_NS, | ||||
|                                 value_filter=c_int_filter) | ||||
| 
 | ||||
|         # test nan | ||||
|         for time_rnd, _ in ROUNDING_MODES: | ||||
|             with self.assertRaises(TypeError): | ||||
|                 PyTime_FromSeconds(float('nan')) | ||||
|                 _PyTime_FromSeconds(float('nan')) | ||||
| 
 | ||||
|     def test_FromSecondsObject(self): | ||||
|         from _testcapi import PyTime_FromSecondsObject | ||||
|         from _testinternalcapi import _PyTime_FromSecondsObject | ||||
| 
 | ||||
|         self.check_int_rounding( | ||||
|             PyTime_FromSecondsObject, | ||||
|             _PyTime_FromSecondsObject, | ||||
|             lambda secs: secs * SEC_TO_NS) | ||||
| 
 | ||||
|         self.check_float_rounding( | ||||
|             PyTime_FromSecondsObject, | ||||
|             _PyTime_FromSecondsObject, | ||||
|             lambda ns: self.decimal_round(ns * SEC_TO_NS)) | ||||
| 
 | ||||
|         # test nan | ||||
|         for time_rnd, _ in ROUNDING_MODES: | ||||
|             with self.assertRaises(ValueError): | ||||
|                 PyTime_FromSecondsObject(float('nan'), time_rnd) | ||||
|                 _PyTime_FromSecondsObject(float('nan'), time_rnd) | ||||
| 
 | ||||
|     def test_AsSecondsDouble(self): | ||||
|         from _testcapi import PyTime_AsSecondsDouble | ||||
|         from _testinternalcapi import _PyTime_AsSecondsDouble | ||||
| 
 | ||||
|         def float_converter(ns): | ||||
|             if abs(ns) % SEC_TO_NS == 0: | ||||
|  | @ -937,14 +942,14 @@ def float_converter(ns): | |||
|             else: | ||||
|                 return float(ns) / SEC_TO_NS | ||||
| 
 | ||||
|         self.check_int_rounding(lambda ns, rnd: PyTime_AsSecondsDouble(ns), | ||||
|         self.check_int_rounding(lambda ns, rnd: _PyTime_AsSecondsDouble(ns), | ||||
|                                 float_converter, | ||||
|                                 NS_TO_SEC) | ||||
| 
 | ||||
|         # test nan | ||||
|         for time_rnd, _ in ROUNDING_MODES: | ||||
|             with self.assertRaises(TypeError): | ||||
|                 PyTime_AsSecondsDouble(float('nan')) | ||||
|                 _PyTime_AsSecondsDouble(float('nan')) | ||||
| 
 | ||||
|     def create_decimal_converter(self, denominator): | ||||
|         denom = decimal.Decimal(denominator) | ||||
|  | @ -956,7 +961,7 @@ def converter(value): | |||
|         return converter | ||||
| 
 | ||||
|     def test_AsTimeval(self): | ||||
|         from _testcapi import PyTime_AsTimeval | ||||
|         from _testinternalcapi import _PyTime_AsTimeval | ||||
| 
 | ||||
|         us_converter = self.create_decimal_converter(US_TO_NS) | ||||
| 
 | ||||
|  | @ -973,28 +978,28 @@ def seconds_filter(secs): | |||
|         else: | ||||
|             seconds_filter = self.time_t_filter | ||||
| 
 | ||||
|         self.check_int_rounding(PyTime_AsTimeval, | ||||
|         self.check_int_rounding(_PyTime_AsTimeval, | ||||
|                                 timeval_converter, | ||||
|                                 NS_TO_SEC, | ||||
|                                 value_filter=seconds_filter) | ||||
| 
 | ||||
|     @unittest.skipUnless(hasattr(_testcapi, 'PyTime_AsTimespec'), | ||||
|                          'need _testcapi.PyTime_AsTimespec') | ||||
|     @unittest.skipUnless(hasattr(_testinternalcapi, '_PyTime_AsTimespec'), | ||||
|                          'need _testinternalcapi._PyTime_AsTimespec') | ||||
|     def test_AsTimespec(self): | ||||
|         from _testcapi import PyTime_AsTimespec | ||||
|         from _testinternalcapi import _PyTime_AsTimespec | ||||
| 
 | ||||
|         def timespec_converter(ns): | ||||
|             return divmod(ns, SEC_TO_NS) | ||||
| 
 | ||||
|         self.check_int_rounding(lambda ns, rnd: PyTime_AsTimespec(ns), | ||||
|         self.check_int_rounding(lambda ns, rnd: _PyTime_AsTimespec(ns), | ||||
|                                 timespec_converter, | ||||
|                                 NS_TO_SEC, | ||||
|                                 value_filter=self.time_t_filter) | ||||
| 
 | ||||
|     @unittest.skipUnless(hasattr(_testcapi, 'PyTime_AsTimeval_clamp'), | ||||
|                          'need _testcapi.PyTime_AsTimeval_clamp') | ||||
|     @unittest.skipUnless(hasattr(_testinternalcapi, '_PyTime_AsTimeval_clamp'), | ||||
|                          'need _testinternalcapi._PyTime_AsTimeval_clamp') | ||||
|     def test_AsTimeval_clamp(self): | ||||
|         from _testcapi import PyTime_AsTimeval_clamp | ||||
|         from _testinternalcapi import _PyTime_AsTimeval_clamp | ||||
| 
 | ||||
|         if sys.platform == 'win32': | ||||
|             from _testcapi import LONG_MIN, LONG_MAX | ||||
|  | @ -1005,7 +1010,7 @@ def test_AsTimeval_clamp(self): | |||
|             tv_sec_min = self.time_t_min | ||||
| 
 | ||||
|         for t in (_PyTime_MIN, _PyTime_MAX): | ||||
|             ts = PyTime_AsTimeval_clamp(t, _PyTime.ROUND_CEILING) | ||||
|             ts = _PyTime_AsTimeval_clamp(t, _PyTime.ROUND_CEILING) | ||||
|             with decimal.localcontext() as context: | ||||
|                 context.rounding = decimal.ROUND_CEILING | ||||
|                 us = self.decimal_round(decimal.Decimal(t) / US_TO_NS) | ||||
|  | @ -1018,13 +1023,13 @@ def test_AsTimeval_clamp(self): | |||
|                 tv_usec = 0 | ||||
|             self.assertEqual(ts, (tv_sec, tv_usec)) | ||||
| 
 | ||||
|     @unittest.skipUnless(hasattr(_testcapi, 'PyTime_AsTimespec_clamp'), | ||||
|                          'need _testcapi.PyTime_AsTimespec_clamp') | ||||
|     @unittest.skipUnless(hasattr(_testinternalcapi, '_PyTime_AsTimespec_clamp'), | ||||
|                          'need _testinternalcapi._PyTime_AsTimespec_clamp') | ||||
|     def test_AsTimespec_clamp(self): | ||||
|         from _testcapi import PyTime_AsTimespec_clamp | ||||
|         from _testinternalcapi import _PyTime_AsTimespec_clamp | ||||
| 
 | ||||
|         for t in (_PyTime_MIN, _PyTime_MAX): | ||||
|             ts = PyTime_AsTimespec_clamp(t) | ||||
|             ts = _PyTime_AsTimespec_clamp(t) | ||||
|             tv_sec, tv_nsec = divmod(t, NS_TO_SEC) | ||||
|             if self.time_t_max < tv_sec: | ||||
|                 tv_sec = self.time_t_max | ||||
|  | @ -1035,16 +1040,16 @@ def test_AsTimespec_clamp(self): | |||
|             self.assertEqual(ts, (tv_sec, tv_nsec)) | ||||
| 
 | ||||
|     def test_AsMilliseconds(self): | ||||
|         from _testcapi import PyTime_AsMilliseconds | ||||
|         from _testinternalcapi import _PyTime_AsMilliseconds | ||||
| 
 | ||||
|         self.check_int_rounding(PyTime_AsMilliseconds, | ||||
|         self.check_int_rounding(_PyTime_AsMilliseconds, | ||||
|                                 self.create_decimal_converter(MS_TO_NS), | ||||
|                                 NS_TO_SEC) | ||||
| 
 | ||||
|     def test_AsMicroseconds(self): | ||||
|         from _testcapi import PyTime_AsMicroseconds | ||||
|         from _testinternalcapi import _PyTime_AsMicroseconds | ||||
| 
 | ||||
|         self.check_int_rounding(PyTime_AsMicroseconds, | ||||
|         self.check_int_rounding(_PyTime_AsMicroseconds, | ||||
|                                 self.create_decimal_converter(US_TO_NS), | ||||
|                                 NS_TO_SEC) | ||||
| 
 | ||||
|  | @ -1058,13 +1063,13 @@ class TestOldPyTime(CPyTimeTestCase, unittest.TestCase): | |||
|     OVERFLOW_SECONDS = 2 ** 64 | ||||
| 
 | ||||
|     def test_object_to_time_t(self): | ||||
|         from _testcapi import pytime_object_to_time_t | ||||
|         from _testinternalcapi import _PyTime_ObjectToTime_t | ||||
| 
 | ||||
|         self.check_int_rounding(pytime_object_to_time_t, | ||||
|         self.check_int_rounding(_PyTime_ObjectToTime_t, | ||||
|                                 lambda secs: secs, | ||||
|                                 value_filter=self.time_t_filter) | ||||
| 
 | ||||
|         self.check_float_rounding(pytime_object_to_time_t, | ||||
|         self.check_float_rounding(_PyTime_ObjectToTime_t, | ||||
|                                   self.decimal_round, | ||||
|                                   value_filter=self.time_t_filter) | ||||
| 
 | ||||
|  | @ -1084,36 +1089,36 @@ def converter(secs): | |||
|         return converter | ||||
| 
 | ||||
|     def test_object_to_timeval(self): | ||||
|         from _testcapi import pytime_object_to_timeval | ||||
|         from _testinternalcapi import _PyTime_ObjectToTimeval | ||||
| 
 | ||||
|         self.check_int_rounding(pytime_object_to_timeval, | ||||
|         self.check_int_rounding(_PyTime_ObjectToTimeval, | ||||
|                                 lambda secs: (secs, 0), | ||||
|                                 value_filter=self.time_t_filter) | ||||
| 
 | ||||
|         self.check_float_rounding(pytime_object_to_timeval, | ||||
|         self.check_float_rounding(_PyTime_ObjectToTimeval, | ||||
|                                   self.create_converter(SEC_TO_US), | ||||
|                                   value_filter=self.time_t_filter) | ||||
| 
 | ||||
|          # test nan | ||||
|         for time_rnd, _ in ROUNDING_MODES: | ||||
|             with self.assertRaises(ValueError): | ||||
|                 pytime_object_to_timeval(float('nan'), time_rnd) | ||||
|                 _PyTime_ObjectToTimeval(float('nan'), time_rnd) | ||||
| 
 | ||||
|     def test_object_to_timespec(self): | ||||
|         from _testcapi import pytime_object_to_timespec | ||||
|         from _testinternalcapi import _PyTime_ObjectToTimespec | ||||
| 
 | ||||
|         self.check_int_rounding(pytime_object_to_timespec, | ||||
|         self.check_int_rounding(_PyTime_ObjectToTimespec, | ||||
|                                 lambda secs: (secs, 0), | ||||
|                                 value_filter=self.time_t_filter) | ||||
| 
 | ||||
|         self.check_float_rounding(pytime_object_to_timespec, | ||||
|         self.check_float_rounding(_PyTime_ObjectToTimespec, | ||||
|                                   self.create_converter(SEC_TO_NS), | ||||
|                                   value_filter=self.time_t_filter) | ||||
| 
 | ||||
|         # test nan | ||||
|         for time_rnd, _ in ROUNDING_MODES: | ||||
|             with self.assertRaises(ValueError): | ||||
|                 pytime_object_to_timespec(float('nan'), time_rnd) | ||||
|                 _PyTime_ObjectToTimespec(float('nan'), time_rnd) | ||||
| 
 | ||||
| @unittest.skipUnless(sys.platform == "darwin", "test weak linking on macOS") | ||||
| class TestTimeWeaklinking(unittest.TestCase): | ||||
|  |  | |||
|  | @ -1710,7 +1710,6 @@ PYTHON_HEADERS= \ | |||
| 		$(srcdir)/Include/cpython/pystate.h \ | ||||
| 		$(srcdir)/Include/cpython/pythonrun.h \ | ||||
| 		$(srcdir)/Include/cpython/pythread.h \ | ||||
| 		$(srcdir)/Include/cpython/pytime.h \ | ||||
| 		$(srcdir)/Include/cpython/setobject.h \ | ||||
| 		$(srcdir)/Include/cpython/sysmodule.h \ | ||||
| 		$(srcdir)/Include/cpython/traceback.h \ | ||||
|  |  | |||
|  | @ -0,0 +1,2 @@ | |||
| Remove ``cpython/pytime.h`` header file: it only contained private | ||||
| functions. Patch by Victor Stinner. | ||||
|  | @ -159,7 +159,7 @@ | |||
| @MODULE__XXTESTFUZZ_TRUE@_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c | ||||
| @MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c | ||||
| @MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c | ||||
| @MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/vectorcall_limited.c _testcapi/heaptype.c _testcapi/unicode.c _testcapi/getargs.c _testcapi/pytime.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyos.c _testcapi/immortal.c _testcapi/heaptype_relative.c _testcapi/gc.c | ||||
| @MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/vectorcall_limited.c _testcapi/heaptype.c _testcapi/unicode.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyos.c _testcapi/immortal.c _testcapi/heaptype_relative.c _testcapi/gc.c | ||||
| @MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c | ||||
| 
 | ||||
| # Some testing modules MUST be built as shared libraries. | ||||
|  |  | |||
|  | @ -4,6 +4,7 @@ | |||
| 
 | ||||
| #include "Python.h" | ||||
| #include "pycore_moduleobject.h"  // _PyModule_GetState() | ||||
| #include "pycore_time.h"          // _PyTime_t | ||||
| #include "structmember.h"         // PyMemberDef | ||||
| #include <stddef.h>               // offsetof() | ||||
| 
 | ||||
|  |  | |||
|  | @ -28,7 +28,6 @@ int _PyTestCapi_Init_Vectorcall(PyObject *module); | |||
| int _PyTestCapi_Init_Heaptype(PyObject *module); | ||||
| int _PyTestCapi_Init_Unicode(PyObject *module); | ||||
| int _PyTestCapi_Init_GetArgs(PyObject *module); | ||||
| int _PyTestCapi_Init_PyTime(PyObject *module); | ||||
| int _PyTestCapi_Init_DateTime(PyObject *module); | ||||
| int _PyTestCapi_Init_Docstring(PyObject *module); | ||||
| int _PyTestCapi_Init_Mem(PyObject *module); | ||||
|  |  | |||
|  | @ -1,274 +0,0 @@ | |||
| #include "parts.h" | ||||
| 
 | ||||
| #ifdef MS_WINDOWS | ||||
| #  include <winsock2.h>           // struct timeval
 | ||||
| #endif | ||||
| 
 | ||||
| static PyObject * | ||||
| test_pytime_fromseconds(PyObject *self, PyObject *args) | ||||
| { | ||||
|     int seconds; | ||||
|     if (!PyArg_ParseTuple(args, "i", &seconds)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t ts = _PyTime_FromSeconds(seconds); | ||||
|     return _PyTime_AsNanosecondsObject(ts); | ||||
| } | ||||
| 
 | ||||
| static int | ||||
| check_time_rounding(int round) | ||||
| { | ||||
|     if (round != _PyTime_ROUND_FLOOR | ||||
|         && round != _PyTime_ROUND_CEILING | ||||
|         && round != _PyTime_ROUND_HALF_EVEN | ||||
|         && round != _PyTime_ROUND_UP) | ||||
|     { | ||||
|         PyErr_SetString(PyExc_ValueError, "invalid rounding"); | ||||
|         return -1; | ||||
|     } | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_pytime_fromsecondsobject(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     int round; | ||||
|     if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (check_time_rounding(round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t ts; | ||||
|     if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) { | ||||
|         return NULL; | ||||
|     } | ||||
|     return _PyTime_AsNanosecondsObject(ts); | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_pytime_assecondsdouble(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     if (!PyArg_ParseTuple(args, "O", &obj)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t ts; | ||||
|     if (_PyTime_FromNanosecondsObject(&ts, obj) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     double d = _PyTime_AsSecondsDouble(ts); | ||||
|     return PyFloat_FromDouble(d); | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_PyTime_AsTimeval(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     int round; | ||||
|     if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (check_time_rounding(round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t t; | ||||
|     if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     struct timeval tv; | ||||
|     if (_PyTime_AsTimeval(t, &tv, round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
| 
 | ||||
|     PyObject *seconds = PyLong_FromLongLong(tv.tv_sec); | ||||
|     if (seconds == NULL) { | ||||
|         return NULL; | ||||
|     } | ||||
|     return Py_BuildValue("Nl", seconds, (long)tv.tv_usec); | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_PyTime_AsTimeval_clamp(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     int round; | ||||
|     if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (check_time_rounding(round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t t; | ||||
|     if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     struct timeval tv; | ||||
|     _PyTime_AsTimeval_clamp(t, &tv, round); | ||||
| 
 | ||||
|     PyObject *seconds = PyLong_FromLongLong(tv.tv_sec); | ||||
|     if (seconds == NULL) { | ||||
|         return NULL; | ||||
|     } | ||||
|     return Py_BuildValue("Nl", seconds, (long)tv.tv_usec); | ||||
| } | ||||
| 
 | ||||
| #ifdef HAVE_CLOCK_GETTIME | ||||
| static PyObject * | ||||
| test_PyTime_AsTimespec(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     if (!PyArg_ParseTuple(args, "O", &obj)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t t; | ||||
|     if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     struct timespec ts; | ||||
|     if (_PyTime_AsTimespec(t, &ts) == -1) { | ||||
|         return NULL; | ||||
|     } | ||||
|     return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec); | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_PyTime_AsTimespec_clamp(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     if (!PyArg_ParseTuple(args, "O", &obj)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t t; | ||||
|     if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     struct timespec ts; | ||||
|     _PyTime_AsTimespec_clamp(t, &ts); | ||||
|     return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec); | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
| static PyObject * | ||||
| test_PyTime_AsMilliseconds(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     int round; | ||||
|     if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t t; | ||||
|     if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (check_time_rounding(round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t ms = _PyTime_AsMilliseconds(t, round); | ||||
|     _PyTime_t ns = _PyTime_FromNanoseconds(ms); | ||||
|     return _PyTime_AsNanosecondsObject(ns); | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_PyTime_AsMicroseconds(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     int round; | ||||
|     if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t t; | ||||
|     if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (check_time_rounding(round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t us = _PyTime_AsMicroseconds(t, round); | ||||
|     _PyTime_t ns = _PyTime_FromNanoseconds(us); | ||||
|     return _PyTime_AsNanosecondsObject(ns); | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_pytime_object_to_time_t(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     time_t sec; | ||||
|     int round; | ||||
|     if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_time_t", &obj, &round)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (check_time_rounding(round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1) { | ||||
|         return NULL; | ||||
|     } | ||||
|     return _PyLong_FromTime_t(sec); | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_pytime_object_to_timeval(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     time_t sec; | ||||
|     long usec; | ||||
|     int round; | ||||
|     if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timeval", &obj, &round)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (check_time_rounding(round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1) { | ||||
|         return NULL; | ||||
|     } | ||||
|     return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec); | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_pytime_object_to_timespec(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     time_t sec; | ||||
|     long nsec; | ||||
|     int round; | ||||
|     if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timespec", &obj, &round)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (check_time_rounding(round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1) { | ||||
|         return NULL; | ||||
|     } | ||||
|     return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec); | ||||
| } | ||||
| 
 | ||||
| static PyMethodDef test_methods[] = { | ||||
|     {"PyTime_AsMicroseconds",       test_PyTime_AsMicroseconds,     METH_VARARGS}, | ||||
|     {"PyTime_AsMilliseconds",       test_PyTime_AsMilliseconds,     METH_VARARGS}, | ||||
|     {"PyTime_AsSecondsDouble",      test_pytime_assecondsdouble,    METH_VARARGS}, | ||||
| #ifdef HAVE_CLOCK_GETTIME | ||||
|     {"PyTime_AsTimespec",           test_PyTime_AsTimespec,         METH_VARARGS}, | ||||
|     {"PyTime_AsTimespec_clamp",     test_PyTime_AsTimespec_clamp,   METH_VARARGS}, | ||||
| #endif | ||||
|     {"PyTime_AsTimeval",            test_PyTime_AsTimeval,          METH_VARARGS}, | ||||
|     {"PyTime_AsTimeval_clamp",      test_PyTime_AsTimeval_clamp,    METH_VARARGS}, | ||||
|     {"PyTime_FromSeconds",          test_pytime_fromseconds,        METH_VARARGS}, | ||||
|     {"PyTime_FromSecondsObject",    test_pytime_fromsecondsobject,  METH_VARARGS}, | ||||
|     {"pytime_object_to_time_t",     test_pytime_object_to_time_t,   METH_VARARGS}, | ||||
|     {"pytime_object_to_timespec",   test_pytime_object_to_timespec, METH_VARARGS}, | ||||
|     {"pytime_object_to_timeval",    test_pytime_object_to_timeval,  METH_VARARGS}, | ||||
|     {NULL}, | ||||
| }; | ||||
| 
 | ||||
| int | ||||
| _PyTestCapi_Init_PyTime(PyObject *mod) | ||||
| { | ||||
|     if (PyModule_AddFunctions(mod, test_methods) < 0) { | ||||
|         return -1; | ||||
|     } | ||||
|     return 0; | ||||
| } | ||||
|  | @ -4216,9 +4216,6 @@ PyInit__testcapi(void) | |||
|     if (_PyTestCapi_Init_GetArgs(m) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (_PyTestCapi_Init_PyTime(m) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (_PyTestCapi_Init_DateTime(m) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|  |  | |||
|  | @ -31,6 +31,10 @@ | |||
| 
 | ||||
| #include "clinic/_testinternalcapi.c.h" | ||||
| 
 | ||||
| #ifdef MS_WINDOWS | ||||
| #  include <winsock2.h>           // struct timeval
 | ||||
| #endif | ||||
| 
 | ||||
| 
 | ||||
| #define MODULE_NAME "_testinternalcapi" | ||||
| 
 | ||||
|  | @ -969,6 +973,249 @@ pending_identify(PyObject *self, PyObject *args) | |||
| } | ||||
| 
 | ||||
| 
 | ||||
| static PyObject * | ||||
| test_pytime_fromseconds(PyObject *self, PyObject *args) | ||||
| { | ||||
|     int seconds; | ||||
|     if (!PyArg_ParseTuple(args, "i", &seconds)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t ts = _PyTime_FromSeconds(seconds); | ||||
|     return _PyTime_AsNanosecondsObject(ts); | ||||
| } | ||||
| 
 | ||||
| static int | ||||
| check_time_rounding(int round) | ||||
| { | ||||
|     if (round != _PyTime_ROUND_FLOOR | ||||
|         && round != _PyTime_ROUND_CEILING | ||||
|         && round != _PyTime_ROUND_HALF_EVEN | ||||
|         && round != _PyTime_ROUND_UP) | ||||
|     { | ||||
|         PyErr_SetString(PyExc_ValueError, "invalid rounding"); | ||||
|         return -1; | ||||
|     } | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_pytime_fromsecondsobject(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     int round; | ||||
|     if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (check_time_rounding(round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t ts; | ||||
|     if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) { | ||||
|         return NULL; | ||||
|     } | ||||
|     return _PyTime_AsNanosecondsObject(ts); | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_pytime_assecondsdouble(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     if (!PyArg_ParseTuple(args, "O", &obj)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t ts; | ||||
|     if (_PyTime_FromNanosecondsObject(&ts, obj) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     double d = _PyTime_AsSecondsDouble(ts); | ||||
|     return PyFloat_FromDouble(d); | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_PyTime_AsTimeval(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     int round; | ||||
|     if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (check_time_rounding(round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t t; | ||||
|     if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     struct timeval tv; | ||||
|     if (_PyTime_AsTimeval(t, &tv, round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
| 
 | ||||
|     PyObject *seconds = PyLong_FromLongLong(tv.tv_sec); | ||||
|     if (seconds == NULL) { | ||||
|         return NULL; | ||||
|     } | ||||
|     return Py_BuildValue("Nl", seconds, (long)tv.tv_usec); | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_PyTime_AsTimeval_clamp(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     int round; | ||||
|     if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (check_time_rounding(round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t t; | ||||
|     if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     struct timeval tv; | ||||
|     _PyTime_AsTimeval_clamp(t, &tv, round); | ||||
| 
 | ||||
|     PyObject *seconds = PyLong_FromLongLong(tv.tv_sec); | ||||
|     if (seconds == NULL) { | ||||
|         return NULL; | ||||
|     } | ||||
|     return Py_BuildValue("Nl", seconds, (long)tv.tv_usec); | ||||
| } | ||||
| 
 | ||||
| #ifdef HAVE_CLOCK_GETTIME | ||||
| static PyObject * | ||||
| test_PyTime_AsTimespec(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     if (!PyArg_ParseTuple(args, "O", &obj)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t t; | ||||
|     if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     struct timespec ts; | ||||
|     if (_PyTime_AsTimespec(t, &ts) == -1) { | ||||
|         return NULL; | ||||
|     } | ||||
|     return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec); | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_PyTime_AsTimespec_clamp(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     if (!PyArg_ParseTuple(args, "O", &obj)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t t; | ||||
|     if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     struct timespec ts; | ||||
|     _PyTime_AsTimespec_clamp(t, &ts); | ||||
|     return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec); | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
| static PyObject * | ||||
| test_PyTime_AsMilliseconds(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     int round; | ||||
|     if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t t; | ||||
|     if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (check_time_rounding(round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t ms = _PyTime_AsMilliseconds(t, round); | ||||
|     _PyTime_t ns = _PyTime_FromNanoseconds(ms); | ||||
|     return _PyTime_AsNanosecondsObject(ns); | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_PyTime_AsMicroseconds(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     int round; | ||||
|     if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t t; | ||||
|     if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (check_time_rounding(round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     _PyTime_t us = _PyTime_AsMicroseconds(t, round); | ||||
|     _PyTime_t ns = _PyTime_FromNanoseconds(us); | ||||
|     return _PyTime_AsNanosecondsObject(ns); | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_pytime_object_to_time_t(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     time_t sec; | ||||
|     int round; | ||||
|     if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (check_time_rounding(round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1) { | ||||
|         return NULL; | ||||
|     } | ||||
|     return _PyLong_FromTime_t(sec); | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_pytime_object_to_timeval(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     time_t sec; | ||||
|     long usec; | ||||
|     int round; | ||||
|     if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (check_time_rounding(round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1) { | ||||
|         return NULL; | ||||
|     } | ||||
|     return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec); | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| test_pytime_object_to_timespec(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *obj; | ||||
|     time_t sec; | ||||
|     long nsec; | ||||
|     int round; | ||||
|     if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (check_time_rounding(round) < 0) { | ||||
|         return NULL; | ||||
|     } | ||||
|     if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1) { | ||||
|         return NULL; | ||||
|     } | ||||
|     return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| static PyMethodDef module_functions[] = { | ||||
|     {"get_configs", get_configs, METH_NOARGS}, | ||||
|     {"get_recursion_depth", get_recursion_depth, METH_NOARGS}, | ||||
|  | @ -1005,6 +1252,20 @@ static PyMethodDef module_functions[] = { | |||
|      METH_VARARGS | METH_KEYWORDS}, | ||||
| //    {"pending_fd_identify", pending_fd_identify, METH_VARARGS, NULL},
 | ||||
|     {"pending_identify", pending_identify, METH_VARARGS, NULL}, | ||||
|     {"_PyTime_AsMicroseconds",    test_PyTime_AsMicroseconds,     METH_VARARGS}, | ||||
|     {"_PyTime_AsMilliseconds",    test_PyTime_AsMilliseconds,     METH_VARARGS}, | ||||
|     {"_PyTime_AsSecondsDouble",   test_pytime_assecondsdouble,    METH_VARARGS}, | ||||
| #ifdef HAVE_CLOCK_GETTIME | ||||
|     {"_PyTime_AsTimespec",        test_PyTime_AsTimespec,         METH_VARARGS}, | ||||
|     {"_PyTime_AsTimespec_clamp",  test_PyTime_AsTimespec_clamp,   METH_VARARGS}, | ||||
| #endif | ||||
|     {"_PyTime_AsTimeval",         test_PyTime_AsTimeval,          METH_VARARGS}, | ||||
|     {"_PyTime_AsTimeval_clamp",   test_PyTime_AsTimeval_clamp,    METH_VARARGS}, | ||||
|     {"_PyTime_FromSeconds",       test_pytime_fromseconds,        METH_VARARGS}, | ||||
|     {"_PyTime_FromSecondsObject", test_pytime_fromsecondsobject,  METH_VARARGS}, | ||||
|     {"_PyTime_ObjectToTime_t",    test_pytime_object_to_time_t,   METH_VARARGS}, | ||||
|     {"_PyTime_ObjectToTimespec",  test_pytime_object_to_timespec, METH_VARARGS}, | ||||
|     {"_PyTime_ObjectToTimeval",   test_pytime_object_to_timeval,  METH_VARARGS}, | ||||
|     {NULL, NULL} /* sentinel */ | ||||
| }; | ||||
| 
 | ||||
|  | @ -1019,6 +1280,11 @@ module_exec(PyObject *module) | |||
|         return 1; | ||||
|     } | ||||
| 
 | ||||
|     if (PyModule_AddObject(module, "SIZEOF_TIME_T", | ||||
|                            PyLong_FromSsize_t(sizeof(time_t))) < 0) { | ||||
|         return 1; | ||||
|     } | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -8,6 +8,7 @@ | |||
| //#include <time.h>
 | ||||
| #include "Python.h" | ||||
| #include "pycore_namespace.h"     // _PyNamespace_New() | ||||
| #include "pycore_time.h"          // _PyTime_t | ||||
| 
 | ||||
| 
 | ||||
| typedef struct { | ||||
|  |  | |||
|  | @ -14,6 +14,7 @@ | |||
| 
 | ||||
| #include "Python.h" | ||||
| #include "pycore_fileutils.h"     // _Py_set_inheritable() | ||||
| #include "pycore_time.h"          // _PyTime_t | ||||
| #include "structmember.h"         // PyMemberDef | ||||
| 
 | ||||
| #ifdef HAVE_SYS_DEVPOLL_H | ||||
|  |  | |||
|  | @ -1,5 +1,7 @@ | |||
| /* Socket module header file */ | ||||
| 
 | ||||
| #include "pycore_time.h"          // _PyTime_t | ||||
| 
 | ||||
| /* Includes needed for the sockaddr_* symbols below */ | ||||
| #ifndef MS_WINDOWS | ||||
| #ifdef __VMS | ||||
|  |  | |||
|  | @ -100,7 +100,6 @@ | |||
|     <ClCompile Include="..\Modules\_testcapi\heaptype.c" /> | ||||
|     <ClCompile Include="..\Modules\_testcapi\heaptype_relative.c" /> | ||||
|     <ClCompile Include="..\Modules\_testcapi\unicode.c" /> | ||||
|     <ClCompile Include="..\Modules\_testcapi\pytime.c" /> | ||||
|     <ClCompile Include="..\Modules\_testcapi\datetime.c" /> | ||||
|     <ClCompile Include="..\Modules\_testcapi\docstring.c" /> | ||||
|     <ClCompile Include="..\Modules\_testcapi\mem.c" /> | ||||
|  |  | |||
|  | @ -30,9 +30,6 @@ | |||
|     <ClCompile Include="..\Modules\_testcapi\unicode.c"> | ||||
|       <Filter>Source Files</Filter> | ||||
|     </ClCompile> | ||||
|     <ClCompile Include="..\Modules\_testcapi\pytime.c"> | ||||
|       <Filter>Source Files</Filter> | ||||
|     </ClCompile> | ||||
|     <ClCompile Include="..\Modules\_testcapi\datetime.c"> | ||||
|       <Filter>Source Files</Filter> | ||||
|     </ClCompile> | ||||
|  |  | |||
|  | @ -176,7 +176,6 @@ | |||
|     <ClInclude Include="..\Include\cpython\pystate.h" /> | ||||
|     <ClInclude Include="..\Include\cpython\pythonrun.h" /> | ||||
|     <ClInclude Include="..\Include\cpython\pythread.h" /> | ||||
|     <ClInclude Include="..\Include\cpython\pytime.h" /> | ||||
|     <ClInclude Include="..\Include\cpython\setobject.h" /> | ||||
|     <ClInclude Include="..\Include\cpython\sysmodule.h" /> | ||||
|     <ClInclude Include="..\Include\cpython\traceback.h" /> | ||||
|  |  | |||
|  | @ -447,9 +447,6 @@ | |||
|     <ClInclude Include="..\Include\cpython\pylifecycle.h"> | ||||
|       <Filter>Include\cpython</Filter> | ||||
|     </ClInclude> | ||||
|     <ClInclude Include="..\Include\cpython\pytime.h"> | ||||
|       <Filter>Include\cpython</Filter> | ||||
|     </ClInclude> | ||||
|     <ClInclude Include="..\Include\cpython\tupleobject.h"> | ||||
|       <Filter>Include\cpython</Filter> | ||||
|     </ClInclude> | ||||
|  |  | |||
|  | @ -1,4 +1,5 @@ | |||
| #include "Python.h" | ||||
| #include "pycore_time.h"          // _PyTime_t | ||||
| #ifdef MS_WINDOWS | ||||
| #  include <winsock2.h>           // struct timeval
 | ||||
| #endif | ||||
|  |  | |||
|  | @ -40,7 +40,6 @@ | |||
|     "longintrepr.h", | ||||
|     "parsetok.h", | ||||
|     "pyatomic.h", | ||||
|     "pytime.h", | ||||
|     "token.h", | ||||
|     "ucnhash.h", | ||||
| } | ||||
|  |  | |||
|  | @ -60,6 +60,13 @@ def preprocess(filename, | |||
|     if not cwd or not os.path.isabs(cwd): | ||||
|         cwd = os.path.abspath(cwd or '.') | ||||
|     filename = _normpath(filename, cwd) | ||||
| 
 | ||||
|     postargs = POST_ARGS | ||||
|     if os.path.basename(filename) == 'socketmodule.h': | ||||
|         # Modules/socketmodule.h uses pycore_time.h which needs Py_BUILD_CORE. | ||||
|         # Usually it's defined by the C file which includes it. | ||||
|         postargs += ('-DPy_BUILD_CORE=1',) | ||||
| 
 | ||||
|     text = _common.preprocess( | ||||
|         TOOL, | ||||
|         filename, | ||||
|  | @ -67,7 +74,7 @@ def preprocess(filename, | |||
|         includes=includes, | ||||
|         macros=macros, | ||||
|         #preargs=PRE_ARGS, | ||||
|         postargs=POST_ARGS, | ||||
|         postargs=postargs, | ||||
|         executable=['gcc'], | ||||
|         compiler='unix', | ||||
|         cwd=cwd, | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Victor Stinner
						Victor Stinner