summaryrefslogtreecommitdiff
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
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.
-rw-r--r--_dbus_bindings/abstract.c20
-rw-r--r--_dbus_bindings/bus.c6
-rw-r--r--_dbus_bindings/bytes.c34
-rw-r--r--_dbus_bindings/conn-methods.c2
-rw-r--r--_dbus_bindings/conn.c2
-rw-r--r--_dbus_bindings/containers.c11
-rw-r--r--_dbus_bindings/generic.c4
-rw-r--r--_dbus_bindings/int.c33
-rw-r--r--_dbus_bindings/message-append.c21
-rw-r--r--_dbus_bindings/message-get-args.c2
-rw-r--r--_dbus_bindings/message.c2
-rw-r--r--_dbus_bindings/types-internal.h3
-rw-r--r--_dbus_bindings/unixfd.c95
-rwxr-xr-xtest/test-standalone.py7
14 files changed, 166 insertions, 76 deletions
diff --git a/_dbus_bindings/abstract.c b/_dbus_bindings/abstract.c
index 3fdf286..b251eae 100644
--- a/_dbus_bindings/abstract.c
+++ b/_dbus_bindings/abstract.c
@@ -61,7 +61,7 @@ dbus_py_variant_level_get(PyObject *obj)
*/
return 0;
}
- variant_level = PyInt_AsLong(vl_obj);
+ variant_level = PyLong_AsLong(vl_obj);
if (variant_level == -1 && PyErr_Occurred()) {
/* variant_level < 0 can never be inserted into the dictionary; see
* dbus_py_variant_level_set() below. The semantics of setting
@@ -92,7 +92,7 @@ dbus_py_variant_level_set(PyObject *obj, long variant_level)
}
}
else {
- PyObject *vl_obj = PyInt_FromLong(variant_level);
+ PyObject *vl_obj = PyLong_FromLong(variant_level);
if (!vl_obj) {
Py_CLEAR(key);
return FALSE;
@@ -143,7 +143,7 @@ dbus_py_variant_level_getattro(PyObject *obj, PyObject *name)
Py_CLEAR(key);
if (!value)
- return PyInt_FromLong(0);
+ return PyLong_FromLong(0);
Py_INCREF(value);
return value;
}
@@ -434,8 +434,13 @@ DBusPythonString_tp_repr(PyObject *self)
Py_CLEAR(parent_repr);
return NULL;
}
- variant_level = PyInt_AsLong(vl_obj);
+ variant_level = PyLong_AsLong(vl_obj);
Py_CLEAR(vl_obj);
+ if (variant_level == -1 && PyErr_Occurred()) {
+ Py_CLEAR(parent_repr);
+ return NULL;
+ }
+
if (variant_level > 0) {
my_repr = PyUnicode_FromFormat("%s(%V, variant_level=%ld)",
Py_TYPE(self)->tp_name,
@@ -551,8 +556,13 @@ DBusPythonLong_tp_repr(PyObject *self)
Py_CLEAR(parent_repr);
return NULL;
}
- variant_level = PyInt_AsLong(vl_obj);
+ variant_level = PyLong_AsLong(vl_obj);
Py_CLEAR(vl_obj);
+ if (variant_level < 0 && PyErr_Occurred()) {
+ Py_CLEAR(parent_repr);
+ return NULL;
+ }
+
if (variant_level) {
my_repr = PyUnicode_FromFormat("%s(%V, variant_level=%ld)",
Py_TYPE(self)->tp_name,
diff --git a/_dbus_bindings/bus.c b/_dbus_bindings/bus.c
index cc9294a..680ba8e 100644
--- a/_dbus_bindings/bus.c
+++ b/_dbus_bindings/bus.c
@@ -62,7 +62,7 @@ DBusPyConnection_NewForBus(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
return (PyObject *)self;
}
- else if (!first || PyInt_Check(first)) {
+ else if (!first || PyLong_Check(first) || PyInt_Check(first)) {
long type;
PyObject *libdbusconn;
PyObject *new_args;
@@ -73,7 +73,9 @@ DBusPyConnection_NewForBus(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
DBUS_BUS_SESSION. */
if (first) {
- type = PyInt_AsLong(first);
+ type = PyLong_AsLong(first);
+ if (type == -1 && PyErr_Occurred())
+ return NULL;
if (type != DBUS_BUS_SESSION && type != DBUS_BUS_SYSTEM
&& type != DBUS_BUS_STARTER) {
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);
}
diff --git a/_dbus_bindings/conn-methods.c b/_dbus_bindings/conn-methods.c
index d2ebeb1..3964611 100644
--- a/_dbus_bindings/conn-methods.c
+++ b/_dbus_bindings/conn-methods.c
@@ -564,7 +564,7 @@ Connection_get_unix_fd (Connection *self, PyObject *unused UNUSED)
ok = dbus_connection_get_unix_fd (self->conn, &fd);
Py_END_ALLOW_THREADS
if (!ok) Py_RETURN_NONE;
- return PyInt_FromLong(fd);
+ return PyLong_FromLong(fd);
}
PyDoc_STRVAR(Connection_get_peer_unix_user__doc__,
diff --git a/_dbus_bindings/conn.c b/_dbus_bindings/conn.c
index 1cc6858..ffb5a35 100644
--- a/_dbus_bindings/conn.c
+++ b/_dbus_bindings/conn.c
@@ -99,7 +99,7 @@ DBusPyConnection_HandleMessage(Connection *conn,
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
else {
- long i = PyInt_AsLong(obj);
+ long i = PyLong_AsLong(obj);
DBG("%p: handler %p returned %ld", conn, callable, i);
Py_CLEAR(obj);
if (i == -1 && PyErr_Occurred()) {
diff --git a/_dbus_bindings/containers.c b/_dbus_bindings/containers.c
index 86a7675..e296406 100644
--- a/_dbus_bindings/containers.c
+++ b/_dbus_bindings/containers.c
@@ -127,11 +127,12 @@ Array_tp_new (PyTypeObject *cls, PyObject *args, PyObject *kwargs)
variant_level = PyDict_GetItem(kwargs, dbus_py_variant_level_const);
}
if (variant_level) {
- self->variant_level = PyInt_AsLong(variant_level);
- if (PyErr_Occurred()) {
+ long new_variant_level = PyLong_AsLong(variant_level);
+ if (new_variant_level == -1 && PyErr_Occurred()) {
Py_CLEAR(self);
return NULL;
}
+ self->variant_level = new_variant_level;
}
return (PyObject *)self;
}
@@ -334,11 +335,13 @@ Dict_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
variant_level = PyDict_GetItem(kwargs, dbus_py_variant_level_const);
}
if (variant_level) {
- self->variant_level = PyInt_AsLong(variant_level);
- if (PyErr_Occurred()) {
+ long new_variant_level = PyLong_AsLong(variant_level);
+
+ if (new_variant_level == -1 && PyErr_Occurred()) {
Py_CLEAR(self);
return NULL;
}
+ self->variant_level = new_variant_level;
}
return (PyObject *)self;
}
diff --git a/_dbus_bindings/generic.c b/_dbus_bindings/generic.c
index 8894bb5..ed12025 100644
--- a/_dbus_bindings/generic.c
+++ b/_dbus_bindings/generic.c
@@ -33,8 +33,8 @@ PyObject *dbus_py_empty_tuple = NULL;
int
dbus_py_immutable_setattro(PyObject *obj UNUSED,
- PyObject *name UNUSED,
- PyObject *value UNUSED)
+ PyObject *name UNUSED,
+ PyObject *value UNUSED)
{
PyErr_SetString(PyExc_AttributeError, "Object is immutable");
return -1;
diff --git a/_dbus_bindings/int.c b/_dbus_bindings/int.c
index 1c4b2d4..97061e1 100644
--- a/_dbus_bindings/int.c
+++ b/_dbus_bindings/int.c
@@ -73,16 +73,21 @@ Boolean_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
static PyObject *
Boolean_tp_repr (PyObject *self)
{
+ int is_true = PyObject_IsTrue(self);
long variant_level = ((DBusPyIntBase *)self)->variant_level;
+
+ if (is_true == -1)
+ return NULL;
+
if (variant_level > 0) {
return PyUnicode_FromFormat("%s(%s, variant_level=%ld)",
Py_TYPE(self)->tp_name,
- PyInt_AsLong(self) ? "True" : "False",
+ is_true ? "True" : "False",
variant_level);
}
return PyUnicode_FromFormat("%s(%s)",
Py_TYPE(self)->tp_name,
- PyInt_AsLong(self) ? "True" : "False");
+ is_true ? "True" : "False");
}
PyTypeObject DBusPyBoolean_Type = {
@@ -152,14 +157,16 @@ PyDoc_STRVAR(Int16_tp_doc,
dbus_int16_t
dbus_py_int16_range_check(PyObject *obj)
{
- long i = PyInt_AsLong (obj);
- if (i == -1 && PyErr_Occurred ()) return -1;
+ long i = PyLong_AsLong(obj);
+ if (i == -1 && PyErr_Occurred())
+ return -1;
+
if (i < -0x8000 || i > 0x7fff) {
PyErr_Format(PyExc_OverflowError, "Value %d out of range for Int16",
(int)i);
return -1;
}
- return i;
+ return (dbus_int16_t)i;
}
static PyObject *
@@ -240,14 +247,16 @@ PyDoc_STRVAR(UInt16_tp_doc,
dbus_uint16_t
dbus_py_uint16_range_check(PyObject *obj)
{
- long i = PyInt_AsLong(obj);
- if (i == -1 && PyErr_Occurred()) return (dbus_uint16_t)(-1);
+ long i = PyLong_AsLong(obj);
+ if (i == -1 && PyErr_Occurred())
+ return (dbus_uint16_t)(-1);
+
if (i < 0 || i > 0xffff) {
PyErr_Format(PyExc_OverflowError, "Value %d out of range for UInt16",
(int)i);
return (dbus_uint16_t)(-1);
}
- return i;
+ return (dbus_uint16_t)i;
}
static PyObject *
@@ -329,14 +338,16 @@ PyDoc_STRVAR(Int32_tp_doc,
dbus_int32_t
dbus_py_int32_range_check(PyObject *obj)
{
- long i = PyInt_AsLong(obj);
- if (i == -1 && PyErr_Occurred()) return -1;
+ long i = PyLong_AsLong(obj);
+ if (i == -1 && PyErr_Occurred())
+ return -1;
+
if (i < INT32_MIN || i > INT32_MAX) {
PyErr_Format(PyExc_OverflowError, "Value %d out of range for Int32",
(int)i);
return -1;
}
- return i;
+ return (dbus_int32_t)i;
}
static PyObject *
diff --git a/_dbus_bindings/message-append.c b/_dbus_bindings/message-append.c
index ea12a7d..4298b3b 100644
--- a/_dbus_bindings/message-append.c
+++ b/_dbus_bindings/message-append.c
@@ -515,7 +515,7 @@ _message_iter_append_byte(DBusMessageIter *appender, PyObject *obj)
y = *(unsigned char *)PyBytes_AS_STRING(obj);
}
else {
- long i = PyInt_AsLong(obj);
+ long i = PyLong_AsLong(obj);
if (i == -1 && PyErr_Occurred()) return -1;
if (i < 0 || i > 0xff) {
@@ -550,12 +550,23 @@ static int
_message_iter_append_unixfd(DBusMessageIter *appender, PyObject *obj)
{
int fd;
+ long original_fd;
- if (PyInt_Check(obj)) {
- fd = PyInt_AsLong(obj);
- } else if (PyObject_IsInstance(obj, (PyObject*) &DBusPyUnixFd_Type)) {
+ if (PyLong_Check(obj) || PyInt_Check(obj)) {
+ original_fd = PyLong_AsLong(obj);
+ if (original_fd == -1 && PyErr_Occurred())
+ return -1;
+ if (original_fd < INT_MIN || original_fd > INT_MAX) {
+ PyErr_Format(PyExc_ValueError, "out of int range: %ld",
+ original_fd);
+ return -1;
+ }
+ fd = (int)original_fd;
+ }
+ else if (PyObject_IsInstance(obj, (PyObject*) &DBusPyUnixFd_Type)) {
fd = dbus_py_unix_fd_get_fd(obj);
- } else {
+ }
+ else {
return -1;
}
diff --git a/_dbus_bindings/message-get-args.c b/_dbus_bindings/message-get-args.c
index fef2a45..8d99bf7 100644
--- a/_dbus_bindings/message-get-args.c
+++ b/_dbus_bindings/message-get-args.c
@@ -213,7 +213,7 @@ _message_iter_get_pyobject(DBusMessageIter *iter,
if (variant_level > 0 && type != DBUS_TYPE_VARIANT) {
PyObject *variant_level_int;
- variant_level_int = PyInt_FromLong(variant_level);
+ variant_level_int = PyLong_FromLong(variant_level);
if (!variant_level_int) {
return NULL;
}
diff --git a/_dbus_bindings/message.c b/_dbus_bindings/message.c
index f96459f..e89bd2f 100644
--- a/_dbus_bindings/message.c
+++ b/_dbus_bindings/message.c
@@ -341,7 +341,7 @@ static PyObject *
Message_get_type(Message *self, PyObject *unused UNUSED)
{
if (!self->msg) return DBusPy_RaiseUnusableMessage();
- return PyInt_FromLong(dbus_message_get_type(self->msg));
+ return PyLong_FromLong(dbus_message_get_type(self->msg));
}
PyDoc_STRVAR(Message_get_serial__doc__,
diff --git a/_dbus_bindings/types-internal.h b/_dbus_bindings/types-internal.h
index d24c1c0..f06f093 100644
--- a/_dbus_bindings/types-internal.h
+++ b/_dbus_bindings/types-internal.h
@@ -33,6 +33,9 @@
#include <bytesobject.h>
#define PyBytes_InternFromString PyString_InternFromString
+/* In Python 2.x, we need this to define the type of PyLongObject */
+#include <longintrepr.h>
+
#include "dbus_bindings-internal.h"
#ifndef DBUS_BINDINGS_TYPES_INTERNAL_H
diff --git a/_dbus_bindings/unixfd.c b/_dbus_bindings/unixfd.c
index 3d6bc11..7b19144 100644
--- a/_dbus_bindings/unixfd.c
+++ b/_dbus_bindings/unixfd.c
@@ -55,49 +55,81 @@ typedef struct {
int fd;
} UnixFdObject;
+/* Return values:
+ * -2 - the long value overflows an int
+ * -1 - Python failed producing a long (or in Python 2 an int)
+ * 0 - success
+ * 1 - arg is not a long (or in Python 2 an int)
+ *
+ * Or to summarize:
+ * status < 0 - an error occurred, and a Python exception is set.
+ * status == 0 - all is okay, output argument *fd is set.
+ * status > 0 - try something else
+ */
+static int
+make_fd(PyObject *arg, int *fd)
+{
+ long fd_arg;
+
+ if (PyLong_Check(arg) || PyInt_Check(arg)) {
+ fd_arg = PyLong_AsLong(arg);
+ if (fd_arg == -1 && PyErr_Occurred()) {
+ return -1;
+ }
+ }
+ else {
+ return 1;
+ }
+ /* Check for int overflow. */
+ if (fd_arg < INT_MIN || fd_arg > INT_MAX) {
+ PyErr_Format(PyExc_ValueError, "int is outside fd range");
+ return -2;
+ }
+ *fd = (int)fd_arg;
+ return 0;
+}
+
static PyObject *
UnixFd_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs UNUSED)
{
UnixFdObject *self = NULL;
PyObject *arg;
- PyObject *fdnumber;
- int fd_original, fd;
+ int status, fd, fd_original = -1;
- if (! PyArg_ParseTuple(args, "O", &arg, NULL)) {
+ if (!PyArg_ParseTuple(args, "O", &arg, NULL)) {
return NULL;
}
- if (PyInt_Check(arg)) {
- fd_original = PyInt_AsLong(arg);
- fd = dup(fd_original);
- if (fd < 0) {
- PyErr_Format(PyExc_ValueError, "Invalid file descriptor");
- return NULL;
- }
+ status = make_fd(arg, &fd_original);
+ if (status < 0)
+ return NULL;
- } else if (PyObject_HasAttrString(arg, "fileno")) {
- fdnumber = PyObject_CallMethod(arg, "fileno", NULL);
- if (! fdnumber) {
- PyErr_Format(PyExc_ValueError, "Argument's fileno() method "
- "is not callable");
- return NULL;
+ if (status > 0) {
+ if (PyObject_HasAttrString(arg, "fileno")) {
+ PyObject *fd_number = PyObject_CallMethod(arg, "fileno", NULL);
+ if (!fd_number)
+ return NULL;
+ status = make_fd(fd_number, &fd_original);
+ Py_CLEAR(fd_number);
+ if (status < 0)
+ return NULL;
+ if (status > 0) {
+ PyErr_Format(PyExc_ValueError, "Argument's fileno() method "
+ "returned a non-int value");
+ return NULL;
+ }
+ /* fd_original is all good. */
}
- if (! PyInt_Check(fdnumber)) {
- PyErr_Format(PyExc_ValueError, "Argument's fileno() method "
- "returned a non-int value");
+ else {
+ PyErr_Format(PyExc_ValueError, "Argument is not int and does not "
+ "implement fileno() method");
return NULL;
}
- fd_original = PyInt_AsLong(fdnumber);
- Py_DECREF(fdnumber);
- fd = dup(fd_original);
- if (fd < 0) {
- PyErr_Format(PyExc_ValueError, "Invalid file descriptor from fileno()");
- return NULL;
- }
-
- } else {
- PyErr_Format(PyExc_ValueError, "Argument is not int and does not "
- "implement fileno() method");
+ }
+ assert(fd_original >= 0);
+ fd = dup(fd_original);
+ if (fd < 0) {
+ PyErr_Format(PyExc_ValueError, "Invalid file descriptor");
return NULL;
}
@@ -106,8 +138,7 @@ UnixFd_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs UNUSED)
return NULL;
self->fd = fd;
-
- return (PyObject *) self;
+ return (PyObject *)self;
}
static void
diff --git a/test/test-standalone.py b/test/test-standalone.py
index 04d1e6d..5a86390 100755
--- a/test/test-standalone.py
+++ b/test/test-standalone.py
@@ -29,8 +29,6 @@ run in isolation.
import sys
import os
import unittest
-import time
-from traceback import print_exc
builddir = os.path.normpath(os.environ["DBUS_TOP_BUILDDIR"])
pydir = os.path.normpath(os.environ["DBUS_TOP_SRCDIR"])
@@ -80,7 +78,10 @@ class TestTypes(unittest.TestCase):
self.assertEquals(x, ('a','b','c'))
def test_Byte(self):
- self.assertEquals(types.Byte('x', variant_level=2), types.Byte(ord('x')))
+ self.assertEquals(types.Byte('x', variant_level=2),
+ types.Byte(ord('x')))
+ self.assertEquals(types.Byte(1), 1)
+ self.assertEquals(types.Byte(1L), 1)
def test_ByteArray(self):
self.assertEquals(types.ByteArray(''), '')