From: Lisandro Dalcin Date: Thu, 15 Apr 2010 00:15:42 +0000 (-0300) Subject: do not emit C code for unused special method docstrings X-Git-Tag: 0.13.beta0~205^2 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=24f0ceeed648977206c413104faaf9807e0b92b7;p=cython.git do not emit C code for unused special method docstrings --- diff --git a/Cython/Compiler/AnalysedTreeTransforms.py b/Cython/Compiler/AnalysedTreeTransforms.py index f7ef59bd..6c4e1182 100644 --- a/Cython/Compiler/AnalysedTreeTransforms.py +++ b/Cython/Compiler/AnalysedTreeTransforms.py @@ -11,7 +11,8 @@ import Symtab class AutoTestDictTransform(ScopeTrackingTransform): # Handles autotestdict directive - blacklist = ['__cinit__', '__dealloc__', '__richcmp__', '__nonzero__'] + blacklist = ['__cinit__', '__dealloc__', '__richcmp__', '__nonzero__', + '__len__', '__contains__'] def visit_ModuleNode(self, node): if node.is_pxd: diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py index 55c386d1..9ef17f20 100644 --- a/Cython/Compiler/ModuleNode.py +++ b/Cython/Compiler/ModuleNode.py @@ -1535,7 +1535,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): def generate_method_table(self, env, code): code.putln("") code.putln( - "static struct PyMethodDef %s[] = {" % + "static PyMethodDef %s[] = {" % env.method_table_cname) for entry in env.pyfunc_entries: code.put_pymethoddef(entry, ",") diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index 8781cc1e..696882a5 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -2086,7 +2086,11 @@ class DefNode(FuncDefNode): code.putln("%s; /*proto*/" % header) if proto_only: return - if self.entry.doc and Options.docstrings: + if (Options.docstrings and self.entry.doc and + (not self.entry.is_special or + self.entry.signature.method_flags()) and + not self.entry.scope.is_property_scope + ): docstr = self.entry.doc if docstr.is_unicode: docstr = docstr.utf8encode() diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py index d2ffa6a4..5b74ebc1 100644 --- a/Cython/Compiler/Symtab.py +++ b/Cython/Compiler/Symtab.py @@ -207,6 +207,8 @@ class Scope(object): # return_type PyrexType or None Return type of function owning scope # is_py_class_scope boolean Is a Python class scope # is_c_class_scope boolean Is an extension type scope + # is_cpp_class_scope boolean Is a C++ class scope + # is_property_scope boolean Is a extension type property scope # scope_prefix string Disambiguator for C names # in_cinclude boolean Suppress C declaration code # qualified_name string "modname" or "modname.classname" @@ -220,6 +222,7 @@ class Scope(object): is_py_class_scope = 0 is_c_class_scope = 0 is_cpp_class_scope = 0 + is_property_scope = 0 is_module_scope = 0 scope_prefix = "" in_cinclude = 0 @@ -1619,6 +1622,8 @@ class PropertyScope(Scope): # a property of an extension type. # # parent_type PyExtensionType The type to which the property belongs + + is_property_scope = 1 def declare_pyfunction(self, name, pos): # Add an entry for a method. diff --git a/tests/compile/specmethdocstring.pyx b/tests/compile/specmethdocstring.pyx index 7de1a5d6..b28875a8 100644 --- a/tests/compile/specmethdocstring.pyx +++ b/tests/compile/specmethdocstring.pyx @@ -1,8 +1,30 @@ cdef class C: + def __cinit__(self): + "This is an unusable docstring." def __init__(self): "This is an unusable docstring." + def __dealloc__(self): + "This is an unusable docstring." + def __richcmp__(self, other, int op): + "This is an unusable docstring." + def __nonzero__(self): + "This is an unusable docstring." + return False + def __contains__(self, other): + "This is an unusable docstring." + property foo: def __get__(self): "So is this." def __set__(self, x): "And here is another one." + + def __add__(self, other): + "usable docstring" + def __iter__(self): + "usable docstring" + return False + def __next__(self): + "usable docstring" + return False + diff --git a/tests/run/autotestdict.pyx b/tests/run/autotestdict.pyx index 1a9a6135..2f8bc711 100644 --- a/tests/run/autotestdict.pyx +++ b/tests/run/autotestdict.pyx @@ -110,4 +110,20 @@ cdef class MyCdefClass: False """ + def __len__(self): + """ + Should not be included, as it can't be looked up with getattr in Py 3.1 + + >>> True + False + """ + + def __contains__(self, value): + """ + Should not be included, as it can't be looked up with getattr in Py 3.1 + + >>> True + False + """ + cdeffunc()