cython-devel
changeset 1159:d115fd6ed2df
Signature embedding: Proper display of native types (#2).
| author | LisandroDalcin |
|---|---|
| date | Sat Sep 20 13:34:03 2008 +0200 (3 years ago) |
| parents | 137cca05a493 |
| children | 947f6697b088 |
| files | Cython/Compiler/AutoDocTransforms.py tests/run/embedsignatures.pyx |
line diff
1.1 --- a/Cython/Compiler/AutoDocTransforms.py Fri Sep 19 16:38:34 2008 +0200
1.2 +++ b/Cython/Compiler/AutoDocTransforms.py Sat Sep 20 13:34:03 2008 +0200
1.3 @@ -17,11 +17,31 @@
1.4 self.is_in_class = False
1.5 self.class_name = None
1.6
1.7 + def _fmt_basic_c_type_modifiers(self, ctype):
1.8 + longness = ctype.longness
1.9 + modifiers = ''
1.10 + if longness < 0:
1.11 + modifiers = 'short '
1.12 + elif longness > 0:
1.13 + modifiers = 'long ' * longness
1.14 + signed = ctype.signed
1.15 + if signed == 0:
1.16 + modifiers = 'unsigned ' + modifiers
1.17 + elif signed == 2:
1.18 + modifiers = 'signed ' + modifiers
1.19 + return modifiers[:-1] # strip final space
1.20 +
1.21 def _fmt_arg_type(self, arg):
1.22 try:
1.23 - return arg.base_type.name
1.24 + base_type = arg.base_type
1.25 + arg_type = base_type.name
1.26 except AttributeError:
1.27 - return ""
1.28 + return ''
1.29 + if base_type.is_basic_c_type:
1.30 + modifiers = self._fmt_basic_c_type_modifiers(base_type)
1.31 + if modifiers:
1.32 + arg_type = '%s %s' % (modifiers, arg_type)
1.33 + return arg_type
1.34
1.35 def _fmt_arg_name(self, arg):
1.36 try:
1.37 @@ -68,6 +88,15 @@
1.38 arglist.append('**%s' % kargs.name)
1.39 return arglist
1.40
1.41 + def _fmt_ret_type(self, ret):
1.42 + ret_type = ret.name
1.43 + if ret_type is None:
1.44 + return ''
1.45 + modifiers = self._fmt_basic_c_type_modifiers(ret)
1.46 + if modifiers:
1.47 + ret_type = '%s %s' % (modifiers, ret_type)
1.48 + return ret_type
1.49 +
1.50 def _fmt_signature(self, cls_name, func_name, args,
1.51 npargs=0, pargs=None,
1.52 nkargs=0, kargs=None,
1.53 @@ -75,12 +104,14 @@
1.54 arglist = self._fmt_arglist(args,
1.55 npargs, pargs,
1.56 nkargs, kargs)
1.57 - arglist = ', '.join(arglist)
1.58 - func_doc = '%s(%s)' % (func_name, arglist)
1.59 + arglist_doc = ', '.join(arglist)
1.60 + func_doc = '%s(%s)' % (func_name, arglist_doc)
1.61 if cls_name:
1.62 - func_doc = ('%s.' % cls_name) + func_doc
1.63 + func_doc = '%s.%s' % (cls_name, func_doc)
1.64 if return_type:
1.65 - func_doc = func_doc + ' -> %s' % return_type
1.66 + ret_doc = self._fmt_ret_type(return_type)
1.67 + if ret_doc:
1.68 + func_doc = '%s -> %s' % (func_doc, ret_doc)
1.69 return func_doc
1.70
1.71 def _embed_signature(self, signature, node_doc):
1.72 @@ -133,7 +164,7 @@
1.73 signature = self._fmt_signature(
1.74 self.class_name, node.declarator.base.name,
1.75 node.declarator.args,
1.76 - return_type=node.base_type.name)
1.77 + return_type=node.base_type)
1.78 else: # should not fall here ...
1.79 assert False
1.80 if signature:
2.1 --- a/tests/run/embedsignatures.pyx Fri Sep 19 16:38:34 2008 +0200
2.2 +++ b/tests/run/embedsignatures.pyx Sat Sep 20 13:34:03 2008 +0200
2.3 @@ -48,7 +48,66 @@
2.4 'with_doc_2(a, b, c)\n\n Existing string\n '
2.5
2.6 >>> types.__doc__
2.7 - 'types(Ext a, int b, int c, float d, e)'
2.8 + 'types(Ext a, int b, unsigned short int c, float d, e)'
2.9 +
2.10 + >>> print (f_c.__doc__)
2.11 + f_c(char c) -> char
2.12 +
2.13 + >>> print (f_uc.__doc__)
2.14 + f_uc(unsigned char c) -> unsigned char
2.15 +
2.16 + >>> print (f_sc.__doc__)
2.17 + f_sc(signed char c) -> signed char
2.18 +
2.19 +
2.20 + >>> print (f_s.__doc__)
2.21 + f_s(short int s) -> short int
2.22 +
2.23 + >>> print (f_us.__doc__)
2.24 + f_us(unsigned short int s) -> unsigned short int
2.25 +
2.26 + >>> print (f_ss.__doc__)
2.27 + f_ss(signed short int s) -> signed short int
2.28 +
2.29 +
2.30 + >>> print (f_i.__doc__)
2.31 + f_i(int i) -> int
2.32 +
2.33 + >>> print (f_ui.__doc__)
2.34 + f_ui(unsigned int i) -> unsigned int
2.35 +
2.36 + >>> print (f_si.__doc__)
2.37 + f_si(signed int i) -> signed int
2.38 +
2.39 +
2.40 + >>> print (f_l.__doc__)
2.41 + f_l(long int l) -> long int
2.42 +
2.43 + >>> print (f_ul.__doc__)
2.44 + f_ul(unsigned long int l) -> unsigned long int
2.45 +
2.46 + >>> print (f_sl.__doc__)
2.47 + f_sl(signed long int l) -> signed long int
2.48 +
2.49 +
2.50 + >>> print (f_L.__doc__)
2.51 + f_L(long long int L) -> long long int
2.52 +
2.53 + >>> print (f_uL.__doc__)
2.54 + f_uL(unsigned long long int L) -> unsigned long long int
2.55 +
2.56 + >>> print (f_sL.__doc__)
2.57 + f_sL(signed long long int L) -> signed long long int
2.58 +
2.59 +
2.60 + >>> print (f_f.__doc__)
2.61 + f_f(float f) -> float
2.62 +
2.63 + >>> print (f_d.__doc__)
2.64 + f_d(double d) -> double
2.65 +
2.66 + >>> print (f_D.__doc__)
2.67 + f_D(long double D) -> long double
2.68
2.69 """
2.70
2.71 @@ -105,3 +164,62 @@
2.72 Existing string
2.73 """
2.74 pass
2.75 +
2.76 +cpdef char f_c(char c):
2.77 + return c
2.78 +
2.79 +cpdef unsigned char f_uc(unsigned char c):
2.80 + return c
2.81 +
2.82 +cpdef signed char f_sc(signed char c):
2.83 + return c
2.84 +
2.85 +
2.86 +cpdef short f_s(short s):
2.87 + return s
2.88 +
2.89 +cpdef unsigned short f_us(unsigned short s):
2.90 + return s
2.91 +
2.92 +cpdef signed short f_ss(signed short s):
2.93 + return s
2.94 +
2.95 +
2.96 +cpdef int f_i(int i):
2.97 + return i
2.98 +
2.99 +cpdef unsigned int f_ui(unsigned int i):
2.100 + return i
2.101 +
2.102 +cpdef signed int f_si(signed int i):
2.103 + return i
2.104 +
2.105 +
2.106 +cpdef long f_l(long l):
2.107 + return l
2.108 +
2.109 +cpdef unsigned long f_ul(unsigned long l):
2.110 + return l
2.111 +
2.112 +cpdef signed long f_sl(signed long l):
2.113 + return l
2.114 +
2.115 +
2.116 +cpdef long long f_L(long long L):
2.117 + return L
2.118 +
2.119 +cpdef unsigned long long f_uL(unsigned long long L):
2.120 + return L
2.121 +
2.122 +cpdef signed long long f_sL(signed long long L):
2.123 + return L
2.124 +
2.125 +
2.126 +cpdef float f_f(float f):
2.127 + return f
2.128 +
2.129 +cpdef double f_d(double d):
2.130 + return d
2.131 +
2.132 +cpdef long double f_D(long double D):
2.133 + return D
