You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Please expose _PyTime_t, _PyTime_FromTimeval, and _PyTime_AsSecondsDouble as public APIs #110850
changed the title [-]Please expose _PyTime_t, _PyTime_FromTimeval, and _PyTime_AsSecondsDouble as public apis[/-][+]Please expose _PyTime_t, _PyTime_FromTimeval, and _PyTime_AsSecondsDouble as public APIs[/+]on Oct 14, 2023
I'm open to make some _Py_Time functions public, but apparently here, there wasn't a strong need for that. Maybe it can wait. Thanks for the report anyway.
Correct. It basically mimics the Python time module and reimplements a part of it in C.
I'd still recommend exposing the complete C-API. We are talking about a standard library module here, not a part of the core C-API, so it represents a natural API border already. We are not talking about internals here. Let's just keep that up and allow regular external use as before.
I designed _PyTime_t API for Python internal usage, but apparently some people discovered it and decided to use it since it's convenient.
_PyTime_t API was designed to be able to write easily "deadline = time + timeout", but later I started to add "clamp" variants in code which cannot report errors to the caller. Clamp the result in the range [_PyTime_MIN; _PyTime_MAX] is better than having an undefined behavior.
One issue is that _PyTime_t range is smaller than time_t range:
// 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].
64-bit time_t (standard even on 32-bit platforms) is way larger than that. In practice, it should not be an issue. But the datetime module cannot use _PyTime_t everywhere for example, since datetime max year is 9999, not year 2262.
I tried to make the _PyTime_t unit an implementation details and enforce _PyTime_FromNanoseconds() or _PyTime_FromSeconds() usage.
About raised exception, ValueError is raised for Not-A-Number floating pointer number, and OverflowError on conversions (_PyTime_t, time_t, timeval, timespec, etc.).
Victor, do you have any intention of putting the removed API back?
My plan in this issue is to add public functions replacing removed _PyTime functions. I'm collecting data to see how the private API was used and which API should be added.
Activity
[-]Please expose _PyTime_t, _PyTime_FromTimeval, and _PyTime_AsSecondsDouble as public apis[/-][+]Please expose _PyTime_t, _PyTime_FromTimeval, and _PyTime_AsSecondsDouble as public APIs[/+]hugovk commentedon Oct 14, 2023
Please could you say something about your use case?
Why do you need these functions?
Have you tried alternative APIs to achieve the same result?
cc @vstinner
ryancdotorg commentedon Oct 14, 2023
I have a C/C++ module that was using them which I wrote a couple years ago. It broke.
I actually dug into what I was doing, and it turns out it was basically just (error checking omitted):
and I was able to more sensibly replace it with
It seems I just didn't want to bother figuring out the safest way to do the conversion.
This can be closed.
vstinner commentedon Oct 14, 2023
I'm open to make some _Py_Time functions public, but apparently here, there wasn't a strong need for that. Maybe it can wait. Thanks for the report anyway.
scoder commentedon Oct 30, 2023
We actually expose that API in Cython's
cpython/time.pxd. Please move it back to where it was.vstinner commentedon Oct 30, 2023
This file uses the following APIs:
scoder commentedon Oct 30, 2023
Correct. It basically mimics the Python
timemodule and reimplements a part of it in C.I'd still recommend exposing the complete C-API. We are talking about a standard library module here, not a part of the core C-API, so it represents a natural API border already. We are not talking about internals here. Let's just keep that up and allow regular external use as before.
vstinner commentedon Oct 30, 2023
I designed _PyTime_t API for Python internal usage, but apparently some people discovered it and decided to use it since it's convenient.
_PyTime_t API was designed to be able to write easily "deadline = time + timeout", but later I started to add "clamp" variants in code which cannot report errors to the caller. Clamp the result in the range [_PyTime_MIN; _PyTime_MAX] is better than having an undefined behavior.
One issue is that
_PyTime_trange is smaller than time_t range:64-bit time_t (standard even on 32-bit platforms) is way larger than that. In practice, it should not be an issue. But the datetime module cannot use
_PyTime_teverywhere for example, since datetime max year is 9999, not year 2262.I tried to make the
_PyTime_tunit an implementation details and enforce _PyTime_FromNanoseconds() or _PyTime_FromSeconds() usage.About raised exception, ValueError is raised for Not-A-Number floating pointer number, and OverflowError on conversions (_PyTime_t, time_t, timeval, timespec, etc.).
encukou commentedon Oct 31, 2023
Victor, do you have any intention of putting the removed API back?
vstinner commentedon Nov 1, 2023
My plan in this issue is to add public functions replacing removed _PyTime functions. I'm collecting data to see how the private API was used and which API should be added.
pythongh-110850: Add PyTime_t C API
91 remaining items
pythongh-110850: Remove _PyTime_TimeUnchecked() function
pythongh-110850: Remove _PyTime_TimeUnchecked() function
pythongh-110850: Remove _PyTime_TimeUnchecked() function
gh-110850: Remove _PyTime_TimeUnchecked() function (#118552)
pythongh-110850: Add PyTime_TimeRaw() function (python#118394)
pythongh-110850: Use _PyDeadline_Get() in EnterNonRecursiveMutex() (p…
pythongh-110850: Remove _PyTime_TimeUnchecked() function (python#118552)
pythongh-110850: PyTime_Time() return 0 on success (pythonGH-115713)
pythongh-110850: Enhance PyTime C API tests (python#115715)
pythongh-110850: Replace _PyTime_t with PyTime_t (python#115719)
pythongh-110850: Cleanup pycore_time.h includes (python#115724)
pythongh-110850: Rename internal PyTime C API functions (python#115734)
pythongh-110850: Use public PyTime functions (python#115746)
pythongh-110850: Replace private _PyTime_MAX with public PyTime_MAX (p…
pythongh-110850: Cleanup PyTime API: PyTime_t are nanoseconds (python…