Make raising a KeyError not the default for parsing names.
authorRobert Bradshaw <robertwb@math.washington.edu>
Wed, 12 Nov 2008 01:36:19 +0000 (17:36 -0800)
committerRobert Bradshaw <robertwb@math.washington.edu>
Wed, 12 Nov 2008 01:36:19 +0000 (17:36 -0800)
Cython/Compiler/Parsing.py
Cython/Compiler/Scanning.pxd
Cython/Compiler/Scanning.py

index 5b963a74f8a75a57f3d449405044b40abde2801d..0e5a20dad9e5292e0cfe6e66767e32b96108899f 100644 (file)
@@ -532,26 +532,22 @@ def p_atom(s):
 
 def p_name(s, name):
     pos = s.position()
-    if not s.compile_time_expr:
-        try:
-            value = s.compile_time_env.lookup_here(name)
-        except KeyError:
-            pass
+    if not s.compile_time_expr and name in s.compile_time_env:
+        value = s.compile_time_env.lookup_here(name)
+        rep = repr(value)
+        if isinstance(value, bool):
+            return ExprNodes.BoolNode(pos, value = value)
+        elif isinstance(value, int):
+            return ExprNodes.IntNode(pos, value = rep)
+        elif isinstance(value, long):
+            return ExprNodes.IntNode(pos, value = rep, longness = "L")
+        elif isinstance(value, float):
+            return ExprNodes.FloatNode(pos, value = rep)
+        elif isinstance(value, (str, unicode)):
+            return ExprNodes.StringNode(pos, value = value)
         else:
-            rep = repr(value)
-            if isinstance(value, bool):
-                return ExprNodes.BoolNode(pos, value = value)
-            elif isinstance(value, int):
-                return ExprNodes.IntNode(pos, value = rep)
-            elif isinstance(value, long):
-                return ExprNodes.IntNode(pos, value = rep, longness = "L")
-            elif isinstance(value, float):
-                return ExprNodes.FloatNode(pos, value = rep)
-            elif isinstance(value, (str, unicode)):
-                return ExprNodes.StringNode(pos, value = value)
-            else:
-                error(pos, "Invalid type for compile-time constant: %s"
-                    % value.__class__.__name__)
+            error(pos, "Invalid type for compile-time constant: %s"
+                % value.__class__.__name__)
     return ExprNodes.NameNode(pos, name = name)
 
 def p_cat_string_literal(s):
index 10b19277f9b4891b26b3d00975906e184e72e632..9850562dab0b593f7f1ee2a807974df76f807a32 100644 (file)
@@ -1,5 +1,9 @@
 from Cython.Plex.Scanners cimport Scanner
 
+cdef class CompileTimeScope:
+    cdef public entries
+    cdef public outer
+
 cdef class PyrexScanner(Scanner):
     cdef public context
     cdef public list included_files
index b19d67f72272b04dd7e4ba41b5faae1d683ce3a8..ad620c260e3c94ff41d3b93b1e6004cadd4c5547 100644 (file)
@@ -176,7 +176,7 @@ resword_dict = build_resword_dict()
 
 #------------------------------------------------------------------
 
-class CompileTimeScope(object):
+class CompileTimeScope:
 
     def __init__(self, outer = None):
         self.entries = {}
@@ -187,6 +187,9 @@ class CompileTimeScope(object):
     
     def lookup_here(self, name):
         return self.entries[name]
+        
+    def __contains__(self, name):
+        return name in self.entries
     
     def lookup(self, name):
         try: