Skip to content

Commit

Permalink
Stop accessing None as a dictionary
Browse files Browse the repository at this point in the history
PyMapping_HasKeyString does not work on None
Accidentally, it used to return false result on silent errors.

Since Python 3.13, this is what happens instead:

    >>> import pylibmc
    >>> m = pylibmc.Client(["10.0.0.1"], binary=True)
    Exception ignored in PyMapping_HasKeyString(); consider using PyMapping_HasKeyStringWithError(), PyMapping_GetOptionalItemString() or PyMapping_GetItemString():
    Traceback (most recent call last):
      File "/usr/lib64/python3.13/site-packages/pylibmc/client.py", line 142, in __init__
        super().__init__(servers=translate_server_specs(servers),
    TypeError: 'NoneType' object is not subscriptable
    ...
    Exception ignored in PyMapping_HasKeyString(); consider using PyMapping_HasKeyStringWithError(), PyMapping_GetOptionalItemString() or PyMapping_GetItemString():
    Traceback (most recent call last):
      File "/usr/lib64/python3.13/site-packages/pylibmc/client.py", line 142, in __init__
        super().__init__(servers=translate_server_specs(servers),
    TypeError: 'NoneType' object is not subscriptable

When this is run via pytest, it leads to:

    pytest.PytestUnraisableExceptionWarning: Exception ignored in PyMapping_HasKeyString(); consider using PyMapping_HasKeyStringWithError(), PyMapping_GetOptionalItemString() or PyMapping_GetItemString(): None

Fixes pallets-eco/cachelib#400
  • Loading branch information
hroncok committed Jul 3, 2024
1 parent 1183e48 commit d18ad29
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/_pylibmcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2082,7 +2082,7 @@ static PyObject *PylibMC_Client_set_behaviors(PylibMC_Client *self,
char *key;

for (b = PylibMC_behaviors; b->name != NULL; b++) {
if (!PyMapping_HasKeyString(behaviors, b->name)) {
if (behaviors == Py_None || !PyMapping_HasKeyString(behaviors, b->name)) {
continue;
} else if ((py_v = PyMapping_GetItemString(behaviors, b->name)) == NULL) {
goto error;
Expand Down Expand Up @@ -2112,7 +2112,7 @@ static PyObject *PylibMC_Client_set_behaviors(PylibMC_Client *self,
}

for (b = PylibMC_callbacks; b->name != NULL; b++) {
if (!PyMapping_HasKeyString(behaviors, b->name)) {
if (behaviors == Py_None || !PyMapping_HasKeyString(behaviors, b->name)) {
continue;
} else if ((py_v = PyMapping_GetItemString(behaviors, b->name)) == NULL) {
goto error;
Expand Down

0 comments on commit d18ad29

Please sign in to comment.