diff options
Diffstat (limited to 'Cython/Parser')
-rw-r--r-- | Cython/Parser/ConcreteSyntaxTree.pyx | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/Cython/Parser/ConcreteSyntaxTree.pyx b/Cython/Parser/ConcreteSyntaxTree.pyx index f9888c561..90969fee7 100644 --- a/Cython/Parser/ConcreteSyntaxTree.pyx +++ b/Cython/Parser/ConcreteSyntaxTree.pyx @@ -32,13 +32,14 @@ import re def extract_names(path): # All parse tree types are #defined in these files as ints. type_names = {} - for line in open(path): - if line.startswith('#define'): - try: - _, name, value = line.strip().split() - type_names[int(value)] = name - except: - pass + with open(path) as fid: + for line in fid: + if line.startswith('#define'): + try: + _, name, value = line.strip().split() + type_names[int(value)] = name + except: + pass return type_names cdef dict type_names = {} @@ -61,7 +62,8 @@ def handle_includes(source, path): included = os.path.join(os.path.dirname(path), include_line.group(1)[1:-1]) if not os.path.exists(included): return include_line.group(0) + ' # no such path: ' + included - return handle_includes(open(included).read(), path) + with open(included) as fid: + return handle_includes(fid.read(), path) # TODO: Proper string tokenizing. return re.sub(r'^include\s+([^\n]+[\'"])\s*(#.*)?$', include_here, source, flags=re.M) @@ -69,7 +71,8 @@ def p_module(path): cdef perrdetail err cdef int flags cdef node* n - source = open(path).read() + with open(path) as fid: + source = fid.read() if '\ninclude ' in source: # TODO: Tokanizer needs to understand includes. source = handle_includes(source, path) |