Remove 'CUSTOM_NAMESPACE'/'os_traits' from traits

The get_traits() function was erroneously including 'CUSTOM_NAMESPACE' and
'os_traits' in the list of returned traits because the matching expression in
the list comprehension wasn't filtering out these two module-level string
attributes.

Also removes the name.upper() in symbolize() because that's actually dangerous
-- it may make a symbol in os_traits that doesn't actually match the trait name
defined in the submodule. For instance, if someone creates an
os_traits/foo/bar.py module with::

TRAITS = [
    'baz',
]

the name.upper() would make a symbol os_traits.foo.bar.BAZ and
os_traits.FOO_BAR_baz

Change-Id: I8633cb29649ad0ca67f7669fd2b405b55e16258e
This commit is contained in:
Jay Pipes 2017-05-04 19:07:31 -04:00
parent 5a704fd08d
commit 8503afd938
2 changed files with 5 additions and 2 deletions

View File

@ -37,7 +37,7 @@ def symbolize(mod_name, name):
value_base = '_'.join([m.upper() for m in mod_name.split('.')[1:]])
value = value_base + '_' + name.upper()
setattr(this_lib, value, value) # os_traits.HW_CPU_X86_SSE
setattr(leaf_mod, name.upper(), value) # os_traits.hw.cpu.x86.SSE
setattr(leaf_mod, name, value) # os_traits.hw.cpu.x86.SSE
def import_submodules(package, recursive=True):
@ -76,7 +76,8 @@ def get_traits(prefix=None):
v for k, v in sys.modules[__name__].__dict__.items()
if isinstance(v, six.string_types) and
not k.startswith('_') and
v.startswith(prefix)
v.startswith(prefix) and
k not in ('CUSTOM_NAMESPACE', 'this_name')
]

View File

@ -37,6 +37,8 @@ class TestSymbols(base.TestCase):
self.assertIn(ot.HW_CPU_X86_AVX2, traits)
self.assertNotIn(ot.STORAGE_DISK_SSD, traits)
self.assertNotIn(ot.HW_NIC_SRIOV, traits)
self.assertNotIn('CUSTOM_NAMESPACE', traits)
self.assertNotIn('os_traits', traits)
def test_check_traits(self):
traits = set(["HW_CPU_X86_SSE42", "HW_CPU_X86_XOP"])