From 3f829d6a28c749fe4ae1be7cd931fde5f5847f3c Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Sun, 3 May 2009 19:47:48 +0200 Subject: [PATCH] removed various invalid syntax combinations for the c(p)def statement (#280) --- Cython/Compiler/Parsing.py | 25 +++++++++++++------------ tests/errors/cdef_syntax.pyx | 10 ++++++++++ tests/errors/cpdef_syntax.pyx | 9 +++++++++ 3 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 tests/errors/cdef_syntax.pyx create mode 100644 tests/errors/cpdef_syntax.pyx diff --git a/Cython/Compiler/Parsing.py b/Cython/Compiler/Parsing.py index 8cc72997..55e87550 100644 --- a/Cython/Compiler/Parsing.py +++ b/Cython/Compiler/Parsing.py @@ -2068,31 +2068,30 @@ def p_cdef_statement(s, ctx): elif s.sy == 'import': s.next() return p_cdef_extern_block(s, pos, ctx) - if p_nogil(s): + elif p_nogil(s): ctx.nogil = 1 - if s.sy == ':': + if ctx.overridable: + error(pos, "cdef blocks cannot be declared cpdef") + return p_cdef_block(s, ctx) + elif s.sy == ':': + if ctx.overridable: + error(pos, "cdef blocks cannot be declared cpdef") return p_cdef_block(s, ctx) elif s.sy == 'class': if ctx.level not in ('module', 'module_pxd'): error(pos, "Extension type definition not allowed here") - #if ctx.api: - # error(pos, "'api' not allowed with extension class") + if ctx.overridable: + error(pos, "Extension types cannot be declared cpdef") return p_c_class_definition(s, pos, ctx) elif s.sy == 'IDENT' and s.systring in ("struct", "union", "enum", "packed"): if ctx.level not in ('module', 'module_pxd'): error(pos, "C struct/union/enum definition not allowed here") - #if ctx.visibility == 'public': - # error(pos, "Public struct/union/enum definition not implemented") - #if ctx.api: - # error(pos, "'api' not allowed with '%s'" % s.systring) + if ctx.overridable: + error(pos, "C struct/union/enum cannot be declared cpdef") if s.systring == "enum": return p_c_enum_definition(s, pos, ctx) else: return p_c_struct_or_union_definition(s, pos, ctx) - elif s.sy == 'pass': - node = p_pass_statement(s) - s.expect_newline('Expected a newline') - return node else: return p_c_func_or_var_declaration(s, pos, ctx) @@ -2100,6 +2099,8 @@ def p_cdef_block(s, ctx): return p_suite(s, ctx(cdef_flag = 1)) def p_cdef_extern_block(s, pos, ctx): + if ctx.overridable: + error(pos, "cdef extern blocks cannot be declared cpdef") include_file = None s.expect('from') if s.sy == '*': diff --git a/tests/errors/cdef_syntax.pyx b/tests/errors/cdef_syntax.pyx new file mode 100644 index 00000000..aad39b30 --- /dev/null +++ b/tests/errors/cdef_syntax.pyx @@ -0,0 +1,10 @@ + +cdef pass +cdef void +cdef nogil class test: pass + +_ERRORS = u""" + 2: 5: Expected an identifier, found 'pass' + 3: 9: Empty declarator + 4:11: Expected ':' +""" diff --git a/tests/errors/cpdef_syntax.pyx b/tests/errors/cpdef_syntax.pyx new file mode 100644 index 00000000..f67cd1c4 --- /dev/null +++ b/tests/errors/cpdef_syntax.pyx @@ -0,0 +1,9 @@ + +cpdef nogil: pass +cpdef nogil class test: pass + +_ERRORS = u""" + 2: 6: cdef blocks cannot be declared cpdef + 3: 6: cdef blocks cannot be declared cpdef + 3:12: Expected ':' +""" -- 2.26.2