From: Stefan Behnel <scoder@users.berlios.de>
Date: Fri, 18 Mar 2011 14:46:28 +0000 (+0100)
Subject: provide BaseException in Python 2.3/2.4
X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=63bcd56149d6d2baa89b2f8f073a82f90d293b45;p=cython.git

provide BaseException in Python 2.3/2.4
---

diff --git a/Cython/Compiler/Code.py b/Cython/Compiler/Code.py
index 41221cf8..2c34515c 100644
--- a/Cython/Compiler/Code.py
+++ b/Cython/Compiler/Code.py
@@ -650,17 +650,27 @@ class GlobalState(object):
             if self.should_declare(entry.cname, entry):
                 self.put_pyobject_decl(entry)
                 w = self.parts['cached_builtins']
+                conditional_name = False
                 if entry.name == 'xrange':
                     # replaced by range() in Py3
+                    conditional_name = True
                     w.putln('#if PY_MAJOR_VERSION >= 3')
                     self.put_cached_builtin_init(
                         entry.pos, StringEncoding.EncodedString('range'),
                         entry.cname)
+                elif entry.name == 'BaseException':
+                    # replace BaseException by Exception in Py<2.5
+                    conditional_name = True
+                    w.putln('#if PY_VERSION_HEX < 0x02050000')
+                    self.put_cached_builtin_init(
+                        entry.pos, StringEncoding.EncodedString('Exception'),
+                        entry.cname)
+                if conditional_name:
                     w.putln('#else')
                 self.put_cached_builtin_init(
                     entry.pos, StringEncoding.EncodedString(entry.name),
                     entry.cname)
-                if entry.name == 'xrange':
+                if conditional_name:
                     w.putln('#endif')
 
     def put_cached_builtin_init(self, pos, name, cname):
diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py
index 17fc9758..d5c8dd20 100644
--- a/Cython/Compiler/Symtab.py
+++ b/Cython/Compiler/Symtab.py
@@ -902,8 +902,8 @@ class ModuleScope(Scope):
         return self.outer_scope.lookup(name, language_level = self.context.language_level)
 
     def declare_builtin(self, name, pos):
-        if not hasattr(builtins, name) and name != 'xrange':
-            # 'xrange' is special cased in Code.py
+        if not hasattr(builtins, name) and name not in ('xrange', 'BaseException'):
+            # 'xrange' and 'BaseException' are special cased in Code.py
             if self.has_import_star:
                 entry = self.declare_var(name, py_object_type, pos)
                 return entry
diff --git a/tests/run/builtinnames.pyx b/tests/run/builtinnames.pyx
index 46539582..1d018a55 100644
--- a/tests/run/builtinnames.pyx
+++ b/tests/run/builtinnames.pyx
@@ -56,3 +56,14 @@ def test_for_in_range(arg):
     for c in range(arg):
         l.append(c)
     return l
+
+def raise_and_catch_BaseException():
+    """
+    >>> raise_and_catch_BaseException()
+    1
+    """
+    try:
+        raise BaseException
+    except BaseException:
+        return 1
+    return 2