Skip to content

Commit 0316063

Browse files
authored
gdb libpython.py: Remove compatibility code (#105739)
Remove compatibility code for Python 2 and early Python 3 versions. * Remove os_fsencode() reimplementation: use os.fsencode() directly. os.fsencode() was added to Python 3.2. * Remove references to Python 2 and "Python 3": just say "Python". * Remove outdated u'' string format: use '' instead.
1 parent e5d45b7 commit 0316063

File tree

1 file changed

+22
-47
lines changed

1 file changed

+22
-47
lines changed

Tools/gdb/libpython.py

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -111,26 +111,6 @@ def safe_range(val):
111111
# threshold in case the data was corrupted
112112
return range(safety_limit(int(val)))
113113

114-
try:
115-
os_fsencode = os.fsencode
116-
except AttributeError:
117-
def os_fsencode(filename):
118-
if not isinstance(filename, unicode):
119-
return filename
120-
encoding = sys.getfilesystemencoding()
121-
if encoding == 'mbcs':
122-
# mbcs doesn't support surrogateescape
123-
return filename.encode(encoding)
124-
encoded = []
125-
for char in filename:
126-
# surrogateescape error handler
127-
if 0xDC80 <= ord(char) <= 0xDCFF:
128-
byte = chr(ord(char) - 0xDC00)
129-
else:
130-
byte = char.encode(encoding)
131-
encoded.append(byte)
132-
return ''.join(encoded)
133-
134114
class StringTruncated(RuntimeError):
135115
pass
136116

@@ -174,16 +154,12 @@ def __init__(self, gdbval, cast_to=None):
174154

175155
def field(self, name):
176156
'''
177-
Get the gdb.Value for the given field within the PyObject, coping with
178-
some python 2 versus python 3 differences.
157+
Get the gdb.Value for the given field within the PyObject.
179158
180159
Various libpython types are defined using the "PyObject_HEAD" and
181160
"PyObject_VAR_HEAD" macros.
182161
183-
In Python 2, this these are defined so that "ob_type" and (for a var
184-
object) "ob_size" are fields of the type in question.
185-
186-
In Python 3, this is defined as an embedded PyVarObject type thus:
162+
In Python, this is defined as an embedded PyVarObject type thus:
187163
PyVarObject ob_base;
188164
so that the "ob_size" field is located insize the "ob_base" field, and
189165
the "ob_type" is most easily accessed by casting back to a (PyObject*).
@@ -204,8 +180,7 @@ def field(self, name):
204180

205181
def pyop_field(self, name):
206182
'''
207-
Get a PyObjectPtr for the given PyObject* field within this PyObject,
208-
coping with some python 2 versus python 3 differences.
183+
Get a PyObjectPtr for the given PyObject* field within this PyObject.
209184
'''
210185
return PyObjectPtr.from_pyobject_ptr(self.field(name))
211186

@@ -924,7 +899,7 @@ def proxyval(self, visited):
924899
return result
925900

926901
def write_repr(self, out, visited):
927-
# Write this out as a Python 3 int literal, i.e. without the "L" suffix
902+
# Write this out as a Python int literal
928903
proxy = self.proxyval(visited)
929904
out.write("%s" % proxy)
930905

@@ -1170,7 +1145,7 @@ def current_line(self):
11701145

11711146
filename = self.filename()
11721147
try:
1173-
with open(os_fsencode(filename), 'r', encoding="utf-8") as fp:
1148+
with open(os.fsencode(filename), 'r', encoding="utf-8") as fp:
11741149
lines = fp.readlines()
11751150
except IOError:
11761151
return None
@@ -1263,7 +1238,7 @@ def proxyval(self, visited):
12631238
return set(members)
12641239

12651240
def write_repr(self, out, visited):
1266-
# Emulate Python 3's set_repr
1241+
# Emulate Python's set_repr
12671242
tp_name = self.safe_tp_name()
12681243

12691244
# Guard against infinite loops:
@@ -1272,13 +1247,13 @@ def write_repr(self, out, visited):
12721247
return
12731248
visited.add(self.as_address())
12741249

1275-
# Python 3's set_repr special-cases the empty set:
1250+
# Python's set_repr special-cases the empty set:
12761251
if not self.field('used'):
12771252
out.write(tp_name)
12781253
out.write('()')
12791254
return
12801255

1281-
# Python 3 uses {} for set literals:
1256+
# Python uses {} for set literals:
12821257
if tp_name != 'set':
12831258
out.write(tp_name)
12841259
out.write('(')
@@ -1309,13 +1284,13 @@ def proxyval(self, visited):
13091284
return str(self)
13101285

13111286
def write_repr(self, out, visited):
1312-
# Write this out as a Python 3 bytes literal, i.e. with a "b" prefix
1287+
# Write this out as a Python bytes literal, i.e. with a "b" prefix
13131288

1314-
# Get a PyStringObject* within the Python 2 gdb process:
1289+
# Get a PyStringObject* within the Python gdb process:
13151290
proxy = self.proxyval(visited)
13161291

1317-
# Transliteration of Python 3's Objects/bytesobject.c:PyBytes_Repr
1318-
# to Python 2 code:
1292+
# Transliteration of Python's Objects/bytesobject.c:PyBytes_Repr
1293+
# to Python code:
13191294
quote = "'"
13201295
if "'" in proxy and not '"' in proxy:
13211296
quote = '"'
@@ -1380,7 +1355,7 @@ class PyTypeObjectPtr(PyObjectPtr):
13801355

13811356

13821357
def _unichr_is_printable(char):
1383-
# Logic adapted from Python 3's Tools/unicode/makeunicodedata.py
1358+
# Logic adapted from Python's Tools/unicode/makeunicodedata.py
13841359
if char == u" ":
13851360
return True
13861361
import unicodedata
@@ -1416,17 +1391,17 @@ def proxyval(self, visited):
14161391

14171392
# Convert the int code points to unicode characters, and generate a
14181393
# local unicode instance.
1419-
result = u''.join(map(chr, code_points))
1394+
result = ''.join(map(chr, code_points))
14201395
return result
14211396

14221397
def write_repr(self, out, visited):
1423-
# Write this out as a Python 3 str literal, i.e. without a "u" prefix
1398+
# Write this out as a Python str literal
14241399

1425-
# Get a PyUnicodeObject* within the Python 2 gdb process:
1400+
# Get a PyUnicodeObject* within the Python gdb process:
14261401
proxy = self.proxyval(visited)
14271402

1428-
# Transliteration of Python 3's Object/unicodeobject.c:unicode_repr
1429-
# to Python 2:
1403+
# Transliteration of Python's Object/unicodeobject.c:unicode_repr
1404+
# to Python:
14301405
if "'" in proxy and '"' not in proxy:
14311406
quote = '"'
14321407
else:
@@ -1477,7 +1452,7 @@ def write_repr(self, out, visited):
14771452
# (categories Z* and C* except ASCII space)
14781453
if not printable:
14791454
if ch2 is not None:
1480-
# Match Python 3's representation of non-printable
1455+
# Match Python's representation of non-printable
14811456
# wide characters.
14821457
code = (ord(ch) & 0x03FF) << 10
14831458
code |= ord(ch2) & 0x03FF
@@ -1608,8 +1583,8 @@ def pretty_printer_lookup(gdbval):
16081583
if the code is autoloaded by gdb when visiting libpython.so, provided
16091584
that this python file is installed to the same path as the library (or its
16101585
.debug file) plus a "-gdb.py" suffix, e.g:
1611-
/usr/lib/libpython2.6.so.1.0-gdb.py
1612-
/usr/lib/debug/usr/lib/libpython2.6.so.1.0.debug-gdb.py
1586+
/usr/lib/libpython3.12.so.1.0-gdb.py
1587+
/usr/lib/debug/usr/lib/libpython3.12.so.1.0.debug-gdb.py
16131588
"""
16141589
def register (obj):
16151590
if obj is None:
@@ -1928,7 +1903,7 @@ def invoke(self, args, from_tty):
19281903
start = 1
19291904

19301905
try:
1931-
f = open(os_fsencode(filename), 'r', encoding="utf-8")
1906+
f = open(os.fsencode(filename), 'r', encoding="utf-8")
19321907
except IOError as err:
19331908
sys.stdout.write('Unable to open %s: %s\n'
19341909
% (filename, err))

0 commit comments

Comments
 (0)