Skip to content

Commit d4c31c1

Browse files
authored
Merge branch 'python:main' into posix_spawn-cwd-support
2 parents 12553e1 + d372472 commit d4c31c1

Some content is hidden

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

49 files changed

+1330
-600
lines changed

Doc/c-api/type.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,6 @@ The following functions and structs are used to create
311311
312312
Metaclasses that override :c:member:`~PyTypeObject.tp_new` are not
313313
supported, except if ``tp_new`` is ``NULL``.
314-
(For backwards compatibility, other ``PyType_From*`` functions allow
315-
such metaclasses. They ignore ``tp_new``, which may result in incomplete
316-
initialization. This is deprecated and in Python 3.14+ such metaclasses will
317-
not be supported.)
318314
319315
The *bases* argument can be used to specify base classes; it can either
320316
be only one class or a tuple of classes.

Doc/library/ctypes.rst

Lines changed: 98 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -657,12 +657,13 @@ Nested structures can also be initialized in the constructor in several ways::
657657
>>> r = RECT((1, 2), (3, 4))
658658

659659
Field :term:`descriptor`\s can be retrieved from the *class*, they are useful
660-
for debugging because they can provide useful information::
660+
for debugging because they can provide useful information.
661+
See :class:`CField`::
661662

662-
>>> print(POINT.x)
663-
<Field type=c_long, ofs=0, size=4>
664-
>>> print(POINT.y)
665-
<Field type=c_long, ofs=4, size=4>
663+
>>> POINT.x
664+
<ctypes.CField 'x' type=c_int, ofs=0, size=4>
665+
>>> POINT.y
666+
<ctypes.CField 'y' type=c_int, ofs=4, size=4>
666667
>>>
667668

668669

@@ -2812,6 +2813,98 @@ fields, or any other data types containing pointer type fields.
28122813
present in :attr:`_fields_`.
28132814

28142815

2816+
.. class:: CField(*args, **kw)
2817+
2818+
Descriptor for fields of a :class:`Structure` and :class:`Union`.
2819+
For example::
2820+
2821+
>>> class Color(Structure):
2822+
... _fields_ = (
2823+
... ('red', c_uint8),
2824+
... ('green', c_uint8),
2825+
... ('blue', c_uint8),
2826+
... ('intense', c_bool, 1),
2827+
... ('blinking', c_bool, 1),
2828+
... )
2829+
...
2830+
>>> Color.red
2831+
<ctypes.CField 'red' type=c_ubyte, ofs=0, size=1>
2832+
>>> Color.green.type
2833+
<class 'ctypes.c_ubyte'>
2834+
>>> Color.blue.byte_offset
2835+
2
2836+
>>> Color.intense
2837+
<ctypes.CField 'intense' type=c_bool, ofs=3, bit_size=1, bit_offset=0>
2838+
>>> Color.blinking.bit_offset
2839+
1
2840+
2841+
All attributes are read-only.
2842+
2843+
:class:`!CField` objects are created via :attr:`~Structure._fields_`;
2844+
do not instantiate the class directly.
2845+
2846+
.. versionadded:: next
2847+
2848+
Previously, descriptors only had ``offset`` and ``size`` attributes
2849+
and a readable string representation; the :class:`!CField` class was not
2850+
available directly.
2851+
2852+
.. attribute:: name
2853+
2854+
Name of the field, as a string.
2855+
2856+
.. attribute:: type
2857+
2858+
Type of the field, as a :ref:`ctypes class <ctypes-data-types>`.
2859+
2860+
.. attribute:: offset
2861+
byte_offset
2862+
2863+
Offset of the field, in bytes.
2864+
2865+
For bitfields, this is the offset of the underlying byte-aligned
2866+
*storage unit*; see :attr:`~CField.bit_offset`.
2867+
2868+
.. attribute:: byte_size
2869+
2870+
Size of the field, in bytes.
2871+
2872+
For bitfields, this is the size of the underlying *storage unit*.
2873+
Typically, it has the same size as the bitfield's type.
2874+
2875+
.. attribute:: size
2876+
2877+
For non-bitfields, equivalent to :attr:`~CField.byte_size`.
2878+
2879+
For bitfields, this contains a backwards-compatible bit-packed
2880+
value that combines :attr:`~CField.bit_size` and
2881+
:attr:`~CField.bit_offset`.
2882+
Prefer using the explicit attributes instead.
2883+
2884+
.. attribute:: is_bitfield
2885+
2886+
True if this is a bitfield.
2887+
2888+
.. attribute:: bit_offset
2889+
bit_size
2890+
2891+
The location of a bitfield within its *storage unit*, that is, within
2892+
:attr:`~CField.byte_size` bytes of memory starting at
2893+
:attr:`~CField.byte_offset`.
2894+
2895+
To get the field's value, read the storage unit as an integer,
2896+
:ref:`shift left <shifting>` by :attr:`!bit_offset` and
2897+
take the :attr:`!bit_size` least significant bits.
2898+
2899+
For non-bitfields, :attr:`!bit_offset` is zero
2900+
and :attr:`!bit_size` is equal to ``byte_size * 8``.
2901+
2902+
.. attribute:: is_anonymous
2903+
2904+
True if this field is anonymous, that is, it contains nested sub-fields
2905+
that should be be merged into a containing structure or union.
2906+
2907+
28152908
.. _ctypes-arrays-pointers:
28162909

28172910
Arrays and pointers

Doc/library/datetime.rst

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,8 @@ Instance methods:
699699

700700
.. method:: date.replace(year=self.year, month=self.month, day=self.day)
701701

702-
Return a date with the same value, except for those parameters given new
703-
values by whichever keyword arguments are specified.
702+
Return a new :class:`date` object with the same values, but with specified
703+
parameters updated.
704704

705705
Example::
706706

@@ -709,8 +709,8 @@ Instance methods:
709709
>>> d.replace(day=26)
710710
datetime.date(2002, 12, 26)
711711

712-
:class:`date` objects are also supported by generic function
713-
:func:`copy.replace`.
712+
The generic function :func:`copy.replace` also supports :class:`date`
713+
objects.
714714

715715

716716
.. method:: date.timetuple()
@@ -1348,10 +1348,10 @@ Instance methods:
13481348
hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, \
13491349
tzinfo=self.tzinfo, *, fold=0)
13501350
1351-
Return a datetime with the same attributes, except for those attributes given
1352-
new values by whichever keyword arguments are specified. Note that
1353-
``tzinfo=None`` can be specified to create a naive datetime from an aware
1354-
datetime with no conversion of date and time data.
1351+
Return a new :class:`datetime` object with the same attributes, but with
1352+
specified parameters updated. Note that ``tzinfo=None`` can be specified to
1353+
create a naive datetime from an aware datetime with no conversion of date
1354+
and time data.
13551355

13561356
:class:`.datetime` objects are also supported by generic function
13571357
:func:`copy.replace`.
@@ -1942,10 +1942,10 @@ Instance methods:
19421942
.. method:: time.replace(hour=self.hour, minute=self.minute, second=self.second, \
19431943
microsecond=self.microsecond, tzinfo=self.tzinfo, *, fold=0)
19441944
1945-
Return a :class:`.time` with the same value, except for those attributes given
1946-
new values by whichever keyword arguments are specified. Note that
1947-
``tzinfo=None`` can be specified to create a naive :class:`.time` from an
1948-
aware :class:`.time`, without conversion of the time data.
1945+
Return a new :class:`.time` with the same values, but with specified
1946+
parameters updated. Note that ``tzinfo=None`` can be specified to create a
1947+
naive :class:`.time` from an aware :class:`.time`, without conversion of the
1948+
time data.
19491949

19501950
:class:`.time` objects are also supported by generic function
19511951
:func:`copy.replace`.

Doc/whatsnew/3.14.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,11 @@ ctypes
502502
to help match a non-default ABI.
503503
(Contributed by Petr Viktorin in :gh:`97702`.)
504504

505+
* The class of :class:`~ctypes.Structure`/:class:`~ctypes.Union`
506+
field descriptors is now available as :class:`~ctypes.CField`,
507+
and has new attributes to aid debugging and introspection.
508+
(Contributed by Petr Viktorin in :gh:`128715`.)
509+
505510
* On Windows, the :exc:`~ctypes.COMError` exception is now public.
506511
(Contributed by Jun Komoda in :gh:`126686`.)
507512

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ struct _Py_global_strings {
247247
STRUCT_FOR_ID(_get_sourcefile)
248248
STRUCT_FOR_ID(_handle_fromlist)
249249
STRUCT_FOR_ID(_initializing)
250+
STRUCT_FOR_ID(_internal_use)
250251
STRUCT_FOR_ID(_io)
251252
STRUCT_FOR_ID(_is_text_encoding)
252253
STRUCT_FOR_ID(_isatty_open_only)
@@ -297,6 +298,7 @@ struct _Py_global_strings {
297298
STRUCT_FOR_ID(before)
298299
STRUCT_FOR_ID(big)
299300
STRUCT_FOR_ID(binary_form)
301+
STRUCT_FOR_ID(bit_offset)
300302
STRUCT_FOR_ID(bit_size)
301303
STRUCT_FOR_ID(block)
302304
STRUCT_FOR_ID(bound)
@@ -307,6 +309,8 @@ struct _Py_global_strings {
307309
STRUCT_FOR_ID(buffers)
308310
STRUCT_FOR_ID(bufsize)
309311
STRUCT_FOR_ID(builtins)
312+
STRUCT_FOR_ID(byte_offset)
313+
STRUCT_FOR_ID(byte_size)
310314
STRUCT_FOR_ID(byteorder)
311315
STRUCT_FOR_ID(bytes)
312316
STRUCT_FOR_ID(bytes_per_sep)

Include/internal/pycore_interpframe.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,11 @@ _PyFrame_SetStackPointer(_PyInterpreterFrame *frame, _PyStackRef *stack_pointer)
189189
* Frames on the frame stack are incomplete until the
190190
* first RESUME instruction.
191191
* Frames owned by a generator are always complete.
192+
*
193+
* NOTE: We allow racy accesses to the instruction pointer
194+
* from other threads for sys._current_frames() and similar APIs.
192195
*/
193-
static inline bool
196+
static inline bool _Py_NO_SANITIZE_THREAD
194197
_PyFrame_IsIncomplete(_PyInterpreterFrame *frame)
195198
{
196199
if (frame->owner >= FRAME_OWNED_BY_INTERPRETER) {

Include/internal/pycore_runtime_init_generated.h

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/pyport.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,27 +565,45 @@ extern "C" {
565565
# if __has_feature(memory_sanitizer)
566566
# if !defined(_Py_MEMORY_SANITIZER)
567567
# define _Py_MEMORY_SANITIZER
568+
# define _Py_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
568569
# endif
569570
# endif
570571
# if __has_feature(address_sanitizer)
571572
# if !defined(_Py_ADDRESS_SANITIZER)
572573
# define _Py_ADDRESS_SANITIZER
574+
# define _Py_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
573575
# endif
574576
# endif
575577
# if __has_feature(thread_sanitizer)
576578
# if !defined(_Py_THREAD_SANITIZER)
577579
# define _Py_THREAD_SANITIZER
580+
# define _Py_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
578581
# endif
579582
# endif
580583
#elif defined(__GNUC__)
581584
# if defined(__SANITIZE_ADDRESS__)
582585
# define _Py_ADDRESS_SANITIZER
586+
# define _Py_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
583587
# endif
584588
# if defined(__SANITIZE_THREAD__)
585589
# define _Py_THREAD_SANITIZER
590+
# define _Py_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
591+
# elif __GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1)
592+
// TSAN is supported since GCC 5.1, but __SANITIZE_THREAD__ macro
593+
// is provided only since GCC 7.
594+
# define _Py_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
586595
# endif
587596
#endif
588597

598+
#ifndef _Py_NO_SANITIZE_ADDRESS
599+
# define _Py_NO_SANITIZE_ADDRESS
600+
#endif
601+
#ifndef _Py_NO_SANITIZE_THREAD
602+
# define _Py_NO_SANITIZE_THREAD
603+
#endif
604+
#ifndef _Py_NO_SANITIZE_MEMORY
605+
# define _Py_NO_SANITIZE_MEMORY
606+
#endif
589607

590608
/* AIX has __bool__ redefined in it's system header file. */
591609
#if defined(_AIX) && defined(__bool__)

0 commit comments

Comments
 (0)