# HG changeset patch
# User Lisandro Dalcin <dalcinl@gmail.com>
# Date 1239156313 10800
# Node ID fe1aa7bf0d92f20b91f77f7a67b5fe084552f675
# Parent d3b49a84a8215fefc0ec6ebd19f12ff5305c7682
fixes for "exec" statement implementation.
- fix broken compile with MSVC (does not like preprocessor #if/#else/#endif inside call to macro PyRun_String)
- enable "exectest" testcase for Python 3
diff -r d3b49a84a821 -r fe1aa7bf0d92 Cython/Compiler/Builtin.py
|
a
|
b
|
|
| 168 | 168 | static PyObject* __Pyx_PyRun(PyObject* o, PyObject* globals, PyObject* locals) { |
| 169 | 169 | PyObject* result; |
| 170 | 170 | PyObject* s = 0; |
| | 171 | char *code = 0; |
| 171 | 172 | |
| 172 | 173 | if (!locals && !globals) { |
| 173 | 174 | globals = PyModule_GetDict(%s);""" % Naming.module_cname + """ |
| … |
… |
|
| 195 | 196 | goto bad; |
| 196 | 197 | } |
| 197 | 198 | |
| 198 | | result = PyRun_String( |
| 199 | 199 | #if PY_MAJOR_VERSION >= 3 |
| 200 | | PyBytes_AS_STRING(o), |
| | 200 | code = PyBytes_AS_STRING(o); |
| 201 | 201 | #else |
| 202 | | PyString_AS_STRING(o), |
| | 202 | code = PyString_AS_STRING(o); |
| 203 | 203 | #endif |
| 204 | | Py_file_input, globals, locals); |
| | 204 | result = PyRun_String(code, Py_file_input, globals, locals); |
| 205 | 205 | |
| 206 | 206 | Py_XDECREF(s); |
| 207 | 207 | return result; |
diff -r d3b49a84a821 -r fe1aa7bf0d92 tests/run/exectest.pyx
|
a
|
b
|
|
| 1 | | __doc__ = """# no unicode string, not tested in Python3! |
| | 1 | __doc__ = u""" |
| 2 | 2 | #>>> a |
| 3 | 3 | #Traceback (most recent call last): |
| 4 | 4 | #NameError: name 'a' is not defined |
| … |
… |
|
| 10 | 10 | |
| 11 | 11 | >>> d = {} |
| 12 | 12 | >>> test_dict_scope2(d) |
| 13 | | >>> print d['b'] |
| | 13 | >>> print (d['b']) |
| 14 | 14 | 2 |
| 15 | 15 | |
| 16 | 16 | >>> d1 = {} |
| 17 | 17 | >>> test_dict_scope3(d1, d1) |
| 18 | | >>> print d1['b'] |
| | 18 | >>> print (d1['b']) |
| 19 | 19 | 2 |
| 20 | 20 | |
| 21 | 21 | >>> d1, d2 = {}, {} |
| 22 | 22 | >>> test_dict_scope3(d1, d2) |
| 23 | | >>> print d1.get('b'), d2.get('b') |
| 24 | | None 2 |
| | 23 | >>> print ((d1.get('b'), d2.get('b'))) |
| | 24 | (None, 2) |
| 25 | 25 | |
| 26 | 26 | >>> d1, d2 = {}, {} |
| 27 | 27 | >>> test_dict_scope3(d1, d2) |
| 28 | | >>> print d1.get('b'), d2.get('b') |
| 29 | | None 2 |
| | 28 | >>> print ((d1.get('b'), d2.get('b'))) |
| | 29 | (None, 2) |
| 30 | 30 | |
| 31 | 31 | >>> d1, d2 = dict(a=11), dict(c=5) |
| 32 | 32 | >>> test_dict_scope_ref(d1, d2) |
| 33 | | >>> print d1.get('b'), d2.get('b') |
| 34 | | None 16 |
| | 33 | >>> print ((d1.get('b'), d2.get('b'))) |
| | 34 | (None, 16) |
| 35 | 35 | |
| 36 | 36 | >>> d = dict(a=11, c=5) |
| 37 | 37 | >>> test_dict_scope_ref(d, d) |
| 38 | | >>> print d['b'] |
| | 38 | >>> print (d['b']) |
| 39 | 39 | 16 |
| 40 | 40 | |
| 41 | 41 | >>> d = dict(seq = [1,2,3,4]) |
| … |
… |
|
| 57 | 57 | |
| 58 | 58 | def test_dict_scope1(): |
| 59 | 59 | cdef dict d = {} |
| 60 | | exec "b=1+1" in d |
| 61 | | return d['b'] |
| | 60 | exec u"b=1+1" in d |
| | 61 | return d[u'b'] |
| 62 | 62 | |
| 63 | 63 | def test_dict_scope2(d): |
| 64 | | exec "b=1+1" in d |
| | 64 | exec u"b=1+1" in d |
| 65 | 65 | |
| 66 | 66 | def test_dict_scope3(d1, d2): |
| 67 | | exec "b=1+1" in d1, d2 |
| | 67 | exec u"b=1+1" in d1, d2 |
| 68 | 68 | |
| 69 | 69 | def test_dict_scope_ref(d1, d2): |
| 70 | | exec "b=a+c" in d1, d2 |
| | 70 | exec u"b=a+c" in d1, d2 |
| 71 | 71 | |
| 72 | 72 | def test_def(d, varref): |
| 73 | | exec """ |
| | 73 | exec u""" |
| 74 | 74 | def test(): |
| 75 | 75 | for x in %s: |
| 76 | 76 | yield x+1 |
| 77 | 77 | """ % varref in d |
| 78 | | return d['test'] |
| | 78 | return d[u'test'] |