Completed
Push — master ( cc9f84...e076a6 )
by russianidiot
01:19
created

ispythoncompiled()   A

Complexity

Conditions 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
c 3
b 0
f 0
dl 0
loc 9
rs 9.2
1
#!/usr/bin/env python
2
import marshal
3
import os
4
from assert_exists import assert_exists
5
from public import public
6
7
# # http://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html
8
9
10
def code(path):
11
    """return marshal.load(file)"""
12
    if os.path.exists(path):
13
        h = open(path, "rb")
14
        return marshal.load(h)
15
16
17
def is_loaded(path):
18
    try:
19
        code(path)
20
        return True
21
    except Exception:
22
        return False
23
24
25
@public
26
def ispythoncompiled(path):
27
    """return True if file compiled"""
28
    if not path or path[-1] != "c":
29
        return False
30
    if os.path.splitext(path)[1] == ".pyc":
31
        return True
32
    assert_exists(path)
33
    return is_loaded(path)
34