From e352a0b37b76a3e0168b07c4893e7cd5e9a17272 Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Wed, 10 Oct 2007 04:32:21 -0700 Subject: [PATCH] rdef keyword for overridable methods --- Cython/Compiler/Nodes.py | 10 +++------- Cython/Compiler/Parsing.py | 23 +++++++++++++++++------ Cython/Compiler/Scanning.py | 2 +- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index 8976cd7a..83f0cf03 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -714,9 +714,7 @@ class CFuncDefNode(FuncDefNode): defining = self.body is not None) self.return_type = type.return_type - if 'overrideable' in self.modifiers or 'visible' in self.modifiers: - if 'visible' in self.modifiers: - self.modifiers.remove('visible') + if self.overridable: import ExprNodes arg_names = [arg.name for arg in self.type.args] self_arg = ExprNodes.NameNode(self.pos, name=arg_names[0]) @@ -735,10 +733,8 @@ class CFuncDefNode(FuncDefNode): env.entries[name] = self.entry if Options.intern_names: self.py_func.interned_attr_cname = env.intern(self.py_func.entry.name) - if 'overrideable' in self.modifiers: - self.modifiers.remove('overrideable') - self.override = OverrideCheckNode(self.pos, py_func = self.py_func) - self.body.stats.insert(0, self.override) + self.override = OverrideCheckNode(self.pos, py_func = self.py_func) + self.body.stats.insert(0, self.override) def declare_arguments(self, env): diff --git a/Cython/Compiler/Parsing.py b/Cython/Compiler/Parsing.py index 409c9f87..0c556d71 100644 --- a/Cython/Compiler/Parsing.py +++ b/Cython/Compiler/Parsing.py @@ -1211,11 +1211,19 @@ def p_statement(s, level, cdef_flag = 0, visibility = 'private'): return p_ctypedef_statement(s, level, visibility) if s.sy == 'cdef': cdef_flag = 1 + overridable = 0 + s.next() + if s.sy == 'rdef': + cdef_flag = 1 + overridable = 1 s.next() if cdef_flag: if level not in ('module', 'module_pxd', 'function', 'c_class', 'c_class_pxd'): s.error('cdef statement not allowed here') - return p_cdef_statement(s, level, visibility) + return p_cdef_statement(s, level, visibility, overridable = overridable) +# elif s.sy == 'rdef': +# s.next() +# return p_c_func_or_var_declaration(s, level, s.position(), visibility = visibility, overridable = True) elif s.sy == 'def': if level not in ('module', 'class', 'c_class', 'property'): s.error('def statement not allowed here') @@ -1570,8 +1578,10 @@ def p_c_arg_decl(s, in_pyfunc, cmethod_flag = 0, kw_only = 0): default = default, kw_only = kw_only) -def p_cdef_statement(s, level, visibility = 'private'): +def p_cdef_statement(s, level, visibility = 'private', overridable = False): pos = s.position() + if overridable and level not in ('c_class', 'c_class_pxd'): + error(pos, "Overridable cdef function not allowed here") visibility = p_visibility(s, visibility) if visibility == 'extern' and s.sy in ('from' ,':'): return p_cdef_extern_block(s, level, pos) @@ -1593,7 +1603,7 @@ def p_cdef_statement(s, level, visibility = 'private'): s.expect_newline('Expected a newline') return node else: - return p_c_func_or_var_declaration(s, level, pos, visibility) + return p_c_func_or_var_declaration(s, level, pos, visibility, overridable) def p_cdef_extern_block(s, level, pos): include_file = None @@ -1698,13 +1708,13 @@ def p_visibility(s, prev_visibility): return visibility def p_c_modifiers(s): - if s.sy == 'IDENT' and s.systring in ('inline', 'visible', 'overrideable'): + if s.sy == 'IDENT' and s.systring in ('inline',): modifier = s.systring s.next() return [modifier] + p_c_modifiers(s) return [] -def p_c_func_or_var_declaration(s, level, pos, visibility = 'private'): +def p_c_func_or_var_declaration(s, level, pos, visibility = 'private', overridable = False): cmethod_flag = level in ('c_class', 'c_class_pxd') modifiers = p_c_modifiers(s) base_type = p_c_base_type(s) @@ -1718,7 +1728,8 @@ def p_c_func_or_var_declaration(s, level, pos, visibility = 'private'): base_type = base_type, declarator = declarator, body = suite, - modifiers = modifiers) + modifiers = modifiers, + overridable = overridable) else: if level == 'module_pxd' and visibility <> 'extern': error(pos, diff --git a/Cython/Compiler/Scanning.py b/Cython/Compiler/Scanning.py index 48ae39af..4bb1b867 100644 --- a/Cython/Compiler/Scanning.py +++ b/Cython/Compiler/Scanning.py @@ -138,7 +138,7 @@ reserved_words = [ "raise", "import", "exec", "try", "except", "finally", "while", "if", "elif", "else", "for", "in", "assert", "and", "or", "not", "is", "in", "lambda", "from", - "NULL", "cimport", "by", "with" + "NULL", "cimport", "by", "with", "rdef" ] function_contexts = [ # allowed arguments to the "with" option -- 2.26.2