Skip to content

Commit e248b38

Browse files
authored
Merge branch 'main' into fix-issue-106634
2 parents 1cc3d67 + be1b968 commit e248b38

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

+1567
-1350
lines changed

Doc/c-api/mapping.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,36 @@ See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and
3333
See also :c:func:`PyObject_GetItem`.
3434
3535
36+
.. c:function:: int PyMapping_GetOptionalItem(PyObject *obj, PyObject *key, PyObject **result)
37+
38+
Variant of :c:func:`PyObject_GetItem` which doesn't raise
39+
:exc:`KeyError` if the key is not found.
40+
41+
If the key is found, return ``1`` and set *\*result* to a new
42+
:term:`strong reference` to the corresponding value.
43+
If the key is not found, return ``0`` and set *\*result* to ``NULL``;
44+
the :exc:`KeyError` is silenced.
45+
If an error other than :exc:`KeyError` is raised, return ``-1`` and
46+
set *\*result* to ``NULL``.
47+
48+
.. versionadded:: 3.13
49+
50+
51+
.. c:function:: int PyMapping_GetOptionalItemString(PyObject *obj, const char *key, PyObject **result)
52+
53+
Variant of :c:func:`PyMapping_GetItemString` which doesn't raise
54+
:exc:`KeyError` if the key is not found.
55+
56+
If the key is found, return ``1`` and set *\*result* to a new
57+
:term:`strong reference` to the corresponding value.
58+
If the key is not found, return ``0`` and set *\*result* to ``NULL``;
59+
the :exc:`KeyError` is silenced.
60+
If an error other than :exc:`KeyError` is raised, return ``-1`` and
61+
set *\*result* to ``NULL``.
62+
63+
.. versionadded:: 3.13
64+
65+
3666
.. c:function:: int PyMapping_SetItemString(PyObject *o, const char *key, PyObject *v)
3767
3868
Map the string *key* to the value *v* in object *o*. Returns ``-1`` on

Doc/c-api/object.rst

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ Object Protocol
3737
3838
Exceptions that occur when this calls :meth:`~object.__getattr__` and
3939
:meth:`~object.__getattribute__` methods are silently ignored.
40-
For proper error handling, use :c:func:`PyObject_GetAttr` instead.
40+
For proper error handling, use :c:func:`PyObject_GetOptionalAttr` or
41+
:c:func:`PyObject_GetAttr` instead.
4142
4243
4344
.. c:function:: int PyObject_HasAttrString(PyObject *o, const char *attr_name)
@@ -51,7 +52,8 @@ Object Protocol
5152
Exceptions that occur when this calls :meth:`~object.__getattr__` and
5253
:meth:`~object.__getattribute__` methods or while creating the temporary :class:`str`
5354
object are silently ignored.
54-
For proper error handling, use :c:func:`PyObject_GetAttrString` instead.
55+
For proper error handling, use :c:func:`PyObject_GetOptionalAttrString`
56+
or :c:func:`PyObject_GetAttrString` instead.
5557
5658
5759
.. c:function:: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name)
@@ -60,13 +62,48 @@ Object Protocol
6062
value on success, or ``NULL`` on failure. This is the equivalent of the Python
6163
expression ``o.attr_name``.
6264
65+
If the missing attribute should not be treated as a failure, you can use
66+
:c:func:`PyObject_GetOptionalAttr` instead.
67+
6368
6469
.. c:function:: PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name)
6570
6671
Retrieve an attribute named *attr_name* from object *o*. Returns the attribute
6772
value on success, or ``NULL`` on failure. This is the equivalent of the Python
6873
expression ``o.attr_name``.
6974
75+
If the missing attribute should not be treated as a failure, you can use
76+
:c:func:`PyObject_GetOptionalAttrString` instead.
77+
78+
79+
.. c:function:: int PyObject_GetOptionalAttr(PyObject *obj, PyObject *attr_name, PyObject **result);
80+
81+
Variant of :c:func:`PyObject_GetAttr` which doesn't raise
82+
:exc:`AttributeError` if the attribute is not found.
83+
84+
If the attribute is found, return ``1`` and set *\*result* to a new
85+
:term:`strong reference` to the attribute.
86+
If the attribute is not found, return ``0`` and set *\*result* to ``NULL``;
87+
the :exc:`AttributeError` is silenced.
88+
If an error other than :exc:`AttributeError` is raised, return ``-1`` and
89+
set *\*result* to ``NULL``.
90+
91+
.. versionadded:: 3.13
92+
93+
94+
.. c:function:: int PyObject_GetOptionalAttrString(PyObject *obj, const char *attr_name, PyObject **result);
95+
96+
Variant of :c:func:`PyObject_GetAttrString` which doesn't raise
97+
:exc:`AttributeError` if the attribute is not found.
98+
99+
If the attribute is found, return ``1`` and set *\*result* to a new
100+
:term:`strong reference` to the attribute.
101+
If the attribute is not found, return ``0`` and set *\*result* to ``NULL``;
102+
the :exc:`AttributeError` is silenced.
103+
If an error other than :exc:`AttributeError` is raised, return ``-1`` and
104+
set *\*result* to ``NULL``.
105+
106+
.. versionadded:: 3.13
70107
71108
.. c:function:: PyObject* PyObject_GenericGetAttr(PyObject *o, PyObject *name)
72109

Doc/data/stable_abi.dat

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

Doc/library/sqlite3.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,9 @@ Module functions
266266

267267
:param database:
268268
The path to the database file to be opened.
269-
Pass ``":memory:"`` to open a connection to a database that is
270-
in RAM instead of on disk.
269+
You can pass ``":memory:"`` to create an `SQLite database existing only
270+
in memory <https://sqlite.org/inmemorydb.html>`_, and open a connection
271+
to it.
271272
:type database: :term:`path-like object`
272273

273274
:param float timeout:
@@ -2519,6 +2520,13 @@ Queries now return :class:`!Row` objects:
25192520
>>> row["RADIUS"] # Column names are case-insensitive.
25202521
6378
25212522

2523+
.. note::
2524+
2525+
The ``FROM`` clause can be omitted in the ``SELECT`` statement, as in the
2526+
above example. In such cases, SQLite returns a single row with columns
2527+
defined by expressions, e.g. literals, with the given aliases
2528+
``expr AS alias``.
2529+
25222530
You can create a custom :attr:`~Cursor.row_factory`
25232531
that returns each row as a :class:`dict`, with column names mapped to values:
25242532

Doc/tools/templates/layout.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
{% endblock %}
2727

2828
{% block extrahead %}
29+
<script defer data-domain="docs.python.org" src="https://plausible.io/js/script.js"></script>
2930
<link rel="canonical" href="https://docs.python.org/3/{{pagename}}.html" />
3031
{% if builder != "htmlhelp" %}
3132
{% if pagename == 'whatsnew/changelog' and not embedded %}

Doc/tutorial/controlflow.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,9 @@ you can use the class name followed by an argument list resembling a
307307
constructor, but with the ability to capture attributes into variables::
308308

309309
class Point:
310-
x: int
311-
y: int
310+
def __init__(self, x, y):
311+
self.x = x
312+
self.y = y
312313

313314
def where_is(point):
314315
match point:

Doc/whatsnew/3.13.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,22 @@ New Features
734734
``NULL`` if the referent is no longer live.
735735
(Contributed by Victor Stinner in :gh:`105927`.)
736736

737+
* Add :c:func:`PyObject_GetOptionalAttr` and
738+
:c:func:`PyObject_GetOptionalAttrString`, variants of
739+
:c:func:`PyObject_GetAttr` and :c:func:`PyObject_GetAttrString` which
740+
don't raise :exc:`AttributeError` if the attribute is not found.
741+
These variants are more convenient and faster if the missing attribute
742+
should not be treated as a failure.
743+
(Contributed by Serhiy Storchaka in :gh:`106521`.)
744+
745+
* Add :c:func:`PyMapping_GetOptionalItem` and
746+
:c:func:`PyMapping_GetOptionalItemString`: variants of
747+
:c:func:`PyObject_GetItem` and :c:func:`PyMapping_GetItemString` which don't
748+
raise :exc:`KeyError` if the key is not found.
749+
These variants are more convenient and faster if the missing key should not
750+
be treated as a failure.
751+
(Contributed by Serhiy Storchaka in :gh:`106307`.)
752+
737753
* If Python is built in :ref:`debug mode <debug-build>` or :option:`with
738754
assertions <--with-assertions>`, :c:func:`PyTuple_SET_ITEM` and
739755
:c:func:`PyList_SET_ITEM` now check the index argument with an assertion.

Include/abstract.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,38 @@ extern "C" {
6060
This is the equivalent of the Python expression: o.attr_name. */
6161

6262

63+
/* Implemented elsewhere:
64+
65+
int PyObject_GetOptionalAttr(PyObject *obj, PyObject *attr_name, PyObject **result);
66+
67+
Variant of PyObject_GetAttr() which doesn't raise AttributeError
68+
if the attribute is not found.
69+
70+
If the attribute is found, return 1 and set *result to a new strong
71+
reference to the attribute.
72+
If the attribute is not found, return 0 and set *result to NULL;
73+
the AttributeError is silenced.
74+
If an error other than AttributeError is raised, return -1 and
75+
set *result to NULL.
76+
*/
77+
78+
79+
/* Implemented elsewhere:
80+
81+
int PyObject_GetOptionalAttrString(PyObject *obj, const char *attr_name, PyObject **result);
82+
83+
Variant of PyObject_GetAttrString() which doesn't raise AttributeError
84+
if the attribute is not found.
85+
86+
If the attribute is found, return 1 and set *result to a new strong
87+
reference to the attribute.
88+
If the attribute is not found, return 0 and set *result to NULL;
89+
the AttributeError is silenced.
90+
If an error other than AttributeError is raised, return -1 and
91+
set *result to NULL.
92+
*/
93+
94+
6395
/* Implemented elsewhere:
6496
6597
int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v);
@@ -808,6 +840,21 @@ PyAPI_FUNC(PyObject *) PyMapping_Items(PyObject *o);
808840
PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o,
809841
const char *key);
810842

843+
/* Variants of PyObject_GetItem() and PyMapping_GetItemString() which don't
844+
raise KeyError if the key is not found.
845+
846+
If the key is found, return 1 and set *result to a new strong
847+
reference to the corresponding value.
848+
If the key is not found, return 0 and set *result to NULL;
849+
the KeyError is silenced.
850+
If an error other than KeyError is raised, return -1 and
851+
set *result to NULL.
852+
*/
853+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
854+
PyAPI_FUNC(int) PyMapping_GetOptionalItem(PyObject *, PyObject *, PyObject **);
855+
PyAPI_FUNC(int) PyMapping_GetOptionalItemString(PyObject *, const char *, PyObject **);
856+
#endif
857+
811858
/* Map the string 'key' to the value 'v' in the mapping 'o'.
812859
Returns -1 on failure.
813860

Include/cpython/object.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,6 @@ PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *);
294294
PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *);
295295
PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, _Py_Identifier *);
296296
PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, _Py_Identifier *, PyObject *);
297-
/* Replacements of PyObject_GetAttr() and _PyObject_GetAttrId() which
298-
don't raise AttributeError.
299-
300-
Return 1 and set *result != NULL if an attribute is found.
301-
Return 0 and set *result == NULL if an attribute is not found;
302-
an AttributeError is silenced.
303-
Return -1 and set *result == NULL if an error other than AttributeError
304-
is raised.
305-
*/
306-
PyAPI_FUNC(int) _PyObject_LookupAttr(PyObject *, PyObject *, PyObject **);
307-
PyAPI_FUNC(int) _PyObject_LookupAttrId(PyObject *, _Py_Identifier *, PyObject **);
308297

309298
PyAPI_FUNC(int) _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method);
310299

Include/internal/pycore_runtime.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,101 @@ typedef struct _Py_AuditHookEntry {
5353
void *userData;
5454
} _Py_AuditHookEntry;
5555

56+
typedef struct _Py_DebugOffsets {
57+
char cookie[8];
58+
uint64_t version;
59+
// Runtime state offset;
60+
struct _runtime_state {
61+
off_t finalizing;
62+
off_t interpreters_head;
63+
} runtime_state;
64+
65+
// Interpreter state offset;
66+
struct _interpreter_state {
67+
off_t next;
68+
off_t threads_head;
69+
off_t gc;
70+
off_t imports_modules;
71+
off_t sysdict;
72+
off_t builtins;
73+
off_t ceval_gil;
74+
off_t gil_runtime_state_locked;
75+
off_t gil_runtime_state_holder;
76+
} interpreter_state;
77+
78+
// Thread state offset;
79+
struct _thread_state{
80+
off_t prev;
81+
off_t next;
82+
off_t interp;
83+
off_t cframe;
84+
off_t thread_id;
85+
off_t native_thread_id;
86+
} thread_state;
87+
88+
// InterpreterFrame offset;
89+
struct _interpreter_frame {
90+
off_t previous;
91+
off_t executable;
92+
off_t prev_instr;
93+
off_t localsplus;
94+
off_t owner;
95+
} interpreter_frame;
96+
97+
// CFrame offset;
98+
struct _cframe {
99+
off_t current_frame;
100+
off_t previous;
101+
} cframe;
102+
103+
// Code object offset;
104+
struct _code_object {
105+
off_t filename;
106+
off_t name;
107+
off_t linetable;
108+
off_t firstlineno;
109+
off_t argcount;
110+
off_t localsplusnames;
111+
off_t localspluskinds;
112+
off_t co_code_adaptive;
113+
} code_object;
114+
115+
// PyObject offset;
116+
struct _pyobject {
117+
off_t ob_type;
118+
} pyobject;
119+
120+
// PyTypeObject object offset;
121+
struct _type_object {
122+
off_t tp_name;
123+
} type_object;
124+
125+
// PyTuple object offset;
126+
struct _tuple_object {
127+
off_t ob_item;
128+
} tuple_object;
129+
} _Py_DebugOffsets;
130+
56131
/* Full Python runtime state */
57132

58133
/* _PyRuntimeState holds the global state for the CPython runtime.
59134
That data is exposed in the internal API as a static variable (_PyRuntime).
60135
*/
61136
typedef struct pyruntimestate {
137+
/* This field must be first to facilitate locating it by out of process
138+
* debuggers. Out of process debuggers will use the offsets contained in this
139+
* field to be able to locate other fields in several interpreter structures
140+
* in a way that doesn't require them to know the exact layout of those
141+
* structures.
142+
*
143+
* IMPORTANT:
144+
* This struct is **NOT** backwards compatible between minor version of the
145+
* interpreter and the members, order of members and size can change between
146+
* minor versions. This struct is only guaranteed to be stable between patch
147+
* versions for a given minor version of the interpreter.
148+
*/
149+
_Py_DebugOffsets debug_offsets;
150+
62151
/* Has been initialized to a safe state.
63152
64153
In order to be effective, this must be set to 0 during or right

0 commit comments

Comments
 (0)