1# mode: run
2# tag: pep489
3
4import os.path
5
6module_spec = __spec__
7module_file = __file__
8
9
10def check_spec(spec=__spec__):
11    """
12    >>> check_spec()
13    """
14    assert __spec__ is not None
15    assert __spec__ is spec
16
17    assert __name__
18    assert __name__ == spec.name
19
20    assert spec.loader is not None
21    assert spec.loader is __loader__
22
23    assert not spec.parent
24    assert not __package__
25
26    assert spec.origin
27    assert spec.origin == module_file
28    assert spec.origin == __file__
29    assert os.path.basename(spec.origin).startswith(__name__)
30
31
32# validate that ModuleSpec is already complete at module initialisation time
33check_spec()
34check_spec(__spec__)
35check_spec(module_spec)
36
37
38def file_in_module():
39    """
40    >>> print(file_in_module())
41    mod__spec__
42    """
43    return os.path.basename(module_file).split('.', 1)[0]
44
45
46def file_in_function():
47    """
48    >>> print(file_in_function())
49    mod__spec__
50    """
51    return os.path.basename(__file__).split('.', 1)[0]
52