Skip to content

Commit 64111f4

Browse files
committed
Merge remote-tracking branch 'cpython/main' into pythongh-99726
2 parents 7835bde + 85c128e commit 64111f4

37 files changed

+1194
-363
lines changed

Doc/c-api/refcounting.rst

+2-44
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
Reference Counting
88
******************
99

10-
The functions and macros in this section are used for managing reference counts
11-
of Python objects.
10+
The macros in this section are used for managing reference counts of Python
11+
objects.
1212

1313

1414
.. c:function:: Py_ssize_t Py_REFCNT(PyObject *o)
@@ -129,11 +129,6 @@ of Python objects.
129129
It is a good idea to use this macro whenever decrementing the reference
130130
count of an object that might be traversed during garbage collection.
131131
132-
.. versionchanged:: 3.12
133-
The macro argument is now only evaluated once. If the argument has side
134-
effects, these are no longer duplicated.
135-
136-
137132
.. c:function:: void Py_IncRef(PyObject *o)
138133
139134
Increment the reference count for object *o*. A function version of :c:func:`Py_XINCREF`.
@@ -144,40 +139,3 @@ of Python objects.
144139
145140
Decrement the reference count for object *o*. A function version of :c:func:`Py_XDECREF`.
146141
It can be used for runtime dynamic embedding of Python.
147-
148-
149-
.. c:macro:: Py_SETREF(dst, src)
150-
151-
Macro safely decrementing the `dst` reference count and setting `dst` to
152-
`src`.
153-
154-
As in case of :c:func:`Py_CLEAR`, "the obvious" code can be deadly::
155-
156-
Py_DECREF(dst);
157-
dst = src;
158-
159-
The safe way is::
160-
161-
Py_SETREF(dst, src);
162-
163-
That arranges to set `dst` to `src` _before_ decrementing reference count of
164-
*dst* old value, so that any code triggered as a side-effect of `dst`
165-
getting torn down no longer believes `dst` points to a valid object.
166-
167-
.. versionadded:: 3.6
168-
169-
.. versionchanged:: 3.12
170-
The macro arguments are now only evaluated once. If an argument has side
171-
effects, these are no longer duplicated.
172-
173-
174-
.. c:macro:: Py_XSETREF(dst, src)
175-
176-
Variant of :c:macro:`Py_SETREF` macro that uses :c:func:`Py_XDECREF` instead
177-
of :c:func:`Py_DECREF`.
178-
179-
.. versionadded:: 3.6
180-
181-
.. versionchanged:: 3.12
182-
The macro arguments are now only evaluated once. If an argument has side
183-
effects, these are no longer duplicated.

Doc/library/sqlite3.rst

+119-41
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ inserted data and retrieved values from it in multiple ways.
239239
* :ref:`sqlite3-adapters`
240240
* :ref:`sqlite3-converters`
241241
* :ref:`sqlite3-connection-context-manager`
242+
* :ref:`sqlite3-howto-row-factory`
242243

243244
* :ref:`sqlite3-explanation` for in-depth background on transaction control.
244245

@@ -1316,31 +1317,14 @@ Connection objects
13161317

13171318
.. attribute:: row_factory
13181319

1319-
A callable that accepts two arguments,
1320-
a :class:`Cursor` object and the raw row results as a :class:`tuple`,
1321-
and returns a custom object representing an SQLite row.
1322-
1323-
Example:
1320+
The initial :attr:`~Cursor.row_factory`
1321+
for :class:`Cursor` objects created from this connection.
1322+
Assigning to this attribute does not affect the :attr:`!row_factory`
1323+
of existing cursors belonging to this connection, only new ones.
1324+
Is ``None`` by default,
1325+
meaning each row is returned as a :class:`tuple`.
13241326

1325-
.. doctest::
1326-
1327-
>>> def dict_factory(cursor, row):
1328-
... col_names = [col[0] for col in cursor.description]
1329-
... return {key: value for key, value in zip(col_names, row)}
1330-
>>> con = sqlite3.connect(":memory:")
1331-
>>> con.row_factory = dict_factory
1332-
>>> for row in con.execute("SELECT 1 AS a, 2 AS b"):
1333-
... print(row)
1334-
{'a': 1, 'b': 2}
1335-
1336-
If returning a tuple doesn't suffice and you want name-based access to
1337-
columns, you should consider setting :attr:`row_factory` to the
1338-
highly optimized :class:`sqlite3.Row` type. :class:`Row` provides both
1339-
index-based and case-insensitive name-based access to columns with almost no
1340-
memory overhead. It will probably be better than your own custom
1341-
dictionary-based approach or even a db_row based solution.
1342-
1343-
.. XXX what's a db_row-based solution?
1327+
See :ref:`sqlite3-howto-row-factory` for more details.
13441328

13451329
.. attribute:: text_factory
13461330

@@ -1497,7 +1481,7 @@ Cursor objects
14971481

14981482
.. method:: fetchone()
14991483

1500-
If :attr:`~Connection.row_factory` is ``None``,
1484+
If :attr:`~Cursor.row_factory` is ``None``,
15011485
return the next row query result set as a :class:`tuple`.
15021486
Else, pass it to the row factory and return its result.
15031487
Return ``None`` if no more data is available.
@@ -1591,6 +1575,22 @@ Cursor objects
15911575
including :abbr:`CTE (Common Table Expression)` queries.
15921576
It is only updated by the :meth:`execute` and :meth:`executemany` methods.
15931577

1578+
.. attribute:: row_factory
1579+
1580+
Control how a row fetched from this :class:`!Cursor` is represented.
1581+
If ``None``, a row is represented as a :class:`tuple`.
1582+
Can be set to the included :class:`sqlite3.Row`;
1583+
or a :term:`callable` that accepts two arguments,
1584+
a :class:`Cursor` object and the :class:`!tuple` of row values,
1585+
and returns a custom object representing an SQLite row.
1586+
1587+
Defaults to what :attr:`Connection.row_factory` was set to
1588+
when the :class:`!Cursor` was created.
1589+
Assigning to this attribute does not affect
1590+
:attr:`Connection.row_factory` of the parent connection.
1591+
1592+
See :ref:`sqlite3-howto-row-factory` for more details.
1593+
15941594

15951595
.. The sqlite3.Row example used to be a how-to. It has now been incorporated
15961596
into the Row reference. We keep the anchor here in order not to break
@@ -1609,7 +1609,10 @@ Row objects
16091609
It supports iteration, equality testing, :func:`len`,
16101610
and :term:`mapping` access by column name and index.
16111611

1612-
Two row objects compare equal if have equal columns and equal members.
1612+
Two :class:`!Row` objects compare equal
1613+
if they have identical column names and values.
1614+
1615+
See :ref:`sqlite3-howto-row-factory` for more details.
16131616

16141617
.. method:: keys
16151618

@@ -1620,21 +1623,6 @@ Row objects
16201623
.. versionchanged:: 3.5
16211624
Added support of slicing.
16221625

1623-
Example:
1624-
1625-
.. doctest::
1626-
1627-
>>> con = sqlite3.connect(":memory:")
1628-
>>> con.row_factory = sqlite3.Row
1629-
>>> res = con.execute("SELECT 'Earth' AS name, 6378 AS radius")
1630-
>>> row = res.fetchone()
1631-
>>> row.keys()
1632-
['name', 'radius']
1633-
>>> row[0], row["name"] # Access by index and name.
1634-
('Earth', 'Earth')
1635-
>>> row["RADIUS"] # Column names are case-insensitive.
1636-
6378
1637-
16381626

16391627
.. _sqlite3-blob-objects:
16401628

@@ -2358,6 +2346,96 @@ can be found in the `SQLite URI documentation`_.
23582346
.. _SQLite URI documentation: https://www.sqlite.org/uri.html
23592347

23602348

2349+
.. _sqlite3-howto-row-factory:
2350+
2351+
How to create and use row factories
2352+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2353+
2354+
By default, :mod:`!sqlite3` represents each row as a :class:`tuple`.
2355+
If a :class:`!tuple` does not suit your needs,
2356+
you can use the :class:`sqlite3.Row` class
2357+
or a custom :attr:`~Cursor.row_factory`.
2358+
2359+
While :attr:`!row_factory` exists as an attribute both on the
2360+
:class:`Cursor` and the :class:`Connection`,
2361+
it is recommended to set :class:`Connection.row_factory`,
2362+
so all cursors created from the connection will use the same row factory.
2363+
2364+
:class:`!Row` provides indexed and case-insensitive named access to columns,
2365+
with minimal memory overhead and performance impact over a :class:`!tuple`.
2366+
To use :class:`!Row` as a row factory,
2367+
assign it to the :attr:`!row_factory` attribute:
2368+
2369+
.. doctest::
2370+
2371+
>>> con = sqlite3.connect(":memory:")
2372+
>>> con.row_factory = sqlite3.Row
2373+
2374+
Queries now return :class:`!Row` objects:
2375+
2376+
.. doctest::
2377+
2378+
>>> res = con.execute("SELECT 'Earth' AS name, 6378 AS radius")
2379+
>>> row = res.fetchone()
2380+
>>> row.keys()
2381+
['name', 'radius']
2382+
>>> row[0] # Access by index.
2383+
'Earth'
2384+
>>> row["name"] # Access by name.
2385+
'Earth'
2386+
>>> row["RADIUS"] # Column names are case-insensitive.
2387+
6378
2388+
2389+
You can create a custom :attr:`~Cursor.row_factory`
2390+
that returns each row as a :class:`dict`, with column names mapped to values:
2391+
2392+
.. testcode::
2393+
2394+
def dict_factory(cursor, row):
2395+
fields = [column[0] for column in cursor.description]
2396+
return {key: value for key, value in zip(fields, row)}
2397+
2398+
Using it, queries now return a :class:`!dict` instead of a :class:`!tuple`:
2399+
2400+
.. doctest::
2401+
2402+
>>> con = sqlite3.connect(":memory:")
2403+
>>> con.row_factory = dict_factory
2404+
>>> for row in con.execute("SELECT 1 AS a, 2 AS b"):
2405+
... print(row)
2406+
{'a': 1, 'b': 2}
2407+
2408+
The following row factory returns a :term:`named tuple`:
2409+
2410+
.. testcode::
2411+
2412+
from collections import namedtuple
2413+
2414+
def namedtuple_factory(cursor, row):
2415+
fields = [column[0] for column in cursor.description]
2416+
cls = namedtuple("Row", fields)
2417+
return cls._make(row)
2418+
2419+
:func:`!namedtuple_factory` can be used as follows:
2420+
2421+
.. doctest::
2422+
2423+
>>> con = sqlite3.connect(":memory:")
2424+
>>> con.row_factory = namedtuple_factory
2425+
>>> cur = con.execute("SELECT 1 AS a, 2 AS b")
2426+
>>> row = cur.fetchone()
2427+
>>> row
2428+
Row(a=1, b=2)
2429+
>>> row[0] # Indexed access.
2430+
1
2431+
>>> row.b # Attribute access.
2432+
2
2433+
2434+
With some adjustments, the above recipe can be adapted to use a
2435+
:class:`~dataclasses.dataclass`, or any other custom class,
2436+
instead of a :class:`~collections.namedtuple`.
2437+
2438+
23612439
.. _sqlite3-explanation:
23622440

23632441
Explanation

Doc/license.rst

+4
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ Project, https://www.wide.ad.jp/. ::
383383
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
384384
SUCH DAMAGE.
385385

386+
386387
Asynchronous socket services
387388
----------------------------
388389

@@ -988,9 +989,12 @@ https://www.w3.org/TR/xml-c14n2-testcases/ and is distributed under the
988989
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
989990
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
990991

992+
991993
Audioop
992994
-------
995+
993996
The audioop module uses the code base in g771.c file of the SoX project::
997+
994998
Programming the AdLib/Sound Blaster
995999
FM Music Chips
9961000
Version 2.0 (24 Feb 1992)

Doc/whatsnew/3.12.rst

-5
Original file line numberDiff line numberDiff line change
@@ -822,11 +822,6 @@ Porting to Python 3.12
822822
:class:`bytes` type is accepted for bytes strings.
823823
(Contributed by Victor Stinner in :gh:`98393`.)
824824

825-
* The :c:macro:`Py_CLEAR`, :c:macro:`Py_SETREF` and :c:macro:`Py_XSETREF`
826-
macros now only evaluate their argument once. If the argument has side
827-
effects, these side effects are no longer duplicated.
828-
(Contributed by Victor Stinner in :gh:`98724`.)
829-
830825
Deprecated
831826
----------
832827

Include/cpython/object.h

+20-24
Original file line numberDiff line numberDiff line change
@@ -305,41 +305,37 @@ _PyObject_GenericSetAttrWithDict(PyObject *, PyObject *,
305305

306306
PyAPI_FUNC(PyObject *) _PyObject_FunctionStr(PyObject *);
307307

308-
/* Safely decref `dst` and set `dst` to `src`.
308+
/* Safely decref `op` and set `op` to `op2`.
309309
*
310310
* As in case of Py_CLEAR "the obvious" code can be deadly:
311311
*
312-
* Py_DECREF(dst);
313-
* dst = src;
312+
* Py_DECREF(op);
313+
* op = op2;
314314
*
315315
* The safe way is:
316316
*
317-
* Py_SETREF(dst, src);
317+
* Py_SETREF(op, op2);
318318
*
319-
* That arranges to set `dst` to `src` _before_ decref'ing, so that any code
320-
* triggered as a side-effect of `dst` getting torn down no longer believes
321-
* `dst` points to a valid object.
319+
* That arranges to set `op` to `op2` _before_ decref'ing, so that any code
320+
* triggered as a side-effect of `op` getting torn down no longer believes
321+
* `op` points to a valid object.
322322
*
323-
* gh-98724: Use the _tmp_dst_ptr variable to evaluate the 'dst' macro argument
324-
* exactly once, to prevent the duplication of side effects in this macro.
323+
* Py_XSETREF is a variant of Py_SETREF that uses Py_XDECREF instead of
324+
* Py_DECREF.
325325
*/
326-
#define Py_SETREF(dst, src) \
327-
do { \
328-
PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
329-
PyObject *_tmp_dst = (*_tmp_dst_ptr); \
330-
*_tmp_dst_ptr = _PyObject_CAST(src); \
331-
Py_DECREF(_tmp_dst); \
326+
327+
#define Py_SETREF(op, op2) \
328+
do { \
329+
PyObject *_py_tmp = _PyObject_CAST(op); \
330+
(op) = (op2); \
331+
Py_DECREF(_py_tmp); \
332332
} while (0)
333333

334-
/* Py_XSETREF() is a variant of Py_SETREF() that uses Py_XDECREF() instead of
335-
* Py_DECREF().
336-
*/
337-
#define Py_XSETREF(dst, src) \
338-
do { \
339-
PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
340-
PyObject *_tmp_dst = (*_tmp_dst_ptr); \
341-
*_tmp_dst_ptr = _PyObject_CAST(src); \
342-
Py_XDECREF(_tmp_dst); \
334+
#define Py_XSETREF(op, op2) \
335+
do { \
336+
PyObject *_py_tmp = _PyObject_CAST(op); \
337+
(op) = (op2); \
338+
Py_XDECREF(_py_tmp); \
343339
} while (0)
344340

345341

Include/internal/pycore_global_objects_fini_generated.h

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)