41
41
The module also extends gdb with some python-specific commands.
42
42
'''
43
43
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
-
49
44
import gdb
50
45
import os
51
46
import locale
52
47
import sys
53
48
54
- if sys .version_info [0 ] >= 3 :
55
- unichr = chr
56
- xrange = range
57
- long = int
58
49
59
50
# Look up the gdb.Type for some standard types:
60
51
# Those need to be refreshed as types (pointer sizes) may change when
@@ -80,9 +71,6 @@ def _sizeof_void_p():
80
71
return gdb .lookup_type ('void' ).pointer ().sizeof
81
72
82
73
83
- # value computed later, see PyUnicodeObjectPtr.proxy()
84
- _is_pep393 = None
85
-
86
74
Py_TPFLAGS_MANAGED_DICT = (1 << 4 )
87
75
Py_TPFLAGS_HEAPTYPE = (1 << 9 )
88
76
Py_TPFLAGS_LONG_SUBCLASS = (1 << 24 )
@@ -119,19 +107,7 @@ def safety_limit(val):
119
107
def safe_range (val ):
120
108
# As per range, but don't trust the value too much: cap it to a safety
121
109
# 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 )))
135
111
136
112
try :
137
113
os_fsencode = os .fsencode
@@ -258,7 +234,7 @@ def type(self):
258
234
return PyTypeObjectPtr (self .field ('ob_type' ))
259
235
260
236
def is_null (self ):
261
- return 0 == long (self ._gdbval )
237
+ return 0 == int (self ._gdbval )
262
238
263
239
def is_optimized_out (self ):
264
240
'''
@@ -320,7 +296,7 @@ def __repr__(self):
320
296
return '<%s at remote 0x%x>' % (self .tp_name , self .address )
321
297
322
298
return FakeRepr (self .safe_tp_name (),
323
- long (self ._gdbval ))
299
+ int (self ._gdbval ))
324
300
325
301
def write_repr (self , out , visited ):
326
302
'''
@@ -420,7 +396,7 @@ def get_gdb_type(cls):
420
396
return gdb .lookup_type (cls ._typename ).pointer ()
421
397
422
398
def as_address (self ):
423
- return long (self ._gdbval )
399
+ return int (self ._gdbval )
424
400
425
401
class PyVarObjectPtr (PyObjectPtr ):
426
402
_typename = 'PyVarObject'
@@ -529,7 +505,7 @@ def get_keys_values(self):
529
505
PyDictValuesPtrPtr = gdb .lookup_type ("PyDictValues" ).pointer ().pointer ()
530
506
valuesptr = self ._gdbval .cast (PyDictValuesPtrPtr ) - 4
531
507
values = valuesptr .dereference ()
532
- if long (values ) == 0 :
508
+ if int (values ) == 0 :
533
509
return None
534
510
values = values ['values' ]
535
511
return PyKeysValuesPair (self .get_cached_keys (), values )
@@ -562,7 +538,7 @@ def proxyval(self, visited):
562
538
tp_name = self .safe_tp_name ()
563
539
564
540
# Class:
565
- return InstanceProxy (tp_name , attr_dict , long (self ._gdbval ))
541
+ return InstanceProxy (tp_name , attr_dict , int (self ._gdbval ))
566
542
567
543
def write_repr (self , out , visited ):
568
544
# Guard against infinite loops:
@@ -737,7 +713,7 @@ def iteritems(self):
737
713
'''
738
714
keys = self .field ('ma_keys' )
739
715
values = self .field ('ma_values' )
740
- has_values = long (values )
716
+ has_values = int (values )
741
717
if has_values :
742
718
values = values ['values' ]
743
719
if has_values :
@@ -862,7 +838,7 @@ def proxyval(self, visited):
862
838
#define PyLong_SHIFT 30
863
839
#define PyLong_SHIFT 15
864
840
'''
865
- ob_size = long (self .field ('ob_size' ))
841
+ ob_size = int (self .field ('ob_size' ))
866
842
if ob_size == 0 :
867
843
return 0
868
844
@@ -873,7 +849,7 @@ def proxyval(self, visited):
873
849
else :
874
850
SHIFT = 30
875
851
876
- digits = [long (ob_digit [i ]) * 2 ** (SHIFT * i )
852
+ digits = [int (ob_digit [i ]) * 2 ** (SHIFT * i )
877
853
for i in safe_range (abs (ob_size ))]
878
854
result = sum (digits )
879
855
if ob_size < 0 :
@@ -1124,7 +1100,7 @@ def current_line(self):
1124
1100
1125
1101
filename = self .filename ()
1126
1102
try :
1127
- with open (os_fsencode (filename ), 'r' ) as fp :
1103
+ with open (os_fsencode (filename ), 'r' , encoding = "utf-8" ) as fp :
1128
1104
lines = fp .readlines ()
1129
1105
except IOError :
1130
1106
return None
@@ -1159,7 +1135,7 @@ def write_repr(self, out, visited):
1159
1135
out .write (')' )
1160
1136
1161
1137
def as_address (self ):
1162
- return long (self ._gdbval )
1138
+ return int (self ._gdbval )
1163
1139
1164
1140
def print_traceback (self ):
1165
1141
if self .is_optimized_out ():
@@ -1340,18 +1316,6 @@ def _unichr_is_printable(char):
1340
1316
import unicodedata
1341
1317
return unicodedata .category (char ) not in ("C" , "Z" )
1342
1318
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
-
1355
1319
1356
1320
class PyUnicodeObjectPtr (PyObjectPtr ):
1357
1321
_typename = 'PyUnicodeObject'
@@ -1361,42 +1325,31 @@ def char_width(self):
1361
1325
return _type_Py_UNICODE .sizeof
1362
1326
1363
1327
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' ]
1395
1338
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' ])
<
F438
tr class="diff-line-row">
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 ())
1400
1353
1401
1354
# Gather a list of ints from the Py_UNICODE array; these are either
1402
1355
# UCS-1, UCS-2 or UCS-4 code points:
@@ -1426,10 +1379,7 @@ def proxyval(self, visited):
1426
1379
1427
1380
# Convert the int code points to unicode characters, and generate a
1428
1381
# 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 ))
1433
1383
return result
1434
1384
1435
1385
def write_repr (self , out , visited ):
@@ -1478,19 +1428,8 @@ def write_repr(self, out, visited):
1478
1428
else :
1479
1429
ucs = ch
1480
1430
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 ()
1494
1433
if printable :
1495
1434
try :
1496
1435
ucs .encode (ENCODING )
@@ -1559,7 +1498,7 @@ def safe_tp_name(self):
1559
1498
1560
1499
def safe_self_addresss (self ):
1561
1500
try :
1562
- address = long (self .field ('self' ))
1501
+ address = int (self .field ('self' ))
1563
1502
return '%#x' % address
1564
1503
except (NullPyObjectPtr , RuntimeError ):
1565
1504
return '<failed to get self address>'
@@ -1852,7 +1791,7 @@ def print_summary(self):
1852
1791
while True :
1853
1792
if interp_frame :
1854
1793
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 ))
1856
1795
if not interp_frame .is_optimized_out ():
1857
1796
line = interp_frame .current_line ()
1858
1797
if line is not None :
@@ -1952,7 +1891,7 @@ def invoke(self, args, from_tty):
1952
1891
start = 1
1953
1892
1954
1893
try :
1955
- f = open (os_fsencode (filename ), 'r' )
1894
+ f = open (os_fsencode (filename ), 'r' , encoding = "utf-8" )
1956
1895
except IOError as err :
1957
1896
sys .stdout .write ('Unable to open %s: %s\n '
1958
1897
% (<
3A0E
span class=pl-s1>filename, err ))
0 commit comments