Commit 5249c536 authored by Robert Bradshaw's avatar Robert Bradshaw

Move autodoc transform to later in the pipeline, remove redundant type formatting code.

parent 8cee959e
import re
from Cython.Compiler.Visitor import CythonTransform from Cython.Compiler.Visitor import CythonTransform
from Cython.Compiler.Nodes import DefNode, CFuncDefNode from Cython.Compiler.Nodes import DefNode, CFuncDefNode
from Cython.Compiler.Errors import CompileError from Cython.Compiler.Errors import CompileError
from Cython.Compiler.StringEncoding import EncodedString from Cython.Compiler.StringEncoding import EncodedString
from Cython.Compiler import Options from Cython.Compiler import Options
from Cython.Compiler import PyrexTypes
class EmbedSignature(CythonTransform):
SPECIAL_METHOD_RE = re.compile(r'__\w+__') class EmbedSignature(CythonTransform):
def __init__(self, context): def __init__(self, context):
super(EmbedSignature, self).__init__(context) super(EmbedSignature, self).__init__(context)
self.denv = None # XXX self.denv = None # XXX
self.is_in_class = False
self.class_name = None self.class_name = None
def _fmt_basic_c_type_modifiers(self, ctype):
longness = ctype.longness
modifiers = ''
if longness < 0:
modifiers = 'short '
elif longness > 0:
modifiers = 'long ' * longness
signed = ctype.signed
if signed == 0:
modifiers = 'unsigned ' + modifiers
elif signed == 2:
modifiers = 'signed ' + modifiers
return modifiers[:-1] # strip final space
def _fmt_arg_type(self, arg):
try:
base_type = arg.base_type
arg_type = base_type.name
except AttributeError:
return ''
if base_type.is_basic_c_type:
modifiers = self._fmt_basic_c_type_modifiers(base_type)
if modifiers:
arg_type = '%s %s' % (modifiers, arg_type)
return arg_type
def _fmt_arg_name(self, arg):
try:
return arg.declarator.name
except AttributeError:
return arg.declarator.base.name
def _fmt_arg_defv(self, arg): def _fmt_arg_defv(self, arg):
if not arg.default: if not arg.default:
return None return None
...@@ -63,14 +28,14 @@ class EmbedSignature(CythonTransform): ...@@ -63,14 +28,14 @@ class EmbedSignature(CythonTransform):
return '<???>' return '<???>'
def _fmt_arg(self, arg): def _fmt_arg(self, arg):
arg_type = self._fmt_arg_type(arg) if arg.type is PyrexTypes.py_object_type or arg.is_self_arg:
arg_name = self._fmt_arg_name(arg) doc = arg.name
arg_defv = self._fmt_arg_defv(arg) else:
doc = arg_name doc = arg.type.declaration_code(arg.name, for_display=1)
if arg_type: if arg.default:
doc = ('%s ' % arg_type) + doc arg_defv = self._fmt_arg_defv(arg)
if arg_defv: if arg_defv:
doc = doc + ('=%s' % arg_defv) doc = doc + ('=%s' % arg_defv)
return doc return doc
def _fmt_arglist(self, args, def _fmt_arglist(self, args,
...@@ -89,13 +54,10 @@ class EmbedSignature(CythonTransform): ...@@ -89,13 +54,10 @@ class EmbedSignature(CythonTransform):
return arglist return arglist
def _fmt_ret_type(self, ret): def _fmt_ret_type(self, ret):
ret_type = ret.name if ret is PyrexTypes.py_object_type:
if ret_type is None: return None
return '' else:
modifiers = self._fmt_basic_c_type_modifiers(ret) return ret.declaration_code("", for_display=1)
if modifiers:
ret_type = '%s %s' % (modifiers, ret_type)
return ret_type
def _fmt_signature(self, cls_name, func_name, args, def _fmt_signature(self, cls_name, func_name, args,
npargs=0, pargs=None, npargs=0, pargs=None,
...@@ -128,9 +90,7 @@ class EmbedSignature(CythonTransform): ...@@ -128,9 +90,7 @@ class EmbedSignature(CythonTransform):
return super(EmbedSignature, self).__call__(node) return super(EmbedSignature, self).__call__(node)
def visit_ClassDefNode(self, node): def visit_ClassDefNode(self, node):
oldincls = self.is_in_class
oldname = self.class_name oldname = self.class_name
self.is_in_class = True
try: try:
# PyClassDefNode # PyClassDefNode
self.class_name = node.name self.class_name = node.name
...@@ -138,7 +98,6 @@ class EmbedSignature(CythonTransform): ...@@ -138,7 +98,6 @@ class EmbedSignature(CythonTransform):
# CClassDefNode # CClassDefNode
self.class_name = node.class_name self.class_name = node.class_name
self.visitchildren(node) self.visitchildren(node)
self.is_in_class = oldincls
self.class_name = oldname self.class_name = oldname
return node return node
...@@ -148,9 +107,7 @@ class EmbedSignature(CythonTransform): ...@@ -148,9 +107,7 @@ class EmbedSignature(CythonTransform):
signature = None signature = None
if type(node) is DefNode: # def FOO(...): if type(node) is DefNode: # def FOO(...):
special_method = (self.is_in_class and \ if not node.entry.is_special:
self.SPECIAL_METHOD_RE.match(node.name))
if not special_method:
nkargs = getattr(node, 'num_kwonly_args', 0) nkargs = getattr(node, 'num_kwonly_args', 0)
npargs = len(node.args) - nkargs npargs = len(node.args) - nkargs
signature = self._fmt_signature( signature = self._fmt_signature(
...@@ -163,10 +120,12 @@ class EmbedSignature(CythonTransform): ...@@ -163,10 +120,12 @@ class EmbedSignature(CythonTransform):
signature = self._fmt_signature( signature = self._fmt_signature(
self.class_name, node.declarator.base.name, self.class_name, node.declarator.base.name,
node.declarator.args, node.declarator.args,
return_type=node.base_type) return_type=node.return_type)
else: # should not fall here ... else: # should not fall here ...
assert False assert False
if signature: if signature:
new_doc = self._embed_signature(signature, node.doc) new_doc = self._embed_signature(signature, node.entry.doc)
node.doc = EncodedString(new_doc) # XXX node.entry.doc = EncodedString(new_doc)
if hasattr(node, 'py_func') and node.py_func is not None:
node.py_func.entry.doc = EncodedString(new_doc)
return node return node
...@@ -97,11 +97,11 @@ class Context: ...@@ -97,11 +97,11 @@ class Context:
PostParse(self), PostParse(self),
_specific_post_parse, _specific_post_parse,
InterpretCompilerDirectives(self, self.pragma_overrides), InterpretCompilerDirectives(self, self.pragma_overrides),
EmbedSignature(self),
FlattenInListTransform(), FlattenInListTransform(),
WithTransform(self), WithTransform(self),
DecoratorTransform(self), DecoratorTransform(self),
AnalyseDeclarationsTransform(self), AnalyseDeclarationsTransform(self),
EmbedSignature(self),
TransformBuiltinMethods(self), TransformBuiltinMethods(self),
IntroduceBufferAuxiliaryVars(self), IntroduceBufferAuxiliaryVars(self),
_check_c_classes, _check_c_classes,
......
...@@ -472,6 +472,8 @@ class CNumericType(CType): ...@@ -472,6 +472,8 @@ class CNumericType(CType):
def declaration_code(self, entity_code, def declaration_code(self, entity_code,
for_display = 0, dll_linkage = None, pyrex = 0): for_display = 0, dll_linkage = None, pyrex = 0):
base = public_decl(self.sign_and_name(), dll_linkage) base = public_decl(self.sign_and_name(), dll_linkage)
if for_display and self.is_longlong:
base = base.replace('PY_LONG_LONG', 'long long')
return self.base_declaration_code(base, entity_code) return self.base_declaration_code(base, entity_code)
...@@ -556,7 +558,7 @@ class CULongType(CUIntType): ...@@ -556,7 +558,7 @@ class CULongType(CUIntType):
from_py_function = "PyInt_AsUnsignedLongMask" from_py_function = "PyInt_AsUnsignedLongMask"
class CLongLongType(CUIntType): class CLongLongType(CIntType):
is_longlong = 1 is_longlong = 1
to_py_function = "PyLong_FromLongLong" to_py_function = "PyLong_FromLongLong"
......
...@@ -48,7 +48,7 @@ __doc__ = ur""" ...@@ -48,7 +48,7 @@ __doc__ = ur"""
'with_doc_2(a, b, c)\n\n Existing string\n ' 'with_doc_2(a, b, c)\n\n Existing string\n '
>>> types.__doc__ >>> types.__doc__
'types(Ext a, int b, unsigned short int c, float d, e)' 'types(Ext a, int b, unsigned short c, float d, e)'
>>> print (f_c.__doc__) >>> print (f_c.__doc__)
f_c(char c) -> char f_c(char c) -> char
...@@ -61,13 +61,13 @@ __doc__ = ur""" ...@@ -61,13 +61,13 @@ __doc__ = ur"""
>>> print (f_s.__doc__) >>> print (f_s.__doc__)
f_s(short int s) -> short int f_s(short s) -> short
>>> print (f_us.__doc__) >>> print (f_us.__doc__)
f_us(unsigned short int s) -> unsigned short int f_us(unsigned short s) -> unsigned short
>>> print (f_ss.__doc__) >>> print (f_ss.__doc__)
f_ss(signed short int s) -> signed short int f_ss(signed short s) -> signed short
>>> print (f_i.__doc__) >>> print (f_i.__doc__)
...@@ -81,23 +81,23 @@ __doc__ = ur""" ...@@ -81,23 +81,23 @@ __doc__ = ur"""
>>> print (f_l.__doc__) >>> print (f_l.__doc__)
f_l(long int l) -> long int f_l(long l) -> long
>>> print (f_ul.__doc__) >>> print (f_ul.__doc__)
f_ul(unsigned long int l) -> unsigned long int f_ul(unsigned long l) -> unsigned long
>>> print (f_sl.__doc__) >>> print (f_sl.__doc__)
f_sl(signed long int l) -> signed long int f_sl(signed long l) -> signed long
>>> print (f_L.__doc__) >>> print (f_L.__doc__)
f_L(long long int L) -> long long int f_L(long long L) -> long long
>>> print (f_uL.__doc__) >>> print (f_uL.__doc__)
f_uL(unsigned long long int L) -> unsigned long long int f_uL(unsigned long long L) -> unsigned long long
>>> print (f_sL.__doc__) >>> print (f_sL.__doc__)
f_sL(signed long long int L) -> signed long long int f_sL(signed long long L) -> signed long long
>>> print (f_f.__doc__) >>> print (f_f.__doc__)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment