From d0627fcb5e68c038903e12ee9f84385a7a27c63c Mon Sep 17 00:00:00 2001 From: Vitja Makarov Date: Thu, 23 May 2013 09:44:19 +0400 Subject: [PATCH] Add error_on_uninitialized option and disable it for pyregr testsuite --- Cython/Compiler/FlowControl.py | 4 +++- Cython/Compiler/Options.py | 6 ++++++ runtests.py | 5 ++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Cython/Compiler/FlowControl.py b/Cython/Compiler/FlowControl.py index 7c812cb..efb25b6 100644 --- a/Cython/Compiler/FlowControl.py +++ b/Cython/Compiler/FlowControl.py @@ -9,6 +9,7 @@ cython.declare(PyrexTypes=object, ExprNodes=object, Nodes=object, import Builtin import ExprNodes import Nodes +import Options from PyrexTypes import py_object_type, unspecified_type import PyrexTypes @@ -574,7 +575,8 @@ def check_definitions(flow, compiler_directives): if node.allow_null or entry.from_closure or entry.is_pyclass_attr: pass # Can be uninitialized here elif node.cf_is_null: - if (entry.type.is_pyobject or entry.type.is_unspecified or + if Options.error_on_uninitialized and ( + entry.type.is_pyobject or entry.type.is_unspecified or entry.error_on_uninitialized): messages.error( node.pos, diff --git a/Cython/Compiler/Options.py b/Cython/Compiler/Options.py index 50fa18c..795ae86 100644 --- a/Cython/Compiler/Options.py +++ b/Cython/Compiler/Options.py @@ -34,6 +34,12 @@ warning_errors = False # you should disable this option and also 'cache_builtins'. error_on_unknown_names = True +# Make uninitialized local variable reference a compile time error. +# Python raises UnboundLocalError at runtime, whereas this option makes +# them a compile time error. Note that this option affects only variables +# of "python object" type. +error_on_uninitialized = True + # This will convert statements of the form "for i in range(...)" # to "for i from ..." when i is a cdef'd integer type, and the direction # (i.e. sign of step) can be determined. diff --git a/runtests.py b/runtests.py index 53ff850..04a4404 100755 --- a/runtests.py +++ b/runtests.py @@ -520,7 +520,9 @@ class CythonCompileTestCase(unittest.TestCase): def setUp(self): from Cython.Compiler import Options self._saved_options = [ (name, getattr(Options, name)) - for name in ('warning_errors', 'error_on_unknown_names') ] + for name in ('warning_errors', + 'error_on_unknown_names', + 'error_on_uninitialized') ] self._saved_default_directives = Options.directive_defaults.items() Options.warning_errors = self.warning_errors @@ -989,6 +991,7 @@ class CythonPyregrTestCase(CythonRunTestCase): CythonRunTestCase.setUp(self) from Cython.Compiler import Options Options.error_on_unknown_names = False + Options.error_on_uninitialized = False Options.directive_defaults.update(dict( binding=True, always_allow_keywords=True, set_initial_path="SOURCEFILE")) -- 2.7.4