Skip to content

Commit 1ab7f52

Browse files
Merge branch 'python:main' into clean_suggestion
2 parents 59c1cc1 + 953b49e commit 1ab7f52

File tree

78 files changed

+1499
-727
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1499
-727
lines changed

.github/workflows/reusable-docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ jobs:
9999
# Run "doctest" on HEAD as new syntax doesn't exist in the latest stable release
100100
doctest:
101101
name: 'Doctest'
102-
runs-on: ubuntu-22.04
102+
runs-on: ubuntu-24.04
103103
timeout-minutes: 60
104104
steps:
105105
- uses: actions/checkout@v4

Doc/c-api/arg.rst

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,24 @@ There are three ways strings and buffers can be converted to C:
229229
Numbers
230230
-------
231231

232+
These formats allow representing Python numbers or single characters as C numbers.
233+
Formats that require :class:`int`, :class:`float` or :class:`complex` can
234+
also use the corresponding special methods :meth:`~object.__index__`,
235+
:meth:`~object.__float__` or :meth:`~object.__complex__` to convert
236+
the Python object to the required type.
237+
238+
For signed integer formats, :exc:`OverflowError` is raised if the value
239+
is out of range for the C type.
240+
For unsigned integer formats, no range checking is done --- the
241+
most significant bits are silently truncated when the receiving field is too
242+
small to receive the value.
243+
232244
``b`` (:class:`int`) [unsigned char]
233-
Convert a nonnegative Python integer to an unsigned tiny int, stored in a C
245+
Convert a nonnegative Python integer to an unsigned tiny integer, stored in a C
234246
:c:expr:`unsigned char`.
235247

236248
``B`` (:class:`int`) [unsigned char]
237-
Convert a Python integer to a tiny int without overflow checking, stored in a C
249+
Convert a Python integer to a tiny integer without overflow checking, stored in a C
238250
:c:expr:`unsigned char`.
239251

240252
``h`` (:class:`int`) [short int]
@@ -307,7 +319,7 @@ Other objects
307319

308320
.. _o_ampersand:
309321

310-
``O&`` (object) [*converter*, *anything*]
322+
``O&`` (object) [*converter*, *address*]
311323
Convert a Python object to a C variable through a *converter* function. This
312324
takes two arguments: the first is a function, the second is the address of a C
313325
variable (of arbitrary type), converted to :c:expr:`void *`. The *converter*
@@ -321,14 +333,20 @@ Other objects
321333
the conversion has failed. When the conversion fails, the *converter* function
322334
should raise an exception and leave the content of *address* unmodified.
323335

324-
If the *converter* returns ``Py_CLEANUP_SUPPORTED``, it may get called a
336+
.. c:macro:: Py_CLEANUP_SUPPORTED
337+
:no-typesetting:
338+
339+
If the *converter* returns :c:macro:`!Py_CLEANUP_SUPPORTED`, it may get called a
325340
second time if the argument parsing eventually fails, giving the converter a
326341
chance to release any memory that it had already allocated. In this second
327342
call, the *object* parameter will be ``NULL``; *address* will have the same value
328343
as in the original call.
329344

345+
Examples of converters: :c:func:`PyUnicode_FSConverter` and
346+
:c:func:`PyUnicode_FSDecoder`.
347+
330348
.. versionchanged:: 3.1
331-
``Py_CLEANUP_SUPPORTED`` was added.
349+
:c:macro:`!Py_CLEANUP_SUPPORTED` was added.
332350

333351
``p`` (:class:`bool`) [int]
334352
Tests the value passed in for truth (a boolean **p**\ redicate) and converts
@@ -344,12 +362,6 @@ Other objects
344362
in *items*. The C arguments must correspond to the individual format units in
345363
*items*. Format units for sequences may be nested.
346364

347-
It is possible to pass "long" integers (integers whose value exceeds the
348-
platform's :c:macro:`LONG_MAX`) however no proper range checking is done --- the
349-
most significant bits are silently truncated when the receiving field is too
350-
small to receive the value (actually, the semantics are inherited from downcasts
351-
in C --- your mileage may vary).
352-
353365
A few other characters have a meaning in a format string. These may not occur
354366
inside nested parentheses. They are:
355367

Doc/c-api/sys.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,38 @@ Operating System Utilities
216216
The function now uses the UTF-8 encoding on Windows if
217217
:c:member:`PyPreConfig.legacy_windows_fs_encoding` is zero.
218218
219+
.. c:function:: FILE* Py_fopen(PyObject *path, const char *mode)
220+
221+
Similar to :c:func:`!fopen`, but *path* is a Python object and
222+
an exception is set on error.
223+
224+
*path* must be a :class:`str` object, a :class:`bytes` object,
225+
or a :term:`path-like object`.
226+
227+
On success, return the new file pointer.
228+
On error, set an exception and return ``NULL``.
229+
230+
The file must be closed by :c:func:`Py_fclose` rather than calling directly
231+
:c:func:`!fclose`.
232+
233+
The file descriptor is created non-inheritable (:pep:`446`).
234+
235+
The caller must hold the GIL.
236+
237+
.. versionadded:: next
238+
239+
240+
.. c:function:: int Py_fclose(FILE *file)
241+
242+
Close a file that was opened by :c:func:`Py_fopen`.
243+
244+
On success, return ``0``.
245+
On error, return ``EOF`` and ``errno`` is set to indicate the error.
246+
In either case, any further access (including another call to
247+
:c:func:`Py_fclose`) to the stream results in undefined behavior.
248+
249+
.. versionadded:: next
250+
219251
220252
.. _systemfunctions:
221253

Doc/c-api/unicode.rst

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -786,33 +786,52 @@ Functions encoding to and decoding from the :term:`filesystem encoding and
786786
error handler` (:pep:`383` and :pep:`529`).
787787
788788
To encode file names to :class:`bytes` during argument parsing, the ``"O&"``
789-
converter should be used, passing :c:func:`PyUnicode_FSConverter` as the
789+
converter should be used, passing :c:func:`!PyUnicode_FSConverter` as the
790790
conversion function:
791791
792792
.. c:function:: int PyUnicode_FSConverter(PyObject* obj, void* result)
793793
794-
ParseTuple converter: encode :class:`str` objects -- obtained directly or
794+
:ref:`PyArg_Parse\* converter <arg-parsing>`: encode :class:`str` objects -- obtained directly or
795795
through the :class:`os.PathLike` interface -- to :class:`bytes` using
796796
:c:func:`PyUnicode_EncodeFSDefault`; :class:`bytes` objects are output as-is.
797-
*result* must be a :c:expr:`PyBytesObject*` which must be released when it is
798-
no longer used.
797+
*result* must be an address of a C variable of type :c:expr:`PyObject*`
798+
(or :c:expr:`PyBytesObject*`).
799+
On success, set the variable to a new :term:`strong reference` to
800+
a :ref:`bytes object <bytesobjects>` which must be released
801+
when it is no longer used and return a non-zero value
802+
(:c:macro:`Py_CLEANUP_SUPPORTED`).
803+
Embedded null bytes are not allowed in the result.
804+
On failure, return ``0`` with an exception set.
805+
806+
If *obj* is ``NULL``, the function releases a strong reference
807+
stored in the variable referred by *result* and returns ``1``.
799808
800809
.. versionadded:: 3.1
801810
802811
.. versionchanged:: 3.6
803812
Accepts a :term:`path-like object`.
804813
805814
To decode file names to :class:`str` during argument parsing, the ``"O&"``
806-
converter should be used, passing :c:func:`PyUnicode_FSDecoder` as the
815+
converter should be used, passing :c:func:`!PyUnicode_FSDecoder` as the
807816
conversion function:
808817
809818
.. c:function:: int PyUnicode_FSDecoder(PyObject* obj, void* result)
810819
811-
ParseTuple converter: decode :class:`bytes` objects -- obtained either
820+
:ref:`PyArg_Parse\* converter <arg-parsing>`: decode :class:`bytes` objects -- obtained either
812821
directly or indirectly through the :class:`os.PathLike` interface -- to
813822
:class:`str` using :c:func:`PyUnicode_DecodeFSDefaultAndSize`; :class:`str`
814-
objects are output as-is. *result* must be a :c:expr:`PyUnicodeObject*` which
815-
must be released when it is no longer used.
823+
objects are output as-is.
824+
*result* must be an address of a C variable of type :c:expr:`PyObject*`
825+
(or :c:expr:`PyUnicodeObject*`).
826+
On success, set the variable to a new :term:`strong reference` to
827+
a :ref:`Unicode object <unicodeobjects>` which must be released
828+
when it is no longer used and return a non-zero value
829+
(:c:macro:`Py_CLEANUP_SUPPORTED`).
830+
Embedded null characters are not allowed in the result.
831+
On failure, return ``0`` with an exception set.
832+
833+
If *obj* is ``NULL``, release the strong reference
834+
to the object referred to by *result* and return ``1``.
816835
817836
.. versionadded:: 3.2
818837

Doc/library/calendar.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is
4040

4141
:class:`Calendar` instances have the following methods:
4242

43+
.. method:: getfirstweekday()
44+
45+
Return an :class:`int` for the current first weekday (0-6).
46+
47+
.. method:: setfirstweekday(firstweekday)
48+
49+
Set the first weekday to *firstweekday*, passed as an :class:`int` where Monday is 0 and Sunday is 6.
50+
4351
.. method:: iterweekdays()
4452

4553
Return an iterator for the week day numbers that will be used for one

Doc/library/email.policy.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ added matters. To illustrate::
267267

268268
Handle a *defect* found on *obj*. When the email package calls this
269269
method, *defect* will always be a subclass of
270-
:class:`~email.errors.Defect`.
270+
:class:`~email.errors.MessageDefect`.
271271

272272
The default implementation checks the :attr:`raise_on_defect` flag. If
273273
it is ``True``, *defect* is raised as an exception. If it is ``False``
@@ -277,7 +277,7 @@ added matters. To illustrate::
277277
.. method:: register_defect(obj, defect)
278278

279279
Register a *defect* on *obj*. In the email package, *defect* will always
280-
be a subclass of :class:`~email.errors.Defect`.
280+
be a subclass of :class:`~email.errors.MessageDefect`.
281281

282282
The default implementation calls the ``append`` method of the ``defects``
283283
attribute of *obj*. When the email package calls :attr:`handle_defect`,

Doc/tools/.nitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ Doc/library/email.charset.rst
2323
Doc/library/email.compat32-message.rst
2424
Doc/library/email.errors.rst
2525
Doc/library/email.parser.rst
26-
Doc/library/email.policy.rst
2726
Doc/library/exceptions.rst
2827
Doc/library/functools.rst
2928
Doc/library/http.cookiejar.rst

Doc/whatsnew/3.14.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,12 @@ New features
12371237
:monitoring-event:`BRANCH_LEFT` and :monitoring-event:`BRANCH_RIGHT`
12381238
events, respectively.
12391239

1240+
* Add :c:func:`Py_fopen` function to open a file. Similar to the
1241+
:c:func:`!fopen` function, but the *path* parameter is a Python object and an
1242+
exception is set on error. Add also :c:func:`Py_fclose` function to close a
1243+
file.
1244+
(Contributed by Victor Stinner in :gh:`127350`.)
1245+
12401246

12411247
Porting to Python 3.14
12421248
----------------------

Include/cpython/fileutils.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
# error "this header file must not be included directly"
33
#endif
44

5-
// Used by _testcapi which must not use the internal C API
6-
PyAPI_FUNC(FILE*) _Py_fopen_obj(
5+
PyAPI_FUNC(FILE*) Py_fopen(
76
PyObject *path,
87
const char *mode);
8+
9+
// Deprecated alias to Py_fopen() kept for backward compatibility
10+
Py_DEPRECATED(3.14) PyAPI_FUNC(FILE*) _Py_fopen_obj(
11+
PyObject *path,
12+
const char *mode);
13+
14+
PyAPI_FUNC(int) Py_fclose(FILE *file);

Include/cpython/unicodeobject.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ typedef struct {
109109
3: Interned, Immortal, and Static
110110
This categorization allows the runtime to determine the right
111111
cleanup mechanism at runtime shutdown. */
112-
unsigned int interned:2;
112+
uint16_t interned;
113113
/* Character size:
114114
115115
- PyUnicode_1BYTE_KIND (1):
@@ -132,21 +132,23 @@ typedef struct {
132132
* all characters are in the range U+0000-U+10FFFF
133133
* at least one character is in the range U+10000-U+10FFFF
134134
*/
135-
unsigned int kind:3;
135+
unsigned short kind:3;
136136
/* Compact is with respect to the allocation scheme. Compact unicode
137137
objects only require one memory block while non-compact objects use
138138
one block for the PyUnicodeObject struct and another for its data
139139
buffer. */
140-
unsigned int compact:1;
140+
unsigned short compact:1;
141141
/* The string only contains characters in the range U+0000-U+007F (ASCII)
142142
and the kind is PyUnicode_1BYTE_KIND. If ascii is set and compact is
143143
set, use the PyASCIIObject structure. */
144-
unsigned int ascii:1;
144+
unsigned short ascii:1;
145145
/* The object is statically allocated. */
146-
unsigned int statically_allocated:1;
146+
unsigned short statically_allocated:1;
147147
/* Padding to ensure that PyUnicode_DATA() is always aligned to
148-
4 bytes (see issue #19537 on m68k). */
149-
unsigned int :24;
148+
4 bytes (see issue #19537 on m68k) and we use unsigned short to avoid
149+
the extra four bytes on 32-bit Windows. This is restricted features
150+
for specific compilers including GCC, MSVC, Clang and IBM's XL compiler. */
151+
unsigned short :10;
150152
} state;
151153
} PyASCIIObject;
152154

@@ -195,7 +197,11 @@ typedef struct {
195197

196198
/* Use only if you know it's a string */
197199
static inline unsigned int PyUnicode_CHECK_INTERNED(PyObject *op) {
200+
#ifdef Py_GIL_DISABLED
201+
return _Py_atomic_load_uint16_relaxed(&_PyASCIIObject_CAST(op)->state.interned);
202+
#else
198203
return _PyASCIIObject_CAST(op)->state.interned;
204+
#endif
199205
}
200206
#define PyUnicode_CHECK_INTERNED(op) PyUnicode_CHECK_INTERNED(_PyObject_CAST(op))
201207

0 commit comments

Comments
 (0)