Skip to content

Commit

Permalink
Do not assume that binary addresses are valid unicode
Browse files Browse the repository at this point in the history
On lookup failure we would previously assume that binary addresses are
valid unicode when constructing exception messages. With
python/cpython#105375 which appeared e.g., in python-3.11.9 this starts
to cause failures as invalid unicode is now more consistently rejected; e.g., in
the test `pysubnettree.lookup` we construct a binary address `1.3.3.255`
which corresponds to `b'\x01\x03\x03\xff'` which is not valid unicode.

With this patch we set messages for `KeyError` from a `bytes` object
instead of a `str`, so that user's see e.g.,

    KeyError: b'1:3:3::3'

instead of the previous

    KeyError: '1:3:3::3'

This should still provide all the information necessary while working
with our interface which allows both `str` and `bytes` inputs.

The changes to `SubnetTree_wrap.cc` are generated automatically with
swig-3.0.12.
  • Loading branch information
bbannier committed May 8, 2024
1 parent 726aefa commit d3e0afd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 53 deletions.
86 changes: 34 additions & 52 deletions SubnetTree_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1632,14 +1632,6 @@ SwigPyObject_repr(SwigPyObject *v, PyObject *args)
return repr;
}

/* We need a version taking two PyObject* parameters so it's a valid
* PyCFunction to use in swigobject_methods[]. */
SWIGRUNTIME PyObject *
SwigPyObject_repr2(PyObject *v, PyObject *SWIGUNUSEDPARM(args))
{
return SwigPyObject_repr((SwigPyObject*)v);
}

SWIGRUNTIME int
SwigPyObject_compare(SwigPyObject *v, SwigPyObject *w)
{
Expand Down Expand Up @@ -1769,7 +1761,11 @@ SwigPyObject_append(PyObject* v, PyObject* next)
}

SWIGRUNTIME PyObject*
#ifdef METH_NOARGS
SwigPyObject_next(PyObject* v)
#else
SwigPyObject_next(PyObject* v, PyObject *SWIGUNUSEDPARM(args))
#endif
{
SwigPyObject *sobj = (SwigPyObject *) v;
if (sobj->next) {
Expand Down Expand Up @@ -1804,20 +1800,6 @@ SwigPyObject_acquire(PyObject* v, PyObject *SWIGUNUSEDPARM(args))
return SWIG_Py_Void();
}

#ifdef METH_NOARGS
static PyObject*
SwigPyObject_disown2(PyObject* v, PyObject *SWIGUNUSEDPARM(args))
{
return SwigPyObject_disown(v);
}

static PyObject*
SwigPyObject_acquire2(PyObject* v, PyObject *SWIGUNUSEDPARM(args))
{
return SwigPyObject_acquire(v);
}
#endif

SWIGINTERN PyObject*
SwigPyObject_own(PyObject *v, PyObject *args)
{
Expand Down Expand Up @@ -1858,12 +1840,12 @@ SwigPyObject_own(PyObject *v, PyObject *args)
#ifdef METH_O
static PyMethodDef
swigobject_methods[] = {
{(char *)"disown", (PyCFunction)SwigPyObject_disown2, METH_NOARGS, (char *)"releases ownership of the pointer"},
{(char *)"acquire", (PyCFunction)SwigPyObject_acquire2,METH_NOARGS, (char *)"acquires ownership of the pointer"},
{(char *)"disown", (PyCFunction)SwigPyObject_disown, METH_NOARGS, (char *)"releases ownership of the pointer"},
{(char *)"acquire", (PyCFunction)SwigPyObject_acquire, METH_NOARGS, (char *)"acquires ownership of the pointer"},
{(char *)"own", (PyCFunction)SwigPyObject_own, METH_VARARGS, (char *)"returns/sets ownership of the pointer"},
{(char *)"append", (PyCFunction)SwigPyObject_append, METH_O, (char *)"appends another 'this' object"},
{(char *)"next", (PyCFunction)SwigPyObject_next, METH_NOARGS, (char *)"returns the next 'this' object"},
{(char *)"__repr__",(PyCFunction)SwigPyObject_repr2, METH_NOARGS, (char *)"returns object representation"},
{(char *)"__repr__",(PyCFunction)SwigPyObject_repr, METH_NOARGS, (char *)"returns object representation"},
{0, 0, 0, 0}
};
#else
Expand All @@ -1874,7 +1856,7 @@ swigobject_methods[] = {
{(char *)"own", (PyCFunction)SwigPyObject_own, METH_VARARGS, (char *)"returns/sets ownership of the pointer"},
{(char *)"append", (PyCFunction)SwigPyObject_append, METH_VARARGS, (char *)"appends another 'this' object"},
{(char *)"next", (PyCFunction)SwigPyObject_next, METH_VARARGS, (char *)"returns the next 'this' object"},
{(char *)"__repr__",(PyCFunction)SwigPyObject_repr, METH_VARARGS, (char *)"returns object representation"},
{(char *)"__repr__",(PyCFunction)SwigPyObject_repr, METH_VARARGS, (char *)"returns object representation"},
{0, 0, 0, 0}
};
#endif
Expand Down Expand Up @@ -3543,7 +3525,7 @@ SWIGINTERN PyObject *SubnetTree___getitem__(SubnetTree *self,char *cidr,int size

PyObject* data = self->lookup(cidr, size);
if ( ! data ) {
PyErr_SetString(PyExc_KeyError, cidr);
PyErr_SetObject(PyExc_KeyError, PyBytes_FromStringAndSize(cidr, size));
return 0;
}

Expand Down Expand Up @@ -4934,27 +4916,27 @@ SWIGINTERN PyObject *SubnetTree_swigregister(PyObject *SWIGUNUSEDPARM(self), PyO
}

static PyMethodDef SwigMethods[] = {
{ "SWIG_PyInstanceMethod_New", SWIG_PyInstanceMethod_New, METH_O, NULL},
{ "inx_addr_sin_set", _wrap_inx_addr_sin_set, METH_VARARGS, NULL},
{ "inx_addr_sin_get", _wrap_inx_addr_sin_get, METH_VARARGS, NULL},
{ "inx_addr_sin6_set", _wrap_inx_addr_sin6_set, METH_VARARGS, NULL},
{ "inx_addr_sin6_get", _wrap_inx_addr_sin6_get, METH_VARARGS, NULL},
{ "new_inx_addr", _wrap_new_inx_addr, METH_VARARGS, NULL},
{ "delete_inx_addr", _wrap_delete_inx_addr, METH_VARARGS, NULL},
{ "inx_addr_swigregister", inx_addr_swigregister, METH_VARARGS, NULL},
{ "new_SubnetTree", _wrap_new_SubnetTree, METH_VARARGS, NULL},
{ "delete_SubnetTree", _wrap_delete_SubnetTree, METH_VARARGS, NULL},
{ "SubnetTree_insert", _wrap_SubnetTree_insert, METH_VARARGS, NULL},
{ "SubnetTree_remove", _wrap_SubnetTree_remove, METH_VARARGS, NULL},
{ "SubnetTree_lookup", _wrap_SubnetTree_lookup, METH_VARARGS, NULL},
{ "SubnetTree_prefixes", _wrap_SubnetTree_prefixes, METH_VARARGS, NULL},
{ "SubnetTree_get_binary_lookup_mode", _wrap_SubnetTree_get_binary_lookup_mode, METH_VARARGS, NULL},
{ "SubnetTree_set_binary_lookup_mode", _wrap_SubnetTree_set_binary_lookup_mode, METH_VARARGS, NULL},
{ "SubnetTree___contains__", _wrap_SubnetTree___contains__, METH_VARARGS, NULL},
{ "SubnetTree___getitem__", _wrap_SubnetTree___getitem__, METH_VARARGS, NULL},
{ "SubnetTree___setitem__", _wrap_SubnetTree___setitem__, METH_VARARGS, NULL},
{ "SubnetTree___delitem__", _wrap_SubnetTree___delitem__, METH_VARARGS, NULL},
{ "SubnetTree_swigregister", SubnetTree_swigregister, METH_VARARGS, NULL},
{ (char *)"SWIG_PyInstanceMethod_New", (PyCFunction)SWIG_PyInstanceMethod_New, METH_O, NULL},
{ (char *)"inx_addr_sin_set", _wrap_inx_addr_sin_set, METH_VARARGS, NULL},
{ (char *)"inx_addr_sin_get", _wrap_inx_addr_sin_get, METH_VARARGS, NULL},
{ (char *)"inx_addr_sin6_set", _wrap_inx_addr_sin6_set, METH_VARARGS, NULL},
{ (char *)"inx_addr_sin6_get", _wrap_inx_addr_sin6_get, METH_VARARGS, NULL},
{ (char *)"new_inx_addr", _wrap_new_inx_addr, METH_VARARGS, NULL},
{ (char *)"delete_inx_addr", _wrap_delete_inx_addr, METH_VARARGS, NULL},
{ (char *)"inx_addr_swigregister", inx_addr_swigregister, METH_VARARGS, NULL},
{ (char *)"new_SubnetTree", _wrap_new_SubnetTree, METH_VARARGS, NULL},
{ (char *)"delete_SubnetTree", _wrap_delete_SubnetTree, METH_VARARGS, NULL},
{ (char *)"SubnetTree_insert", _wrap_SubnetTree_insert, METH_VARARGS, NULL},
{ (char *)"SubnetTree_remove", _wrap_SubnetTree_remove, METH_VARARGS, NULL},
{ (char *)"SubnetTree_lookup", _wrap_SubnetTree_lookup, METH_VARARGS, NULL},
{ (char *)"SubnetTree_prefixes", _wrap_SubnetTree_prefixes, METH_VARARGS, NULL},
{ (char *)"SubnetTree_get_binary_lookup_mode", _wrap_SubnetTree_get_binary_lookup_mode, METH_VARARGS, NULL},
{ (char *)"SubnetTree_set_binary_lookup_mode", _wrap_SubnetTree_set_binary_lookup_mode, METH_VARARGS, NULL},
{ (char *)"SubnetTree___contains__", _wrap_SubnetTree___contains__, METH_VARARGS, NULL},
{ (char *)"SubnetTree___getitem__", _wrap_SubnetTree___getitem__, METH_VARARGS, NULL},
{ (char *)"SubnetTree___setitem__", _wrap_SubnetTree___setitem__, METH_VARARGS, NULL},
{ (char *)"SubnetTree___delitem__", _wrap_SubnetTree___delitem__, METH_VARARGS, NULL},
{ (char *)"SubnetTree_swigregister", SubnetTree_swigregister, METH_VARARGS, NULL},
{ NULL, NULL, 0, NULL }
};

Expand Down Expand Up @@ -5527,9 +5509,9 @@ extern "C" {
char *ndoc = (char*)malloc(ldoc + lptr + 10);
if (ndoc) {
char *buff = ndoc;
memcpy(buff, methods[i].ml_doc, ldoc);
strncpy(buff, methods[i].ml_doc, ldoc);
buff += ldoc;
memcpy(buff, "swig_ptr: ", 10);
strncpy(buff, "swig_ptr: ", 10);
buff += 10;
SWIG_PackVoidPtr(buff, ptr, ty->name, lptr);
methods[i].ml_doc = ndoc;
Expand Down Expand Up @@ -5591,8 +5573,8 @@ SWIG_init(void) {
(char *)"this", &SwigPyBuiltin_ThisClosure, NULL, NULL, NULL
};
static SwigPyGetSet thisown_getset_closure = {
SwigPyObject_own,
SwigPyObject_own
(PyCFunction) SwigPyObject_own,
(PyCFunction) SwigPyObject_own
};
static PyGetSetDef thisown_getset_def = {
(char *)"thisown", SwigPyBuiltin_GetterClosure, SwigPyBuiltin_SetterClosure, NULL, &thisown_getset_closure
Expand Down
2 changes: 1 addition & 1 deletion include/SubnetTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class SubnetTree

PyObject* data = self->lookup(cidr, size);
if ( ! data ) {
PyErr_SetString(PyExc_KeyError, cidr);
PyErr_SetObject(PyExc_KeyError, PyBytes_FromStringAndSize(cidr, size));
return 0;
}

Expand Down

0 comments on commit d3e0afd

Please sign in to comment.