bpo-28638: Optimize namedtuple() creation time by minimizing use of e… · python/cpython@8b57d73 (original) (raw)
`@@ -194,7 +194,6 @@ def test_factory(self):
`
194
194
`self.assertEqual(Point.module, name)
`
195
195
`self.assertEqual(Point.getitem, tuple.getitem)
`
196
196
`self.assertEqual(Point._fields, ('x', 'y'))
`
197
``
`-
self.assertIn('class Point(tuple)', Point._source)
`
198
197
``
199
198
`self.assertRaises(ValueError, namedtuple, 'abc%', 'efg ghi') # type has non-alpha char
`
200
199
`self.assertRaises(ValueError, namedtuple, 'class', 'efg ghi') # type has keyword
`
`@@ -366,11 +365,37 @@ def test_name_conflicts(self):
`
366
365
`newt = t._replace(itemgetter=10, property=20, self=30, cls=40, tuple=50)
`
367
366
`self.assertEqual(newt, (10,20,30,40,50))
`
368
367
``
369
``
`-
Broader test of all interesting names in a template
`
370
``
`-
with support.captured_stdout() as template:
`
371
``
`-
T = namedtuple('T', 'x', verbose=True)
`
372
``
`-
words = set(re.findall('[A-Za-z]+', template.getvalue()))
`
373
``
`-
words -= set(keyword.kwlist)
`
``
368
`+
Broader test of all interesting names taken from the code, old
`
``
369
`+
template, and an example
`
``
370
`+
words = {'Alias', 'At', 'AttributeError', 'Build', 'Bypass', 'Create',
`
``
371
`+
'Encountered', 'Expected', 'Field', 'For', 'Got', 'Helper',
`
``
372
`+
'IronPython', 'Jython', 'KeyError', 'Make', 'Modify', 'Note',
`
``
373
`+
'OrderedDict', 'Point', 'Return', 'Returns', 'Type', 'TypeError',
`
``
374
`+
'Used', 'Validate', 'ValueError', 'Variables', 'a', 'accessible', 'add',
`
``
375
`+
'added', 'all', 'also', 'an', 'arg_list', 'args', 'arguments',
`
``
376
`+
'automatically', 'be', 'build', 'builtins', 'but', 'by', 'cannot',
`
``
377
`+
'class_namespace', 'classmethod', 'cls', 'collections', 'convert',
`
``
378
`+
'copy', 'created', 'creation', 'd', 'debugging', 'defined', 'dict',
`
``
379
`+
'dictionary', 'doc', 'docstring', 'docstrings', 'duplicate', 'effect',
`
``
380
`+
'either', 'enumerate', 'environments', 'error', 'example', 'exec', 'f',
`
``
381
`+
'f_globals', 'field', 'field_names', 'fields', 'formatted', 'frame',
`
``
382
`+
'function', 'functions', 'generate', 'get', 'getter', 'got', 'greater',
`
``
383
`+
'has', 'help', 'identifiers', 'index', 'indexable', 'instance',
`
``
384
`+
'instantiate', 'interning', 'introspection', 'isidentifier',
`
``
385
`+
'isinstance', 'itemgetter', 'iterable', 'join', 'keyword', 'keywords',
`
``
386
`+
'kwds', 'len', 'like', 'list', 'map', 'maps', 'message', 'metadata',
`
``
387
`+
'method', 'methods', 'module', 'module_name', 'must', 'name', 'named',
`
``
388
`+
'namedtuple', 'namedtuple_', 'names', 'namespace', 'needs', 'new',
`
``
389
`+
'nicely', 'num_fields', 'number', 'object', 'of', 'operator', 'option',
`
``
390
`+
'p', 'particular', 'pickle', 'pickling', 'plain', 'pop', 'positional',
`
``
391
`+
'property', 'r', 'regular', 'rename', 'replace', 'replacing', 'repr',
`
``
392
`+
'repr_fmt', 'representation', 'result', 'reuse_itemgetter', 's', 'seen',
`
``
393
`+
'self', 'sequence', 'set', 'side', 'specified', 'split', 'start',
`
``
394
`+
'startswith', 'step', 'str', 'string', 'strings', 'subclass', 'sys',
`
``
395
`+
'targets', 'than', 'the', 'their', 'this', 'to', 'tuple', 'tuple_new',
`
``
396
`+
'type', 'typename', 'underscore', 'unexpected', 'unpack', 'up', 'use',
`
``
397
`+
'used', 'user', 'valid', 'values', 'variable', 'verbose', 'where',
`
``
398
`+
'which', 'work', 'x', 'y', 'z', 'zip'}
`
374
399
`T = namedtuple('T', words)
`
375
400
`# test new
`
376
401
`values = tuple(range(len(words)))
`
`@@ -396,30 +421,15 @@ def test_name_conflicts(self):
`
396
421
`self.assertEqual(t.getnewargs(), values)
`
397
422
``
398
423
`def test_repr(self):
`
399
``
`-
with support.captured_stdout() as template:
`
400
``
`-
A = namedtuple('A', 'x', verbose=True)
`
``
424
`+
A = namedtuple('A', 'x')
`
401
425
`self.assertEqual(repr(A(1)), 'A(x=1)')
`
402
426
`# repr should show the name of the subclass
`
403
427
`class B(A):
`
404
428
`pass
`
405
429
`self.assertEqual(repr(B(1)), 'B(x=1)')
`
406
430
``
407
``
`-
def test_source(self):
`
408
``
`-
verify that _source can be run through exec()
`
409
``
`-
tmp = namedtuple('NTColor', 'red green blue')
`
410
``
`-
globals().pop('NTColor', None) # remove artifacts from other tests
`
411
``
`-
exec(tmp._source, globals())
`
412
``
`-
self.assertIn('NTColor', globals())
`
413
``
`-
c = NTColor(10, 20, 30)
`
414
``
`-
self.assertEqual((c.red, c.green, c.blue), (10, 20, 30))
`
415
``
`-
self.assertEqual(NTColor._fields, ('red', 'green', 'blue'))
`
416
``
`-
globals().pop('NTColor', None) # clean-up after this test
`
417
``
-
418
431
`def test_keyword_only_arguments(self):
`
419
432
`# See issue 25628
`
420
``
`-
with support.captured_stdout() as template:
`
421
``
`-
NT = namedtuple('NT', ['x', 'y'], verbose=True)
`
422
``
`-
self.assertIn('class NT', NT._source)
`
423
433
`with self.assertRaises(TypeError):
`
424
434
`NT = namedtuple('NT', ['x', 'y'], True)
`
425
435
``