summaryrefslogtreecommitdiff
path: root/tests/run/embedsignatures.pyx
blob: 952e4ee3628cbc9bafbbd3da4e72afc04b9793ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#cython: embedsignature=True

import sys

if sys.version_info >= (3, 4):
    def funcdoc(f):
        if not f.__text_signature__:
            return f.__doc__
        doc = '%s%s' % (f.__name__, f.__text_signature__)
        if f.__doc__:
            if '\n' in f.__doc__:
                # preceding line endings get stripped
                doc = '%s\n\n%s' % (doc, f.__doc__)
            else:
                doc = '%s\n%s' % (doc, f.__doc__)
        return doc

else:
    def funcdoc(f):
        return f.__doc__


# note the r, we use \n below
__doc__ = ur"""
    >>> print (Ext.__doc__)
    Ext(a, b, c=None)

    >>> print (Ext.attr0.__doc__)
    attr0: 'int'
    attr0 docstring
    >>> print (Ext.attr1.__doc__)
    attr1: object
    attr1 docstring
    >>> print (Ext.attr2.__doc__)
    attr2: list
    >>> print (Ext.attr3.__doc__)
    attr3: embedsignatures.Ext

    >>> print (Ext.prop0.__doc__)
    prop0 docstring
    >>> print (Ext.prop1.__doc__)
    None
    >>> print (Ext.attr4.__doc__)
    attr4 docstring
    >>> print (Ext.attr5.__doc__)
    attr5: 'int'
    attr5 docstring

    >>> print (Ext.a.__doc__)
    Ext.a(self)

    >>> print (Ext.b.__doc__)
    Ext.b(self, a, b, c)

    >>> print (Ext.c.__doc__)
    Ext.c(self, a, b, c=1)

    >>> print (Ext.d.__doc__)
    Ext.d(self, a, b, *, c=88)

    >>> print (Ext.e.__doc__)
    Ext.e(self, a, b, c=88, **kwds)

    >>> print (Ext.f.__doc__)
    Ext.f(self, a, b, *, c, d=42)

    >>> print (Ext.g.__doc__)
    Ext.g(self, a, b, *, c, d=42, e=17, f, **kwds)

    >>> print (Ext.h.__doc__)
    Ext.h(self, a, b, *args, c, d=42, e=17, f, **kwds)

    >>> print (Ext.k.__doc__)
    Ext.k(self, a, b, c=1, *args, d=42, e=17, f, **kwds)

    >>> print (Ext.l.__doc__)
    Ext.l(self, a, b, c=1, *args, d=42, e=17, f, **kwds)
    Existing string

    >>> print (Ext.m.__doc__)
    Ext.m(self, a=u'spam')

    >>> print (Ext.n.__doc__)
    Ext.n(self, a: int, b: float = 1.0, *args: tuple, **kwargs: dict) -> (None, True)

    >>> print (Ext.get_int.__doc__)
    Ext.get_int(self) -> int

    >>> print (Ext.get_float.__doc__)
    Ext.get_float(self) -> float

    >>> print (Ext.get_str.__doc__)
    Ext.get_str(self) -> str
    Existing string

    >>> print (Ext.clone.__doc__)
    Ext.clone(self) -> Ext

    >>> print (funcdoc(foo))
    foo()

    >>> funcdoc(with_doc_1)
    'with_doc_1(a, b, c)\nExisting string'

    >>> funcdoc(with_doc_2)
    'with_doc_2(a, b, c)\n\n    Existing string\n    '

    >>> funcdoc(with_doc_3)
    'with_doc_3(a, b, c)\nExisting string'

    >>> funcdoc(with_doc_4)
    'with_doc_4(int a, str b, list c) -> str\n\n    Existing string\n    '

    >>> funcdoc(f_sd)
    "f_sd(str s='spam')"

    >>> funcdoc(cf_sd)
    "cf_sd(str s='spam') -> str"

    >>> funcdoc(types)
    'types(Ext a, int b, unsigned short c, float d, e)'

    >>> print(funcdoc(f_c))
    f_c(char c) -> char

    >>> print(funcdoc(f_uc))
    f_uc(unsigned char c) -> unsigned char

    >>> print(funcdoc(f_sc))
    f_sc(signed char c) -> signed char

    >>> print(funcdoc(f_s))
    f_s(short s) -> short

    >>> print(funcdoc(f_us))
    f_us(unsigned short s) -> unsigned short


    >>> print(funcdoc(f_i))
    f_i(int i) -> int

    >>> print(funcdoc(f_ui))
    f_ui(unsigned int i) -> unsigned int

    >>> print(funcdoc(f_bint))
    f_bint(bool i) -> bool


    >>> print(funcdoc(f_l))
    f_l(long l) -> long

    >>> print(funcdoc(f_ul))
    f_ul(unsigned long l) -> unsigned long


    >>> print(funcdoc(f_L))
    f_L(long long L) -> long long

    >>> print(funcdoc(f_uL))
    f_uL(unsigned long long L) -> unsigned long long


    >>> print(funcdoc(f_f))
    f_f(float f) -> float

    >>> print(funcdoc(f_d))
    f_d(double d) -> double

    >>> print(funcdoc(f_D))
    f_D(long double D) -> long double

    >>> print(funcdoc(f_my_i))
    f_my_i(MyInt i) -> MyInt

    >>> print(funcdoc(f_my_f))
    f_my_f(MyFloat f) -> MyFloat

    >>> print(funcdoc(f_defexpr1))
    f_defexpr1(int x=FLAG1, int y=FLAG2)

    >>> print(funcdoc(f_defexpr2))
    f_defexpr2(int x=FLAG1 | FLAG2, y=FLAG1 & FLAG2)

    >>> print(funcdoc(f_defexpr3))
    f_defexpr3(int x=Ext.CONST1, f=__builtins__.abs)

    >>> print(funcdoc(f_defexpr4))
    f_defexpr4(int x=(Ext.CONST1 + FLAG1) * Ext.CONST2)

    >>> print(funcdoc(f_defexpr5))
    f_defexpr5(int x=2 + 2)

    >>> print(funcdoc(f_charptr_null))
    f_charptr_null(char *s=NULL) -> char *
"""

cdef class Ext:

    cdef public int  attr0
    """attr0 docstring"""
    cdef public      attr1
    """attr1 docstring"""
    cdef public list attr2
    cdef public Ext attr3

    """NOT attr3 docstring"""
    cdef        int  attr4
    cdef public int \
        attr5
    """attr5 docstring"""

    CONST1, CONST2 = 1, 2

    property prop0:
        """prop0 docstring"""
        def __get__(self):
            return self.attr0

    property prop1:
        def __get__(self):
            return self.attr1

    property attr4:
        """attr4 docstring"""
        def __get__(self):
            return self.attr4

    def __init__(self, a, b, c=None):
        pass

    def a(self):
        pass

    def b(self, a, b, c):
        pass

    def c(self, a, b, c=1):
        pass

    def d(self, a, b, *, c = 88):
        pass

    def e(self, a, b, c = 88, **kwds):
        pass

    def f(self, a, b, *, c, d = 42):
        pass

    def g(self, a, b, *, c, d = 42, e = 17, f, **kwds):
        pass

    def h(self, a, b, *args, c, d = 42, e = 17, f, **kwds):
        pass

    def k(self, a, b, c=1, *args, d = 42, e = 17, f, **kwds):
        pass

    def l(self, a, b, c=1, *args, d = 42, e = 17, f, **kwds):
        """Existing string"""
        pass

    def m(self, a=u'spam'):
        pass

    def n(self, a: int, b: float = 1.0, *args: tuple, **kwargs: dict) -> (None, True):
        pass

    cpdef int get_int(self):
        return 0

    cpdef float get_float(self):
        return 0.0

    cpdef str get_str(self):
        """Existing string"""
        return "string"

    cpdef Ext clone(self):
        return Ext(1,2)

def foo():
    pass

def types(Ext a, int b, unsigned short c, float d, e):
    pass

def with_doc_1(a, b, c):
    """Existing string"""
    pass

def with_doc_2(a, b, c):
    """
    Existing string
    """
    pass

cpdef with_doc_3(a, b, c):
    """Existing string"""
    pass

cpdef str with_doc_4(int a, str b, list c):
    """
    Existing string
    """
    return b

def f_sd(str s='spam'):
    return s

cpdef str cf_sd(str s='spam'):
    return s

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 int f_i(int i):
    return i

cpdef unsigned int f_ui(unsigned int i):
    return i

cpdef bint f_bint(bint i):
    return i


cpdef long f_l(long l):
    return l

cpdef unsigned long f_ul(unsigned 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 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

ctypedef int MyInt
cpdef MyInt f_my_i(MyInt i):
    return i

ctypedef float MyFloat
cpdef MyFloat f_my_f(MyFloat f):
    return f

cdef enum:
    FLAG1
    FLAG2

cpdef f_defexpr1(int x = FLAG1, int y = FLAG2):
    pass

cpdef f_defexpr2(int x = FLAG1 | FLAG2, y = FLAG1 & FLAG2):
    pass

cpdef f_defexpr3(int x = Ext.CONST1, f = __builtins__.abs):
    pass

cpdef f_defexpr4(int x = (Ext.CONST1 + FLAG1) * Ext.CONST2):
    pass

cpdef f_defexpr5(int x = 2+2):
    pass

cpdef (char*) f_charptr_null(char* s=NULL):
    return s or b'abc'


# no signatures for lambda functions
lambda_foo = lambda x: 10
lambda_bar = lambda x: 20


cdef class Foo:
    def m00(self, a: None) ->  None: pass
    def m01(self, a: ...) ->  Ellipsis: pass
    def m02(self, a: True, b: False) ->  bool: pass
    def m03(self, a: 42, b: +42, c: -42) ->  int : pass  # XXX +42 -> 42
    def m04(self, a: 3.14, b: +3.14, c: -3.14) -> float : pass
    def m05(self, a: 1 + 2j, b: +2j, c: -2j) -> complex : pass
    def m06(self, a: "abc", b: b"abc", c: u"abc") -> (str, bytes, unicode) : pass
    def m07(self, a: [1, 2, 3], b: []) -> list: pass
    def m08(self, a: (1, 2, 3), b: ()) -> tuple: pass
    def m09(self, a: {1, 2, 3}, b: {i for i in ()}) -> set: pass
    def m10(self, a: {1: 1, 2: 2, 3: 3}, b: {}) -> dict: pass
   #def m11(self, a: [str(i) for i in range(3)]): pass  # Issue 1782
    def m12(self, a: (str(i) for i in range(3))): pass
    def m13(self, a: (str(i) for i in range(3) if bool(i))): pass
    def m14(self, a: {str(i) for i in range(3)}): pass
    def m15(self, a: {str(i) for i in range(3) if bool(i)}): pass
    def m16(self, a: {str(i): id(i) for i in range(3)}): pass
    def m17(self, a: {str(i): id(i) for i in range(3) if bool(i)}): pass
    def m18(self, a: dict.update(x=42, **dict(), **{})): pass
    def m19(self, a: sys is None, b: sys is not None): pass
    def m20(self, a: sys in [], b: sys not in []): pass
    def m21(self, a: (sys or sys) and sys, b: not (sys or sys)): pass
    def m22(self, a: 42 if sys else None): pass
    def m23(self, a: +int(), b: -int(), c: ~int()): pass
    def m24(self, a: (1+int(2))*3+(4*int(5))**(1+0.0/1)): pass
    def m25(self, a: list(range(3))[:]): pass
    def m26(self, a: list(range(3))[1:]): pass
    def m27(self, a: list(range(3))[:1]): pass
    def m28(self, a: list(range(3))[::1]): pass
    def m29(self, a: list(range(3))[0:1:1]): pass
    def m30(self, a: list(range(3))[7, 3:2:1, ...]): pass

__doc__ += ur"""
>>> print(Foo.m00.__doc__)
Foo.m00(self, a: None) -> None

>>> print(Foo.m01.__doc__)
Foo.m01(self, a: ...) -> Ellipsis

>>> print(Foo.m02.__doc__)
Foo.m02(self, a: True, b: False) -> bool

>>> print(Foo.m03.__doc__)
Foo.m03(self, a: 42, b: 42, c: -42) -> int

>>> print(Foo.m04.__doc__)
Foo.m04(self, a: 3.14, b: 3.14, c: -3.14) -> float

>>> print(Foo.m05.__doc__)
Foo.m05(self, a: 1 + 2j, b: +2j, c: -2j) -> complex

>>> print(Foo.m06.__doc__)
Foo.m06(self, a: 'abc', b: b'abc', c: u'abc') -> (str, bytes, unicode)

>>> print(Foo.m07.__doc__)
Foo.m07(self, a: [1, 2, 3], b: []) -> list

>>> print(Foo.m08.__doc__)
Foo.m08(self, a: (1, 2, 3), b: ()) -> tuple

>>> print(Foo.m09.__doc__)
Foo.m09(self, a: {1, 2, 3}, b: set()) -> set

>>> print(Foo.m10.__doc__)
Foo.m10(self, a: {1: 1, 2: 2, 3: 3}, b: {}) -> dict

# >>> print(Foo.m11.__doc__)
# Foo.m11(self, a: [str(i) for i in range(3)])

>>> print(Foo.m12.__doc__)
Foo.m12(self, a: (str(i) for i in range(3)))

>>> print(Foo.m13.__doc__)
Foo.m13(self, a: (str(i) for i in range(3) if bool(i)))

>>> print(Foo.m14.__doc__)
Foo.m14(self, a: {str(i) for i in range(3)})

>>> print(Foo.m15.__doc__)
Foo.m15(self, a: {str(i) for i in range(3) if bool(i)})

>>> print(Foo.m16.__doc__)
Foo.m16(self, a: {str(i): id(i) for i in range(3)})

>>> print(Foo.m17.__doc__)
Foo.m17(self, a: {str(i): id(i) for i in range(3) if bool(i)})

>>> print(Foo.m18.__doc__)
Foo.m18(self, a: dict.update(x=42, **dict()))

>>> print(Foo.m19.__doc__)
Foo.m19(self, a: sys is None, b: sys is not None)

>>> print(Foo.m20.__doc__)
Foo.m20(self, a: sys in [], b: sys not in [])

>>> print(Foo.m21.__doc__)
Foo.m21(self, a: (sys or sys) and sys, b: not (sys or sys))

>>> print(Foo.m22.__doc__)
Foo.m22(self, a: 42 if sys else None)

>>> print(Foo.m23.__doc__)
Foo.m23(self, a: +int(), b: -int(), c: ~int())

>>> print(Foo.m24.__doc__)
Foo.m24(self, a: (1 + int(2)) * 3 + (4 * int(5)) ** (1 + 0.0 / 1))

>>> print(Foo.m25.__doc__)
Foo.m25(self, a: list(range(3))[:])

>>> print(Foo.m26.__doc__)
Foo.m26(self, a: list(range(3))[1:])

>>> print(Foo.m27.__doc__)
Foo.m27(self, a: list(range(3))[:1])

>>> print(Foo.m28.__doc__)
Foo.m28(self, a: list(range(3))[::1])

>>> print(Foo.m29.__doc__)
Foo.m29(self, a: list(range(3))[0:1:1])

>>> print(Foo.m30.__doc__)
Foo.m30(self, a: list(range(3))[7, 3:2:1, ...])
"""