Skip to content

Commit 96cdc6c

Browse files
committed
WIP: use named attributes
inspect: keep tuple API for 3 namedtuple
1 parent 2bccd2c commit 96cdc6c

14 files changed

Lines changed: 34 additions & 20 deletions

File tree

Lib/collections/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def __ror__(self, other):
361361
def namedtuple(typename, field_names, *, rename=False, defaults=None, module=None, deprecate_tuple_api=True):
362362
"""Returns a new subclass of tuple with named fields.
363363
364-
>>> Point = namedtuple('Point', ['x', 'y'])
364+
>>> Point = namedtuple('Point', ['x', 'y'], deprecate_tuple_api=False)
365365
>>> Point.__doc__ # docstring for the new class
366366
'Point(x, y)'
367367
>>> p = Point(11, y=22) # instantiate with positional args or keywords

Lib/inspect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ def getmembers_static(object, predicate=None):
540540
"""
541541
return _getmembers(object, predicate, getattr_static)
542542

543-
Attribute = namedtuple('Attribute', 'name kind defining_class object')
543+
Attribute = namedtuple('Attribute', 'name kind defining_class object', deprecate_tuple_api=False)
544544

545545
def classify_class_attrs(cls):
546546
"""Return list of attribute-descriptor tuples.
@@ -1643,7 +1643,7 @@ def getlineno(frame):
16431643
"""Get the line number from a frame object, allowing for optimization."""
16441644
return frame.f_lineno
16451645

1646-
_FrameInfo = namedtuple('_FrameInfo', ('frame',) + Traceback._fields)
1646+
_FrameInfo = namedtuple('_FrameInfo', ('frame',) + Traceback._fields, deprecate_tuple_api=False)
16471647
class FrameInfo(_FrameInfo):
16481648
def __new__(cls, frame, filename, lineno, function, code_context, index, *, positions=None):
16491649
instance = super().__new__(cls, frame, filename, lineno, function, code_context, index)

Lib/pydoc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2022,7 +2022,7 @@ def output(self):
20222022
return self._output or sys.stdout
20232023

20242024
def __repr__(self):
2025-
if inspect.stack()[1][3] == '?':
2025+
if inspect.stack()[1].function == '?':
20262026
self()
20272027
return ''
20282028
return '<%s.%s instance>' % (self.__class__.__module__,

Lib/tarfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,12 +2837,12 @@ def chown(self, tarinfo, targetpath, numeric_owner):
28372837
if not numeric_owner:
28382838
try:
28392839
if grp and tarinfo.gname:
2840-
g = grp.getgrnam(tarinfo.gname)[2]
2840+
g = grp.getgrnam(tarinfo.gname).gr_gid
28412841
except KeyError:
28422842
pass
28432843
try:
28442844
if pwd and tarinfo.uname:
2845-
u = pwd.getpwnam(tarinfo.uname)[2]
2845+
u = pwd.getpwnam(tarinfo.uname).pw_uid
28462846
except KeyError:
28472847
pass
28482848
if g is None:

Lib/test/test_calendar.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,8 @@ def run_cli_ok(self, *args):
11081108
return stdout.buffer.read()
11091109

11101110
def run_cmd_ok(self, *args):
1111-
return assert_python_ok('-m', 'calendar', *args)[1]
1111+
proc = assert_python_ok('-m', 'calendar', *args)
1112+
return proc.out
11121113

11131114
def assertCLIFails(self, *args):
11141115
with self.captured_stderr_with_buffer() as stderr:

Lib/test/test_collections.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def __ror__(self, other):
319319
class TestNamedTuple(unittest.TestCase):
320320

321321
def test_factory(self):
322-
Point = namedtuple('Point', 'x y')
322+
Point = namedtuple('Point', 'x y', deprecate_tuple_api=False)
323323
self.assertEqual(Point.__name__, 'Point')
324324
self.assertEqual(Point.__slots__, ())
325325
self.assertEqual(Point.__module__, __name__)
@@ -398,7 +398,7 @@ def test_defaults(self):
398398
self.assertEqual(Point(), (10, 20))
399399

400400
def test_readonly(self):
401-
Point = namedtuple('Point', 'x y')
401+
Point = namedtuple('Point', 'x y', deprecate_tuple_api=False)
402402
p = Point(11, 22)
403403
with self.assertRaises(AttributeError):
404404
p.x = 33
@@ -504,7 +504,7 @@ def test_instance(self):
504504
self.assertEqual(repr(p), 'Point(x=11, y=22)')
505505

506506
def test_tupleness(self):
507-
Point = namedtuple('Point', 'x y')
507+
Point = namedtuple('Point', 'x y', deprecate_tuple_api=False)
508508
p = Point(11, 22)
509509

510510
self.assertIsInstance(p, tuple)

Lib/test/test_profiling/test_sampling_profiler/mocks.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
from collections import namedtuple
44

55
# Matches the C structseq LocationInfo from _remote_debugging
6-
LocationInfo = namedtuple('LocationInfo', ['lineno', 'end_lineno', 'col_offset', 'end_col_offset'])
6+
LocationInfo = namedtuple('LocationInfo',
7+
['lineno', 'end_lineno', 'col_offset', 'end_col_offset'],
8+
deprecate_tuple_api=False)
79

810

911
class MockFrameInfo:

Lib/test/test_profiling/test_sampling_profiler/test_dump.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@
3737
StructseqInterpreterInfo = namedtuple(
3838
"StructseqInterpreterInfo",
3939
["interpreter_id", "threads"],
40+
deprecate_tuple_api=False,
4041
)
4142
StructseqThreadInfo = namedtuple(
4243
"StructseqThreadInfo",
4344
["thread_id", "status", "frame_info"],
45+
deprecate_tuple_api=False,
4446
)
4547
StructseqFrameInfo = namedtuple(
4648
"StructseqFrameInfo",
4749
["filename", "location", "funcname", "opcode"],
50+
deprecate_tuple_api=False,
4851
)
4952

5053

Lib/test/test_script_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class TestScriptHelper(unittest.TestCase):
1212

1313
def test_assert_python_ok(self):
1414
t = script_helper.assert_python_ok('-c', 'import sys; sys.exit(0)')
15-
self.assertEqual(0, t[0], 'return code was not 0')
15+
self.assertEqual(0, t.rc, 'return code was not 0')
1616

1717
def test_assert_python_failure(self):
1818
# I didn't import the sys module so this child will fail.

Lib/test/test_sys.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,9 @@ def test_attributes(self):
657657
self.assertIsInstance(sys.int_info.str_digits_check_threshold, int)
658658
self.assertIsInstance(sys.hexversion, int)
659659

660-
self.assertEqual(len(sys.hash_info), 9)
660+
with warnings.catch_warnings(category=DeprecationWarning):
661+
warnings.simplefilter("ignore", category=DeprecationWarning)
662+
self.assertEqual(len(sys.hash_info), 9)
661663
self.assertLess(sys.hash_info.modulus, 2**sys.hash_info.width)
662664
# sys.hash_info.modulus should be a prime; we do a quick
663665
# probable primality test (doesn't exclude the possibility of
@@ -879,7 +881,9 @@ def test_sys_flags_indexable_attributes(self):
879881
self.assertEqual(sys.flags[attr_idx], attr_value,
880882
msg=f"sys.flags .{attr} vs [{attr_idx}]")
881883
self.assertTrue(repr(sys.flags))
882-
self.assertEqual(len(sys.flags), 18, msg="Do not increase, see GH-122575")
884+
with warnings.catch_warnings(category=DeprecationWarning):
885+
warnings.simplefilter("ignore", category=DeprecationWarning)
886+
self.assertEqual(len(sys.flags), 18, msg="Do not increase, see GH-122575")
883887

884888
self.assertIn(sys.flags.utf8_mode, {0, 1, 2})
885889

@@ -1941,7 +1945,9 @@ def test_pythontypes(self):
19411945
# per GH-122575 would be nice...
19421946
# Q: What is the actual point of this sys.flags C size derived from PyStructSequence_Field array assertion?
19431947
non_sequence_fields = 4
1944-
check(sys.flags, vsize('') + self.P + self.P * (non_sequence_fields + len(sys.flags)))
1948+
with warnings.catch_warnings(category=DeprecationWarning):
1949+
warnings.simplefilter("ignore", category=DeprecationWarning)
1950+
check(sys.flags, vsize('') + self.P + self.P * (non_sequence_fields + len(sys.flags)))
19451951

19461952
def test_asyncgen_hooks(self):
19471953
old = sys.get_asyncgen_hooks()

0 commit comments

Comments
 (0)