Skip to content

Commit 71a3790

Browse files
committed
Fix misunderstanding about PyDict_GetItemWithError() in containers_and_refcounts.rst.
1 parent a5e6feb commit 71a3790

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

doc/sphinx/source/containers_and_refcounts.rst

+4-27
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,6 @@ This section describes other dictionary APIs that are simple to describe and hav
15341534

15351535
.. index::
15361536
single: Dictionary; PyDict_GetItemWithError()
1537-
pair: Documentation Lacunae; PyDict_GetItemWithError()
15381537

15391538
.. _chapter_containers_and_refcounts.dictionaries.pydict_getitemwitherror:
15401539

@@ -1550,34 +1549,12 @@ The C signature is:
15501549
15511550
PyObject *PyDict_GetItemWithError(PyObject *p, PyObject *key);
15521551
1553-
.. warning::
1554-
1555-
This function is incorrectly documented as it fails to set an exception with a missing key as this code
1556-
demonstrates:
1557-
1558-
.. code-block:: c
1552+
Currently, the only failure mode is if the first argument is not a dictionary.
15591553

1560-
assert(!PyErr_Occurred());
1561-
PyObject *container = PyDict_New();
1562-
assert(container && Py_REFCNT(container) == 1);
1563-
1564-
PyObject *key = new_unique_string(__FUNCTION__, NULL);
1565-
1566-
assert(!PyErr_Occurred());
1567-
PyObject *get_item = PyDict_GetItemWithError(container, key);
1568-
/* This is the failure point. An exception should have been set with
1569-
* an absent key but it isn't. */
1570-
assert(!PyErr_Occurred());
1571-
1572-
assert(get_item == NULL);
1573-
1574-
Py_DECREF(container);
1575-
Py_DECREF(key);
1576-
1577-
For code and tests see:
1554+
For code and tests see:
15781555

1579-
* C, in ``src/cpy/Containers/DebugContainers.c``:
1580-
* ``dbg_PyDict_GetItemWithError_fails()``
1556+
* C, in ``src/cpy/Containers/DebugContainers.c``:
1557+
* ``dbg_PyDict_GetItemWithError_fails()``
15811558

15821559
.. index::
15831560
single: Dictionary; PyDict_DelItem()

src/cpy/Containers/DebugContainers.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -2220,9 +2220,26 @@ void dbg_PyDict_GetItemWithError_fails(void) {
22202220
assert(!PyErr_Occurred());
22212221
get_item = PyDict_GetItemWithError(container, key);
22222222
assert(get_item == NULL);
2223-
/* This is the failure point. An exception should have been set with an absent key but it isn't. */
2223+
/* This is correct, the key is absent. */
22242224
assert(!PyErr_Occurred());
22252225

2226+
/* So what error conditinos are handled?
2227+
* Firstly this will segfault. */
2228+
#if 0
2229+
assert(!PyErr_Occurred());
2230+
get_item = PyDict_GetItemWithError(container, NULL);
2231+
assert(get_item == NULL);
2232+
assert(PyErr_Occurred());
2233+
#endif
2234+
2235+
PyObject *new_container = PyList_New(0);
2236+
assert(!PyErr_Occurred());
2237+
get_item = PyDict_GetItemWithError(new_container, key);
2238+
assert(get_item == NULL);
2239+
assert(PyErr_Occurred());
2240+
PyErr_Print(); /* Clears exception. */
2241+
Py_DECREF(new_container);
2242+
22262243
Py_DECREF(container);
22272244
ref_count = Py_REFCNT(key);
22282245
assert(ref_count == 1);

0 commit comments

Comments
 (0)