##// END OF EJS Templates
Implement --force by choosing a unique module name (using the current timestamp)....
Bradley M. Froehle -
Show More
@@ -21,6 +21,7 b' import imp'
21 import io
21 import io
22 import os
22 import os
23 import sys
23 import sys
24 import time
24
25
25 try:
26 try:
26 import hashlib
27 import hashlib
@@ -133,13 +134,12 b' class CythonMagics(Magics):'
133 )
134 )
134 @magic_arguments.argument(
135 @magic_arguments.argument(
135 '-f', '--force', action='store_true', default=False,
136 '-f', '--force', action='store_true', default=False,
136 help="Force the compilation of the pyx module even if it hasn't "
137 help="Force the compilation of a new module, even if the source has been "
137 "changed"
138 "previously compiled."
138 )
139 )
139 @magic_arguments.argument(
140 @magic_arguments.argument(
140 '-a', '--annotate', action='store_true', default=False,
141 '-a', '--annotate', action='store_true', default=False,
141 help="Produce a colorized HTML version of the source. "
142 help="Produce a colorized HTML version of the source."
142 "(Implies --force)."
143 )
143 )
144 @cell_magic
144 @cell_magic
145 def cython(self, line, cell):
145 def cython(self, line, cell):
@@ -161,16 +161,27 b' class CythonMagics(Magics):'
161 lib_dir = os.path.join(self.shell.ipython_dir, 'cython')
161 lib_dir = os.path.join(self.shell.ipython_dir, 'cython')
162 quiet = True
162 quiet = True
163 key = code, sys.version_info, sys.executable, Cython.__version__
163 key = code, sys.version_info, sys.executable, Cython.__version__
164
165 if not os.path.exists(lib_dir):
166 os.makedirs(lib_dir)
167
168 if args.force:
169 # Force a new module name by adding the current time to the
170 # key which is hashed to determine the module name.
171 key += time.time(),
172
164 module_name = "_cython_magic_" + hashlib.md5(str(key).encode('utf-8')).hexdigest()
173 module_name = "_cython_magic_" + hashlib.md5(str(key).encode('utf-8')).hexdigest()
165 module_path = os.path.join(lib_dir, module_name+self.so_ext)
174 module_path = os.path.join(lib_dir, module_name + self.so_ext)
166
175
167 if args.annotate:
176 have_module = os.path.isfile(module_path)
168 args.force = True
177 need_cythonize = not have_module
169
178
170 if not os.path.exists(lib_dir):
179 if args.annotate:
171 os.makedirs(lib_dir)
180 html_file = os.path.join(lib_dir, module_name + '.html')
181 if not os.path.isfile(html_file):
182 need_cythonize = True
172
183
173 if args.force or not os.path.isfile(module_path):
184 if need_cythonize:
174 c_include_dirs = args.include
185 c_include_dirs = args.include
175 if 'numpy' in code:
186 if 'numpy' in code:
176 import numpy
187 import numpy
@@ -193,11 +204,13 b' class CythonMagics(Magics):'
193 opts = dict(
204 opts = dict(
194 quiet=quiet,
205 quiet=quiet,
195 annotate = args.annotate,
206 annotate = args.annotate,
196 force = args.force,
207 force = True,
197 )
208 )
198 build_extension.extensions = cythonize([extension], **opts)
209 build_extension.extensions = cythonize([extension], **opts)
199 except CompileError:
210 except CompileError:
200 return
211 return
212
213 if not have_module:
201 build_extension.build_temp = os.path.dirname(pyx_file)
214 build_extension.build_temp = os.path.dirname(pyx_file)
202 build_extension.build_lib = lib_dir
215 build_extension.build_lib = lib_dir
203 build_extension.run()
216 build_extension.run()
@@ -207,7 +220,6 b' class CythonMagics(Magics):'
207 self._import_all(module)
220 self._import_all(module)
208
221
209 if args.annotate:
222 if args.annotate:
210 html_file = os.path.join(lib_dir, module_name + '.html')
211 try:
223 try:
212 with io.open(html_file, encoding='utf-8') as f:
224 with io.open(html_file, encoding='utf-8') as f:
213 annotated_html = f.read()
225 annotated_html = f.read()
General Comments 0
You need to be logged in to leave comments. Login now