--- /dev/null
+def f(a, b):
+ assert a, a+b
--- /dev/null
+cdef class Parrot:
+
+ cdef describe(self):
+ print "This is a parrot."
+
+ cdef action(self):
+ print "Polly wants a cracker!"
--- /dev/null
+cdef extern from "foo.h":
+ ctypedef long long big_t
+ cdef void spam(big_t b)
+
+spam(grail)
--- /dev/null
+cdef int f() except -1:
+ cdef type t
+ cdef object x
+ t = buffer
+ t = enumerate
+ t = file
+ t = float
+ t = int
+ t = long
+ t = open
+ t = property
+ t = str
+ t = tuple
+ t = xrange
+ x = True
+ x = False
+ x = Ellipsis
+ x = Exception
+ x = StopIteration
+ x = StandardError
+ x = ArithmeticError
+ x = LookupError
+ x = AssertionError
+ x = AssertionError
+ x = EOFError
+ x = FloatingPointError
+ x = EnvironmentError
+ x = IOError
+ x = OSError
+ x = ImportError
+ x = IndexError
+ x = KeyError
+ x = KeyboardInterrupt
+ x = MemoryError
+ x = NameError
+ x = OverflowError
+ x = RuntimeError
+ x = NotImplementedError
+ x = SyntaxError
+ x = IndentationError
+ x = TabError
+ x = ReferenceError
+ x = SystemError
+ x = SystemExit
+ x = TypeError
+ x = UnboundLocalError
+ x = UnicodeError
+ x = UnicodeEncodeError
+ x = UnicodeDecodeError
+ x = UnicodeTranslateError
+ x = ValueError
+ x = ZeroDivisionError
+ x = MemoryErrorInst
+ x = Warning
+ x = UserWarning
+ x = DeprecationWarning
+ x = PendingDeprecationWarning
+ x = SyntaxWarning
+ #x = OverflowWarning # Does not seem to exist in 2.5
+ x = RuntimeWarning
+ x = FutureWarning
+ typecheck(x, Exception)
+ try:
+ pass
+ except ValueError:
+ pass
--- /dev/null
+cdef int f() except -1:
+ cdef dict d
+ cdef object x, z
+ cdef int i
+ z = dict
+ d = dict(x)
+ d = dict(*x)
+ d.clear()
+ z = d.copy()
+ z = d.items()
+ z = d.keys()
+ z = d.values()
+ d.merge(x, i)
+ d.update(x)
+ d.merge_pairs(x, i)
--- /dev/null
+cdef int f() except -1:
+ cdef list l
+ cdef object x, y, z
+ z = list
+ l = list(x)
+ l = list(*y)
+ z = l.insert
+ l.insert(17, 42)
+ l.append(88)
+ l.sort()
+ l.reverse()
+ z = l.as_tuple()
--- /dev/null
+cdef int f() except -1:
+ cdef slice s
+ cdef object z
+ cdef int i
+ z = slice
+ s = slice(1, 2, 3)
+ z = slice.indices()
+ i = s.start
+ i = s.stop
+ i = s.step
--- /dev/null
+cdef int f() except -1:
+ cdef type t1, t2
+ cdef object x
+ cdef int b
+ b = typecheck(x, t1)
+ b = issubtype(t1, t2)
--- /dev/null
+cdef void foo():
+ cdef int i, j, k
+ i = j = k
+ a = b = c
+ i = j = c
+ a = b = k
+ (a, b), c = (d, e), f = (x, y), z
+# a, b = p, q = x, y
\ No newline at end of file
--- /dev/null
+cdef extern from "cdefemptysue.h":
+
+ cdef struct spam:
+ pass
+
+ ctypedef union eggs:
+ pass
+
+ cdef enum ham:
+ pass
+
+cdef extern spam s
+cdef extern eggs e
+cdef extern ham h
--- /dev/null
+cdef extern from "cheese.h":
+
+ ctypedef int camembert
+
+ struct roquefort:
+ int x
+
+ char *swiss
+
+ void cheddar()
+
+ class external.runny [object runny_obj]:
+ cdef int a
+ def __init__(self):
+ pass
+
+cdef runny r
+r = x
+r.a = 42
--- /dev/null
+cdef extern from "cheese.h":
+ pass
+
--- /dev/null
+cdef int f():
+ pass
+
+cdef char *g(int k, float z):
+ pass
--- /dev/null
+cimport spam
+cimport pkg.eggs
+
+cdef spam.Spam yummy
+cdef pkg.eggs.Eggs fried
+
+spam.eat(yummy)
+spam.tons = 3.14
+ova = pkg.eggs
+fried = pkg.eggs.Eggs()
--- /dev/null
+from spam cimport Spam
+from pkg.eggs cimport Eggs as ova
+
+cdef extern Spam yummy
+cdef ova fried
+
+fried = None
--- /dev/null
+from package.inpackage cimport Spam
+
+cdef Spam s2
--- /dev/null
+from cexportfunc cimport f, g
--- /dev/null
+cdef extern from "ctypedefextern.h":
+
+ ctypedef int some_int
+ ctypedef some_int *some_ptr
+
+cdef void spam():
+ cdef some_int i
+ cdef some_ptr p
+ p[0] = i
--- /dev/null
+cdef class Spam:
+ answer = 42
--- /dev/null
+cdef object f(object x) nogil:
+ pass
+
+cdef void g(int x) nogil:
+ cdef object z
+ z = None
+
+cdef void h(int x) nogil:
+ p()
+
+cdef object p() nogil:
+ pass
+
+cdef void r() nogil:
+ q()
+
+cdef void (*fp)()
+cdef void (*fq)() nogil
+cdef extern void u()
+
+cdef object m():
+ global fp, fq
+ cdef object x, y, obj
+ cdef int i, j, k
+ global fred
+ q()
+ with nogil:
+ r()
+ q()
+ i = 42
+ obj = None
+ 17L
+ 7j
+ asdf
+ `"Hello"`
+ import fred
+ from fred import obj
+ for x in obj:
+ pass
+ obj[i]
+ obj[i:j]
+ obj[i:j:k]
+ obj.fred
+ (x, y)
+ [x, y]
+ {x: y}
+ obj and x
+ t(obj)
+ f(42)
+ x + obj
+ -obj
+ x = y = obj
+ x, y = y, x
+ obj[i] = x
+ obj.fred = x
+ print obj
+ del fred
+ return obj
+ raise obj
+ if obj:
+ pass
+ while obj:
+ pass
+ for x <= obj <= y:
+ pass
+ try:
+ pass
+ except:
+ pass
+ try:
+ pass
+ finally:
+ pass
+ fq = u
+ fq = fp
+
+cdef void q():
+ pass
+
+cdef class C:
+ pass
+
+cdef void t(C c) nogil:
+ pass
--- /dev/null
+cdef extern from "foo.h":
+
+ int fred()
--- /dev/null
+cdef extern from "externsue.h":
+
+ enum Eggs:
+ runny, firm, hard
+
+ struct Spam:
+ int i
+
+ union Soviet:
+ char c
+
+cdef extern Eggs e
+cdef extern Spam s
+cdef extern Soviet u
+
+cdef void tomato():
+ global e
+ e = runny
+ e = firm
+ e = hard
+
--- /dev/null
+cdef class Widget:
+ pass
+
+cdef class Container:
+ pass
+
+cdef Widget w
+cdef Container c
+w.parent = c
--- /dev/null
+cdef class Spam:
+ cdef public object eggs
+
+ def __getattr__(self, name):
+ print "Spam getattr:", name
--- /dev/null
+cdef int f() except -1:
+ g = getattr3
--- /dev/null
+cdef public int grail
+
+cdef public spam(int servings):
+ pass
+
+cdef public class sandwich [object sandwich, type sandwich_Type]:
+ cdef int tomato
+ cdef float lettuce
--- /dev/null
+include "i_public.pxi"
+
--- /dev/null
+cdef struct S:
+ int q
+
+cdef int f() except -1:
+ cdef int i, j, k
+ cdef float x, y, z
+ cdef object a, b, c, d, e
+ cdef int m[3]
+ cdef S s
+ global g
+ i += j + k
+ x += y + z
+ x += i
+ a += b + c
+ g += a
+ m[i] += j
+ a[i] += b + c
+ a[b + c] += d
+ (a + b)[c] += d
+ a[i : j] += b
+ (a + b)[i : j] += c
+ a.b += c + d
+ (a + b).c += d
+ s.q += i
--- /dev/null
+cdef int f() except -1:
+ cdef object a, b
+ cdef char *p
+ a += b
+ a -= b
+ a *= b
+ a /= b
+ a %= b
+ a **= b
+ a <<= b
+ a >>= b
+ a &= b
+ a ^= b
+ a |= b
+ p += 42
+ p -= 42
+ p += a
--- /dev/null
+cdef int f() except -1:
+ cdef object x, y, z
+ cdef int i
+ cdef unsigned int ui
+ z = x[y]
+ z = x[i]
+ x[y] = z
+ x[i] = z
+ z = x[ui]
+ x[ui] = z
--- /dev/null
+cdef api float f(float x):
+ return 0.5 * x * x
--- /dev/null
+cdef int f(int x):
+ return x * x
+
+cdef int g(int x):
+ return 5 * x
--- /dev/null
+def f(a, *p, **n):
+ pass
--- /dev/null
+cdef int f() except -1:
+ cdef int i, x, y
+ for x < i < y:
+ pass
--- /dev/null
+cdef int f() except -1:
+ cdef int i, x, y
+ for i from x < i < y:
+ pass
--- /dev/null
+cimport spam, eggs
+
+cdef extern spam.Spam yummy
+cdef eggs.Eggs fried
+
+fried = None
--- /dev/null
+from spam cimport Spam
+from eggs cimport Eggs
+
+cdef extern Spam yummy
+cdef Eggs fried
+
+fried = None
--- /dev/null
+def f():
+ cdef list l
+ l = list()
+ l.append("second")
+ l.insert(0, "first")
+ return l
--- /dev/null
+cdef extern from "l_capi_api.h":
+ float f(float)
+ int import_l_capi() except -1
+
+def test():
+ print f(3.1415)
+
+import_l_capi()
--- /dev/null
+cimport l_cfuncexport
+from l_cfuncexport cimport g
+
+print l_cfuncexport.f(42)
+print g(42)
--- /dev/null
+class Spam:
+ """Spam, glorious spam!"""
+
--- /dev/null
+class Spam:
+ pass
--- /dev/null
+cdef int tomato() except -1:
+ print "Entering tomato"
+ raise Exception("Eject! Eject! Eject!")
+ print "Leaving tomato"
+
+cdef void sandwich():
+ print "Entering sandwich"
+ tomato()
+ print "Leaving sandwich"
+
+def snack():
+ print "Entering snack"
+ tomato()
+ print "Leaving snack"
+
+def lunch():
+ print "Entering lunch"
+ sandwich()
+ print "Leaving lunch"
--- /dev/null
+cdef class SpamDish:
+ cdef int spam
+
+ cdef void describe(self):
+ print "This dish contains", self.spam, "tons of spam."
+
+
+cdef class FancySpamDish(SpamDish):
+ cdef int lettuce
+
+ cdef void describe(self):
+ print "This dish contains", self.spam, "tons of spam",
+ print "and", self.lettuce, "milligrams of lettuce."
+
+
+cdef void describe_dish(SpamDish d):
+ d.describe()
+
+def test():
+ cdef SpamDish s
+ cdef FancySpamDish ss
+ s = SpamDish()
+ s.spam = 42
+ ss = FancySpamDish()
+ ss.spam = 88
+ ss.lettuce = 5
+ describe_dish(s)
+ describe_dish(ss)
--- /dev/null
+from b_extimpinherit cimport Parrot
+
+
+cdef class Norwegian(Parrot):
+
+ cdef action(self):
+ print "This parrot is resting."
+
+ cdef plumage(self):
+ print "Lovely plumage!"
+
+
+def main():
+ cdef Parrot p
+ cdef Norwegian n
+ p = Parrot()
+ n = Norwegian()
+ print "Parrot:"
+ p.describe()
+ p.action()
+ print "Norwegian:"
+ n.describe()
+ n.action()
+ n.plumage()
--- /dev/null
+cdef class Parrot:
+
+ cdef object plumage
+
+ def __init__(self):
+ self.plumage = "yellow"
+
+ def describe(self):
+ print "This bird has lovely", self.plumage, "plumage."
+
+
+cdef class Norwegian(Parrot):
+
+ def __init__(self):
+ self.plumage = "blue"
+
--- /dev/null
+cdef class Spam:
+ cdef public int tons
+ cdef readonly float tastiness
+ cdef int temperature
+
+ def __init__(self, tons, tastiness, temperature):
+ self.tons = tons
+ self.tastiness = tastiness
+ self.temperature = temperature
+
+ def get_temperature(self):
+ return self.temperature
--- /dev/null
+cdef extern from "numeric.h":
+
+ struct PyArray_Descr:
+ int type_num, elsize
+ char type
+
+ ctypedef class Numeric.ArrayType [object PyArrayObject]:
+ cdef char *data
+ cdef int nd
+ cdef int *dimensions, *strides
+ cdef object base
+ cdef PyArray_Descr *descr
+ cdef int flags
+
+def ogle(ArrayType a):
+ print "No. of dimensions:", a.nd
+ print " Dim Value"
+ for i in range(a.nd):
+ print "%5d %5d" % (i, a.dimensions[i])
+ print "flags:", a.flags
+ print "Type no.", a.descr.type_num
+ print "Element size:", a.descr.elsize
--- /dev/null
+cdef class CheeseShop:
+ cdef object cheeses
+
+ def __cinit__(self):
+ self.cheeses = []
+
+ property cheese:
+
+ "A senseless waste of a property."
+
+ def __get__(self):
+ return "We don't have: %s" % self.cheeses
+
+ def __set__(self, value):
+ self.cheeses.append(value)
+
+ def __del__(self):
+ del self.cheeses[:]
--- /dev/null
+cdef class Animal:
+ cdef object __weakref__
+ cdef public object name
--- /dev/null
+def test(obj, attr, dflt):
+ return getattr3(obj, attr, dflt)
--- /dev/null
+import spam
+print "Imported spam"
+print dir(spam)
+
+import sys
+print "Imported sys"
+print sys
+
--- /dev/null
+cdef class Parrot:
+
+ cdef void describe(self):
+ print "This parrot is resting."
+
+
+cdef class Norwegian(Parrot):
+
+ cdef void describe(self):
+ Parrot.describe(self)
+ print "Lovely plumage!"
+
+
+cdef Parrot p1, p2
+p1 = Parrot()
+p2 = Norwegian()
+p1.describe()
+p2.describe()
--- /dev/null
+def pd(d):
+ l = []
+ i = d.items()
+ i.sort()
+ for kv in i:
+ l.append("%r: %r" % kv)
+ return "{%s}" % ", ".join(l)
+
+def c(a, b, c):
+ print "a =", a, "b =", b, "c =", c
+
+def d(a, b, *, c = 88):
+ print "a =", a, "b =", b, "c =", c
+
+def e(a, b, c = 88, **kwds):
+ print "a =", a, "b =", b, "c =", c, "kwds =", pd(kwds)
+
+def f(a, b, *, c, d = 42):
+ print "a =", a, "b =", b, "c =", c, "d =", d
+
+def g(a, b, *, c, d = 42, e = 17, f, **kwds):
+ print "a =", a, "b =", b, "c =", c, "d =", d, "e =", e, "f =", f, "kwds =", pd(kwds)
+
+def h(a, b, *args, c, d = 42, e = 17, f, **kwds):
+ print "a =", a, "b =", b, "args =", args, "c =", c, "d =", d, "e =", e, "f =", f, "kwds =", pd(kwds)
--- /dev/null
+class Inquisition(object):
+ """Something that nobody expects."""
+
+ def __repr__(self):
+ return "Surprise!"
--- /dev/null
+def f():
+ print "Spam!"
+
+f()
--- /dev/null
+def foo():
+ raise Exception
--- /dev/null
+cdef int spam() except -1:
+ raise Exception("Spam error")
+
+cdef int grail() except -1:
+ spam()
+
+def tomato():
+ grail()
+
--- /dev/null
+seq = [1, [2, 3]]
+
+def f():
+ a, (b, c) = [1, [2, 3]]
+ print a
+ print b
+ print c
+
+def g():
+ a, b, c = seq
+
+def h():
+ a, = seq
--- /dev/null
+def f():
+ return 42
+
+cdef int g():
+ cdef object x
+ return x
--- /dev/null
+DEF nan = float('nan')
+DEF inf = float('inf')
+DEF minf = -float('inf')
+
+cdef int f() except -1:
+ cdef float x, y, z
+ x = nan
+ y = inf
+ z = minf
--- /dev/null
+import sys
+from Pyrex.Compiler.Main import main
+
+sys.argv[1:] = "-I spam -Ieggs --include-dir ham".split()
+main(command_line = 1)
--- /dev/null
+cdef class Spam:
+ pass
+
+def probe():
+ pass
--- /dev/null
+cdef class Spam:
+ pass
+
+cdef int f() except -1:
+ cdef type t
+ t = Spam
--- /dev/null
+cdef class C:
+ cdef int i
+
+cdef int f() except -1:
+ cdef object x
+ cdef void *p
+ cdef int i
+ x = <object>p
+ p = <void *>x
+ x = (<object>p).foo
+ i = (<C>p).i
+ (<C>p).i = i