fix compiler crash on bad code (complex % x) and move directive handling in ModNode...
authorStefan Behnel <stefan_ml@behnel.de>
Mon, 22 Apr 2013 19:32:06 +0000 (21:32 +0200)
committerStefan Behnel <stefan_ml@behnel.de>
Mon, 22 Apr 2013 19:32:06 +0000 (21:32 +0200)
Cython/Compiler/ExprNodes.py
tests/errors/mod_errors.pyx [new file with mode: 0644]

index 95faae5..2ef2ce6 100755 (executable)
@@ -9029,16 +9029,24 @@ class ModNode(DivNode):
         else:
             return "float divmod()"
 
-    def generate_evaluation_code(self, code):
+    def analyse_operation(self, env):
+        DivNode.analyse_operation(self, env)
         if not self.type.is_pyobject:
             if self.cdivision is None:
-                self.cdivision = code.globalstate.directives['cdivision'] or not self.type.signed
-            if not self.cdivision:
-                if self.type.is_int:
-                    code.globalstate.use_utility_code(mod_int_utility_code.specialize(self.type))
-                else:
-                    code.globalstate.use_utility_code(
-                        mod_float_utility_code.specialize(self.type, math_h_modifier=self.type.math_h_modifier))
+                self.cdivision = env.directives['cdivision'] or not self.type.signed
+            if not self.cdivision and not self.type.is_int and not self.type.is_float:
+                error(self.pos, "mod operator not supported for type '%s'" % self.type)
+
+    def generate_evaluation_code(self, code):
+        if not self.type.is_pyobject and not self.cdivision:
+            if self.type.is_int:
+                code.globalstate.use_utility_code(
+                    mod_int_utility_code.specialize(self.type))
+            else:  # float
+                code.globalstate.use_utility_code(
+                    mod_float_utility_code.specialize(
+                        self.type, math_h_modifier=self.type.math_h_modifier))
+        # note: skipping over DivNode here
         NumBinopNode.generate_evaluation_code(self, code)
         self.generate_div_warning_code(code)
 
diff --git a/tests/errors/mod_errors.pyx b/tests/errors/mod_errors.pyx
new file mode 100644 (file)
index 0000000..43a5cd1
--- /dev/null
@@ -0,0 +1,9 @@
+# mode: error
+
+def mod_complex():
+    x = (1.1+2.0j) % 4
+    return x
+
+_ERRORS = """
+4:19: mod operator not supported for type 'double complex'
+"""