summaryrefslogtreecommitdiffstats
path: root/Modules/_datetimemodule.c
Commit message (Collapse)AuthorAgeFilesLines
* [3.13] gh-52551: Fix encoding issues in strftime() (GH-125193) (GH-125657)Serhiy Storchaka2024-10-171-102/+67
| | | | | | | | | | | | | | | Fix time.strftime(), the strftime() method and formatting of the datetime classes datetime, date and time. * Characters not encodable in the current locale are now acceptable in the format string. * Surrogate pairs and sequence of surrogatescape-encoded bytes are no longer recombinated. * Embedded null character no longer terminates the format string. This fixes also gh-78662 and gh-124531. (cherry picked from commit ad3eac1963a5f195ef9b2c1dbb5e44fa3cce4c72)
* Revert "[3.13] gh-120713: Normalize year with century for datetime.strftime ↵Serhiy Storchaka2024-07-291-50/+4
| | | | | (GH-120820) (GH-121144)" (GH-122408) This reverts commit 009618f1125838af3c4afc772f2593637766fd45.
* [3.13] GH-121832: Assert that the version number of static builtin types is ↵Miss Islington (bot)2024-07-251-46/+42
| | | | | | | | | not changed by PyType_Modified (gh-122290) Update datetime module and test_type_cache.py to not call PyType_Modified. (cherry picked from commit e55b05f29ee62cd92b6b9990fd699b78f19432ba, AKA gh--122182) Co-authored-by: Mark Shannon <mark@hotpy.org>
* [3.13] gh-120782: Update internal type cache when reloading datetime ↵neonene2024-07-031-0/+6
| | | | | | | | (GH-120829) (#120855) * [3.13] gh-120782: Update internal type cache when reloading datetime When reloading _datetime module, the single-phase version did not invoke the PyInit__datetime function, whereas the current multi-phase version updates the static types through the module init. The outdated static type cache in the interpreter state needs to be invalidated at the end of reloading the multi-phase module.
* [3.13] gh-120713: Normalize year with century for datetime.strftime ↵Miss Islington (bot)2024-06-291-4/+50
| | | | | | | (GH-120820) (GH-121144) (cherry picked from commit 6d34938dc8163f4a4bcc68069a1645a7ab76e935) Co-authored-by: blhsing <blhsing@gmail.com>
* [3.13] gh-120161: Fix a Crash in the _datetime Module (gh-120518)Miss Islington (bot)2024-06-141-46/+2
| | | | | | | | | | | In gh-120009 I used an atexit hook to finalize the _datetime module's static types at interpreter shutdown. However, atexit hooks are executed very early in finalization, which is a problem in the few cases where a subclass of one of those static types is still alive until the final GC collection. The static builtin types don't have this probably because they are finalized toward the end, after the final GC collection. To avoid the problem for _datetime, I have applied a similar approach here. Also, credit goes to @mgorny and @neonene for the new tests. FYI, I would have liked to take a slightly cleaner approach with managed static types, but wanted to get a smaller fix in first for the sake of backporting. I'll circle back to the cleaner approach with a future change on the main branch. (cherry picked from commit b2e71ff4f8fa5b7d8117dd8125137aee3d01f015, AKA gh-120182) Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
* [3.13] gh-71587: Drop local reference cache to `_strptime` module in ↵Miss Islington (bot)2024-06-121-7/+7
| | | | | | | | | `_datetime` (gh-120424) The _strptime module object was cached in a static local variable (in the datetime.strptime() implementation). That's a problem when it crosses isolation boundaries, such as reinitializing the runtme or between interpreters. This change fixes the problem by dropping the static variable, instead always relying on the normal sys.modules cache (via PyImport_Import()). (cherry picked from commit 127c1d2771749853e287632c086b6054212bf12a, AKA gh-120224) Co-authored-by: neonene <53406459+neonene@users.noreply.github.com>
* [3.13] gh-117398: Use Per-Interpreter State for the _datetime Static Types ↵Miss Islington (bot)2024-06-031-55/+142
| | | | | | | | | | | (gh-120009) We make use of the same mechanism that we use for the static builtin types. This required a few tweaks. This change is the final piece needed to make _datetime support multiple interpreters. I've updated the module slot accordingly. (cherry picked from commit 105f22ea46ac16866e6df18ebae2a8ba422b7f45, AKA gh-119929) Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
* [3.13] gh-117398: Add datetime Module State (gh-120004)Miss Islington (bot)2024-06-031-162/+370
| | | | | | | | | | | | | | | | I was able to make use of the existing datetime_state struct, but there was one tricky thing I had to sort out. We mostly aren't converting to heap types, so we can't use things like PyType_GetModuleByDef() to look up the module state. The solution I came up with is somewhat novel, but I consider it straightforward. Also, it shouldn't have much impact on performance. In summary, this main changes here are: * I've added some macros to help hide how various objects relate to module state * as a solution to the module state lookup problem, I've stored the last loaded module on the current interpreter's internal dict (actually a weakref) * if the static type method is used after the module has been deleted, it is reloaded * to avoid extra work when loading the module, we directly copy the objects (new refs only) from the old module state into the new state if the old module hasn't been deleted yet * during module init we set various objects on the static types' __dict__s; to simplify things, we only do that the first time; once those static types have a separate __dict__ per interpreter, we'll do it every time * we now clear the module state when the module is destroyed (before, we were leaking everything in _datetime_global_state) (cherry picked from commit d82a7ba041321e7b58a5a9bbc394670be6ceeb7c, AKA gh-119810) Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
* [3.13] gh-117398: Convert datetime.IsoCalendarDate To A Heap Type ↵Miss Islington (bot)2024-05-301-22/+63
| | | | | | | | | | | (gh-119637) (gh-119695) This is the only static type in the module that we will not keep static. (cherry picked from commit 548a11d5cf1dbb32d86ce0c045130c77f50c1427) (cherry-picked from commit 34f9b3e7244615d2372614b20e10250e68cc8e61) Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com> Co-authored by: Kirill Podoprigora <kirill.bast9@mail.ru>
* [3.13] gh-117398: Add multiphase support to _datetime (gh-119694)Eric Snow2024-05-291-15/+11
| | | | | | | | | This is an unrevert of d58ebf0 (gh-119636), which was reverted by 9216a53 (gh-119639) due to problems which have been resolved. This is minimal support for multiphase init. Subinterpreters are not supported yet. That will be addressed in a later change. (cherry picked from commit 3e8b609) Co-authored-by: Erlend E. Aasland erlend@python.org
* [3.13] gh-117398: Statically Allocate the Datetime C-API (GH-119472) (gh-119641)Eric Snow2024-05-281-110/+188
| | | | | | | This is a backport of 3 commits that go together. (cherry picked from commit a895756) (cherry picked from commit b30d30c) (cherry picked from commit a89fc26)
* [3.13] gh-117398: Revert gh-119636, Add multiphase support to _datetime ↵Eric Snow2024-05-281-11/+15
| | | | | | | (#119639) Revert "[3.13] gh-117398: Add multiphase support to _datetime (gh-119373) (gh-119636)" This reverts commit d58ebf073c755c2f0f6e4ef2296b48a4c75e5f1c.
* [3.13] gh-117398: Add multiphase support to _datetime (gh-119373) (gh-119636)Miss Islington (bot)2024-05-271-15/+11
| | | | | | | | This is minimal support. Subinterpreters are not supported yet. That will be addressed in a later change. (cherry picked from commit 3e8b60905e97a4fe89bb24180063732214368938) Co-authored-by: Erlend E. Aasland <erlend@python.org> Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
* gh-116322: Rename PyModule_ExperimentalSetGIL to PyUnstable_Module_SetGIL ↵Petr Viktorin2024-05-061-1/+1
| | | | (GH-118645)
* gh-116322: Add Py_mod_gil module slot (#116882)Brett Simmers2024-05-031-0/+3
| | | | | | | | | | | | | | This PR adds the ability to enable the GIL if it was disabled at interpreter startup, and modifies the multi-phase module initialization path to enable the GIL when loading a module, unless that module's spec includes a slot indicating it can run safely without the GIL. PEP 703 called the constant for the slot `Py_mod_gil_not_used`; I went with `Py_MOD_GIL_NOT_USED` for consistency with gh-104148. A warning will be issued up to once per interpreter for the first GIL-using module that is loaded. If `-v` is given, a shorter message will be printed to stderr every time a GIL-using module is loaded (including the first one that issues a warning).
* gh-117764: Add docstrings and signatures for the __replace__ methods (GH-117768)Serhiy Storchaka2024-04-121-3/+6
|
* gh-117534: Add checking for input parameter in iso_to_ymd (#117543)Vlad48962024-04-091-7/+9
| | | | | | | Moves the validation for invalid years in the C implementation of the `datetime` module into a common location between `fromisoformat` and `fromisocalendar`, which improves the error message and fixes a failed assertion when parsing invalid ISO 8601 years using one of the "ISO weeks" formats. --------- Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
* gh-110850: Use public PyTime functions (#115746)Victor Stinner2024-02-201-1/+5
| | | | | Replace private _PyTime functions with public PyTime functions. random_seed_time_pid() now reports errors to its caller.
* gh-110850: Rename internal PyTime C API functions (#115734)Victor Stinner2024-02-201-1/+1
| | | | | | | | | | | | | | | | | Rename functions: * _PyTime_GetSystemClock() => _PyTime_TimeUnchecked() * _PyTime_GetPerfCounter() => _PyTime_PerfCounterUnchecked() * _PyTime_GetMonotonicClock() => _PyTime_MonotonicUnchecked() * _PyTime_GetSystemClockWithInfo() => _PyTime_TimeWithInfo() * _PyTime_GetMonotonicClockWithInfo() => _PyTime_MonotonicWithInfo() * _PyTime_GetMonotonicClockWithInfo() => _PyTime_MonotonicWithInfo() Changes: * Remove "typedef PyTime_t PyTime_t;" which was "typedef PyTime_t _PyTime_t;" before a previous rename. * Update comments of "Unchecked" functions. * Remove invalid PyTime_Time() comment.
* gh-110850: Cleanup pycore_time.h includes (#115724)Victor Stinner2024-02-201-0/+2
| | | | | <pycore_time.h> include is no longer needed to get the PyTime_t type in internal header files. This type is now provided by <Python.h> include. Add <pycore_time.h> includes to C files instead.
* gh-110850: Replace _PyTime_t with PyTime_t (#115719)Victor Stinner2024-02-201-1/+1
| | | | | Run command: sed -i -e 's!\<_PyTime_t\>!PyTime_t!g' $(find -name "*.c" -o -name "*.h")
* gh-89039: Call subclass constructors in datetime.*.replace (GH-114780)Eugene Toder2024-02-121-15/+62
| | | | | | | | | When replace() method is called on a subclass of datetime, date or time, properly call derived constructor. Previously, only the base class's constructor was called. Also, make sure to pass non-zero fold values when creating subclasses in various methods. Previously, fold was silently ignored.
* gh-49766: Make date-datetime comparison more symmetric and flexible (GH-114760)Serhiy Storchaka2024-02-111-26/+10
| | | | | | | | | | | | | Now the special comparison methods like `__eq__` and `__lt__` return NotImplemented if one of comparands is date and other is datetime instead of ignoring the time part and the time zone or forcefully return "not equal" or raise TypeError. It makes comparison of date and datetime subclasses more symmetric and allows to change the default behavior by overriding the special comparison methods in subclasses. It is now the same as if date and datetime was independent classes.
* gh-112919: Speed-up datetime, date and time.replace() (GH-112921)Eugene Toder2024-01-301-95/+75
| | | | | Use argument clinic and call new_* functions directly. This speeds up these functions 6x to 7.5x when calling with keyword arguments.
* Fix undefined behaviour in datetime.time.fromisoformat() (#111982)T. Wouters2023-11-111-1/+1
| | | Fix undefined behaviour in datetime.time.fromisoformat() when parsing a string without a timezone. 'tzoffset' is not assigned to by parse_isoformat_time if it returns 0, but time_fromisoformat then passes tzoffset to another function, which is undefined behaviour (even if the function in question does not use the value).
* gh-110093: Replace trivial Py_BuildValue() with direct C API call (GH-110094)Serhiy Storchaka2023-10-201-2/+2
|
* gh-71587: Establish global state in `_datetime` (#110475)Erlend E. Aasland2023-10-121-136/+172
| | | | | | | | * Use explicit initialiser for m_base * Add module state stub; establish global state on stack * Put conversion factors in state struct * Move PyDateTime_TimeZone_UTC to state * Move PyDateTime_Epoch to state struct * Fix ref leaks in and clean up initialisation
* gh-108751: Add copy.replace() function (GH-108752)Serhiy Storchaka2023-09-061-0/+6
| | | | | | | | | It creates a modified copy of an object by calling the object's __replace__() method. It is a generalization of dataclasses.replace(), named tuple's _replace() method and replace() methods in various classes, and supports all these stdlib classes.
* Clarify distinction between datetime module and class in deprecation ↵Clément Robert2023-08-271-4/+4
| | | | messages (GH-108073)
* gh-108444: Replace _PyLong_AsInt() with PyLong_AsInt() (#108459)Victor Stinner2023-08-241-3/+3
| | | | | | Change generated by the command: sed -i -e 's!_PyLong_AsInt!PyLong_AsInt!g' \ $(find -name "*.c" -o -name "*.h")
* gh-106869: Use new PyMemberDef constant names (#106871)Victor Stinner2023-07-251-4/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Remove '#include "structmember.h"'. * If needed, add <stddef.h> to get offsetof() function. * Update Parser/asdl_c.py to regenerate Python/Python-ast.c. * Replace: * T_SHORT => Py_T_SHORT * T_INT => Py_T_INT * T_LONG => Py_T_LONG * T_FLOAT => Py_T_FLOAT * T_DOUBLE => Py_T_DOUBLE * T_STRING => Py_T_STRING * T_OBJECT => _Py_T_OBJECT * T_CHAR => Py_T_CHAR * T_BYTE => Py_T_BYTE * T_UBYTE => Py_T_UBYTE * T_USHORT => Py_T_USHORT * T_UINT => Py_T_UINT * T_ULONG => Py_T_ULONG * T_STRING_INPLACE => Py_T_STRING_INPLACE * T_BOOL => Py_T_BOOL * T_OBJECT_EX => Py_T_OBJECT_EX * T_LONGLONG => Py_T_LONGLONG * T_ULONGLONG => Py_T_ULONGLONG * T_PYSSIZET => Py_T_PYSSIZET * T_NONE => _Py_T_NONE * READONLY => Py_READONLY * PY_AUDIT_READ => Py_AUDIT_READ * READ_RESTRICTED => Py_AUDIT_READ * PY_WRITE_RESTRICTED => _Py_WRITE_RESTRICTED * RESTRICTED => (READ_RESTRICTED | _Py_WRITE_RESTRICTED)
* gh-86493: Modernize modules initialization code (GH-106858)Serhiy Storchaka2023-07-251-2/+1
| | | | | Use PyModule_Add() or PyModule_AddObjectRef() instead of soft deprecated PyModule_AddObject().
* gh-106521: Remove _PyObject_LookupAttr() function (GH-106642)Serhiy Storchaka2023-07-121-1/+1
|
* gh-105375: Harden _datetime initialisation (#105604)Erlend E. Aasland2023-06-111-5/+30
| | | Improve error handling so init bails on the first exception.
* gh-92536: Remove PyUnicode_READY() calls (#105210)Victor Stinner2023-06-011-9/+0
| | | | Since Python 3.12, PyUnicode_READY() does nothing and always returns 0.
* gh-103857: Document utcnow and utcfromtimestamp deprecations in What's New ↵Hugo van Kemenade2023-05-211-1/+1
| | | | | | (#104542) Co-authored-by: Paul Ganssle <1377457+pganssle@users.noreply.github.com>
* gh-103857: Update deprecation stacktrace to point to calling line (#104431)Hugo van Kemenade2023-05-121-2/+2
|
* GH-103944: Check error status when raising DeprecationWarning (#103949)Paul Ganssle2023-04-281-12/+12
|
* GH-103857: Deprecate utcnow and utcfromtimestamp (#103858)Paul Ganssle2023-04-271-0/+14
| | | | | Using `datetime.datetime.utcnow()` and `datetime.datetime.utcfromtimestamp()` will now raise a `DeprecationWarning`. We also have removed our internal uses of these functions and documented the change.
* gh-83861: Fix datetime.astimezone() method (GH-101545)Alexander Belopolsky2023-04-191-2/+16
|
* gh-99537: Use Py_SETREF(var, NULL) in C code (#99687)Victor Stinner2022-11-231-8/+4
| | | Replace "Py_DECREF(var); var = NULL;" with "Py_SETREF(var, NULL);".
* gh-99537: Use Py_SETREF() function in C code (#99656)Victor Stinner2022-11-221-6/+2
| | | | | | | | | | | | | | | Fix potential race condition in code patterns: * Replace "Py_DECREF(var); var = new;" with "Py_SETREF(var, new);" * Replace "Py_XDECREF(var); var = new;" with "Py_XSETREF(var, new);" * Replace "Py_CLEAR(var); var = new;" with "Py_XSETREF(var, new);" Other changes: * Replace "old = var; var = new; Py_DECREF(var)" with "Py_SETREF(var, new);" * Replace "old = var; var = new; Py_XDECREF(var)" with "Py_XSETREF(var, new);" * And remove the "old" variable.
* gh-99300: Use Py_NewRef() in Modules/_datetimemodule.c (#99465)Victor Stinner2022-11-141-67/+34
| | | | Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in Modules/_datetimemodule.c and Modules/_zoneinfo.c
* GH-90699: Remove remaining `_Py_IDENTIFIER` stdlib usage (GH-99067)Kumar Aditya2022-11-071-29/+27
|
* gh-69142: add %:z strftime format code (gh-95983)TW2022-08-281-22/+42
| | | | | | | | | | | | | | | | datetime.isoformat generates the tzoffset with colons, but there was no format code to make strftime output the same format. for simplicity and consistency the %:z formatting behaves mostly as %z, with the exception of adding colons. this includes the dynamic behaviour of adding seconds and microseconds only when needed (when not 0). this fixes the still open "generate" part of this issue: https://github.com/python/cpython/issues/69142 Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
* gh-91838: Resolve HTTP links which redirect to HTTPS (GH-95642)Serhiy Storchaka2022-08-041-1/+1
| | | | It updates links which redirect to HTTPS with different authority or path.
* gh-93741: Add private C API _PyImport_GetModuleAttrString() (GH-93742)Serhiy Storchaka2022-06-141-23/+11
| | | | | | It combines PyImport_ImportModule() and PyObject_GetAttrString() and saves 4-6 lines of code on every use. Add also _PyImport_GetModuleAttr() which takes Python strings as arguments.
* gh-89653: Use int type for Unicode kind (#92704)Victor Stinner2022-05-131-1/+1
| | | | Use the same type that PyUnicode_FromKindAndData() kind parameter type (public C API): int.
* Check result of utc_to_seconds and skip fold probe in pure Python (#91582)Paul Ganssle2022-05-121-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The `utc_to_seconds` call can fail, here's a minimal reproducer on Linux: TZ=UTC python -c "from datetime import *; datetime.fromtimestamp(253402300799 + 1)" The old behavior still raised an error in a similar way, but only because subsequent calculations happened to fail as well. Better to fail fast. This also refactors the tests to split out the `fromtimestamp` and `utcfromtimestamp` tests, and to get us closer to the actual desired limits of the functions. As part of this, we also changed the way we detect platforms where the same limits don't necessarily apply (e.g. Windows). As part of refactoring the tests to hit this condition explicitly (even though the user-facing behvior doesn't change in any way we plan to guarantee), I noticed that there was a difference in the places that `datetime.utcfromtimestamp` fails in the C and pure Python versions, which was fixed by skipping the "probe for fold" logic for UTC specifically — since UTC doesn't have any folds or gaps, we were never going to find a fold value anyway. This should prevent some failures in the pure python `utcfromtimestamp` method on timestamps close to 0001-01-01. There are two separate news entries for this because one is a potentially user-facing change, the other is an internal code correctness change that, if anything, changes some error messages. The two happen to be coupled because of the test refactoring, but they are probably best thought of as independent changes. Fixes GH-91581