This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients python-dev, serhiy.storchaka, vstinner
Date 2013-11-06.22:43:12
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1383777792.71.0.0610895554804.issue19512@psf.upfronthosting.co.za>
In-reply-to
Content
Another problem is that PyUnicode_FromString() failure is not handled correctly in some cases. PyUnicode_FromString() can fail because an decoder error, but also because of a MemoryError. For example, PyDict_GetItemString() returns NULL as if the entry does not exist if PyUnicode_FromString() failed :-(
---
PyObject *
PyDict_GetItemString(PyObject *v, const char *key)
{
    PyObject *kv, *rv;
    kv = PyUnicode_FromString(key);
    if (kv == NULL) {
        PyErr_Clear();
        return NULL;
    }
    rv = PyDict_GetItem(v, kv);
    Py_DECREF(kv);
    return rv;
}
---

While working on failmalloc issues (#18048, #19437), I found some places where MemoryError caused tricky bugs because of this. Example of such issue:
---
changeset:   84684:af18829a7754
user:        Victor Stinner <victor.stinner@gmail.com>
date:        Wed Jul 17 01:22:45 2013 +0200
files:       Objects/structseq.c Python/pythonrun.c
description:
Close #18469: Replace PyDict_GetItemString() with _PyDict_GetItemId() in structseq.c

_PyDict_GetItemId() is more efficient: it only builds the Unicode string once.
Identifiers (dictionary keys) are now created at Python initialization, and if
the creation failed, Python does exit with a fatal error.

Before, PyDict_GetItemString() failure was not handled: structseq_new() could
call PyObject_GC_NewVar() with a negative size, and structseq_dealloc() could
also crash.
---

So moving from PyDict_GetItemString() to _PyDict_GetItemId() is for perfomances, but the main motivation is to handle better errors. I hope that the identifier will be initialized quickly at startup, and if its initialization failed, the failure is handled better...

There is also a _PyDict_GetItemIdWithError() function. But it is not used currently (it was in changeset 2dd046be2c88).
History
Date User Action Args
2013-11-06 22:43:12vstinnersetrecipients: + vstinner, python-dev, serhiy.storchaka
2013-11-06 22:43:12vstinnersetmessageid: <1383777792.71.0.0610895554804.issue19512@psf.upfronthosting.co.za>
2013-11-06 22:43:12vstinnerlinkissue19512 messages
2013-11-06 22:43:12vstinnercreate