cy exec test + correct building of locals dict in Cython frames for non-wide python...
authorMark Florisson <markflorisson88@gmail.com>
Sun, 28 Nov 2010 16:12:56 +0000 (17:12 +0100)
committerMark Florisson <markflorisson88@gmail.com>
Sun, 28 Nov 2010 16:12:56 +0000 (17:12 +0100)
Cython/Debugger/Tests/test_libcython_in_gdb.py
Cython/Debugger/libpython.py

index 7042d96ffb821293c2bfd04df9f773d2e9f1b007..583d7db8bf7fa3a9285ff83c710c8bb62eab7873 100644 (file)
@@ -12,6 +12,8 @@ import trace
 import inspect
 import warnings
 import unittest
+import textwrap
+import tempfile
 import traceback
 from test import test_support
 
@@ -292,6 +294,51 @@ class TestUpDown(DebugTestCase):
         assert 'os.path.join("foo", "bar")' in result
 
 
+class TestExec(DebugTestCase):
+    
+    def setUp(self):
+        super(TestExec, self).setUp()
+        self.fd, self.tmpfilename = tempfile.mkstemp()
+        self.tmpfile = os.fdopen(self.fd, 'r+')
+        
+    def tearDown(self):
+        super(TestExec, self).tearDown()
+        
+        try:
+            self.tmpfile.close()
+        finally:
+            os.remove(self.tmpfilename)
+    
+    def eval_command(self, command):
+        gdb.execute('cy exec open(%r, "w").write(str(%s))' % 
+                                                (self.tmpfilename, command))
+        return self.tmpfile.read().strip()
+    
+    def test_cython_exec(self):
+        self.break_and_run('os.path.join("foo", "bar")')
+        
+        # test normal behaviour
+        self.assertEqual("[0]", self.eval_command('[a]'))
+        
+        # test multiline code
+        result = gdb.execute(textwrap.dedent('''\
+            cy exec
+            pass
+            
+            "nothing"
+            end
+            '''))
+        result = self.tmpfile.read().rstrip()
+        self.assertEqual('', result)
+    
+    def test_python_exec(self):
+        self.break_and_run('os.path.join("foo", "bar")')
+        gdb.execute('cy step')
+        
+        gdb.execute('cy exec some_random_var = 14')
+        self.assertEqual('14', self.eval_command('some_random_var'))
+
+
 def _main():
     try:
         gdb.lookup_type('PyModuleObject')
index 05d0276912b36ee953187b31e8c7d75059ec62a8..dfca4dda2ca276bf55326faa22f22a5275cc5a10 100644 (file)
@@ -2185,11 +2185,21 @@ class PythonCodeExecutor(object):
     
     def alloc_pystring(self, string):
         stringp = self.alloc_string(string)
+        PyString_FromStringAndSize = 'PyString_FromStringAndSize'
+        try:
+            gdb.parse_and_eval(PyString_FromStringAndSize)
+        except RuntimeError:
+            try:
+                gdb.parse_and_eval('PyUnicode_FromStringAndSize')
+            except RuntimeError:
+                PyString_FromStringAndSize = 'PyUnicodeUCS2_FromStringAndSize'
+            else:
+                PyString_FromStringAndSize = 'PyUnicode_FromStringAndSize'
+            
         try:
             result = gdb.parse_and_eval(
-                '(PyObject *) PyString_FromStringAndSize('
-                    '(char *) %d,'
-                    '(size_t) %d)' % (stringp, len(string)))
+                '(PyObject *) %s((char *) %d, (size_t) %d)' % (
+                            PyString_FromStringAndSize, stringp, len(string)))
         finally:
             self.free(stringp)