Index: gdb/python/py-value.c |
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c |
index 6f67bdb2eb7ddc989714b2469ec775a103edd701..40254b99261d5afdada507c8bd57fd9cbeb67b5e 100644 |
--- a/gdb/python/py-value.c |
+++ b/gdb/python/py-value.c |
@@ -1,6 +1,6 @@ |
/* Python interface to values. |
- Copyright (C) 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc. |
+ Copyright (C) 2008-2013 Free Software Foundation, Inc. |
This file is part of GDB. |
@@ -106,7 +106,7 @@ valpy_dealloc (PyObject *obj) |
Py_XDECREF (self->dynamic_type); |
- self->ob_type->tp_free (self); |
+ Py_TYPE (self)->tp_free (self); |
} |
/* Helper to push a Value object on the global list. */ |
@@ -334,18 +334,11 @@ valpy_get_dynamic_type (PyObject *self, void *closure) |
GDB_PY_HANDLE_EXCEPTION (except); |
if (type == NULL) |
- { |
- /* Ensure that the TYPE field is ready. */ |
- if (!valpy_get_type (self, NULL)) |
- return NULL; |
- /* We don't need to incref here, because valpy_get_type already |
- did it for us. */ |
- obj->dynamic_type = obj->type; |
- } |
+ obj->dynamic_type = valpy_get_type (self, NULL); |
else |
obj->dynamic_type = type_to_type_object (type); |
- Py_INCREF (obj->dynamic_type); |
+ Py_XINCREF (obj->dynamic_type); |
return obj->dynamic_type; |
} |
@@ -421,7 +414,8 @@ valpy_string (PyObject *self, PyObject *args, PyObject *kw) |
GDB_PY_HANDLE_EXCEPTION (except); |
encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding; |
- unicode = PyUnicode_Decode (buffer, length * TYPE_LENGTH (char_type), |
+ unicode = PyUnicode_Decode ((const char *) buffer, |
+ length * TYPE_LENGTH (char_type), |
encoding, errors); |
xfree (buffer); |
@@ -443,7 +437,7 @@ valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op) |
type = type_object_to_type (type_obj); |
if (! type) |
{ |
- PyErr_SetString (PyExc_RuntimeError, |
+ PyErr_SetString (PyExc_RuntimeError, |
_("Argument must be a type.")); |
return NULL; |
} |
@@ -516,7 +510,7 @@ valpy_getitem (PyObject *self, PyObject *key) |
PyObject *result = NULL; |
if (gdbpy_is_string (key)) |
- { |
+ { |
field = python_string_to_host_string (key); |
if (field == NULL) |
return NULL; |
@@ -729,7 +723,7 @@ valpy_fetch_lazy (PyObject *self, PyObject *args) |
/* Calculate and return the address of the PyObject as the value of |
the builtin __hash__ call. */ |
-static long |
+static long |
valpy_hash (PyObject *self) |
{ |
return (long) (intptr_t) self; |
@@ -775,11 +769,17 @@ valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other) |
a gdb.Value object and need to convert it from python as well. */ |
arg1 = convert_value_from_python (self); |
if (arg1 == NULL) |
- break; |
+ { |
+ do_cleanups (cleanup); |
+ break; |
+ } |
arg2 = convert_value_from_python (other); |
if (arg2 == NULL) |
- break; |
+ { |
+ do_cleanups (cleanup); |
+ break; |
+ } |
switch (opcode) |
{ |
@@ -1129,17 +1129,7 @@ valpy_richcompare (PyObject *self, PyObject *other, int op) |
Py_RETURN_FALSE; |
} |
-/* Helper function to determine if a type is "int-like". */ |
-static int |
-is_intlike (struct type *type, int ptr_ok) |
-{ |
- return (TYPE_CODE (type) == TYPE_CODE_INT |
- || TYPE_CODE (type) == TYPE_CODE_ENUM |
- || TYPE_CODE (type) == TYPE_CODE_BOOL |
- || TYPE_CODE (type) == TYPE_CODE_CHAR |
- || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR)); |
-} |
- |
+#ifndef IS_PY3K |
/* Implements conversion to int. */ |
static PyObject * |
valpy_int (PyObject *self) |
@@ -1151,8 +1141,7 @@ valpy_int (PyObject *self) |
TRY_CATCH (except, RETURN_MASK_ALL) |
{ |
- CHECK_TYPEDEF (type); |
- if (!is_intlike (type, 0)) |
+ if (!is_integral_type (type)) |
error (_("Cannot convert value to int.")); |
l = value_as_long (value); |
@@ -1161,6 +1150,7 @@ valpy_int (PyObject *self) |
return gdb_py_object_from_longest (l); |
} |
+#endif |
/* Implements conversion to long. */ |
static PyObject * |
@@ -1175,7 +1165,8 @@ valpy_long (PyObject *self) |
{ |
CHECK_TYPEDEF (type); |
- if (!is_intlike (type, 1)) |
+ if (!is_integral_type (type) |
+ && TYPE_CODE (type) != TYPE_CODE_PTR) |
error (_("Cannot convert value to long.")); |
l = value_as_long (value); |
@@ -1257,19 +1248,19 @@ convert_value_from_python (PyObject *obj) |
TRY_CATCH (except, RETURN_MASK_ALL) |
{ |
- if (PyBool_Check (obj)) |
+ if (PyBool_Check (obj)) |
{ |
cmp = PyObject_IsTrue (obj); |
if (cmp >= 0) |
value = value_from_longest (builtin_type_pybool, cmp); |
} |
- else if (PyInt_Check (obj)) |
- { |
- long l = PyInt_AsLong (obj); |
- |
- if (! PyErr_Occurred ()) |
- value = value_from_longest (builtin_type_pyint, l); |
- } |
+ /* Make a long logic check first. In Python 3.x, internally, |
+ all integers are represented as longs. In Python 2.x, there |
+ is still a differentiation internally between a PyInt and a |
+ PyLong. Explicitly do this long check conversion first. In |
+ GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has |
+ to be done first to ensure we do not lose information in the |
+ conversion process. */ |
else if (PyLong_Check (obj)) |
{ |
LONGEST l = PyLong_AsLongLong (obj); |
@@ -1304,6 +1295,13 @@ convert_value_from_python (PyObject *obj) |
else |
value = value_from_longest (builtin_type_pylong, l); |
} |
+ else if (PyInt_Check (obj)) |
+ { |
+ long l = PyInt_AsLong (obj); |
+ |
+ if (! PyErr_Occurred ()) |
+ value = value_from_longest (builtin_type_pyint, l); |
+ } |
else if (PyFloat_Check (obj)) |
{ |
double d = PyFloat_AsDouble (obj); |
@@ -1335,9 +1333,14 @@ convert_value_from_python (PyObject *obj) |
value = value_copy (((value_object *) result)->value); |
} |
else |
+#ifdef IS_PY3K |
+ PyErr_Format (PyExc_TypeError, |
+ _("Could not convert Python object: %S."), obj); |
+#else |
PyErr_Format (PyExc_TypeError, |
_("Could not convert Python object: %s."), |
PyString_AsString (PyObject_Str (obj))); |
+#endif |
} |
if (except.reason < 0) |
{ |
@@ -1378,16 +1381,14 @@ gdbpy_is_value_object (PyObject *obj) |
return PyObject_TypeCheck (obj, &value_object_type); |
} |
-void |
+int |
gdbpy_initialize_values (void) |
{ |
if (PyType_Ready (&value_object_type) < 0) |
- return; |
+ return -1; |
- Py_INCREF (&value_object_type); |
- PyModule_AddObject (gdb_module, "Value", (PyObject *) &value_object_type); |
- |
- values_in_python = NULL; |
+ return gdb_pymodule_addobject (gdb_module, "Value", |
+ (PyObject *) &value_object_type); |
} |
@@ -1430,7 +1431,7 @@ Return a lazy string representation of the value." }, |
{ "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS, |
"string ([encoding] [, errors] [, length]) -> string\n\ |
Return Unicode string representation of the value." }, |
- { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS, |
+ { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS, |
"Fetches the value from the inferior, if it was lazy." }, |
{NULL} /* Sentinel */ |
}; |
@@ -1439,7 +1440,9 @@ static PyNumberMethods value_object_as_number = { |
valpy_add, |
valpy_subtract, |
valpy_multiply, |
+#ifndef IS_PY3K |
valpy_divide, |
+#endif |
valpy_remainder, |
NULL, /* nb_divmod */ |
valpy_power, /* nb_power */ |
@@ -1453,12 +1456,31 @@ static PyNumberMethods value_object_as_number = { |
valpy_and, /* nb_and */ |
valpy_xor, /* nb_xor */ |
valpy_or, /* nb_or */ |
+#ifdef IS_PY3K |
+ valpy_long, /* nb_int */ |
+ NULL, /* reserved */ |
+#else |
NULL, /* nb_coerce */ |
valpy_int, /* nb_int */ |
valpy_long, /* nb_long */ |
+#endif |
valpy_float, /* nb_float */ |
+#ifndef IS_PY3K |
NULL, /* nb_oct */ |
- NULL /* nb_hex */ |
+ NULL, /* nb_hex */ |
+#endif |
+ NULL, /* nb_inplace_add */ |
+ NULL, /* nb_inplace_subtract */ |
+ NULL, /* nb_inplace_multiply */ |
+ NULL, /* nb_inplace_remainder */ |
+ NULL, /* nb_inplace_power */ |
+ NULL, /* nb_inplace_lshift */ |
+ NULL, /* nb_inplace_rshift */ |
+ NULL, /* nb_inplace_and */ |
+ NULL, /* nb_inplace_xor */ |
+ NULL, /* nb_inplace_or */ |
+ NULL, /* nb_floor_divide */ |
+ valpy_divide /* nb_true_divide */ |
}; |
static PyMappingMethods value_object_as_mapping = { |
@@ -1468,8 +1490,7 @@ static PyMappingMethods value_object_as_mapping = { |
}; |
PyTypeObject value_object_type = { |
- PyObject_HEAD_INIT (NULL) |
- 0, /*ob_size*/ |
+ PyVarObject_HEAD_INIT (NULL, 0) |
"gdb.Value", /*tp_name*/ |
sizeof (value_object), /*tp_basicsize*/ |
0, /*tp_itemsize*/ |