Skip to content

Commit

Permalink
Implement __bytes__ like __unicode__.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Howitz committed Apr 17, 2019
1 parent 4a46236 commit 7424de4
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Acquisition/_Acquisition.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ static PyObject *py__add__, *py__sub__, *py__mul__, *py__div__,
*py__long__, *py__float__, *py__oct__, *py__hex__,
*py__getitem__, *py__setitem__, *py__delitem__,
*py__getslice__, *py__setslice__, *py__delslice__, *py__contains__,
*py__len__, *py__of__, *py__call__, *py__repr__, *py__str__, *py__unicode__,
*py__len__, *py__of__, *py__call__, *py__repr__,
*py__str__, *py__unicode__, *py__bytes__,
*py__cmp__, *py__parent__, *py__iter__, *py__bool__, *py__index__, *py__iadd__,
*py__isub__, *py__imul__, *py__imod__, *py__ipow__, *py__ilshift__, *py__irshift__,
*py__iand__, *py__ixor__, *py__ior__, *py__floordiv__, *py__truediv__,
Expand Down Expand Up @@ -95,6 +96,7 @@ init_py_names(void)
INIT_PY_NAME(__repr__);
INIT_PY_NAME(__str__);
INIT_PY_NAME(__unicode__);
INIT_PY_NAME(__bytes__);
INIT_PY_NAME(__cmp__);
INIT_PY_NAME(__parent__);
INIT_PY_NAME(__iter__);
Expand Down Expand Up @@ -970,6 +972,20 @@ Wrapper_unicode(Wrapper *self)
}
}

static PyObject *
Wrapper_bytes(Wrapper *self)
{
PyObject *r;

if ((r = PyObject_GetAttr(OBJECT(self), py__bytes__))) {
ASSIGN(r, PyObject_CallFunction(r, NULL, NULL));
return r;
} else {
PyErr_Clear();
return Wrapper_str(self);
}
}

static long
Wrapper_hash(Wrapper *self)
{
Expand Down Expand Up @@ -1487,6 +1503,8 @@ static struct PyMethodDef Wrapper_methods[] = {
"Wrappers are not picklable"},
{"__unicode__", (PyCFunction)Wrapper_unicode, METH_NOARGS,
"Unicode"},
{"__bytes__", (PyCFunction)Wrapper_bytes, METH_NOARGS,
"Bytes"},
{NULL, NULL}
};

Expand Down

0 comments on commit 7424de4

Please sign in to comment.