Commit 9e8cd885 authored by Matthias BUSSONNIER's avatar Matthias BUSSONNIER

use context manager to close file, and more explicit variable name

parent 80e769d8
......@@ -10,6 +10,8 @@ import Version
from Code import CCodeWriter
from Cython import Utils
from contextlib import closing
# need one-characters subsitutions (for now) so offsets aren't off
special_chars = [
(u'&', u'\xF2', u'&'),
......@@ -55,21 +57,21 @@ class AnnotationCCodeWriter(CCodeWriter):
self.annotations.append((pos, item))
def save_annotation(self, source_filename, target_filename):
self.mark_pos(None)
f = Utils.open_source_file(source_filename)
with closing(Utils.open_source_file(source_filename)) as f:
lines = f.readlines()
html_filename = os.path.splitext(target_filename)[0] + ".html"
self.mark_pos(None)
for k, line in enumerate(lines):
for c, cc, html in special_chars:
line = line.replace(c, cc)
lines[k] = line
f.close()
html_filename = os.path.splitext(target_filename)[0] + ".html"
f = codecs.open(html_filename, "w", encoding="UTF-8")
f.write(u'<!DOCTYPE html>\n')
f.write(u'<!-- Generated by Cython %s -->\n' % Version.watermark)
f.write(u'<html>\n')
f.write(u"""
output_buffer = codecs.open(html_filename, "w", encoding="UTF-8")
output_buffer.write(u'<!DOCTYPE html>\n')
output_buffer.write(u'<!-- Generated by Cython %s -->\n' % Version.watermark)
output_buffer.write(u'<html>\n')
output_buffer.write(u"""
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
......@@ -107,10 +109,10 @@ function toggleDiv(id) {
</script>
</head>
""")
f.write(u'<body>\n')
f.write(u'<p>Generated by Cython %s\n' % Version.watermark)
output_buffer.write(u'<body>\n')
output_buffer.write(u'<p>Generated by Cython %s\n' % Version.watermark)
c_file = Utils.decode_filename(os.path.basename(target_filename))
f.write(u'<p>Raw output: <a href="%s">%s</a>\n' % (c_file, c_file))
output_buffer.write(u'<p>Raw output: <a href="%s">%s</a>\n' % (c_file, c_file))
zero_calls = dict((name, 0) for name in
'refnanny py_macro_api py_c_api pyx_macro_api pyx_c_api error_goto'.split())
......@@ -124,6 +126,7 @@ function toggleDiv(id) {
pos_comment_marker = u'/* \N{HORIZONTAL ELLIPSIS} */\n'
k = 0
code_source_file = self.code.get(source_filename, {})
print code_source_file
for line in lines:
k += 1
try:
......@@ -141,17 +144,17 @@ function toggleDiv(id) {
score = (5 * calls['py_c_api'] + 2 * calls['pyx_c_api'] +
calls['py_macro_api'] + calls['pyx_macro_api'])
color = u"FFFF%02x" % int(255/(1+score/10.0))
f.write(u"<pre class='line' style='background-color: #%s' onclick='toggleDiv(\"line%s\")'>" % (color, k))
output_buffer.write(u"<pre class='line' style='background-color: #%s' onclick='toggleDiv(\"line%s\")'>" % (color, k))
f.write(u" %d: " % k)
output_buffer.write(u" %d: " % k)
for c, cc, html in special_chars:
line = line.replace(cc, html)
f.write(line.rstrip())
output_buffer.write(line.rstrip())
f.write(u'</pre>\n')
f.write(u"<pre id='line%s' class='code' style='background-color: #%s'>%s</pre>" % (k, color, code))
f.write(u'</body></html>\n')
f.close()
output_buffer.write(u'</pre>\n')
output_buffer.write(u"<pre id='line%s' class='code' style='background-color: #%s'>%s</pre>" % (k, color, code))
output_buffer.write(u'</body></html>\n')
output_buffer.close()
_parse_code = re.compile(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment