Don't reload inline module if it's already loaded.
authorRobert Bradshaw <robertwb@gmail.com>
Thu, 26 Jul 2012 17:17:14 +0000 (10:17 -0700)
committerRobert Bradshaw <robertwb@gmail.com>
Thu, 26 Jul 2012 17:55:23 +0000 (10:55 -0700)
Cython/Build/Inline.py

index f6a2938..0526a9c 100644 (file)
@@ -146,59 +146,64 @@ def cython_inline(code,
     arg_sigs = tuple([(get_type(kwds[arg], ctx), arg) for arg in arg_names])
     key = orig_code, arg_sigs, sys.version_info, sys.executable, Cython.__version__
     module_name = "_cython_inline_" + hashlib.md5(str(key).encode('utf-8')).hexdigest()
+    
+    if module_name in sys.modules:
+        module = sys.modules[module_name]
+    
+    else:
+        build_extension = None
+        if cython_inline.so_ext is None:
+            # Figure out and cache current extension suffix
+            build_extension = _get_build_extension()
+            cython_inline.so_ext = build_extension.get_ext_filename('')
 
-    build_extension = None
-    if cython_inline.so_ext is None:
-        # Figure out and cache current extension suffix
-        build_extension = _get_build_extension()
-        cython_inline.so_ext = build_extension.get_ext_filename('')
-
-    module_path = os.path.join(lib_dir, module_name + cython_inline.so_ext)
+        module_path = os.path.join(lib_dir, module_name + cython_inline.so_ext)
 
-    if not os.path.exists(lib_dir):
-        os.makedirs(lib_dir)
-    if force or not os.path.isfile(module_path):
-        cflags = []
-        c_include_dirs = []
-        qualified = re.compile(r'([.\w]+)[.]')
-        for type, _ in arg_sigs:
-            m = qualified.match(type)
-            if m:
-                cimports.append('\ncimport %s' % m.groups()[0])
-                # one special case
-                if m.groups()[0] == 'numpy':
-                    import numpy
-                    c_include_dirs.append(numpy.get_include())
-                    # cflags.append('-Wno-unused')
-        module_body, func_body = extract_func_code(code)
-        params = ', '.join(['%s %s' % a for a in arg_sigs])
-        module_code = """
+        if not os.path.exists(lib_dir):
+            os.makedirs(lib_dir)
+        if force or not os.path.isfile(module_path):
+            cflags = []
+            c_include_dirs = []
+            qualified = re.compile(r'([.\w]+)[.]')
+            for type, _ in arg_sigs:
+                m = qualified.match(type)
+                if m:
+                    cimports.append('\ncimport %s' % m.groups()[0])
+                    # one special case
+                    if m.groups()[0] == 'numpy':
+                        import numpy
+                        c_include_dirs.append(numpy.get_include())
+                        # cflags.append('-Wno-unused')
+            module_body, func_body = extract_func_code(code)
+            params = ', '.join(['%s %s' % a for a in arg_sigs])
+            module_code = """
 %(module_body)s
 %(cimports)s
 def __invoke(%(params)s):
 %(func_body)s
-        """ % {'cimports': '\n'.join(cimports), 'module_body': module_body, 'params': params, 'func_body': func_body }
-        for key, value in literals.items():
-            module_code = module_code.replace(key, value)
-        pyx_file = os.path.join(lib_dir, module_name + '.pyx')
-        fh = open(pyx_file, 'w')
-        try: 
-            fh.write(module_code)
-        finally:
-            fh.close()
-        extension = Extension(
-            name = module_name,
-            sources = [pyx_file],
-            include_dirs = c_include_dirs,
-            extra_compile_args = cflags)
-        if build_extension is None:
-            build_extension = _get_build_extension()
-        build_extension.extensions = cythonize([extension], ctx=ctx, quiet=quiet)
-        build_extension.build_temp = os.path.dirname(pyx_file)
-        build_extension.build_lib  = lib_dir
-        build_extension.run()
+            """ % {'cimports': '\n'.join(cimports), 'module_body': module_body, 'params': params, 'func_body': func_body }
+            for key, value in literals.items():
+                module_code = module_code.replace(key, value)
+            pyx_file = os.path.join(lib_dir, module_name + '.pyx')
+            fh = open(pyx_file, 'w')
+            try: 
+                fh.write(module_code)
+            finally:
+                fh.close()
+            extension = Extension(
+                name = module_name,
+                sources = [pyx_file],
+                include_dirs = c_include_dirs,
+                extra_compile_args = cflags)
+            if build_extension is None:
+                build_extension = _get_build_extension()
+            build_extension.extensions = cythonize([extension], ctx=ctx, quiet=quiet)
+            build_extension.build_temp = os.path.dirname(pyx_file)
+            build_extension.build_lib  = lib_dir
+            build_extension.run()
+
+        module = imp.load_dynamic(module_name, module_path)
 
-    module = imp.load_dynamic(module_name, module_path)
     arg_list = [kwds[arg] for arg in arg_names]
     return module.__invoke(*arg_list)