From e05e7c055749ead6f6ed3a069a8887076e3d0dac Mon Sep 17 00:00:00 2001 From: LisandroDalcin Date: Sat, 20 Sep 2008 13:34:03 +0200 Subject: [PATCH] Signature embedding: Proper display of native types (#2). --- Cython/Compiler/AutoDocTransforms.py | 45 ++++++++-- tests/run/embedsignatures.pyx | 120 ++++++++++++++++++++++++++- 2 files changed, 157 insertions(+), 8 deletions(-) diff --git a/Cython/Compiler/AutoDocTransforms.py b/Cython/Compiler/AutoDocTransforms.py index 3bb990b1..0ffb73ea 100644 --- a/Cython/Compiler/AutoDocTransforms.py +++ b/Cython/Compiler/AutoDocTransforms.py @@ -17,11 +17,31 @@ class EmbedSignature(CythonTransform): self.is_in_class = False 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: - return arg.base_type.name + base_type = arg.base_type + arg_type = base_type.name except AttributeError: - return "" + 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: @@ -68,6 +88,15 @@ class EmbedSignature(CythonTransform): arglist.append('**%s' % kargs.name) return arglist + def _fmt_ret_type(self, ret): + ret_type = ret.name + if ret_type is None: + return '' + modifiers = self._fmt_basic_c_type_modifiers(ret) + if modifiers: + ret_type = '%s %s' % (modifiers, ret_type) + return ret_type + def _fmt_signature(self, cls_name, func_name, args, npargs=0, pargs=None, nkargs=0, kargs=None, @@ -75,12 +104,14 @@ class EmbedSignature(CythonTransform): arglist = self._fmt_arglist(args, npargs, pargs, nkargs, kargs) - arglist = ', '.join(arglist) - func_doc = '%s(%s)' % (func_name, arglist) + arglist_doc = ', '.join(arglist) + func_doc = '%s(%s)' % (func_name, arglist_doc) if cls_name: - func_doc = ('%s.' % cls_name) + func_doc + func_doc = '%s.%s' % (cls_name, func_doc) if return_type: - func_doc = func_doc + ' -> %s' % return_type + ret_doc = self._fmt_ret_type(return_type) + if ret_doc: + func_doc = '%s -> %s' % (func_doc, ret_doc) return func_doc def _embed_signature(self, signature, node_doc): @@ -133,7 +164,7 @@ class EmbedSignature(CythonTransform): signature = self._fmt_signature( self.class_name, node.declarator.base.name, node.declarator.args, - return_type=node.base_type.name) + return_type=node.base_type) else: # should not fall here ... assert False if signature: diff --git a/tests/run/embedsignatures.pyx b/tests/run/embedsignatures.pyx index 9ec42e79..af6a12d8 100644 --- a/tests/run/embedsignatures.pyx +++ b/tests/run/embedsignatures.pyx @@ -48,7 +48,66 @@ __doc__ = ur""" 'with_doc_2(a, b, c)\n\n Existing string\n ' >>> types.__doc__ - 'types(Ext a, int b, int c, float d, e)' + 'types(Ext a, int b, unsigned short int c, float d, e)' + + >>> print (f_c.__doc__) + f_c(char c) -> char + + >>> print (f_uc.__doc__) + f_uc(unsigned char c) -> unsigned char + + >>> print (f_sc.__doc__) + f_sc(signed char c) -> signed char + + + >>> print (f_s.__doc__) + f_s(short int s) -> short int + + >>> print (f_us.__doc__) + f_us(unsigned short int s) -> unsigned short int + + >>> print (f_ss.__doc__) + f_ss(signed short int s) -> signed short int + + + >>> print (f_i.__doc__) + f_i(int i) -> int + + >>> print (f_ui.__doc__) + f_ui(unsigned int i) -> unsigned int + + >>> print (f_si.__doc__) + f_si(signed int i) -> signed int + + + >>> print (f_l.__doc__) + f_l(long int l) -> long int + + >>> print (f_ul.__doc__) + f_ul(unsigned long int l) -> unsigned long int + + >>> print (f_sl.__doc__) + f_sl(signed long int l) -> signed long int + + + >>> print (f_L.__doc__) + f_L(long long int L) -> long long int + + >>> print (f_uL.__doc__) + f_uL(unsigned long long int L) -> unsigned long long int + + >>> print (f_sL.__doc__) + f_sL(signed long long int L) -> signed long long int + + + >>> print (f_f.__doc__) + f_f(float f) -> float + + >>> print (f_d.__doc__) + f_d(double d) -> double + + >>> print (f_D.__doc__) + f_D(long double D) -> long double """ @@ -105,3 +164,62 @@ def with_doc_2(a, b, c): Existing string """ pass + +cpdef char f_c(char c): + return c + +cpdef unsigned char f_uc(unsigned char c): + return c + +cpdef signed char f_sc(signed char c): + return c + + +cpdef short f_s(short s): + return s + +cpdef unsigned short f_us(unsigned short s): + return s + +cpdef signed short f_ss(signed short s): + return s + + +cpdef int f_i(int i): + return i + +cpdef unsigned int f_ui(unsigned int i): + return i + +cpdef signed int f_si(signed int i): + return i + + +cpdef long f_l(long l): + return l + +cpdef unsigned long f_ul(unsigned long l): + return l + +cpdef signed long f_sl(signed long l): + return l + + +cpdef long long f_L(long long L): + return L + +cpdef unsigned long long f_uL(unsigned long long L): + return L + +cpdef signed long long f_sL(signed long long L): + return L + + +cpdef float f_f(float f): + return f + +cpdef double f_d(double d): + return d + +cpdef long double f_D(long double D): + return D -- 2.26.2