Ticket #275: exec-statement.diff

File exec-statement.diff, 2.9 KB (added by dalcinl, 17 months ago)
  • Cython/Compiler/Builtin.py

    # 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  
    168168static PyObject* __Pyx_PyRun(PyObject* o, PyObject* globals, PyObject* locals) { 
    169169    PyObject* result; 
    170170    PyObject* s = 0; 
     171    char *code = 0; 
    171172 
    172173    if (!locals && !globals) { 
    173174        globals = PyModule_GetDict(%s);""" % Naming.module_cname + """ 
     
    195196        goto bad; 
    196197    } 
    197198 
    198     result = PyRun_String( 
    199199    #if PY_MAJOR_VERSION >= 3 
    200         PyBytes_AS_STRING(o), 
     200    code = PyBytes_AS_STRING(o); 
    201201    #else 
    202         PyString_AS_STRING(o), 
     202    code = PyString_AS_STRING(o); 
    203203    #endif 
    204         Py_file_input, globals, locals); 
     204    result = PyRun_String(code, Py_file_input, globals, locals); 
    205205 
    206206    Py_XDECREF(s); 
    207207    return result; 
  • tests/run/exectest.pyx

    diff -r d3b49a84a821 -r fe1aa7bf0d92 tests/run/exectest.pyx
    a b  
    1 __doc__ = """# no unicode string, not tested in Python3! 
     1__doc__ = u""" 
    22#>>> a 
    33#Traceback (most recent call last): 
    44#NameError: name 'a' is not defined 
     
    1010 
    1111>>> d = {} 
    1212>>> test_dict_scope2(d) 
    13 >>> print d['b'] 
     13>>> print (d['b']) 
    14142 
    1515 
    1616>>> d1 = {} 
    1717>>> test_dict_scope3(d1, d1) 
    18 >>> print d1['b'] 
     18>>> print (d1['b']) 
    19192 
    2020 
    2121>>> d1, d2 = {}, {} 
    2222>>> 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) 
    2525 
    2626>>> d1, d2 = {}, {} 
    2727>>> 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) 
    3030 
    3131>>> d1, d2 = dict(a=11), dict(c=5) 
    3232>>> 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) 
    3535 
    3636>>> d = dict(a=11, c=5) 
    3737>>> test_dict_scope_ref(d, d) 
    38 >>> print d['b'] 
     38>>> print (d['b']) 
    393916 
    4040 
    4141>>> d = dict(seq = [1,2,3,4]) 
     
    5757 
    5858def test_dict_scope1(): 
    5959    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'] 
    6262 
    6363def test_dict_scope2(d): 
    64     exec "b=1+1" in d 
     64    exec u"b=1+1" in d 
    6565 
    6666def test_dict_scope3(d1, d2): 
    67     exec "b=1+1" in d1, d2 
     67    exec u"b=1+1" in d1, d2 
    6868 
    6969def test_dict_scope_ref(d1, d2): 
    70     exec "b=a+c" in d1, d2 
     70    exec u"b=a+c" in d1, d2 
    7171 
    7272def test_def(d, varref): 
    73     exec """ 
     73    exec u""" 
    7474def test(): 
    7575    for x in %s: 
    7676        yield x+1 
    7777""" % varref in d 
    78     return d['test'] 
     78    return d[u'test']