Debian Codegraph @

pypy/module/_demo/demo.py

raw symbols SCIP
1 from pypy.interpreter.error import oefmt
2 from pypy.interpreter.baseobjspace import W_Root
3 from pypy.interpreter.gateway import interp2app, unwrap_spec
4 from pypy.interpreter.typedef import TypeDef, GetSetProperty
5 from rpython.rtyper.lltypesystem import rffi, lltype
6 from rpython.rtyper.tool import rffi_platform
7 from rpython.translator.tool.cbuild import ExternalCompilationInfo
8 import math
9
10 time_t = rffi_platform.getsimpletype('time_t', '#include <time.h>', rffi.SIGNED)
11
12 eci = ExternalCompilationInfo(includes=['time.h'])
13 time = rffi.llexternal('time', [lltype.Signed], time_t,
14 compilation_info=eci)
15
16 def get(space, name):
17 w_module = space.getbuiltinmodule('_demo')
18 return space.getattr(w_module, space.newtext(name))
19
20
21 @unwrap_spec(repetitions=int)
22 def measuretime(space, repetitions, w_callable):
23 if repetitions <= 0:
24 w_DemoError = get(space, 'DemoError')
25 raise oefmt(w_DemoError, "repetition count must be > 0")
26 starttime = time(0)
27 for i in range(repetitions):
28 space.call_function(w_callable)
29 endtime = time(0)
30 return space.newint(endtime - starttime)
31
32 @unwrap_spec(n=int)
33 def sieve(space, n):
34 lst = range(2, n + 1)
35 head = 0
36 while 1:
37 first = lst[head]
38 if first > math.sqrt(n) + 1:
39 lst_w = [space.newint(i) for i in lst]
40 return space.newlist(lst_w)
41 newlst = []
42 for element in lst:
43 if element <= first:
44 newlst.append(element)
45 elif element % first != 0:
46 newlst.append(element)
47 lst = newlst
48 head += 1
49
50 class W_MyType(W_Root):
51 def __init__(self, space, x=1):
52 self.space = space
53 self.x = x
54
55 def multiply(self, w_y):
56 space = self.space
57 y = space.int_w(w_y)
58 return space.newint(self.x * y)
59
60 def fget_x(self, space):
61 return space.newint(self.x)
62
63 def fset_x(self, space, w_value):
64 self.x = space.int_w(w_value)
65
66 @unwrap_spec(x=int)
67 def mytype_new(space, w_subtype, x):
68 if x == 3:
69 return MySubType(space, x)
70 return W_MyType(space, x)
71
72 getset_x = GetSetProperty(W_MyType.fget_x, W_MyType.fset_x, cls=W_MyType)
73
74 class MySubType(W_MyType):
75 pass
76
77 W_MyType.typedef = TypeDef('MyType',
78 __new__ = interp2app(mytype_new),
79 x = getset_x,
80 multiply = interp2app(W_MyType.multiply),
81 )