locals_T732.pyx 1.27 KB
Newer Older
1 2
# mode: run
# ticket: 731
Stefan Behnel's avatar
Stefan Behnel committed
3
# tag: locals, vars, dir
4

5 6
cimport cython

7 8 9 10
LOCALS = locals()
GLOBALS = globals()
DIR_SAME = sorted(dir()) == sorted(globals().keys())

11

12 13 14 15 16 17 18 19
def test_module_locals_and_dir():
    """
    >>> LOCALS is GLOBALS
    True
    >>> DIR_SAME
    True
    """

20

21 22 23 24 25
def test_class_locals_and_dir():
    """
    >>> klass = test_class_locals_and_dir()
    >>> 'visible' in klass.locs and 'not_visible' not in klass.locs
    True
26 27
    >>> [n for n in klass.names if n != "__qualname__"]
    ['__module__', 'visible']
28 29 30 31 32 33 34
    """
    not_visible = 1234
    class Foo:
        visible = 4321
        names = dir()
        locs = locals()
    return Foo
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64


@cython.test_fail_if_path_exists('//SortedDictKeysNode')
def test_class_dir_contains():
    """
    >>> klass = test_class_dir_contains()
    True
    False
    True
    False
    True
    False
    True
    True
    True
    """
    not_visible = 1234
    class Foo:
        visible = 4321
        print('visible' in dir())
        print('not_visible' in dir())
        print('not_visible' not in dir())
        print('locs' in dir())
        print('visible' in locals())
        print('locs' in locals())
        locs = locals()
        print('visible' in dir())
        print('locs' in dir())
        print('locs' in locals())
    return Foo