Skip to content

Commit e8b16a9

Browse files
committed
Inline a copy of _PyUnicode_Copy
Silences compiler errors in Python 3.13
1 parent 0024e55 commit e8b16a9

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ However, starting in version 2, `backports.datetime_fromisoformat` will apply it
2424

2525
## Unreleased
2626

27-
* Nil.
27+
* Silenced errors when compiling against Python 3.13
28+
* Note: `backports.datetime_fromisoformat` does nothing when used against Python 3.11+. Use `backports-datetime-fromisoformat; python_version < '3.11'` in your dependency list to avoid even downloading it in modern Pythons
2829

2930
## Version 2.0.2
3031

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Quick Start
3333

3434
.. code:: bash
3535
36-
pip install backports-datetime-fromisoformat
36+
pip install "backports-datetime-fromisoformat; python_version < '3.11'"
3737
3838
**Usage:**
3939

backports/datetime_fromisoformat/_datetimemodule.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
* 037e9125527d4a55af566f161c96a61b3c3fd998)
44
* It was then refreshed using the Python 3.11 contents present at
55
* 27d8dc2c9d3de886a884f79f0621d4586c0e0f7a
6+
* Finally, the `_PyUnicode_Copy` implementation was copied from
7+
* c8749b578324ad4089c8d014d9136bc42b065343
68
*
79
* Since then, I have:
810
* - torn out all the functionality that doesn't matter to
911
* `backports.datetime_fromisoformat`
1012
* - switched calls to datetime creation to use the versions found in
1113
* `PyDateTimeAPI`
1214
* - made minor changes to make it compilable for older versions of Python.
15+
* - Including in-lining a copy of _PyUnicode_Copy
1316
*
1417
* Below is a copy of the Python 3.11 code license
1518
* (from https://docs.python.org/3/license.html):
@@ -749,6 +752,29 @@ time_fromisoformat(PyObject *tstr)
749752
return NULL;
750753
}
751754

755+
PyObject *
756+
_PyUnicode_Copy(PyObject *unicode)
757+
{
758+
Py_ssize_t length;
759+
PyObject *copy;
760+
761+
if (!PyUnicode_Check(unicode)) {
762+
PyErr_BadInternalCall();
763+
return NULL;
764+
}
765+
766+
length = PyUnicode_GET_LENGTH(unicode);
767+
copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode));
768+
if (!copy)
769+
return NULL;
770+
assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode));
771+
772+
memcpy(PyUnicode_DATA(copy), PyUnicode_DATA(unicode),
773+
length * PyUnicode_KIND(unicode));
774+
assert(_PyUnicode_CheckConsistency(copy, 1));
775+
return copy;
776+
}
777+
752778
static PyObject *
753779
_sanitize_isoformat_str(PyObject *dtstr)
754780
{

0 commit comments

Comments
 (0)