Commit 9a8c817d authored by Stefan Behnel's avatar Stefan Behnel

loads of doctests

parent b355d4c9
cdef int f() except -1: __doc__ = """
>>> print f()
"""
def f():
g = getattr3 g = getattr3
return g.__name__
cdef extern object g(object x) nogil cdef extern object g(int x) nogil
cdef void f(int x) nogil: cdef void f(int x) nogil:
cdef int y cdef int y
......
def f(): __doc__ = """
cdef int i >>> f(5)
5
"""
def f(int a):
cdef int i,j
cdef int *p cdef int *p
i = a
p = &i p = &i
j = p[0]
return j
__doc__ = """
>>> getg()
5
>>> f(42)
>>> getg()
42
"""
g = 5
def f(a): def f(a):
global f global g
f = a g = a
f = 42
def getg():
return g
def f(a, b): __doc__ = """
cdef int i >>> f(1, 2, 1)
>>> f(0, 2, 1)
Traceback (most recent call last):
AssertionError
>>> f(1, -1, 1)
Traceback (most recent call last):
AssertionError
>>> f(1, 2, 0)
Traceback (most recent call last):
AssertionError
>>> g(1, "works")
>>> g(0, "fails")
Traceback (most recent call last):
AssertionError: fails
"""
def f(a, b, int i):
assert a assert a
assert a+b assert a+b
assert i assert i
......
def f(a, b): __doc__ = """
>>> class Test:
... def __init__(self, i):
... self.i = i
>>> b = Test(1)
>>> b.spam = Test(2)
>>> b.spam.eggs = Test(3)
>>> b.spam.eggs.spam = Test(4)
>>> b.spam.eggs.spam.eggs = Test(5)
>>> a = f(b)
>>> print a.i
2
>>> print b.i
1
>>> print a.spam.i
1
>>> print b.spam.i
2
>>> print a.spam.eggs.i
Traceback (most recent call last):
AttributeError: Test instance has no attribute 'eggs'
>>> print b.spam.eggs.i
3
>>> print a.spam.spam.i
2
>>> print b.spam.spam.i
1
>>> print a.spam.eggs.spam.i
Traceback (most recent call last):
AttributeError: Test instance has no attribute 'eggs'
>>> print b.spam.eggs.spam.i
4
>>> a = g(b)
>>> print a.i
3
>>> print b.i
1
>>> print a.spam.i
4
>>> print b.spam.i
2
>>> print a.spam.eggs.i
1
>>> print b.spam.eggs.i
3
>>> print a.spam.spam.i
Traceback (most recent call last):
AttributeError: Test instance has no attribute 'spam'
>>> print b.spam.spam.i
1
>>> print a.spam.eggs.spam.i
2
>>> print b.spam.eggs.spam.i
4
"""
def f(b):
a = b.spam a = b.spam
a.spam = b a.spam = b
return a
def g(b):
a = b.spam.eggs a = b.spam.eggs
a.spam.eggs = b a.spam.eggs = b
return a
\ No newline at end of file
__doc__ = """
>>> m = MyClass()
>>> m is foo(m)
True
"""
cdef class MyClass: cdef class MyClass:
pass pass
......
__doc__ = """
>>> viking(5)
5
"""
cdef class Spam: cdef class Spam:
cdef eggs(self): cdef eggs(self, a):
pass return a
cdef Spam spam(): cdef Spam spam():
pass return Spam()
def viking(): def viking(a):
return spam().eggs() return spam().eggs(a)
__doc__ = """
>>> f = foo()
>>> 'a' in f
True
>>> 1 in f
True
"""
cdef class foo: cdef class foo:
def __contains__(self, key): def __contains__(self, key):
......
__doc__ = """
>>> test()
"""
cdef void ftang(): cdef void ftang():
cdef int x cdef int x
x = 0 x = 0
...@@ -10,3 +14,8 @@ cdef int foo(int i, char c): ...@@ -10,3 +14,8 @@ cdef int foo(int i, char c):
cdef spam(int i, obj, object object): cdef spam(int i, obj, object object):
cdef char c cdef char c
c = 0 c = 0
def test():
ftang()
foo(0, c'f')
spam(25, None, None)
__doc__ = """
>>> f()
(5376, 67)
"""
def f(): def f():
cdef int int1, int2, int3 cdef int int1, int2, int3
cdef char char1 cdef char char1
cdef long long1, long2 cdef long long1, long2
int2 = 42
int3 = 7
char1 = c'C'
int1 = int2 | int3 int1 = int2 | int3
int1 = int2 ^ int3 int1 = int2 ^ int3
int1 = int2 & int3 int1 = int2 & int3
...@@ -9,5 +18,4 @@ def f(): ...@@ -9,5 +18,4 @@ def f():
int1 = int2 >> int3 int1 = int2 >> int3
int1 = int2 << int3 | int2 >> int3 int1 = int2 << int3 | int2 >> int3
long1 = char1 | long2 long1 = char1 | long2
return int1, long1
\ No newline at end of file
__doc__ = """
>>> test_i()
>>> test_c()
>>> test_p()
>>> test_g()
"""
cdef struct Grail cdef struct Grail
cdef struct Spam: cdef struct Spam:
...@@ -11,9 +18,42 @@ cdef struct Grail: ...@@ -11,9 +18,42 @@ cdef struct Grail:
cdef Spam spam, ham cdef Spam spam, ham
cdef void eggs(Spam s): cdef void eggs_i(Spam s):
cdef int j cdef int j
j = s.i j = s.i
s.i = j s.i = j
cdef void eggs_c(Spam s):
cdef char c
c = s.c
s.c = c
cdef void eggs_p(Spam s):
cdef float *p
p = s.p[0]
s.p[0] = p
cdef void eggs_g(Spam s):
cdef float *p
p = s.p[0]
s.p[0] = p
spam = ham spam = ham
def test_i():
spam.i = 1
eggs_i(spam)
def test_c():
spam.c = c'a'
eggs_c(spam)
def test_p():
cdef float f
spam.p[0] = &f
eggs_p(spam)
def test_g():
cdef Grail l
spam.g = &l
eggs_g(spam)
__doc__ = """
>>> test_i()
>>> test_c()
>>> test_p()
"""
cdef union Spam: cdef union Spam:
int i int i
char c char c
...@@ -5,9 +11,32 @@ cdef union Spam: ...@@ -5,9 +11,32 @@ cdef union Spam:
cdef Spam spam, ham cdef Spam spam, ham
cdef void eggs(Spam s): cdef void eggs_i(Spam s):
cdef int j cdef int j
j = s.i j = s.i
s.i = j s.i = j
cdef void eggs_c(Spam s):
cdef char c
c = s.c
s.c = c
cdef void eggs_p(Spam s):
cdef float *p
p = s.p[0]
s.p[0] = p
spam = ham spam = ham
def test_i():
spam.i = 1
eggs_i(spam)
def test_c():
spam.c = c'a'
eggs_c(spam)
def test_p():
cdef float f
spam.p[0] = &f
eggs_p(spam)
def f(adict, key1, value1, key2, value2): __doc__ = """
adict = {} >>> empty()
adict = {key1:value1} {}
adict = {key1:value1, key2:value2} >>> keyvalue(1, 2)
adict = {key1:value1, key2:value2,} {1: 2}
adict = {"parrot":"resting", "answer":42}
>>> keyvalues(1, 2, 3, 4)
\ No newline at end of file {1: 2, 3: 4}
>>> keyvalues2(1, 2, 3, 4)
{1: 2, 3: 4}
>>> len(constant())
2
>>> constant()['parrot']
'resting'
>>> constant()['answer']
42
"""
def empty():
d = {}
return d
def keyvalue(key, value):
d = {key:value}
return d
def keyvalues(key1, value1, key2, value2):
d = {key1:value1, key2:value2}
return d
def keyvalues2(key1, value1, key2, value2):
d = {key1:value1, key2:value2,}
return d
def constant():
d = {"parrot":"resting", "answer":42}
return d
__doc__ = """
>>> test()
1.0
"""
def test(): def test():
cdef float v[10][10] cdef float v[10][10]
v[1][2] = 1.0 v[1][2] = 1.0
print v[1][2] return v[1][2]
__doc__ = """
>>> p = create()
>>> rest(p)
0
"""
cdef class Parrot: cdef class Parrot:
cdef object name cdef object name
cdef int alive cdef int alive
...@@ -5,11 +11,24 @@ cdef class Parrot: ...@@ -5,11 +11,24 @@ cdef class Parrot:
cdef class Norwegian(Parrot): cdef class Norwegian(Parrot):
cdef object plumage_colour cdef object plumage_colour
cdef void rest(Norwegian polly): def create():
cdef Parrot p
p = Norwegian()
p.alive = 1
return p
def rest(Norwegian polly):
cdef Parrot fred cdef Parrot fred
cdef object spam cdef object spam
spam = None
fred = polly fred = polly
polly = fred polly = fred
polly = spam polly = spam
assert polly is None
assert fred.alive
spam = polly spam = polly
polly.alive = 0 fred.alive = 0
return fred.alive
__doc__ = """
>>> len(Spam())
0
"""
cdef class Spam: cdef class Spam:
def __len__(self): def __len__(self):
......
__doc__ = """
>>> s = Silly(1,2,3, 'test')
>>> s.spam(1,2,3)
>>> s.spam(1,2)
Traceback (most recent call last):
TypeError: function takes exactly 3 arguments (2 given)
>>> s.spam(1,2,3,4)
Traceback (most recent call last):
TypeError: function takes exactly 3 arguments (4 given)
>>> s.spam(1,2,3, a=1)
Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function
>>> s.grail(1,2,3)
>>> s.grail(1,2,3,4)
>>> s.grail(1,2,3,4,5,6,7,8,9)
>>> s.grail(1,2)
Traceback (most recent call last):
TypeError: function takes exactly 3 arguments (2 given)
>>> s.grail(1,2,3, a=1)
Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function
>>> s.swallow(1,2,3)
>>> s.swallow(1,2,3,4)
Traceback (most recent call last):
TypeError: function takes at most 3 positional arguments (4 given)
>>> s.swallow(1,2,3, a=1, b=2)
>>> s.swallow(1,2,3, x=1)
Traceback (most recent call last):
TypeError: keyword parameter 'x' was given by position and by name
>>> s.creosote(1,2,3)
>>> s.creosote(1,2,3,4)
>>> s.creosote(1,2,3, a=1)
>>> s.creosote(1,2,3,4, a=1, b=2)
>>> s.creosote(1,2,3,4, x=1)
Traceback (most recent call last):
TypeError: keyword parameter 'x' was given by position and by name
"""
cdef class Silly: cdef class Silly:
def __init__(self, *a): def __init__(self, *a):
......
__doc__ = """
>>> C().xxx(5)
5
>>> C().xxx()
'a b'
>>> C().xxx(42)
42
>>> C().xxx()
'a b'
"""
class C: class C:
def xxx(self, p="a b"): def xxx(self, p="a b"):
pass return p
__doc__ = """
>>> c(1,2,3)
>>> c(1,2,3,4)
Traceback (most recent call last):
TypeError: function takes exactly 3 arguments (4 given)
>>> d(1,2)
>>> d(1,2, c=1)
>>> d(1,2,3)
Traceback (most recent call last):
TypeError: function takes at most 2 positional arguments (3 given)
>>> d(1,2, d=1)
Traceback (most recent call last):
TypeError: 'd' is an invalid keyword argument for this function
>>> e(1,2)
>>> e(1,2, c=1)
>>> e(1,2, d=1)
>>> e(1,2, c=1, d=2, e=3)
>>> e(1,2,3)
>>> e(1,2,3,4)
Traceback (most recent call last):
TypeError: function takes at most 3 positional arguments (4 given)
>>> f(1,2, c=1)
>>> f(1,2, c=1, d=2)
>>> f(1,2,3)
Traceback (most recent call last):
TypeError: function takes at most 2 positional arguments (3 given)
>>> f(1,2)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
>>> f(1,2, c=1, e=2)
Traceback (most recent call last):
TypeError: 'e' is an invalid keyword argument for this function
>>> g(1,2, c=1, f=2)
>>> g(1,2, c=1, e=0, f=2, d=11)
>>> g(1,2, c=1, f=2, e=0, x=25)
>>> g(1,2,3)
Traceback (most recent call last):
TypeError: function takes at most 2 positional arguments (3 given)
>>> g(1,2)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
>>> g(1,2, c=1)
Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing
>>> h(1,2, c=1, f=2)
>>> h(1,2, c=1, f=2, e=3)
>>> h(1,2,3,4,5,6, c=1, f=2)
>>> h(1,2,3,4,5,6, c=1, f=2, e=3, x=25, y=11)
>>> h(1,2,3)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
>>> h(1,2, d=1)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
"""
def c(a, b, c): def c(a, b, c):
z = 33 z = 33
......
__doc__ = """
>>> f()
>>> g
42
>>> x
'spam'
>>> y
'eggs'
>>> z
'spameggs'
"""
def f(): def f():
pass pass
......
def f(): __doc__ = """
cdef int int1, int2, int3 >>> modobj(9,2)
cdef char *str2, *str3 1
obj1 = 1 >>> modobj('%d', 5)
obj2 = 2 '5'
obj3 = 3
>>> modint(9,2)
1
"""
def modobj(obj2, obj3):
obj1 = obj2 % obj3
return obj1
def modint(int int2, int int3):
cdef int int1
int1 = int2 % int3 int1 = int2 % int3
return int1
cdef modptr():
# FIXME!!!
cdef char *str2, *str3
str2 = "spam"
str3 = "eggs"
obj1 = str2 % str3 obj1 = str2 % str3
obj1 = obj2 % obj3 return obj1
\ No newline at end of file
def f(a, *p, **n):
pass
__doc__ = """
>>> test(Exception('hi'))
Raising: Exception('hi',)
Caught: <type 'exceptions.Exception'> Exception('hi',)
"""
import sys import sys
def test(obj): def test(obj):
......
__doc__ = """
>>> test()
1
"""
cdef class Tri: cdef class Tri:
pass def test(self):
return 1
cdef class Curseur: cdef class Curseur:
cdef Tri tri cdef Tri tri
def detail(self): def detail(self):
produire_fiches(self.tri) return produire_fiches(self.tri)
cdef produire_fiches(Tri tri): cdef produire_fiches(Tri tri):
pass return tri.test()
def test():
cdef Curseur c
c = Curseur()
c.tri = Tri()
return c.detail()
__doc__ = """
>>> c = build()
>>> c.method()
Traceback (most recent call last):
AssertionError: 1
"""
cdef enum Mode: cdef enum Mode:
a = 1 a = 1
b = 2 b = 2
...@@ -7,3 +14,9 @@ cdef class Curseur: ...@@ -7,3 +14,9 @@ cdef class Curseur:
def method(self): def method(self):
assert False, self.mode assert False, self.mode
def build():
cdef Curseur c
c = Curseur()
c.mode = a
return c
__doc__ = """
>>>
"""
cdef class Eggs: cdef class Eggs:
cdef object ham cdef object ham
......
__doc__ = """
>>> import re
>>> t
('2',)
>>> t == re.search('(\\d+)', '-2.80 98\\n').groups()
True
"""
# this is a string constant test, not a test for 're'
import re import re
t = re.search('(\d+)', '-2.80 98\n').groups() t = re.search('(\d+)', '-2.80 98\n').groups()
__doc__ = """
>>> f()
42
"""
def f(): def f():
a = 42 a = 42
b = a b = a
return b
__doc__ = """
>>> f('test')
>>> test_g()
>>> test_h(5)
5
"""
def f(a): def f(a):
return return
return a return a
...@@ -8,5 +15,11 @@ cdef void g(): ...@@ -8,5 +15,11 @@ cdef void g():
cdef int h(a): cdef int h(a):
cdef int i cdef int i
i = a
return i return i
\ No newline at end of file def test_g():
g()
def test_h(i):
return h(i)
def f(obj1, obj2, obj3, obj4, obj5): __doc__ = """
>>> class Test(object):
... def __setitem__(self, key, value):
... print key, value
... def __getitem__(self, key):
... print key
... return self
>>> ellipsis(Test())
Ellipsis
>>> full(Test())
slice(None, None, None)
>>> select(0, Test(), 10, 20, 30)
slice(10, None, None)
slice(None, 20, None)
slice(None, None, 30)
slice(10, 20, None)
slice(10, None, 30)
slice(None, 20, 30)
slice(10, 20, 30)
slice(1, 2, 3)
>>> set(Test(), -11)
slice(1, 2, 3) -11
"""
def ellipsis(o):
obj1 = o[...]
def full(o):
obj1 = o[::]
def set(o, v):
cdef int int3, int4, int5 cdef int int3, int4, int5
obj1 = obj2[...] int3, int4, int5 = 1,2,3
obj1 = obj2[::] o[int3:int4:int5] = v
def select(obj1, obj2, obj3, obj4, obj5):
cdef int int3, int4, int5
int3, int4, int5 = 1,2,3
obj1 = obj2[obj3::] obj1 = obj2[obj3::]
obj1 = obj2[:obj4:] obj1 = obj2[:obj4:]
obj1 = obj2[::obj5] obj1 = obj2[::obj5]
...@@ -10,5 +49,4 @@ def f(obj1, obj2, obj3, obj4, obj5): ...@@ -10,5 +49,4 @@ def f(obj1, obj2, obj3, obj4, obj5):
obj1 = obj2[:obj4:obj5] obj1 = obj2[:obj4:obj5]
obj1 = obj2[obj3:obj4:obj5] obj1 = obj2[obj3:obj4:obj5]
obj1 = obj2[int3:int4:int5] obj1 = obj2[int3:int4:int5]
obj1[int3:int4:int5] = obj2
\ No newline at end of file
__doc__ = """
>>> spam(1,2,3)
>>> spam(1,2)
Traceback (most recent call last):
TypeError: function takes exactly 3 arguments (2 given)
>>> spam(1,2,3,4)
Traceback (most recent call last):
TypeError: function takes exactly 3 arguments (4 given)
>>> spam(1,2,3, a=1)
Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function
>>> grail(1,2,3)
>>> grail(1,2,3,4)
>>> grail(1,2,3,4,5,6,7,8,9)
>>> grail(1,2)
Traceback (most recent call last):
TypeError: function takes exactly 3 arguments (2 given)
>>> grail(1,2,3, a=1)
Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function
>>> swallow(1,2,3)
>>> swallow(1,2,3,4)
Traceback (most recent call last):
TypeError: function takes at most 3 positional arguments (4 given)
>>> swallow(1,2,3, a=1, b=2)
>>> swallow(1,2,3, x=1)
Traceback (most recent call last):
TypeError: keyword parameter 'x' was given by position and by name
>>> creosote(1,2,3)
>>> creosote(1,2,3,4)
>>> creosote(1,2,3, a=1)
>>> creosote(1,2,3,4, a=1, b=2)
>>> creosote(1,2,3,4, x=1)
Traceback (most recent call last):
TypeError: keyword parameter 'x' was given by position and by name
"""
def spam(x, y, z): def spam(x, y, z):
pass pass
...@@ -9,4 +49,3 @@ def swallow(x, y, z, **k): ...@@ -9,4 +49,3 @@ def swallow(x, y, z, **k):
def creosote(x, y, z, *a, **k): def creosote(x, y, z, *a, **k):
pass pass
__doc__ = """
>>> f()
(-1, -1)
>>> p()
0
"""
def f(): def f():
cdef int int1, int2, int3 cdef int int1, int2, int3
cdef char *ptr1, *ptr2, *ptr3
obj1 = 1 obj1 = 1
obj2 = 2 obj2 = 2
obj3 = 3 obj3 = 3
int2 = 2
int3 = 3
int1 = int2 - int3 int1 = int2 - int3
obj1 = obj2 - int3
return int1, obj1
def p():
cdef int int1, int2, int3
cdef char *ptr1, *ptr2, *ptr3
int2 = 2
int3 = 3
ptr2 = "test"
ptr3 = ptr2
ptr1 = ptr2 - int3 ptr1 = ptr2 - int3
int1 = ptr2 - ptr3 int1 = ptr2 - ptr3
obj1 = obj2 - int3 return int1
\ No newline at end of file
__doc__ = """
>>> swallow()
"""
cdef grail(char *blarg, ...): cdef grail(char *blarg, ...):
pass pass
cdef swallow(): def swallow():
grail("spam") grail("spam")
grail("spam", 42) grail("spam", 42)
__doc__ = """
>>> x
5L
"""
cdef unsigned int ui cdef unsigned int ui
ui = 5
x = ui x = ui
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