예제 #1
0
def from_file(
    infile,
    *,
    _read_tsv=read_tsv,
):
    """Return the info for known declarations in the given file."""
    known = {
        'variables': {},
        #'types': {},
        #'constants': {},
        #'macros': {},
    }
    for row in _read_tsv(infile, HEADER):
        filename, funcname, name, kind, declaration = row
        if not funcname or funcname == '-':
            funcname = None
        id = ID(filename, funcname, name)
        if kind == 'variable':
            values = known['variables']
            value = Variable(id, declaration)
            value._isglobal = _is_global(declaration) or id.funcname is None
        else:
            raise ValueError(f'unsupported kind in row {row}')
        if value.name == 'id' and declaration == UNKNOWN:
            # None of these are variables.
            declaration = 'int id'
        else:
            value.validate()
        values[id] = value
    return known
예제 #2
0
파일: known.py 프로젝트: zhitelmar/cpython
def from_file(
    infile,
    *,
    _read_tsv=read_tsv,
):
    """Return the info for known declarations in the given file."""
    known = {
        'variables': {},
        #'types': {},
        #'constants': {},
        #'macros': {},
    }
    for row in _read_tsv(infile, HEADER):
        filename, funcname, name, kind, declaration = row
        if not funcname or funcname == '-':
            funcname = None
        id = ID(filename, funcname, name)
        if kind == 'variable':
            values = known['variables']
            if funcname:
                storage = _get_storage(declaration) or 'local'
            else:
                storage = _get_storage(declaration) or 'implicit'
            value = Variable(id, storage, declaration)
        else:
            raise ValueError(f'unsupported kind in row {row}')
        value.validate()
        #        if value.name == 'id' and declaration == UNKNOWN:
        #            # None of these are variables.
        #            declaration = 'int id';
        #        else:
        #            value.validate()
        values[id] = value
    return known
예제 #3
0
    def test_validate_bad_field(self):
        badch = tuple(c for c in string.punctuation + string.digits)
        notnames = (
            '1a',
            'a.b',
            'a-b',
            '&a',
            'a++',
        ) + badch
        tests = [
            ('id', ()),  # Any non-empty str is okay.
            ('vartype', ()),  # Any non-empty str is okay.
        ]
        seen = set()
        for field, invalid in tests:
            for value in invalid:
                seen.add(value)
                with self.subTest(f'{field}={value!r}'):
                    static = Variable(**self.VALID_KWARGS)
                    static = static._replace(**{field: value})

                    with self.assertRaises(ValueError):
                        static.validate()

        for field, invalid in tests:
            valid = seen - set(invalid)
            for value in valid:
                with self.subTest(f'{field}={value!r}'):
                    static = Variable(**self.VALID_KWARGS)
                    static = static._replace(**{field: value})

                    static.validate()  # This does not fail.
예제 #4
0
    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()
예제 #5
0
    def test_validate_typical(self):
        static = Variable(
            id=ID(
                filename='x/y/z/spam.c',
                funcname='func',
                name='eggs',
            ),
            vartype='int',
        )

        static.validate()  # This does not fail.