import inspect
import warnings
import unittest
+import textwrap
+import tempfile
import traceback
from test import test_support
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')
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)