From 18024c4fea00da3cb25df78c8526e914cb1c58dc Mon Sep 17 00:00:00 2001 From: Mark Florisson Date: Sun, 28 Nov 2010 17:12:56 +0100 Subject: [PATCH] cy exec test + correct building of locals dict in Cython frames for non-wide python 3 unicode builds --- .../Debugger/Tests/test_libcython_in_gdb.py | 47 +++++++++++++++++++ Cython/Debugger/libpython.py | 16 +++++-- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/Cython/Debugger/Tests/test_libcython_in_gdb.py b/Cython/Debugger/Tests/test_libcython_in_gdb.py index 7042d96f..583d7db8 100644 --- a/Cython/Debugger/Tests/test_libcython_in_gdb.py +++ b/Cython/Debugger/Tests/test_libcython_in_gdb.py @@ -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') diff --git a/Cython/Debugger/libpython.py b/Cython/Debugger/libpython.py index 05d02769..dfca4dda 100644 --- a/Cython/Debugger/libpython.py +++ b/Cython/Debugger/libpython.py @@ -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) -- 2.26.2