Commit e05e7c05 authored by LisandroDalcin's avatar LisandroDalcin

Signature embedding: Proper display of native types (#2).

parent 3ed359ce
...@@ -17,11 +17,31 @@ class EmbedSignature(CythonTransform): ...@@ -17,11 +17,31 @@ class EmbedSignature(CythonTransform):
self.is_in_class = False 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): def _fmt_arg_type(self, arg):
try: try:
return arg.base_type.name base_type = arg.base_type
arg_type = base_type.name
except AttributeError: 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): def _fmt_arg_name(self, arg):
try: try:
...@@ -68,6 +88,15 @@ class EmbedSignature(CythonTransform): ...@@ -68,6 +88,15 @@ class EmbedSignature(CythonTransform):
arglist.append('**%s' % kargs.name) arglist.append('**%s' % kargs.name)
return arglist 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, def _fmt_signature(self, cls_name, func_name, args,
npargs=0, pargs=None, npargs=0, pargs=None,
nkargs=0, kargs=None, nkargs=0, kargs=None,
...@@ -75,12 +104,14 @@ class EmbedSignature(CythonTransform): ...@@ -75,12 +104,14 @@ class EmbedSignature(CythonTransform):
arglist = self._fmt_arglist(args, arglist = self._fmt_arglist(args,
npargs, pargs, npargs, pargs,
nkargs, kargs) nkargs, kargs)
arglist = ', '.join(arglist) arglist_doc = ', '.join(arglist)
func_doc = '%s(%s)' % (func_name, arglist) func_doc = '%s(%s)' % (func_name, arglist_doc)
if cls_name: if cls_name:
func_doc = ('%s.' % cls_name) + func_doc func_doc = '%s.%s' % (cls_name, func_doc)
if return_type: 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 return func_doc
def _embed_signature(self, signature, node_doc): def _embed_signature(self, signature, node_doc):
...@@ -133,7 +164,7 @@ class EmbedSignature(CythonTransform): ...@@ -133,7 +164,7 @@ 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.name) return_type=node.base_type)
else: # should not fall here ... else: # should not fall here ...
assert False assert False
if signature: if signature:
......
...@@ -48,7 +48,66 @@ __doc__ = ur""" ...@@ -48,7 +48,66 @@ __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, 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): ...@@ -105,3 +164,62 @@ def with_doc_2(a, b, c):
Existing string Existing string
""" """
pass 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
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