Move UtilityCode to Code.py
authorDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Thu, 21 May 2009 19:28:04 +0000 (21:28 +0200)
committerDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Thu, 21 May 2009 19:28:04 +0000 (21:28 +0200)
Cython/Compiler/Buffer.py
Cython/Compiler/Builtin.py
Cython/Compiler/Code.py
Cython/Compiler/ExprNodes.py
Cython/Compiler/ModuleNode.py
Cython/Compiler/Nodes.py
Cython/Compiler/Optimize.py
Cython/Compiler/PyrexTypes.py
Cython/Compiler/Symtab.py
Cython/Utils.py

index d2788aa167239e02736d28f337cb01cb03e9157d..91fd6adb1217a8aa64f38d15b72fdeb6994fff28 100644 (file)
@@ -1,10 +1,10 @@
-from Cython.Compiler.Visitor import VisitorTransform, CythonTransform
-from Cython.Compiler.ModuleNode import ModuleNode
-from Cython.Compiler.Nodes import *
-from Cython.Compiler.ExprNodes import *
-from Cython.Compiler.StringEncoding import EncodedString
-from Cython.Compiler.Errors import CompileError
-from Cython.Utils import UtilityCode
+from Visitor import VisitorTransform, CythonTransform
+from ModuleNode import ModuleNode
+from Nodes import *
+from ExprNodes import *
+from StringEncoding import EncodedString
+from Errors import CompileError
+from Code import UtilityCode
 import Interpreter
 import PyrexTypes
 
index 3249e8bcea31b0a5d495e11c9804af4f906b5ace..a911fbe522ac772074535705010a1da25bbbeb50 100644 (file)
@@ -3,7 +3,7 @@
 #
 
 from Symtab import BuiltinScope, StructOrUnionScope
-from Cython.Utils import UtilityCode
+from Code import UtilityCode
 from TypeSlots import Signature
 import PyrexTypes
 import Naming
index 4fd2147600e51d6a206fa44244e848982fe95feb..deb4954864c9a5d4951b7cf58eab0ba6fe968eca 100644 (file)
@@ -8,9 +8,6 @@ import Naming
 import Options
 import StringEncoding
 from Cython import Utils
-from PyrexTypes import py_object_type, typecast
-import PyrexTypes
-from TypeSlots import method_coexist
 from Scanning import SourceDescriptor
 from Cython.StringIOTree import StringIOTree
 try:
@@ -19,6 +16,58 @@ except NameError:
     from sets import Set as set
 import DebugFlags
 
+from Cython.Utils import none_or_sub
+
+# a simple class that simplifies the usage of utility code
+
+class UtilityCode(object):
+    def __init__(self, proto=None, impl=None, init=None, cleanup=None, requires=None):
+        self.proto = proto
+        self.impl = impl
+        self.init = init
+        self.cleanup = cleanup
+        self.requires = requires
+        self._cache = {}
+        self.specialize_list = []
+
+    def write_init_code(self, writer, pos):
+        if not self.init:
+            return
+        if isinstance(self.init, basestring):
+            writer.put(self.init)
+        else:
+            self.init(writer, pos)
+
+    def write_cleanup_code(self, writer, pos):
+        if not self.cleanup:
+            return
+        if isinstance(self.cleanup, basestring):
+            writer.put(self.cleanup)
+        else:
+            self.cleanup(writer, pos)
+    
+    def specialize(self, pyrex_type=None, **data):
+        # Dicts aren't hashable...
+        if pyrex_type is not None:
+            data['type'] = pyrex_type.declaration_code('')
+            data['type_name'] = pyrex_type.specalization_name()
+        key = data.items(); key.sort(); key = tuple(key)
+        try:
+            return self._cache[key]
+        except KeyError:
+            if self.requires is None:
+                requires = None
+            else:
+                requires = [r.specialize(data) for r in self.requires]
+            s = self._cache[key] = UtilityCode(
+                                        none_or_sub(self.proto, data),
+                                        none_or_sub(self.impl, data),
+                                        none_or_sub(self.init, data),
+                                        none_or_sub(self.cleanup, data),
+                                        requires)
+            self.specialize_list.append(s)
+            return s
+
 class FunctionState(object):
     # return_label     string          function return point label
     # error_label      string          error catch point label
@@ -397,6 +446,7 @@ class GlobalState(object):
         # utility_code_def
         #
         code = self.parts['utility_code_def']
+        import PyrexTypes
         code.put(PyrexTypes.type_conversion_functions)
         code.putln("")
 
@@ -979,6 +1029,7 @@ class CCodeWriter(object):
             return entry.cname
     
     def as_pyobject(self, cname, type):
+        from PyrexTypes import py_object_type, typecast
         return typecast(py_object_type, type, cname)
     
     def put_gotref(self, cname):
@@ -1026,6 +1077,7 @@ class CCodeWriter(object):
             self.putln("__Pyx_INCREF(%s);" % self.entry_as_pyobject(entry))
     
     def put_decref_clear(self, cname, type, nanny=True):
+        from PyrexTypes import py_object_type, typecast
         if nanny:
             self.putln("__Pyx_DECREF(%s); %s = 0;" % (
                 typecast(py_object_type, type, cname), cname))
@@ -1085,6 +1137,7 @@ class CCodeWriter(object):
             self.put_var_xdecref_clear(entry)
     
     def put_init_to_py_none(self, cname, type, nanny=True):
+        from PyrexTypes import py_object_type, typecast
         py_none = typecast(type, py_object_type, "Py_None")
         if nanny:
             self.putln("%s = %s; __Pyx_INCREF(Py_None);" % (cname, py_none))
@@ -1098,6 +1151,7 @@ class CCodeWriter(object):
         self.put_init_to_py_none(code, entry.type, nanny)
 
     def put_pymethoddef(self, entry, term):
+        from TypeSlots import method_coexist
         if entry.doc:
             doc_code = entry.doc_cname
         else:
index 04de084822411f383a721df5bad18387264b92fe..abc122d7b6530b30042e8098070157a90c42e8b7 100644 (file)
@@ -6,7 +6,7 @@ import operator
 
 from Errors import error, warning, InternalError
 from Errors import hold_errors, release_errors, held_errors, report_error
-from Cython.Utils import UtilityCode
+from Code import UtilityCode
 import StringEncoding
 import Naming
 import Nodes
index f55bb1a2da7b0bb365226b8779c90269851f667a..2bb0c0a47c3e9329cf87fc54fe9adba1803c5df1 100644 (file)
@@ -23,7 +23,8 @@ import DebugFlags
 
 from Errors import error, warning
 from PyrexTypes import py_object_type
-from Cython.Utils import open_new_file, replace_suffix, UtilityCode
+from Cython.Utils import open_new_file, replace_suffix
+from Code import UtilityCode
 from StringEncoding import escape_byte_string, EncodedString
 
 
index facc00ed47aced4a768cc916247d69a41225f590..998d705421f2c933e6f9e8d16468f667062d859e 100644 (file)
@@ -13,7 +13,8 @@ import TypeSlots
 from PyrexTypes import py_object_type, error_type, CTypedefType, CFuncType
 from Symtab import ModuleScope, LocalScope, GeneratorLocalScope, \
     StructOrUnionScope, PyClassScope, CClassScope
-from Cython.Utils import open_new_file, replace_suffix, UtilityCode
+from Cython.Utils import open_new_file, replace_suffix
+from Code import UtilityCode
 from StringEncoding import EncodedString, escape_byte_string, split_docstring
 import Options
 import ControlFlow
index 884a215b1958ed05dc6eff3313f39c3faf12ebbf..2f5936c5c9d22290cd6760359f8986e8e103c942 100644 (file)
@@ -8,7 +8,7 @@ import TypeSlots
 import Symtab
 import Options
 
-from Cython.Utils import UtilityCode
+from Code import UtilityCode
 from StringEncoding import EncodedString
 from Errors import error
 from ParseTreeTransforms import SkipDeclarations
index 51bc7bb569a32f343d69d27d5c6f5a985cfbdab1..682bc28ee570c773e1f34a572b70dc3f297a5665 100644 (file)
@@ -2,7 +2,7 @@
 #   Pyrex - Types
 #
 
-from Cython.Utils import UtilityCode
+from Code import UtilityCode
 import StringEncoding
 import Naming
 import copy
index bbcdaf26017182b18de18b355f3f26075c3995da..87a696d1d5c3519bd9cd7ac400e3aa676c6dfd86 100644 (file)
@@ -14,6 +14,7 @@ from TypeSlots import \
     pyfunction_signature, pymethod_signature, \
     get_special_method_signature, get_property_accessor_signature
 import ControlFlow
+import Code
 import __builtin__
 try:
     set
@@ -1382,7 +1383,7 @@ class PropertyScope(Scope):
 # Should this go elsewhere (and then get imported)?
 #------------------------------------------------------------------------------------
 
-classmethod_utility_code = Utils.UtilityCode(
+classmethod_utility_code = Code.UtilityCode(
 proto = """
 #include "descrobject.h"
 static PyObject* __Pyx_Method_ClassMethod(PyObject *method); /*proto*/
index 9361948c7a1d844dc2ae96fbe74370d1f681245c..c841cc1df69676c3c88a39e03b0907a9075fe714 100644 (file)
@@ -95,52 +95,3 @@ def none_or_sub(s, data):
     else:
         return s % data
 
-# a simple class that simplifies the usage of utility code
-
-class UtilityCode(object):
-    def __init__(self, proto=None, impl=None, init=None, cleanup=None, requires=None):
-        self.proto = proto
-        self.impl = impl
-        self.init = init
-        self.cleanup = cleanup
-        self.requires = requires
-        self._cache = {}
-        self.specialize_list = []
-
-    def write_init_code(self, writer, pos):
-        if not self.init:
-            return
-        if isinstance(self.init, basestring):
-            writer.put(self.init)
-        else:
-            self.init(writer, pos)
-
-    def write_cleanup_code(self, writer, pos):
-        if not self.cleanup:
-            return
-        if isinstance(self.cleanup, basestring):
-            writer.put(self.cleanup)
-        else:
-            self.cleanup(writer, pos)
-    
-    def specialize(self, pyrex_type=None, **data):
-        # Dicts aren't hashable...
-        if pyrex_type is not None:
-            data['type'] = pyrex_type.declaration_code('')
-            data['type_name'] = pyrex_type.specalization_name()
-        key = data.items(); key.sort(); key = tuple(key)
-        try:
-            return self._cache[key]
-        except KeyError:
-            if self.requires is None:
-                requires = None
-            else:
-                requires = [r.specialize(data) for r in self.requires]
-            s = self._cache[key] = UtilityCode(
-                                        none_or_sub(self.proto, data),
-                                        none_or_sub(self.impl, data),
-                                        none_or_sub(self.init, data),
-                                        none_or_sub(self.cleanup, data),
-                                        requires)
-            self.specialize_list.append(s)
-            return s