From 17fabe5aa5649294f36a5782239522eb0f8db5c1 Mon Sep 17 00:00:00 2001
From: Robert Bradshaw <robertwb@math.washington.edu>
Date: Tue, 11 Nov 2008 17:36:19 -0800
Subject: [PATCH] Make raising a KeyError not the default for parsing names.

---
 Cython/Compiler/Parsing.py   | 34 +++++++++++++++-------------------
 Cython/Compiler/Scanning.pxd |  4 ++++
 Cython/Compiler/Scanning.py  |  5 ++++-
 3 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/Cython/Compiler/Parsing.py b/Cython/Compiler/Parsing.py
index 5b963a74f..0e5a20dad 100644
--- a/Cython/Compiler/Parsing.py
+++ b/Cython/Compiler/Parsing.py
@@ -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):
diff --git a/Cython/Compiler/Scanning.pxd b/Cython/Compiler/Scanning.pxd
index 10b19277f..9850562da 100644
--- a/Cython/Compiler/Scanning.pxd
+++ b/Cython/Compiler/Scanning.pxd
@@ -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
diff --git a/Cython/Compiler/Scanning.py b/Cython/Compiler/Scanning.py
index b19d67f72..ad620c260 100644
--- a/Cython/Compiler/Scanning.py
+++ b/Cython/Compiler/Scanning.py
@@ -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:
-- 
2.30.9