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