summaryrefslogtreecommitdiffstats
path: root/Modules/_datetimemodule.c
Commit message (Collapse)AuthorAgeFilesLines
* [3.11] Fix undefined behaviour in datetime.time.fromisoformat() (GH-111982) ↵Miss Islington (bot)2023-11-121-1/+1
| | | | | | | | | | (#111991) Fix undefined behaviour in datetime.time.fromisoformat() (GH-111982) 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). (cherry picked from commit 21615f77b5a580e83589abae618dbe7c298700e2) Co-authored-by: T. Wouters <thomas@python.org>
* [3.11] gh-105375: Harden _datetime initialisation (GH-105604) (#105646)Miss Islington (bot)2023-06-111-5/+30
| | | | | | Improve error handling so init bails on the first exception. (cherry picked from commit 16d49680b56e00c53c00683b949138e584669fd3) Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
* gh-91838: Resolve HTTP links which redirect to HTTPS (GH-95642)Miss Islington (bot)2022-08-041-1/+1
| | | | | | | It updates links which redirect to HTTPS with different authority or path. (cherry picked from commit d0d0154443cafb2f0a2cdfb6a1267d80cce8388e) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* Check result of utc_to_seconds and skip fold probe in pure Python (GH-91582)Miss Islington (bot)2022-05-141-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 (cherry picked from commit 83c0247d47b99f4571e35ea95361436e1d2a61cd) Co-authored-by: Paul Ganssle <1377457+pganssle@users.noreply.github.com>
* gh-80010: Expand fromisoformat to include most of ISO-8601 (#92177)Paul Ganssle2022-05-061-71/+288
| | | This expands `fromisoformat` to cover most of the common uses of ISO 8601. We may expand the scope more in the future.
* gh-91928: Add `datetime.UTC` alias for `datetime.timezone.utc` (GH-91973)Kabir Kwatra2022-05-031-0/+4
| | | | | | | | | ### fixes #91928 `UTC` is now module attribute aliased to `datetime.timezone.utc`. You can now do the following: ```python from datetime import UTC ```
* gh-91320: Use _PyCFunction_CAST() (#92251)Victor Stinner2022-05-031-11/+11
| | | | | | | | | | Replace "(PyCFunction)(void(*)(void))func" cast with _PyCFunction_CAST(func). Change generated by the command: sed -i -e \ 's!(PyCFunction)(void(\*)(void)) *\([A-Za-z0-9_]\+\)!_PyCFunction_CAST(\1)!g' \ $(find -name "*.c")
* gh-91731: Replace Py_BUILD_ASSERT() with static_assert() (#91730)Victor Stinner2022-04-201-3/+3
| | | | | | | | | | | | Python 3.11 now uses C11 standard which adds static_assert() to <assert.h>. * In pytime.c, replace Py_BUILD_ASSERT() with preprocessor checks on SIZEOF_TIME_T with #error. * On macOS, py_mach_timebase_info() now accepts timebase members with the same size than _PyTime_t. * py_get_monotonic_clock() now saturates GetTickCount64() to _PyTime_MAX: GetTickCount64() is unsigned, whereas _PyTime_t is signed.
* bpo-26579: Add object.__getstate__(). (GH-2821)Serhiy Storchaka2022-04-061-26/+4
| | | | | | | Copying and pickling instances of subclasses of builtin types bytearray, set, frozenset, collections.OrderedDict, collections.deque, weakref.WeakSet, and datetime.tzinfo now copies and pickles instance attributes implemented as slots.
* bpo-46541: Replace core use of _Py_IDENTIFIER() with statically initialized ↵Eric Snow2022-02-081-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | global objects. (gh-30928) We're no longer using _Py_IDENTIFIER() (or _Py_static_string()) in any core CPython code. It is still used in a number of non-builtin stdlib modules. The replacement is: PyUnicodeObject (not pointer) fields under _PyRuntimeState, statically initialized as part of _PyRuntime. A new _Py_GET_GLOBAL_IDENTIFIER() macro facilitates lookup of the fields (along with _Py_GET_GLOBAL_STRING() for non-identifier strings). https://bugs.python.org/issue46541#msg411799 explains the rationale for this change. The core of the change is in: * (new) Include/internal/pycore_global_strings.h - the declarations for the global strings, along with the macros * Include/internal/pycore_runtime_init.h - added the static initializers for the global strings * Include/internal/pycore_global_objects.h - where the struct in pycore_global_strings.h is hooked into _PyRuntimeState * Tools/scripts/generate_global_objects.py - added generation of the global string declarations and static initializers I've also added a --check flag to generate_global_objects.py (along with make check-global-objects) to check for unused global strings. That check is added to the PR CI config. The remainder of this change updates the core code to use _Py_GET_GLOBAL_IDENTIFIER() instead of _Py_IDENTIFIER() and the related _Py*Id functions (likewise for _Py_GET_GLOBAL_STRING() instead of _Py_static_string()). This includes adding a few functions where there wasn't already an alternative to _Py*Id(), replacing the _Py_Identifier * parameter with PyObject *. The following are not changed (yet): * stop using _Py_IDENTIFIER() in the stdlib modules * (maybe) get rid of _Py_IDENTIFIER(), etc. entirely -- this may not be doable as at least one package on PyPI using this (private) API * (maybe) intern the strings during runtime init https://bugs.python.org/issue46541
* bpo-45855: Replaced deprecated `PyImport_ImportModuleNoBlock` with ↵Kumar Aditya2021-12-121-4/+4
| | | | PyImport_ImportModule (GH-30046)
* bpo-43974: Move Py_BUILD_CORE_MODULE into module code (GH-29157)Christian Heimes2021-10-221-0/+4
| | | | | | | | | | | | | | setup.py no longer defines Py_BUILD_CORE_MODULE. Instead every module defines the macro before #include "Python.h" unless Py_BUILD_CORE_BUILTIN is already defined. Py_BUILD_CORE_BUILTIN is defined for every module that is built by Modules/Setup. The PR also simplifies Modules/Setup. Makefile and makesetup already define Py_BUILD_CORE_BUILTIN and include Modules/internal for us. Signed-off-by: Christian Heimes <christian@python.org>
* [codemod] Fix non-matching bracket pairs (GH-28473)Mohamad Mansour2021-09-211-1/+1
| | | | | Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Łukasz Langa <lukasz@langa.pl>
* bpo-41798: Allocate the _datetime.datetime_CAPI on the heap memory (GH-24096)Hai Shi2021-01-061-21/+43
|
* bpo-1635741: Enhance _datetime error handling (GH-23139)Mohamed Koubaa2020-11-201-117/+103
|
* bpo-42161: Modules/ uses _PyLong_GetZero() and _PyLong_GetOne() (GH-22998)Victor Stinner2020-10-271-2/+3
| | | | | | Use _PyLong_GetZero() and _PyLong_GetOne() in Modules/ directory. _cursesmodule.c and zoneinfo.c are now built with Py_BUILD_CORE_MODULE macro defined.
* bpo-41867: List options for timespec in docstrings of isoformat methods ↵Ram Rachum2020-10-031-4/+8
| | | | (GH-22418)
* bpo-30155: Add macros to get tzinfo from datetime instances (GH-21633)Zackery Spytz2020-09-231-8/+3
| | | | Add PyDateTime_DATE_GET_TZINFO() and PyDateTime_TIME_GET_TZINFO() macros.
* bpo-40989: PyObject_INIT() becomes an alias to PyObject_Init() (GH-20901)Victor Stinner2020-06-151-18/+13
| | | | | | | | | | | | | | The PyObject_INIT() and PyObject_INIT_VAR() macros become aliases to, respectively, PyObject_Init() and PyObject_InitVar() functions. Rename _PyObject_INIT() and _PyObject_INIT_VAR() static inline functions to, respectively, _PyObject_Init() and _PyObject_InitVar(), and move them to pycore_object.h. Remove their return value: their return type becomes void. The _datetime module is now built with the Py_BUILD_CORE_MODULE macro defined. Remove an outdated comment on _Py_tracemalloc_config.
* bpo-40777: Initialize PyDateTime_IsoCalendarDateType.tp_base at run-time ↵Petr Viktorin2020-05-281-3/+10
| | | | | | | (GH-20493) Recent changes to _datetimemodule broke compilation on mingw; see the comments in this change for details. FWIW, @corona10: this issue is why `PyType_FromModuleAndSpec` & friends take the `bases` argument at run time.
* bpo-24416: Return named tuple from date.isocalendar() (GH-20113)Paul Ganssle2020-05-161-4/+148
| | | | | | | | | | | | | | | | | | | | | | | {date, datetime}.isocalendar() now return a private custom named tuple object IsoCalendarDate rather than a simple tuple. In order to leave IsocalendarDate as a private class and to improve what backwards compatibility is offered for pickling the result of a datetime.isocalendar() call, add a __reduce__ method to the named tuples that reduces them to plain tuples. (This is the part of this PR most likely to cause problems — if it causes major issues, switching to a strucseq or equivalent would be prudent). The pure python implementation of IsoCalendarDate uses positional-only arguments, since it is private and only constructed by position anyway; the equivalent change in the argument clinic on the C side would require us to move the forward declaration of the type above the clinic import for whatever reason, so it seems preferable to hold off on that for now. bpo-24416: https://bugs.python.org/issue24416 Original PR by Dong-hee Na with only minor alterations by Paul Ganssle. Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
* bpo-40268: Remove unused structmember.h includes (GH-19530)Victor Stinner2020-04-151-1/+1
| | | | | | If only offsetof() is needed: include stddef.h instead. When structmember.h is used, add a comment explaining that PyMemberDef is used.
* bpo-40024: Update C extension modules to use PyModule_AddType() (GH-19119)Dong-hee Na2020-03-241-31/+14
| | | | | | Update _asyncio, _bz2, _csv, _curses, _datetime, _io, _operator, _pickle, _queue, blake2, multibytecodec and overlapped C extension modules to use PyModule_AddType().
* closes bpo-39736: const strings in Modules/_datetimemodule.c and ↵Andy Lester2020-02-241-3/+3
| | | | Modules/_testbuffer.c (GH-18637)
* bpo-39573: Clean up modules and headers to use Py_IS_TYPE() function (GH-18521)Dong-hee Na2020-02-171-5/+5
|
* bpo-39573: Use Py_TYPE() macro in Modules directory (GH-18393)Victor Stinner2020-02-071-1/+1
| | | Replace direct access to PyObject.ob_type with Py_TYPE().
* bpo-39237, datetime: Remove redundant call to round from delta_new (GH-17877)Alex Henrie2020-01-081-1/+0
|
* bpo-37994: Fix silencing all errors if an attribute lookup fails. (GH-15630)Serhiy Storchaka2019-09-011-11/+10
| | | Only AttributeError should be silenced.
* bpo-37372: Fix error unpickling datetime.time objects from Python 2 with ↵Justin Blanchard2019-08-291-1/+1
| | | | seconds>=24. (GH-14307)
* bpo-37915: Fix comparison between tzinfo objects and timezone objects (GH-15390)Pablo Galindo2019-08-221-1/+2
| | | | | | | https://bugs.python.org/issue37915 Automerge-Triggered-By: @pablogsal
* bpo-37642: Update acceptable offsets in timezone (GH-14878)Ngalim Siregar2019-08-091-2/+9
| | | | | | | | | This fixes an inconsistency between the Python and C implementations of the datetime module. The pure python version of the code was not accepting offsets greater than 23:59 but less than 24:00. This is an accidental legacy of the original implementation, which was put in place before tzinfo allowed sub-minute time zone offsets. GH-14878
* bpo-37685: Fixed comparisons of datetime.timedelta and datetime.timezone. ↵Serhiy Storchaka2019-08-041-5/+2
| | | | | | | | | (GH-14996) There was a discrepancy between the Python and C implementations. Add singletons ALWAYS_EQ, LARGEST and SMALLEST in test.support to test mixed type comparison.
* bpo-37547: add _PyObject_CallMethodOneArg (GH-14685)Jeroen Demeyer2019-07-111-9/+6
|
* bpo-37337: Add _PyObject_CallMethodNoArgs() (GH-14267)Jeroen Demeyer2019-07-081-5/+5
|
* bpo-37194: Add a new public PyObject_CallNoArgs() function (GH-13890)Victor Stinner2019-06-171-5/+5
| | | | | | | | | | | | | | | Add a new public PyObject_CallNoArgs() function to the C API: call a callable Python object without any arguments. It is the most efficient way to call a callback without any argument. On x86-64, for example, PyObject_CallFunctionObjArgs(func, NULL) allocates 960 bytes on the stack per call, whereas PyObject_CallNoArgs(func) only allocates 624 bytes per call. It is excluded from stable ABI 3.8. Replace private _PyObject_CallNoArg() with public PyObject_CallNoArgs() in C extensions: _asyncio, _datetime, _elementtree, _pickle, _tkinter and readline.
* bpo-36974: tp_print -> tp_vectorcall_offset and tp_reserved -> tp_as_async ↵Jeroen Demeyer2019-05-311-14/+13
| | | | | | | | | (GH-13464) Automatically replace tp_print -> tp_vectorcall_offset tp_compare -> tp_as_async tp_reserved -> tp_as_async
* bpo-36004: Add date.fromisocalendar (GH-11888)Paul Ganssle2019-04-291-0/+67
| | | | This commit implements the first version of date.fromisocalendar, the inverse function for date.isocalendar.
* bpo-36025: Fix PyDate_FromTimestamp API (GH-11922)Paul Ganssle2019-04-271-1/+18
| | | | | | | | | | | | | In the process of converting the date.fromtimestamp function to use argument clinic in GH-8535, the C API for PyDate_FromTimestamp was inadvertently changed to expect a timestamp object rather than an argument tuple. This PR fixes this backwards-incompatible change by adding a new wrapper function for the C API function that unwraps the argument tuple and passes it to the underlying function. This PR also adds tests for both PyDate_FromTimestamp and PyDateTime_FromTimestamp to prevent any further regressions.
* bpo-32417: Make timedelta arithmetic respect subclasses (#10902)Paul Ganssle2019-02-041-4/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | * Make timedelta return subclass types Previously timedelta would always return the `date` and `datetime` types, regardless of what it is added to. This makes it return an object of the type it was added to. * Add tests for timedelta arithmetic on subclasses * Make pure python timedelta return subclass types * Add test for fromtimestamp with tz argument * Add tests for subclass behavior in now * Add news entry. Fixes: bpo-32417 bpo-35364 * More descriptive variable names in tests Addresses Victor's comments
* bpo-35066: _dateime.datetime.strftime copies trailing '%' (GH-10692)MichaelSaah2019-01-141-5/+8
| | | | | | | Previously, calling the strftime() method on a datetime object with a trailing '%' in the format string would result in an exception. However, this only occured when the datetime C module was being used; the python implementation did not match this behavior. Datetime is now PEP-399 compliant, and will not throw an exception on a trailing '%'.
* bpo-22005: Fix condition for unpickling a date object. (GH-11025)Serhiy Storchaka2018-12-071-1/+1
|
* bpo-22005: Fixed unpickling instances of datetime classes pickled by Python ↵Serhiy Storchaka2018-12-071-91/+174
| | | | | | 2. (GH-11017) encoding='latin1' should be used for successful decoding.
* bpo-33012: Fix invalid function cast warnings with gcc 8. (GH-6749)Serhiy Storchaka2018-11-271-10/+10
| | | | | | Fix invalid function cast warnings with gcc 8 for method conventions different from METH_NOARGS, METH_O and METH_VARARGS excluding Argument Clinic generated code.
* bpo-35059: Cleanup usage of Python macros (GH-10648)Victor Stinner2018-11-221-79/+87
| | | | | | | | | | | | | Don't pass complex expressions but regular variables to Python macros. * _datetimemodule.c: split single large "if" into two "if" in date_new(), time_new() and datetime_new(). * _pickle.c, load_extension(): flatten complex "if" expression into more regular C code. * _ssl.c: addbool() now uses a temporary bool_obj to only evaluate the value once. * weakrefobject.c: replace "Py_INCREF(result = proxy);" with "result = proxy; Py_INCREF(result);"
* bpo-35021: Fix assertion failures in _datetimemodule.c. (GH-10039)Serhiy Storchaka2018-11-201-48/+51
| | | | | | | | Fixes assertion failures in _datetimemodule.c introduced in the previous fix (see bpo-31752). Rather of trying to handle an int subclass as exact int, let it to use overridden special methods, but check the result of divmod().
* bpo-35081: Remove Py_BUILD_CORE from datetime.h (GH-10416)Paul Ganssle2018-11-131-8/+21
| | | | | | | | | | | | | | Datetime macros like PyDate_Check() have two implementations, one using the C API capsule and one using direct access to the datetime type symbols defined in _datetimemodule.c. Since the direct access versions of the macros are only used in _datetimemodule.c, they have been moved out of "datetime.h" and into _datetimemodule.c. The _PY_DATETIME_IMPL macro is currently necessary in order to avoid both duplicate definitions of these macros in _datetimemodule.c and unnecessary declarations of C API capsule-related macros and varibles in datetime.h. Co-Authored-By: Victor Stinner <vstinner@redhat.com>
* bpo-35133: Fix mistakes when concatenate string literals on different lines. ↵Serhiy Storchaka2018-11-051-1/+1
| | | | | | | | | | (GH-10284) Two kind of mistakes: 1. Missed space. After concatenating there is no space between words. 2. Missed comma. Causes unintentional concatenating in a list of strings.
* bpo-34454: Clean up datetime.fromisoformat surrogate handling (GH-8959)Paul Ganssle2018-10-221-72/+93
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Use _PyUnicode_Copy in sanitize_isoformat_str * Use repr in fromisoformat error message This reverses commit 67b74a98b2 per Serhiy Storchaka's suggestion: I suggested to use %R in the error message because including the raw string can be confusing in the case of empty string, or string containing trailing whitespaces, invisible or unprintable characters. We agree that it is better to change both the C and pure Python versions to use repr. * Retain non-sanitized dtstr for error printing This does not create an extra string, it just holds on to a reference to the original input string for purposes of creating the error message. * PEP 7 fixes to from_isoformat * Separate handling of Unicode and other errors In the initial implementation, errors other than encoding errors would both raise an error indicating an invalid format, which would not be true for errors like MemoryError. * Drop needs_decref from _sanitize_isoformat_str Instead _sanitize_isoformat_str returns a new reference, even to the original string.
* Elaborate datetime.timedelta docstring (GH-7458)Chris Barker2018-10-191-1/+5
|
* Migrate datetime.date.fromtimestamp to Argument Clinic (GH-8535)Tim Hoffmann2018-09-241-17/+20
|