8000 Tools/gdb: Drop code to support Python 2. (GH-31717) · python/cpython@52f6ce3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 52f6ce3

Browse files
authored
Tools/gdb: Drop code to support Python 2. (GH-31717)
1 parent e3d348a commit 52f6ce3

File tree

1 file changed

+41
-102
lines changed

1 file changed

+41
-102
lines changed

Tools/gdb/libpython.py

Lines changed: 41 additions & 102 deletions
< F438 tr class="diff-line-row">
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,11 @@
4141
The module also extends gdb with some python-specific commands.
4242
'''
4343

44-
# NOTE: some gdbs are linked with Python 3, so this file should be dual-syntax
45-
# compatible (2.6+ and 3.0+). See #19308.
46-
47-
from __future__ import print_function
48-
4944
import gdb
5045
import os
5146
import locale
5247
import sys
5348

54-
if sys.version_info[0] >= 3:
55-
unichr = chr
56-
xrange = range
57-
long = int
5849

5950
# Look up the gdb.Type for some standard types:
6051
# Those need to be refreshed as types (pointer sizes) may change when
@@ -80,9 +71,6 @@ def _sizeof_void_p():
8071
return gdb.lookup_type('void').pointer().sizeof
8172

8273

83-
# value computed later, see PyUnicodeObjectPtr.proxy()
84-
_is_pep393 = None
85-
8674
Py_TPFLAGS_MANAGED_DICT = (1 << 4)
8775
Py_TPFLAGS_HEAPTYPE = (1 << 9)
8876
Py_TPFLAGS_LONG_SUBCLASS = (1 << 24)
@@ -119,19 +107,7 @@ def safety_limit(val):
119107
def safe_range(val):
120108
# As per range, but don't trust the value too much: cap it to a safety
121109
# threshold in case the data was corrupted
122-
return xrange(safety_limit(int(val)))
123-
124-
if sys.version_info[0] >= 3:
125-
def write_unicode(file, text):
126-
file.write(text)
127-
else:
128-
def write_unicode(file, text):
129-
# Write a byte or unicode string to file. Unicode strings are encoded to
130-
# ENCODING encoding with 'backslashreplace' error handler to avoid
131-
# UnicodeEncodeError.
132-
if isinstance(text, unicode):
133-
text = text.encode(ENCODING, 'backslashreplace')
134-
file.write(text)
110+
return range(safety_limit(int(val)))
135111

136112
try:
137113
os_fsencode = os.fsencode
@@ -258,7 +234,7 @@ def type(self):
258234
return PyTypeObjectPtr(self.field('ob_type'))
259235

260236
def is_null(self):
261-
return 0 == long(self._gdbval)
237+
return 0 == int(self._gdbval)
262238

263239
def is_optimized_out(self):
264240
'''
@@ -320,7 +296,7 @@ def __repr__(self):
320296
return '<%s at remote 0x%x>' % (self.tp_name, self.address)
321297

322298
return FakeRepr(self.safe_tp_name(),
323-
long(self._gdbval))
299+
int(self._gdbval))
324300

325301
def write_repr(self, out, visited):
326302
'''
@@ -420,7 +396,7 @@ def get_gdb_type(cls):
420396
return gdb.lookup_type(cls._typename).pointer()
421397

422398
def as_address(self):
423-
return long(self._gdbval)
399+
return int(self._gdbval)
424400

425401
class PyVarObjectPtr(PyObjectPtr):
426402
_typename = 'PyVarObject'
@@ -529,7 +505,7 @@ def get_keys_values(self):
529505
PyDictValuesPtrPtr = gdb.lookup_type("PyDictValues").pointer().pointer()
530506
valuesptr = self._gdbval.cast(PyDictValuesPtrPtr) - 4
531507
values = valuesptr.dereference()
532-
if long(values) == 0:
508+
if int(values) == 0:
533509
return None
534510
values = values['values']
535511
return PyKeysValuesPair(self.get_cached_keys(), values)
@@ -562,7 +538,7 @@ def proxyval(self, visited):
562538
tp_name = self.safe_tp_name()
563539

564540
# Class:
565-
return InstanceProxy(tp_name, attr_dict, long(self._gdbval))
541+
return InstanceProxy(tp_name, attr_dict, int(self._gdbval))
566542

567543
def write_repr(self, out, visited):
568544
# Guard against infinite loops:
@@ -737,7 +713,7 @@ def iteritems(self):
737713
'''
738714
keys = self.field('ma_keys')
739715
values = self.field('ma_values')
740-
has_values = long(values)
716+
has_values = int(values)
741717
if has_values:
742718
values = values['values']
743719
if has_values:
@@ -862,7 +838,7 @@ def proxyval(self, visited):
862838
#define PyLong_SHIFT 30
863839
#define PyLong_SHIFT 15
864840
'''
865-
ob_size = long(self.field('ob_size'))
841+
ob_size = int(self.field('ob_size'))
866842
if ob_size == 0:
867843
return 0
868844

@@ -873,7 +849,7 @@ def proxyval(self, visited):
873849
else:
874850
SHIFT = 30
875851

876-
digits = [long(ob_digit[i]) * 2**(SHIFT*i)
852+
digits = [int(ob_digit[i]) * 2**(SHIFT*i)
877853
for i in safe_range(abs(ob_size))]
878854
result = sum(digits)
879855
if ob_size < 0:
@@ -1124,7 +1100,7 @@ def current_line(self):
11241100

11251101
filename = self.filename()
11261102
try:
1127-
with open(os_fsencode(filename), 'r') as fp:
1103+
with open(os_fsencode(filename), 'r', encoding="utf-8") as fp:
11281104
lines = fp.readlines()
11291105
except IOError:
11301106
return None
@@ -1159,7 +1135,7 @@ def write_repr(self, out, visited):
11591135
out.write(')')
11601136

11611137
def as_address(self):
1162-
return long(self._gdbval)
1138+
return int(self._gdbval)
11631139

11641140
def print_traceback(self):
11651141
if self.is_optimized_out():
@@ -1340,18 +1316,6 @@ def _unichr_is_printable(char):
13401316
import unicodedata
13411317
return unicodedata.category(char) not in ("C", "Z")
13421318

1343-
if sys.maxunicode >= 0x10000:
1344-
_unichr = unichr
1345-
else:
1346-
# Needed for proper surrogate support if sizeof(Py_UNICODE) is 2 in gdb
1347-
def _unichr(x):
1348-
if x < 0x10000:
1349-
return unichr(x)
1350-
x -= 0x10000
1351-
ch1 = 0xD800 | (x >> 10)
1352-
ch2 = 0xDC00 | (x & 0x3FF)
1353-
return unichr(ch1) + unichr(ch2)
1354-
13551319

13561320
class PyUnicodeObjectPtr(PyObjectPtr):
13571321
_typename = 'PyUnicodeObject'
@@ -1361,42 +1325,31 @@ def char_width(self):
13611325
return _type_Py_UNICODE.sizeof
13621326

13631327
def proxyval(self, visited):
1364-
global _is_pep393
1365-
if _is_pep393 is None:
1366-
fields = gdb.lookup_type('PyUnicodeObject').fields()
1367-
_is_pep393 = 'data' in [f.name for f in fields]
1368-
if _is_pep393:
1369-
# Python 3.3 and newer
1370-
may_have_surrogates = False
1371-
compact = self.field('_base')
1372-
ascii = compact['_base']
1373-
state = ascii['state']
1374-
is_compact_ascii = (int(state['ascii']) and int(state['compact']))
1375-
if not int(state['ready']):
1376-
# string is not ready
1377-
field_length = long(compact['wstr_length'])
1378-
may_have_surrogates = True
1379-
field_str = ascii['wstr']
1380-
else:
1381-
field_length = long(ascii['length'])
1382-
if is_compact_ascii:
1383-
field_str = ascii.address + 1
1384-
elif int(state['compact']):
1385-
field_str = compact.address + 1
1386-
else:
1387-
field_str = self.field('data')['any']
1388-
repr_kind = int(state['kind'])
1389-
if repr_kind == 1:
1390-
field_str = field_str.cast(_type_unsigned_char_ptr())
1391-
elif repr_kind == 2:
1392-
field_str = field_str.cast(_type_unsigned_short_ptr())
1393-
elif repr_kind == 4:
1394-
field_str = field_str.cast(_type_unsigned_int_ptr())
1328+
may_have_surrogates = False
1329+
compact = self.field('_base')
1330+
ascii = compact['_base']
1331+
state = ascii['state']
1332+
is_compact_ascii = (int(state['ascii']) and int(state['compact']))
1333+
if not int(state['ready']):
1334+
# string is not ready
1335+
field_length = int(compact['wstr_length'])
1336+
may_have_surrogates = True
1337+
field_str = ascii['wstr']
13951338
else:
1396-
# Python 3.2 and earlier
1397-
field_length = long(self.field('length'))
1398-
field_str = self.field('str')
1399-
may_have_surrogates = self.char_width() == 2
1339+
field_length = int(ascii['length'])
1340+
if is_compact_ascii:
1341+
field_str = ascii.address + 1
1342+
elif int(state['compact']):
1343+
field_str = compact.address + 1
1344+
else:
1345+
field_str = self.field('data')['any']
1346+
repr_kind = int(state['kind'])
1347+
if repr_kind == 1:
1348+
field_str = field_str.cast(_type_unsigned_char_ptr())
1349+
elif repr_kind == 2:
1350+
field_str = field_str.cast(_type_unsigned_short_ptr())
1351+
elif repr_kind == 4:
1352+
field_str = field_str.cast(_type_unsigned_int_ptr())
14001353

14011354
# Gather a list of ints from the Py_UNICODE array; these are either
14021355
# UCS-1, UCS-2 or UCS-4 code points:
@@ -1426,10 +1379,7 @@ def proxyval(self, visited):
14261379

14271380
# Convert the int code points to unicode characters, and generate a
14281381
# local unicode instance.
1429-
# This splits surrogate pairs if sizeof(Py_UNICODE) is 2 here (in gdb).
1430-
result = u''.join([
1431-
(_unichr(ucs) if ucs <= 0x10ffff else '\ufffd')
1432-
for ucs in Py_UNICODEs])
1382+
result = u''.join(map(chr, Py_UNICODEs))
14331383
return result
14341384

14351385
def write_repr(self, out, visited):
@@ -1478,19 +1428,8 @@ def write_repr(self, out, visited):
14781428
else:
14791429
ucs = ch
14801430
ch2 = None
1481-
if sys.maxunicode < 0x10000:
1482-
# If sizeof(Py_UNICODE) is 2 here (in gdb), join
1483-
# surrogate pairs before calling _unichr_is_printable.
1484-
if (i < len(proxy)
1485-
and 0xD800 <= ord(ch) < 0xDC00 \
1486-
and 0xDC00 <= ord(proxy[i]) <= 0xDFFF):
1487-
ch2 = proxy[i]
1488-
ucs = ch + ch2
1489-
i += 1
1490-
1491-
# Unfortuately, Python 2's unicode type doesn't seem
1492-
# to expose the "isprintable" method
1493-
printable = _unichr_is_printable(ucs)
1431+
1432+
printable = ucs.isprintable()
14941433
if printable:
14951434
try:
14961435
ucs.encode(ENCODING)
@@ -1559,7 +1498,7 @@ def safe_tp_name(self):
15591498

15601499
def safe_self_addresss(self):
15611500
try:
1562-
address = long(self.field('self'))
1501+
address = int(self.field('self'))
15631502
return '%#x' % address
15641503
except (NullPyObjectPtr, RuntimeError):
15651504
return '<failed to get self address>'
@@ -1852,7 +1791,7 @@ def print_summary(self):
18521791
while True:
18531792
if interp_frame:
18541793
line = interp_frame.get_truncated_repr(MAX_OUTPUT_LEN)
1855-
write_unicode(sys.stdout, '#%i %s\n' % (self.get_index(), line))
1794+
sys.stdout.write('#%i %s\n' % (self.get_index(), line))
18561795
if not interp_frame.is_optimized_out():
18571796
line = interp_frame.current_line()
18581797
if line is not None:
@@ -1952,7 +1891,7 @@ def invoke(self, args, from_tty):
19521891
start = 1
19531892

19541893
try:
1955-
f = open(os_fsencode(filename), 'r')
1894+
f = open(os_fsencode(filename), 'r', encoding="utf-8")
19561895
except IOError as err:
19571896
sys.stdout.write('Unable to open %s: %s\n'
19581897
% (< 3A0E span class=pl-s1>filename, err))

0 commit comments

Comments
 (0)
0