MCPcopy Index your code
hub / github.com/python/cpython / write_repr

Method write_repr

Tools/gdb/libpython.py:1504–1599  ·  view source on GitHub ↗
(self, out, visited)

Source from the content-addressed store, hash-verified

1502 return result
1503
1504 def write_repr(self, out, visited):
1505 # Write this out as a Python str literal
1506
1507 # Get a PyUnicodeObject* within the Python gdb process:
1508 proxy = self.proxyval(visited)
1509
1510 # Transliteration of Python's Object/unicodeobject.c:unicode_repr
1511 # to Python:
1512 if "'" in proxy and '"' not in proxy:
1513 quote = '"'
1514 else:
1515 quote = "'"
1516 out.write(quote)
1517
1518 i = 0
1519 while i < len(proxy):
1520 ch = proxy[i]
1521 i += 1
1522
1523 # Escape quotes and backslashes
1524 if ch == quote or ch == '\\':
1525 out.write('\\')
1526 out.write(ch)
1527
1528 # Map special whitespace to '\t', \n', '\r'
1529 elif ch == '\t':
1530 out.write('\\t')
1531 elif ch == '\n':
1532 out.write('\\n')
1533 elif ch == '\r':
1534 out.write('\\r')
1535
1536 # Map non-printable US ASCII to '\xhh' */
1537 elif ch < ' ' or ord(ch) == 0x7F:
1538 out.write('\\x')
1539 out.write(hexdigits[(ord(ch) >> 4) & 0x000F])
1540 out.write(hexdigits[ord(ch) & 0x000F])
1541
1542 # Copy ASCII characters as-is
1543 elif ord(ch) < 0x7F:
1544 out.write(ch)
1545
1546 # Non-ASCII characters
1547 else:
1548 ucs = ch
1549 ch2 = None
1550
1551 printable = ucs.isprintable()
1552 if printable:
1553 try:
1554 ucs.encode(ENCODING)
1555 except UnicodeEncodeError:
1556 printable = False
1557
1558 # Map Unicode whitespace and control characters
1559 # (categories Z* and C* except ASCII space)
1560 if not printable:
1561 if ch2 is not None:

Callers

nothing calls this directly

Calls 4

proxyvalMethod · 0.95
isprintableMethod · 0.80
writeMethod · 0.45
encodeMethod · 0.45

Tested by

no test coverage detected