disable C/C++ compiler optimization when compiling with the "debug" flag
authorMark Florisson <markflorisson88@gmail.com>
Mon, 8 Nov 2010 13:35:06 +0000 (14:35 +0100)
committerMark Florisson <markflorisson88@gmail.com>
Mon, 8 Nov 2010 13:35:06 +0000 (14:35 +0100)
Cython/Debugger/libcython.py
Cython/Distutils/build_ext.py

index b0fa9313c7b560f6efce3f64a4373e2035de3e4b..6208777c7f6a2309c3bc4f199994d10090f71558 100644 (file)
@@ -244,8 +244,14 @@ class CythonBase(object):
             if not pyframeobject:
                 raise gdb.GdbError('Unable to read information on python frame')
 
-            filename = pyframeobject.filename()
-            lineno = pyframeobject.current_line_num()
+            try:
+                filename = pyframeobject.filename()
+            except RuntimeError:
+                filename = None
+                lineno = None
+            else:
+                lineno = pyframeobject.current_line_num()
+            
             if pygments:
                 lexer = pygments.lexers.PythonLexer(stripall=False)
         else:
@@ -307,7 +313,10 @@ class CythonBase(object):
                 # print this python function as a C function
                 return self.print_stackframe(frame, index, is_c=True)
             
-            func_name = pyframe.co_name
+            try:
+                func_name = str(pyframe.co_name)
+            except RuntimeError:
+                func_name = 'Unknown Function Name'
             func_cname = 'PyEval_EvalFrameEx'
             func_args = []
         elif self.is_cython_function(frame):
index c08b884955252a45dc073125937e0c92c4aa6df6..604290c5aac199b2e104d59c8622c55f2f45631b 100644 (file)
@@ -17,11 +17,45 @@ from distutils.dep_util import newer, newer_group
 from distutils import log
 from distutils.dir_util import mkpath
 from distutils.command import build_ext as _build_ext
+from distutils import sysconfig
 
 extension_name_re = _build_ext.extension_name_re
 
 show_compilers = _build_ext.show_compilers
 
+class Optimization(object):
+    def __init__(self):
+        self.flags = (
+            'OPT', 
+            'CFLAGS',
+            'CPPFLAGS',
+            'EXTRA_CFLAGS', 
+            'BASECFLAGS',
+            'PY_CFLAGS',
+        )
+        self.state = sysconfig.get_config_vars(*self.flags)
+        self.config_vars = sysconfig.get_config_vars()
+        
+        
+    def disable_optimization(self):
+        "disable optimization for the C or C++ compiler"
+        badoptions = ('-O1', '-O2', '-O3')
+        
+        for flag, option in zip(self.flags, self.state):
+            if option is not None:
+                g = (opt for opt in option.split() if opt not in badoptions)
+                self.config_vars[flag] = ' '.join(g)
+    
+    def restore_state(self):
+        "restore the original state"
+        for flag, option in zip(self.flags, self.state):
+            if option is not None:
+                self.config_vars[flag] = option
+
+
+optimization = Optimization()
+
+
 class build_ext(_build_ext.build_ext):
 
     description = "build C/C++ and Cython extensions (compile/link to build directory)"
@@ -77,10 +111,22 @@ class build_ext(_build_ext.build_ext):
         if self.pyrex_directives is None:
             self.pyrex_directives = {}
     # finalize_options ()
-
+    
+    def run(self):
+        # We have one shot at this before build_ext initializes the compiler.
+        # If --pyrex-debug is in effect as a command line option or as option
+        # of any Extension module, disable optimization for the C or C++
+        # compiler.
+        if (self.pyrex_debug or any(getattr(ext, 'pyrex_debug', False) 
+                                        for ext in self.extensions)):
+            optimization.disable_optimization()
+        
+        _build_ext.build_ext.run(self)
+    
     def build_extensions(self):
         # First, sanity-check the 'extensions' list
         self.check_extensions_list(self.extensions)
+        
         for ext in self.extensions:
             ext.sources = self.cython_sources(ext.sources, ext)
             self.build_extension(ext)