Skip to content

Commit 9a27f2c

Browse files
author
Paul Koning
committed
Add support for Python 3.
* NEWS: Mention Python 3 support. * varobj.c (value_get_print_value): Use python_string_to_target_string. * python/py-block.c: Use PyVarObject_HEAD_INIT in initialization of type objects. * python/py-breakpoint.c: Ditto. * python/py-cmd.c: Ditto. * python/py-event.c: Ditto. * python/py-event.h: Ditto. * python/py-evtregistry.c: Ditto. * python/py-finishbreakpoint.c: Ditto. * python/py-frame.c: Ditto. * python/py-function.c: Ditto. * python/py-infthread.c: Ditto. * python/py-lazy-string.c: Ditto. * python/py-progspace.c: Ditto. * /python/py-symbol.c: Ditto. * python/py-evts.c: (gdbpy_initialize_py_events): Add module initialization for Python 3. * python/py-inferior.c: Use PyVarObject_HEAD_INIT in initialization of type objects. (infpy_read_memory): Return memoryview object if Python 3. (infpy_write_memory): Use "s*" operand parsing code for Python 3. (infpy_search_memory): Ditto. (get_buffer): New function for Python 3. * python/py-objfile.c: Use PyVarObject_HEAD_INIT in initialization of type objects. (objfpy_dealloc): Use Py_TYPE to call tp_free. * python/py-param.c: Use PyVarObject_HEAD_INIT in initialization of type objects. (get_attr): Use PyUnicode_CompareWithASCIIString if Python 3. (set_attr): Ditto. * python/py-prettyprint.c (print_string_repr): use PyBytes methods instead of PyString methods if Python 3. (print_children): Skip push_dummy_python_frame call if Python 3. * python/py-symtab.c: Use PyVarObject_HEAD_INIT in initialization of type objects. (salpy_dealloc): Use Py_TYPE to call tp_free. * python/py-type.c: Use PyVarObject_HEAD_INIT in initialization of type objects. (field_dealloc): Use Py_TYPE to call tp_free. (typy_dealloc): Ditto. (type_object_as_number): Adjust struct initializations for differences in layout for Python 2 vs. Python 3. * python/py-utils.c (python_string_to_unicode): Omit non-Unicode string case for Python 3. (unicode_to_encoded_python_string): Shorten code (no functional change). (python_string_to_target_python_string): Comment that in Python 3 returned value is a Python "bytes" type. (gdbpy_is_string): Omit non-Unicode string check in Python 3. (gdb_py_object_from_longest): Omit non-long integer case in Python 3. (gdb_py_object_from_ulongest): Ditto. * python/py-value.c: Use PyVarObject_HEAD_INIT in initialization of type objects. (valpy_dealloc): Use Py_TYPE to call tp_free. (valpy_int): Omit function if Python 3. (convert_value_from_python): Use "%S" format (Python object as a string) if Python 3. (value_object_as_number): Adjust struct initializations for differences in layout for Python 2 vs. Python 3. * python/python-config.py: Adjust syntax for Python 3 compatibility. Include "sys.abiflags" string as part of python library name, if that attribute exists (Python 3). * python/python-internal.h (IS_PY3): Define if Python 3. (Py_TPFLAGS_HAVE_ITER, Py_TPFLAGS_CHECKTYPES): Define with placeholder value if Python 3. (PyInt_Check, PyInt_FromLong, PyInt_AsLong, PyString_FromString, PyString_Decode, PyString_FromFormat, PyString_Check): Define as analogous Python 3 API function if Python 3. (PyVarObject_HEAD_INIT): Define if not already defined. (Py_TYPE): Ditto. * python/python.c (eval_python_command): Omit Py_FlushLine call if Python 3. Check return values of all Python API calls for error. Supply dummy "python" and "python-interactive" commands if Python initialization failed. (_initialize_python): Convert argc to wchar_t** if Python 3. Add module initialization for Python 3. (finish_python_initialization): Pass wchar_t * argument to PySys_SetPath if Python 3. * python/lib/gdb/__init__.py: Define "reload" if Python 3. (_GdbFile): New class for common output file behavior. (GdbOutFile): Subclass from _GdbFile. (GdbOutputErrorFile): Ditto. (auto_load_packages): Adjust syntax for Python 3 compatibility. * python/lib/gdb/printing.py: Define basestr and int if Python 3. * python/lib/gdb/prompt.py: Use sorted() function rather than sort() method. * python/lib/gdb/command/explore.py: Define raw_input if Python 3. Adjust syntax for Python 3 compatibility. * python/lib/gdb/command/pretty_printers.py: Use sorted() function rather than sort() method. Adjust syntax for Python 3 compatibility. * python/lib/gdb/command/type_printers.py: Ditto. * doc/gdb.texinfo (Inferior.read_memory): Mention that the return value is a memoryview object if Python 3.
1 parent b8b98ad commit 9a27f2c

35 files changed

Lines changed: 514 additions & 156 deletions

‎gdb/ChangeLog‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,106 @@
1+
2012-12-12 Paul Koning <paul_koning@dell.com>
2+
3+
Add support for Python 3.
4+
* NEWS: Mention Python 3 support.
5+
* varobj.c (value_get_print_value): Use
6+
python_string_to_target_string.
7+
* python/py-block.c: Use PyVarObject_HEAD_INIT in initialization
8+
of type objects.
9+
* python/py-breakpoint.c: Ditto.
10+
* python/py-cmd.c: Ditto.
11+
* python/py-event.c: Ditto.
12+
* python/py-event.h: Ditto.
13+
* python/py-evtregistry.c: Ditto.
14+
* python/py-finishbreakpoint.c: Ditto.
15+
* python/py-frame.c: Ditto.
16+
* python/py-function.c: Ditto.
17+
* python/py-infthread.c: Ditto.
18+
* python/py-lazy-string.c: Ditto.
19+
* python/py-progspace.c: Ditto.
20+
* /python/py-symbol.c: Ditto.
21+
* python/py-evts.c: (gdbpy_initialize_py_events): Add module
22+
initialization for Python 3.
23+
* python/py-inferior.c: Use PyVarObject_HEAD_INIT in initialization
24+
of type objects.
25+
(infpy_read_memory): Return memoryview object if Python 3.
26+
(infpy_write_memory): Use "s*" operand parsing code for Python 3.
27+
(infpy_search_memory): Ditto.
28+
(get_buffer): New function for Python 3.
29+
* python/py-objfile.c: Use PyVarObject_HEAD_INIT in initialization
30+
of type objects.
31+
(objfpy_dealloc): Use Py_TYPE to call tp_free.
32+
* python/py-param.c: Use PyVarObject_HEAD_INIT in initialization
33+
of type objects.
34+
(get_attr): Use PyUnicode_CompareWithASCIIString if Python 3.
35+
(set_attr): Ditto.
36+
* python/py-prettyprint.c (print_string_repr): use PyBytes methods
37+
instead of PyString methods if Python 3.
38+
(print_children): Skip push_dummy_python_frame call if Python 3.
39+
* python/py-symtab.c: Use PyVarObject_HEAD_INIT in initialization
40+
of type objects.
41+
(salpy_dealloc): Use Py_TYPE to call tp_free.
42+
* python/py-type.c: Use PyVarObject_HEAD_INIT in initialization
43+
of type objects.
44+
(field_dealloc): Use Py_TYPE to call tp_free.
45+
(typy_dealloc): Ditto.
46+
(type_object_as_number): Adjust struct initializations for
47+
differences in layout for Python 2 vs. Python 3.
48+
* python/py-utils.c (python_string_to_unicode): Omit non-Unicode
49+
string case for Python 3.
50+
(unicode_to_encoded_python_string): Shorten code (no functional
51+
change).
52+
(python_string_to_target_python_string): Comment that in Python 3
53+
returned value is a Python "bytes" type.
54+
(gdbpy_is_string): Omit non-Unicode string check in Python 3.
55+
(gdb_py_object_from_longest): Omit non-long integer case in Python
56+
3.
57+
(gdb_py_object_from_ulongest): Ditto.
58+
* python/py-value.c: Use PyVarObject_HEAD_INIT in initialization
59+
of type objects.
60+
(valpy_dealloc): Use Py_TYPE to call tp_free.
61+
(valpy_int): Omit function if Python 3.
62+
(convert_value_from_python): Use "%S" format (Python object as a
63+
string) if Python 3.
64+
(value_object_as_number): Adjust struct initializations for
65+
differences in layout for Python 2 vs. Python 3.
66+
* python/python-config.py: Adjust syntax for Python 3
67+
compatibility.
68+
Include "sys.abiflags" string as part of python library name, if
69+
that attribute exists (Python 3).
70+
* python/python-internal.h (IS_PY3): Define if Python 3.
71+
(Py_TPFLAGS_HAVE_ITER, Py_TPFLAGS_CHECKTYPES): Define with
72+
placeholder value if Python 3.
73+
(PyInt_Check, PyInt_FromLong, PyInt_AsLong, PyString_FromString,
74+
PyString_Decode, PyString_FromFormat, PyString_Check): Define as
75+
analogous Python 3 API function if Python 3.
76+
(PyVarObject_HEAD_INIT): Define if not already defined.
77+
(Py_TYPE): Ditto.
78+
* python/python.c (eval_python_command): Omit Py_FlushLine call if
79+
Python 3.
80+
Check return values of all Python API calls for error.
81+
Supply dummy "python" and "python-interactive" commands if Python
82+
initialization failed.
83+
(_initialize_python): Convert argc to wchar_t** if Python 3.
84+
Add module initialization for Python 3.
85+
(finish_python_initialization): Pass wchar_t * argument to
86+
PySys_SetPath if Python 3.
87+
* python/lib/gdb/__init__.py: Define "reload" if Python 3.
88+
(_GdbFile): New class for common output file behavior.
89+
(GdbOutFile): Subclass from _GdbFile.
90+
(GdbOutputErrorFile): Ditto.
91+
(auto_load_packages): Adjust syntax for Python 3 compatibility.
92+
* python/lib/gdb/printing.py: Define basestr and int if Python 3.
93+
* python/lib/gdb/prompt.py: Use sorted() function rather than
94+
sort() method.
95+
* python/lib/gdb/command/explore.py: Define raw_input if Python 3.
96+
Adjust syntax for Python 3 compatibility.
97+
* python/lib/gdb/command/pretty_printers.py: Use sorted() function
98+
rather than sort() method.
99+
Adjust syntax for Python 3 compatibility.
100+
* python/lib/gdb/command/type_printers.py: Ditto.
101+
* doc/gdb.texinfo (Inferior.read_memory): Mention that the return
102+
value is a memoryview object if Python 3.
103+
1104
2012-12-12 Tom Tromey <tromey@redhat.com>
2105

3106
* coffread.c (coff_objfile_data_key): New global.

‎gdb/NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
** Types can be pretty-printed via a Python API.
2828

29+
** Python 3 is now supported (in addition to Python 2.4 or later)
30+
2931
* New Python-based convenience functions:
3032

3133
** $_memeq(buf1, buf2, length)

‎gdb/doc/gdb.texinfo‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24171,7 +24171,8 @@ return an empty tuple.
2417124171
Read @var{length} bytes of memory from the inferior, starting at
2417224172
@var{address}. Returns a buffer object, which behaves much like an array
2417324173
or a string. It can be modified and given to the
24174-
@code{Inferior.write_memory} function.
24174+
@code{Inferior.write_memory} function. In @code{Python} 3, the return
24175+
value is a @code{memoryview} object.
2417524176
@end defun
2417624177

2417724178
@findex Inferior.write_memory

‎gdb/python/lib/gdb/__init__.py‎

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,41 @@
1818
import sys
1919
import _gdb
2020

21+
if sys.version_info[0] > 2:
22+
# Python 3 moved "reload"
23+
from imp import reload
24+
2125
from _gdb import *
2226

23-
class GdbOutputFile:
27+
class _GdbFile (object):
28+
# These two are needed in Python 3
29+
encoding = "UTF-8"
30+
errors = "strict"
31+
2432
def close(self):
2533
# Do nothing.
2634
return None
2735

2836
def isatty(self):
2937
return False
3038

31-
def write(self, s):
32-
write(s, stream=STDOUT)
33-
3439
def writelines(self, iterable):
3540
for line in iterable:
3641
self.write(line)
3742

3843
def flush(self):
3944
flush()
4045

41-
sys.stdout = GdbOutputFile()
42-
43-
class GdbOutputErrorFile:
44-
def close(self):
45-
# Do nothing.
46-
return None
46+
class GdbOutputFile (_GdbFile):
47+
def write(self, s):
48+
write(s, stream=STDOUT)
4749

48-
def isatty(self):
49-
return False
50+
sys.stdout = GdbOutputFile()
5051

52+
class GdbOutputErrorFile (_GdbFile):
5153
def write(self, s):
5254
write(s, stream=STDERR)
5355

54-
def writelines(self, iterable):
55-
for line in iterable:
56-
self.write(line)
57-
58-
def flush(self):
59-
flush()
60-
6156
sys.stderr = GdbOutputErrorFile()
6257

6358
# Default prompt hook does nothing.
@@ -107,7 +102,7 @@ def auto_load_packages():
107102
else:
108103
__import__(modname)
109104
except:
110-
print >> sys.stderr, traceback.format_exc()
105+
sys.stderr.write (traceback.format_exc() + "\n")
111106

112107
auto_load_packages()
113108

‎gdb/python/lib/gdb/command/explore.py‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
"""Implementation of the GDB 'explore' command using the GDB Python API."""
1818

1919
import gdb
20+
import sys
2021

22+
if sys.version_info[0] > 2:
23+
# Python 3 renamed raw_input to input
24+
raw_input = input
25+
2126
class Explorer(object):
2227
"""Internal class which invokes other explorers."""
2328

@@ -155,7 +160,7 @@ def return_to_parent_value():
155160
"""A utility function which prints that the current exploration session
156161
is returning to the parent value. Useful when exploring values.
157162
"""
158-
print "\nReturning to parent value...\n"
163+
print ("\nReturning to parent value...\n")
159164

160165
@staticmethod
161166
def return_to_parent_value_prompt():
@@ -170,7 +175,7 @@ def return_to_enclosing_type():
170175
"""A utility function which prints that the current exploration session
171176
is returning to the enclosing type. Useful when exploring types.
172177
"""
173-
print "\nReturning to enclosing type...\n"
178+
print ("\nReturning to enclosing type...\n")
174179

175180
@staticmethod
176181
def return_to_enclosing_type_prompt():
@@ -192,7 +197,7 @@ def explore_expr(expr, value, is_child):
192197
"""
193198
print ("'%s' is a scalar value of type '%s'." %
194199
(expr, value.type))
195-
print "%s = %s" % (expr, str(value))
200+
print ("%s = %s" % (expr, str(value)))
196201

197202
if is_child:
198203
Explorer.return_to_parent_value_prompt()
@@ -211,13 +216,13 @@ def explore_type(name, datatype, is_child):
211216
print ("%s is of an enumerated type '%s'." %
212217
(name, str(datatype)))
213218
else:
214-
print "'%s' is an enumerated type." % name
219+
print ("'%s' is an enumerated type." % name)
215220
else:
216221
if is_child:
217222
print ("%s is of a scalar type '%s'." %
218223
(name, str(datatype)))
219224
else:
220-
print "'%s' is a scalar type." % name
225+
print ("'%s' is a scalar type." % name)
221226

222227
if is_child:
223228
Explorer.return_to_enclosing_type_prompt()
@@ -268,7 +273,7 @@ def explore_expr(expr, value, is_child):
268273
try:
269274
str(element)
270275
except gdb.MemoryError:
271-
print "Cannot read value at index %d." % index
276+
print ("Cannot read value at index %d." % index)
272277
continue
273278
Explorer.explore_expr(element_expr, element, True)
274279
return False
@@ -338,7 +343,7 @@ def explore_expr(expr, value, is_child):
338343
element = value[index]
339344
str(element)
340345
except gdb.MemoryError:
341-
print "Cannot read value at index %d." % index
346+
print ("Cannot read value at index %d." % index)
342347
raw_input("Press enter to continue... ")
343348
return True
344349

@@ -352,7 +357,7 @@ def explore_type(name, datatype, is_child):
352357
See Explorer.explore_type for more information.
353358
"""
354359
target_type = datatype.target()
355-
print "%s is an array of '%s'." % (name, str(target_type))
360+
print ("%s is an array of '%s'." % (name, str(target_type)))
356361

357362
Explorer.explore_type("the array element of %s" % name, target_type,
358363
is_child)
@@ -371,9 +376,8 @@ def _print_fields(print_list):
371376
if max_field_name_length < len(pair[0]):
372377
max_field_name_length = len(pair[0])
373378

374-
format_str = " {0:>%d} = {1}" % max_field_name_length
375379
for pair in print_list:
376-
print format_str.format(pair[0], pair[1])
380+
print (" %*s = %s" % (max_field_name_length, pair[0], pair[1]))
377381

378382
@staticmethod
379383
def _get_real_field_count(fields):
@@ -447,7 +451,7 @@ def explore_expr(expr, value, is_child):
447451
print_list.append((field.name, literal_value))
448452

449453
CompoundExplorer._print_fields(print_list)
450-
print ""
454+
print ("")
451455

452456
if has_explorable_fields:
453457
choice = raw_input("Enter the field number of choice: ")
@@ -484,7 +488,7 @@ def explore_type(name, datatype, is_child):
484488
(name, type_desc, str(datatype)))
485489
Explorer.return_to_enclosing_type_prompt()
486490
else:
487-
print "'%s' is a %s with no fields." % (name, type_desc)
491+
print ("'%s' is a %s with no fields." % (name, type_desc))
488492
return False
489493

490494
if is_child:
@@ -515,7 +519,7 @@ def explore_type(name, datatype, is_child):
515519
current_choice = current_choice + 1
516520

517521
CompoundExplorer._print_fields(print_list)
518-
print ""
522+
print ("")
519523

520524
if len(choice_to_compound_field_map) > 0:
521525
choice = raw_input("Enter the field number of choice: ")
@@ -741,7 +745,7 @@ def invoke(self, arg_str, from_tty):
741745

742746
value = ExploreUtils.get_value_from_str(arg_str)
743747
if value is not None:
744-
print "'%s' is of type '%s'." % (arg_str, str(value.type))
748+
print ("'%s' is of type '%s'." % (arg_str, str(value.type)))
745749
Explorer.explore_type(str(value.type), value.type, False)
746750

747751
raise gdb.GdbError(("'%s' is not a type or value in the current "

‎gdb/python/lib/gdb/command/pretty_printers.py‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,17 @@ def list_pretty_printers(self, pretty_printers, name_re, subname_re):
124124
"""Print a list of pretty-printers."""
125125
# A potential enhancement is to provide an option to list printers in
126126
# "lookup order" (i.e. unsorted).
127-
sorted_pretty_printers = copy.copy(pretty_printers)
128-
sorted_pretty_printers.sort(lambda x, y:
129-
cmp(self.printer_name(x),
130-
self.printer_name(y)))
127+
sorted_pretty_printers = sorted (copy.copy(pretty_printers),
128+
key = self.printer_name)
131129
for printer in sorted_pretty_printers:
132130
name = self.printer_name(printer)
133131
enabled = self.enabled_string(printer)
134132
if name_re.match(name):
135-
print " %s%s" % (name, enabled)
133+
print (" %s%s" % (name, enabled))
136134
if (hasattr(printer, "subprinters") and
137135
printer.subprinters is not None):
138-
sorted_subprinters = copy.copy(printer.subprinters)
139-
sorted_subprinters.sort(lambda x, y:
140-
cmp(self.printer_name(x),
141-
self.printer_name(y)))
136+
sorted_subprinters = sorted (copy.copy(printer.subprinters),
137+
key = self.printer_name)
142138
for subprinter in sorted_subprinters:
143139
if (not subname_re or
144140
subname_re.match(subprinter.name)):
@@ -150,7 +146,7 @@ def invoke1(self, title, printer_list,
150146
obj_name_to_match, object_re, name_re, subname_re):
151147
"""Subroutine of invoke to simplify it."""
152148
if printer_list and object_re.match(obj_name_to_match):
153-
print title
149+
print (title)
154150
self.list_pretty_printers(printer_list, name_re, subname_re)
155151

156152
def invoke(self, arg, from_tty):
@@ -219,7 +215,7 @@ def show_pretty_printer_enabled_summary():
219215
We count subprinters individually.
220216
"""
221217
(enabled_count, total_count) = count_all_enabled_printers()
222-
print "%d of %d printers enabled" % (enabled_count, total_count)
218+
print ("%d of %d printers enabled" % (enabled_count, total_count))
223219

224220

225221
def do_enable_pretty_printer_1 (pretty_printers, name_re, subname_re, flag):
@@ -301,7 +297,7 @@ def do_enable_pretty_printer (arg, flag):
301297
state = "enabled"
302298
else:
303299
state = "disabled"
304-
print "%d %s %s" % (total, pluralize("printer", total), state)
300+
print ("%d %s %s" % (total, pluralize("printer", total), state))
305301

306302
# Print the total list of printers currently enabled/disabled.
307303
# This is to further assist the user in determining whether the result

0 commit comments

Comments
 (0)