From: Robert Bradshaw Date: Sun, 31 Jan 2010 10:42:25 +0000 (-0800) Subject: Trac #478, tests for using closures as method decorators. X-Git-Tag: 0.13.beta0~2^2~106 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=383adadc13a692d9e00fd8995879407aed3077da;p=cython.git Trac #478, tests for using closures as method decorators. --- diff --git a/tests/run/closure_decorators_T478.pyx b/tests/run/closure_decorators_T478.pyx new file mode 100644 index 00000000..e6277d64 --- /dev/null +++ b/tests/run/closure_decorators_T478.pyx @@ -0,0 +1,47 @@ +__doc__ = """ + >>> Num(13).is_prime() + args (Num(13),) kwds {} + True + >>> Num(13).is_prime(True) + args (Num(13), True) kwds {} + True + >>> Num(15).is_prime(print_factors=True) + args (Num(15),) kwds {'print_factors': True} + 3 5 + False +""" + +def print_args(func): + def f(*args, **kwds): + print "args", args, "kwds", kwds + return func(*args, **kwds) + return f + + +cdef class Num: + + cdef int n + + def __init__(self, n): + self.n = n + + def __repr__(self): + return "Num(%s)" % self.n + + @print_args + def is_prime(self, bint print_factors=False): + if self.n == 2: + return True + elif self.n < 2: + return False + elif self.n % 2 == 0: + if print_factors: + print 2, self.n // 2 + cdef int i = 3 + while i*i <= self.n: + if self.n % i == 0: + if print_factors: + print i, self.n // i + return False + i += 2 + return True