summaryrefslogtreecommitdiff
path: root/_dbus_bindings/bytes.c
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2011-12-14 15:05:16 -0500
committerBarry Warsaw <barry@python.org>2011-12-14 15:05:16 -0500
commit667082d0b4aef9c438a2e7fec89614b5b8ef960a (patch)
tree59e2a97463a621805735b4937f7fb708065daf7d /_dbus_bindings/bytes.c
parent11c639e3bd2ca3e6a87f349155dda2acc3cd92ea (diff)
downloaddbus-python-667082d0b4aef9c438a2e7fec89614b5b8ef960a.tar.gz
First round of PyInt -> PyLong changes. These are only compatible with Python
2, since there are still some unconditional PyInt calls, which are not valid in Python 3. However, it lays the framework for conditionalizing on Python 3 and using only PyLong in that case. Where it doesn't matter, PyLong is used unconditionally.
Diffstat (limited to '_dbus_bindings/bytes.c')
-rw-r--r--_dbus_bindings/bytes.c34
1 files changed, 26 insertions, 8 deletions
diff --git a/_dbus_bindings/bytes.c b/_dbus_bindings/bytes.c
index e608ec2..f43697b 100644
--- a/_dbus_bindings/bytes.c
+++ b/_dbus_bindings/bytes.c
@@ -76,7 +76,9 @@ Byte_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
return NULL;
}
- /* obj is only a borrowed ref for the moment */
+ /* obj is a borrowed reference. It gets turned into an owned reference on
+ * the good-path of the if-statements below.
+ */
obj = PyTuple_GetItem(args, 0);
if (PyBytes_Check(obj)) {
@@ -84,13 +86,19 @@ Byte_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
if (PyBytes_GET_SIZE(obj) != 1) {
goto bad_arg;
}
- obj = PyInt_FromLong((unsigned char)(PyBytes_AS_STRING(obj)[0]));
+ obj = PyLong_FromLong((unsigned char)(PyBytes_AS_STRING(obj)[0]));
+ if (!obj)
+ goto bad_arg;
}
- else if (PyInt_Check(obj)) {
- long i = PyInt_AS_LONG(obj);
+ else if (PyLong_Check(obj) || PyInt_Check(obj)) {
+ long i = PyLong_AsLong(obj);
+
+ if (i == -1 && PyErr_Occurred())
+ goto bad_arg;
if (Py_TYPE(obj) == cls &&
- ((DBusPyIntBase *)obj)->variant_level == variantness) {
+ ((DBusPyIntBase *)obj)->variant_level == variantness)
+ {
Py_INCREF(obj);
return obj;
}
@@ -102,9 +110,9 @@ Byte_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
goto bad_arg;
}
- tuple = Py_BuildValue("(O)", obj);
+ /* The tuple steals the reference to obj. */
+ tuple = Py_BuildValue("(N)", obj);
if (!tuple) return NULL;
- Py_CLEAR(obj);
obj = DBusPyIntBase_Type.tp_new(cls, tuple, kwargs);
Py_CLEAR(tuple);
@@ -122,7 +130,17 @@ bad_range:
static PyObject *
Byte_tp_str(PyObject *self)
{
- unsigned char str[2] = { (unsigned char)PyInt_AS_LONG(self), 0 };
+ long i = PyLong_AsLong(self);
+ unsigned char str[2] = { 0, 0 };
+
+ if (i == -1 && PyErr_Occurred())
+ return NULL;
+ if (i < 0 || i > 255) {
+ PyErr_SetString(PyExc_RuntimeError, "Integer outside range 0-255");
+ return NULL;
+ }
+
+ str[0] = (unsigned char)i;
return PyBytes_FromStringAndSize((char *)str, 1);
}