bpo-38187: Fix a refleak in Tools/c-analyzer. by ericsnowcurrently · Pull Request #16304 · python/cpython
VALID_ARGS = ( ('x/y/z/spam.c', 'func', 'eggs'), 'static', 'int', ) VALID_KWARGS = dict(zip(Variable._fields, VALID_ARGS)) VALID_EXPECTED = VALID_ARGS
def test_init_typical_global(self): static = Variable( id=ID( filename='x/y/z/spam.c', funcname=None, name='eggs', ), vartype='int', ) for storage in ('static', 'extern', 'implicit'): with self.subTest(storage): static = Variable( id=ID( filename='x/y/z/spam.c', funcname=None, name='eggs', ), storage=storage, vartype='int', )
self.assertEqual(static, ( ('x/y/z/spam.c', None, 'eggs'), 'int', )) self.assertEqual(static, ( ('x/y/z/spam.c', None, 'eggs'), storage, 'int', ))
def test_init_typical_local(self): static = Variable( id=ID( filename='x/y/z/spam.c', funcname='func', name='eggs', ), vartype='int', ) for storage in ('static', 'local'): with self.subTest(storage): static = Variable( id=ID( filename='x/y/z/spam.c', funcname='func', name='eggs', ), storage=storage, vartype='int', )
self.assertEqual(static, ( ('x/y/z/spam.c', 'func', 'eggs'), storage, 'int', ))
self.assertEqual(static, ( None, None, None, ))
id, vartype = static id, storage, vartype = static
values = (id, vartype) values = (id, storage, vartype) for value, expected in zip(values, self.VALID_EXPECTED): self.assertEqual(value, expected)
def test_fields(self): static = Variable(('a', 'b', 'z'), 'x') static = Variable(('a', 'b', 'z'), 'x', 'y')
self.assertEqual(static.id, ('a', 'b', 'z')) self.assertEqual(static.vartype, 'x') self.assertEqual(static.storage, 'x') self.assertEqual(static.vartype, 'y')
def test___getattr__(self): static = Variable(('a', 'b', 'z'), 'x') static = Variable(('a', 'b', 'z'), 'x', 'y')
self.assertEqual(static.filename, 'a') self.assertEqual(static.funcname, 'b') self.assertEqual(static.name, 'z')
def test_validate_typical(self): static = Variable( id=ID( filename='x/y/z/spam.c', funcname='func', name='eggs', ), vartype='int', ) validstorage = ('static', 'extern', 'implicit', 'local') self.assertEqual(set(validstorage), set(Variable.STORAGE))
for storage in validstorage: with self.subTest(storage): static = Variable( id=ID( filename='x/y/z/spam.c', funcname='func', name='eggs', ), storage=storage, vartype='int', )
static.validate() # This does not fail. static.validate() # This does not fail.
def test_validate_missing_field(self): for field in Variable._fields: with self.subTest(field): static = Variable(**self.VALID_KWARGS) static = static._replace(**{field: None})
with self.assertRaises(TypeError): static.validate() for field in ('storage', 'vartype'): with self.subTest(field): static = Variable(**self.VALID_KWARGS) static = static._replace(**{field: UNKNOWN})
with self.assertRaises(TypeError): static.validate()
for field, invalid in tests: if field == 'id': continue valid = seen - set(invalid) for value in valid: with self.subTest(f'{field}={value!r}'):