| 1 |
|
| 2 |
|
| 3 |
from __future__ import division, print_function |
| 4 |
from pypy.interpreter import gateway, argument |
| 5 |
from pypy.interpreter.gateway import ObjSpace, W_Root, WrappedDefault |
| 6 |
from pypy.interpreter.signature import Signature |
| 7 |
from pypy.interpreter.error import OperationError |
| 8 |
import py |
| 9 |
import sys |
| 10 |
|
| 11 |
|
| 12 |
class FakeFunc(object): |
| 13 |
def __init__(self, space, name): |
| 14 |
self.space = space |
| 15 |
self.name = name |
| 16 |
self.qualname = name |
| 17 |
self.defs_w = [] |
| 18 |
self.w_kw_defs = None |
| 19 |
|
| 20 |
|
| 21 |
class TestBuiltinCode: |
| 22 |
def test_signature(self, space): |
| 23 |
def c(space, w_x, w_y, hello_w): |
| 24 |
pass |
| 25 |
code = gateway.BuiltinCode(c, unwrap_spec=[gateway.ObjSpace, |
| 26 |
gateway.W_Root, |
| 27 |
gateway.W_Root, |
| 28 |
'args_w']) |
| 29 |
assert code.signature() == Signature(['x', 'y'], 'hello', None) |
| 30 |
def d(self, w_boo): |
| 31 |
pass |
| 32 |
|
| 33 |
class W_X(W_Root): |
| 34 |
pass |
| 35 |
|
| 36 |
code = gateway.BuiltinCode(d, unwrap_spec= ['self', |
| 37 |
gateway.W_Root], self_type=W_X) |
| 38 |
assert code.signature() == Signature(['self', 'boo'], None, None) |
| 39 |
def e(space, w_x, w_y, __args__): |
| 40 |
pass |
| 41 |
code = gateway.BuiltinCode(e, unwrap_spec=[gateway.ObjSpace, |
| 42 |
gateway.W_Root, |
| 43 |
gateway.W_Root, |
| 44 |
gateway.Arguments]) |
| 45 |
assert code.signature() == Signature(['x', 'y'], 'args', 'keywords') |
| 46 |
|
| 47 |
def f(space, index): |
| 48 |
pass |
| 49 |
code = gateway.BuiltinCode(f, unwrap_spec=[gateway.ObjSpace, "index"]) |
| 50 |
assert code.signature() == Signature(["index"], None, None) |
| 51 |
|
| 52 |
def f(space, __kwonly__, w_x): |
| 53 |
pass |
| 54 |
code = gateway.BuiltinCode(f, unwrap_spec=[gateway.ObjSpace, |
| 55 |
"kwonly", W_Root]) |
| 56 |
assert code.signature() == Signature(['x'], kwonlyargcount=1) |
| 57 |
assert space.int_w(space.getattr( |
| 58 |
code, space.newtext('co_kwonlyargcount'))) == 1 |
| 59 |
|
| 60 |
|
| 61 |
def test_call(self): |
| 62 |
def c(space, w_x, w_y, hello_w): |
| 63 |
u = space.unwrap |
| 64 |
w = space.wrap |
| 65 |
assert len(hello_w) == 2 |
| 66 |
assert u(hello_w[0]) == 0 |
| 67 |
assert u(hello_w[1]) == True |
| 68 |
return w((u(w_x) - u(w_y) + len(hello_w))) |
| 69 |
code = gateway.BuiltinCode(c, unwrap_spec=[gateway.ObjSpace, |
| 70 |
gateway.W_Root, |
| 71 |
gateway.W_Root, |
| 72 |
'args_w']) |
| 73 |
w = self.space.wrap |
| 74 |
args = argument.Arguments(self.space, [w(123), w(23), w(0), w(True)]) |
| 75 |
w_result = code.funcrun(FakeFunc(self.space, "c"), args) |
| 76 |
assert self.space.eq_w(w_result, w(102)) |
| 77 |
|
| 78 |
def test_call_index(self): |
| 79 |
def c(space, index): |
| 80 |
assert type(index) is int |
| 81 |
code = gateway.BuiltinCode(c, unwrap_spec=[gateway.ObjSpace, |
| 82 |
"index"]) |
| 83 |
w = self.space.wrap |
| 84 |
args = argument.Arguments(self.space, [w(123)]) |
| 85 |
code.funcrun(FakeFunc(self.space, "c"), args) |
| 86 |
|
| 87 |
def test_call_args(self): |
| 88 |
def c(space, w_x, w_y, __args__): |
| 89 |
args_w, kwds_w = __args__.unpack() |
| 90 |
u = space.unwrap |
| 91 |
w = space.wrap |
| 92 |
return w((u(w_x) - u(w_y) + len(args_w)) |
| 93 |
* u(kwds_w['boo'])) |
| 94 |
code = gateway.BuiltinCode(c, unwrap_spec=[gateway.ObjSpace, |
| 95 |
gateway.W_Root, |
| 96 |
gateway.W_Root, |
| 97 |
gateway.Arguments]) |
| 98 |
w = self.space.wrap |
| 99 |
args = argument.Arguments(self.space, [w(123), w(23)], [], [], |
| 100 |
w_stararg = w((0, True)), |
| 101 |
w_starstararg = w({'boo': 10})) |
| 102 |
w_result = code.funcrun(FakeFunc(self.space, "c"), args) |
| 103 |
assert self.space.eq_w(w_result, w(1020)) |
| 104 |
|
| 105 |
|
| 106 |
class TestGateway: |
| 107 |
def test_app2interp(self): |
| 108 |
w = self.space.wrap |
| 109 |
def app_g3(a, b): |
| 110 |
return a+b |
| 111 |
g3 = gateway.app2interp_temp(app_g3) |
| 112 |
assert self.space.eq_w(g3(self.space, w('foo'), w('bar')), w('foobar')) |
| 113 |
|
| 114 |
def test_app2interp1(self): |
| 115 |
w = self.space.wrap |
| 116 |
def noapp_g3(a, b): |
| 117 |
return a+b |
| 118 |
g3 = gateway.app2interp_temp(noapp_g3, gateway.applevel_temp) |
| 119 |
assert self.space.eq_w(g3(self.space, w('foo'), w('bar')), w('foobar')) |
| 120 |
|
| 121 |
def test_app2interp_general_args(self): |
| 122 |
w = self.space.wrap |
| 123 |
def app_general(x, *args, **kwds): |
| 124 |
assert type(args) is tuple |
| 125 |
assert type(kwds) is dict |
| 126 |
return x + 10 * len(args) + 100 * len(kwds) |
| 127 |
gg = gateway.app2interp_temp(app_general) |
| 128 |
args = gateway.Arguments(self.space, [w(6), w(7)]) |
| 129 |
assert self.space.int_w(gg(self.space, w(3), args)) == 23 |
| 130 |
args = gateway.Arguments(self.space, [w(6)], [self.space.newtext('hello'), self.space.newtext('world')], [w(7), w(8)]) |
| 131 |
assert self.space.int_w(gg(self.space, w(3), args)) == 213 |
| 132 |
|
| 133 |
def test_app2interp_future(self): |
| 134 |
w = self.space.wrap |
| 135 |
def app_g3(a, b): |
| 136 |
print(end='') |
| 137 |
return a / b |
| 138 |
g3 = gateway.app2interp_temp(app_g3) |
| 139 |
assert self.space.eq_w(g3(self.space, w(1), w(4),), w(0.25)) |
| 140 |
|
| 141 |
def test_interp2app(self): |
| 142 |
space = self.space |
| 143 |
w = space.wrap |
| 144 |
def g3(space, w_a, w_b): |
| 145 |
return space.add(w_a, w_b) |
| 146 |
app_g3 = gateway.interp2app_temp(g3) |
| 147 |
w_app_g3 = space.wrap(app_g3) |
| 148 |
assert self.space.eq_w( |
| 149 |
space.call(w_app_g3, |
| 150 |
space.newtuple([w('foo'), w('bar')]), |
| 151 |
space.newdict()), |
| 152 |
w('foobar')) |
| 153 |
assert self.space.eq_w( |
| 154 |
space.call_function(w_app_g3, w('foo'), w('bar')), |
| 155 |
w('foobar')) |
| 156 |
|
| 157 |
def test_interpindirect2app(self): |
| 158 |
space = self.space |
| 159 |
|
| 160 |
class BaseA(W_Root): |
| 161 |
def method(self, space, x): |
| 162 |
"This is a method" |
| 163 |
pass |
| 164 |
|
| 165 |
def method_with_default(self, space, x=5): |
| 166 |
pass |
| 167 |
|
| 168 |
@gateway.unwrap_spec(x=int) |
| 169 |
def method_with_unwrap_spec(self, space, x): |
| 170 |
pass |
| 171 |
|
| 172 |
def method_with_args(self, space, __args__): |
| 173 |
pass |
| 174 |
|
| 175 |
class A(BaseA): |
| 176 |
def method(self, space, x): |
| 177 |
return space.wrap(x + 2) |
| 178 |
|
| 179 |
def method_with_default(self, space, x): |
| 180 |
return space.wrap(x + 2) |
| 181 |
|
| 182 |
def method_with_unwrap_spec(self, space, x): |
| 183 |
return space.wrap(x + 2) |
| 184 |
|
| 185 |
def method_with_args(self, space, __args__): |
| 186 |
return space.wrap(42) |
| 187 |
|
| 188 |
class B(BaseA): |
| 189 |
def method(self, space, x): |
| 190 |
return space.wrap(x + 1) |
| 191 |
|
| 192 |
def method_with_default(self, space, x): |
| 193 |
return space.wrap(x + 1) |
| 194 |
|
| 195 |
def method_with_unwrap_spec(self, space, x): |
| 196 |
return space.wrap(x + 1) |
| 197 |
|
| 198 |
def method_with_args(self, space, __args__): |
| 199 |
return space.wrap(43) |
| 200 |
|
| 201 |
class FakeTypeDef(object): |
| 202 |
rawdict = {} |
| 203 |
bases = {} |
| 204 |
applevel_subclasses_base = None |
| 205 |
name = 'foo' |
| 206 |
hasdict = False |
| 207 |
weakrefable = False |
| 208 |
doc = 'xyz' |
| 209 |
|
| 210 |
meth = gateway.interpindirect2app(BaseA.method, {'x': int}) |
| 211 |
w_c = space.wrap(meth) |
| 212 |
w_a = A() |
| 213 |
w_b = B() |
| 214 |
assert space.int_w(space.call_function(w_c, w_a, space.wrap(1))) == 1 + 2 |
| 215 |
assert space.int_w(space.call_function(w_c, w_b, space.wrap(-10))) == -10 + 1 |
| 216 |
|
| 217 |
doc = space.text_w(space.getattr(w_c, space.wrap('__doc__'))) |
| 218 |
assert doc == "This is a method" |
| 219 |
|
| 220 |
meth_with_default = gateway.interpindirect2app( |
| 221 |
BaseA.method_with_default, {'x': int}) |
| 222 |
w_d = space.wrap(meth_with_default) |
| 223 |
|
| 224 |
assert space.int_w(space.call_function(w_d, w_a, space.wrap(4))) == 4 + 2 |
| 225 |
assert space.int_w(space.call_function(w_d, w_b, space.wrap(-10))) == -10 + 1 |
| 226 |
assert space.int_w(space.call_function(w_d, w_a)) == 5 + 2 |
| 227 |
assert space.int_w(space.call_function(w_d, w_b)) == 5 + 1 |
| 228 |
|
| 229 |
meth_with_unwrap_spec = gateway.interpindirect2app( |
| 230 |
BaseA.method_with_unwrap_spec) |
| 231 |
w_e = space.wrap(meth_with_unwrap_spec) |
| 232 |
assert space.int_w(space.call_function(w_e, w_a, space.wrap(4))) == 4 + 2 |
| 233 |
|
| 234 |
meth_with_args = gateway.interpindirect2app( |
| 235 |
BaseA.method_with_args) |
| 236 |
w_f = space.wrap(meth_with_args) |
| 237 |
assert space.int_w(space.call_function(w_f, w_a)) == 42 |
| 238 |
assert space.int_w(space.call_function(w_f, w_b, |
| 239 |
space.wrap("ignored"))) == 43 |
| 240 |
|
| 241 |
|
| 242 |
assert isinstance(meth_with_args._code, |
| 243 |
gateway.BuiltinCodePassThroughArguments1) |
| 244 |
|
| 245 |
def test_interp2app_unwrap_spec(self): |
| 246 |
space = self.space |
| 247 |
w = space.wrap |
| 248 |
def g3(space, w_a, w_b): |
| 249 |
return space.add(w_a, w_b) |
| 250 |
app_g3 = gateway.interp2app_temp(g3, |
| 251 |
unwrap_spec=[gateway.ObjSpace, |
| 252 |
gateway.W_Root, |
| 253 |
gateway.W_Root]) |
| 254 |
w_app_g3 = space.wrap(app_g3) |
| 255 |
assert self.space.eq_w( |
| 256 |
space.call(w_app_g3, |
| 257 |
space.newtuple([w('foo'), w('bar')]), |
| 258 |
space.newdict()), |
| 259 |
w('foobar')) |
| 260 |
assert self.space.eq_w( |
| 261 |
space.call_function(w_app_g3, w('foo'), w('bar')), |
| 262 |
w('foobar')) |
| 263 |
|
| 264 |
def test_interp2app_unwrap_spec_auto(self): |
| 265 |
def f(space, w_a, w_b): |
| 266 |
pass |
| 267 |
unwrap_spec = gateway.BuiltinCode(f)._unwrap_spec |
| 268 |
assert unwrap_spec == [ObjSpace, W_Root, W_Root] |
| 269 |
|
| 270 |
def test_interp2app_unwrap_spec_bool(self): |
| 271 |
space = self.space |
| 272 |
w = space.wrap |
| 273 |
def g(space, b): |
| 274 |
return space.wrap(b) |
| 275 |
app_g = gateway.interp2app(g, unwrap_spec=[gateway.ObjSpace, bool]) |
| 276 |
app_g2 = gateway.interp2app(g, unwrap_spec=[gateway.ObjSpace, bool]) |
| 277 |
assert app_g is app_g2 |
| 278 |
w_app_g = space.wrap(app_g) |
| 279 |
assert self.space.eq_w(space.call_function(w_app_g, space.wrap(True)), |
| 280 |
space.wrap(True)) |
| 281 |
|
| 282 |
def test_interp2app_unwrap_spec_bytes(self): |
| 283 |
|
| 284 |
|
| 285 |
space = self.space |
| 286 |
def g(space, b): |
| 287 |
return space.newbytes(b) |
| 288 |
app_g = gateway.interp2app(g, unwrap_spec=[gateway.ObjSpace, 'bytes']) |
| 289 |
app_g2 = gateway.interp2app(g, unwrap_spec=[gateway.ObjSpace, 'bytes']) |
| 290 |
assert app_g is app_g2 |
| 291 |
w_app_g = space.wrap(app_g) |
| 292 |
assert self.space.eq_w(space.call_function(w_app_g, space.newbytes("abc")), |
| 293 |
space.newbytes("abc")) |
| 294 |
|
| 295 |
def test_interp2app_unwrap_spec_text(self): |
| 296 |
space = self.space |
| 297 |
def g(space, b): |
| 298 |
assert isinstance(b, str) |
| 299 |
return space.newtext(b) |
| 300 |
app_g = gateway.interp2app(g, unwrap_spec=[gateway.ObjSpace, 'text']) |
| 301 |
app_g2 = gateway.interp2app(g, unwrap_spec=[gateway.ObjSpace, 'text']) |
| 302 |
assert app_g is app_g2 |
| 303 |
w_app_g = space.wrap(app_g) |
| 304 |
assert self.space.eq_w(space.call_function(w_app_g, space.newtext("abc")), |
| 305 |
space.newtext("abc")) |
| 306 |
|
| 307 |
def test_caching_methods(self): |
| 308 |
class Base(gateway.W_Root): |
| 309 |
def f(self): |
| 310 |
return 1 |
| 311 |
|
| 312 |
class A(Base): |
| 313 |
pass |
| 314 |
class B(Base): |
| 315 |
pass |
| 316 |
app_A = gateway.interp2app(A.f) |
| 317 |
app_B = gateway.interp2app(B.f) |
| 318 |
assert app_A is not app_B |
| 319 |
|
| 320 |
def test_interp2app_unwrap_spec_nonnegint(self): |
| 321 |
space = self.space |
| 322 |
w = space.wrap |
| 323 |
def g(space, x): |
| 324 |
return space.wrap(x * 6) |
| 325 |
app_g = gateway.interp2app(g, unwrap_spec=[gateway.ObjSpace, |
| 326 |
'nonnegint']) |
| 327 |
app_g2 = gateway.interp2app(g, unwrap_spec=[gateway.ObjSpace, |
| 328 |
'nonnegint']) |
| 329 |
assert app_g is app_g2 |
| 330 |
w_app_g = space.wrap(app_g) |
| 331 |
assert self.space.eq_w(space.call_function(w_app_g, space.wrap(7)), |
| 332 |
space.wrap(42)) |
| 333 |
assert self.space.eq_w(space.call_function(w_app_g, space.wrap(0)), |
| 334 |
space.wrap(0)) |
| 335 |
space.raises_w(space.w_ValueError, |
| 336 |
space.call_function, w_app_g, space.wrap(-1)) |
| 337 |
|
| 338 |
def test_interp2app_unwrap_spec_c_int(self): |
| 339 |
from rpython.rlib.rarithmetic import r_longlong |
| 340 |
space = self.space |
| 341 |
w = space.wrap |
| 342 |
def g(space, x): |
| 343 |
return space.wrap(x + 6) |
| 344 |
app_g = gateway.interp2app(g, unwrap_spec=[gateway.ObjSpace, |
| 345 |
'c_int']) |
| 346 |
app_ug = gateway.interp2app(g, unwrap_spec=[gateway.ObjSpace, |
| 347 |
'c_uint']) |
| 348 |
app_ng = gateway.interp2app(g, unwrap_spec=[gateway.ObjSpace, |
| 349 |
'c_nonnegint']) |
| 350 |
assert app_ug is not app_g |
| 351 |
w_app_g = space.wrap(app_g) |
| 352 |
w_app_ug = space.wrap(app_ug) |
| 353 |
w_app_ng = space.wrap(app_ng) |
| 354 |
|
| 355 |
assert self.space.eq_w(space.call_function(w_app_g, space.wrap(7)), |
| 356 |
space.wrap(13)) |
| 357 |
space.raises_w(space.w_OverflowError, |
| 358 |
space.call_function, w_app_g, |
| 359 |
space.wrap(r_longlong(0x80000000))) |
| 360 |
space.raises_w(space.w_OverflowError, |
| 361 |
space.call_function, w_app_g, |
| 362 |
space.wrap(r_longlong(-0x80000001))) |
| 363 |
|
| 364 |
assert self.space.eq_w(space.call_function(w_app_ug, space.wrap(7)), |
| 365 |
space.wrap(13)) |
| 366 |
assert self.space.eq_w(space.call_function(w_app_ug, |
| 367 |
space.wrap(0x7FFFFFFF)), |
| 368 |
space.wrap(r_longlong(0x7FFFFFFF+6))) |
| 369 |
space.raises_w(space.w_ValueError, |
| 370 |
space.call_function, w_app_ug, space.wrap(-1)) |
| 371 |
space.raises_w(space.w_OverflowError, |
| 372 |
space.call_function, w_app_ug, |
| 373 |
space.wrap(r_longlong(0x100000000))) |
| 374 |
|
| 375 |
assert self.space.eq_w(space.call_function(w_app_ng, space.wrap(7)), |
| 376 |
space.wrap(13)) |
| 377 |
space.raises_w(space.w_OverflowError, |
| 378 |
space.call_function, w_app_ng, |
| 379 |
space.wrap(r_longlong(0x80000000))) |
| 380 |
space.raises_w(space.w_ValueError, |
| 381 |
space.call_function, w_app_ng, space.wrap(-1)) |
| 382 |
|
| 383 |
def test_interp2app_unwrap_spec_args_w(self): |
| 384 |
space = self.space |
| 385 |
w = space.wrap |
| 386 |
def g3_args_w(space, args_w): |
| 387 |
return space.add(args_w[0], args_w[1]) |
| 388 |
app_g3_args_w = gateway.interp2app_temp(g3_args_w, |
| 389 |
unwrap_spec=[gateway.ObjSpace, |
| 390 |
'args_w']) |
| 391 |
w_app_g3_args_w = space.wrap(app_g3_args_w) |
| 392 |
assert self.space.eq_w( |
| 393 |
space.call(w_app_g3_args_w, |
| 394 |
space.newtuple([w('foo'), w('bar')]), |
| 395 |
space.newdict()), |
| 396 |
w('foobar')) |
| 397 |
assert self.space.eq_w( |
| 398 |
space.call_function(w_app_g3_args_w, w('foo'), w('bar')), |
| 399 |
w('foobar')) |
| 400 |
|
| 401 |
def test_interp2app_unwrap_spec_str(self): |
| 402 |
space = self.space |
| 403 |
w = space.wrap |
| 404 |
def g3_ss(space, s0, s1): |
| 405 |
if s1 is None: |
| 406 |
return space.wrap(42) |
| 407 |
return space.wrap(s0+s1) |
| 408 |
app_g3_ss = gateway.interp2app_temp(g3_ss, |
| 409 |
unwrap_spec=[gateway.ObjSpace, |
| 410 |
'text', 'text_or_none']) |
| 411 |
w_app_g3_ss = space.wrap(app_g3_ss) |
| 412 |
assert self.space.eq_w( |
| 413 |
space.call(w_app_g3_ss, |
| 414 |
space.newtuple([w('foo'), w('bar')]), |
| 415 |
space.newdict()), |
| 416 |
w('foobar')) |
| 417 |
assert self.space.eq_w( |
| 418 |
space.call_function(w_app_g3_ss, w('foo'), w('bar')), |
| 419 |
w('foobar')) |
| 420 |
assert self.space.eq_w( |
| 421 |
space.call_function(w_app_g3_ss, w('foo'), space.w_None), |
| 422 |
w(42)) |
| 423 |
space.raises_w(space.w_TypeError, space.call_function, |
| 424 |
w_app_g3_ss, space.w_None, w('bar')) |
| 425 |
|
| 426 |
def test_interp2app_unwrap_spec_int_float(self): |
| 427 |
space = self.space |
| 428 |
w = space.wrap |
| 429 |
def g3_if(space, i0, f1): |
| 430 |
return space.wrap(i0+f1) |
| 431 |
app_g3_if = gateway.interp2app_temp(g3_if, |
| 432 |
unwrap_spec=[gateway.ObjSpace, |
| 433 |
int,float]) |
| 434 |
w_app_g3_if = space.wrap(app_g3_if) |
| 435 |
assert self.space.eq_w( |
| 436 |
space.call(w_app_g3_if, |
| 437 |
space.newtuple([w(1), w(1.0)]), |
| 438 |
space.newdict()), |
| 439 |
w(2.0)) |
| 440 |
assert self.space.eq_w( |
| 441 |
space.call_function(w_app_g3_if, w(1), w(1.0)), |
| 442 |
w(2.0)) |
| 443 |
|
| 444 |
def test_interp2app_unwrap_spec_r_longlong(self): |
| 445 |
space = self.space |
| 446 |
w = space.wrap |
| 447 |
def g3_ll(space, n): |
| 448 |
return space.wrap(n * 3) |
| 449 |
app_g3_ll = gateway.interp2app_temp(g3_ll, |
| 450 |
unwrap_spec=[gateway.ObjSpace, |
| 451 |
gateway.r_longlong]) |
| 452 |
w_app_g3_ll = space.wrap(app_g3_ll) |
| 453 |
w_big = w(gateway.r_longlong(10**10)) |
| 454 |
assert space.eq_w( |
| 455 |
space.call(w_app_g3_ll, |
| 456 |
space.newtuple([w_big]), |
| 457 |
space.newdict()), |
| 458 |
w(gateway.r_longlong(3 * 10**10))) |
| 459 |
assert space.eq_w( |
| 460 |
space.call_function(w_app_g3_ll, w_big), |
| 461 |
w(gateway.r_longlong(3 * 10**10))) |
| 462 |
w_huge = w(10L**100) |
| 463 |
space.raises_w(space.w_OverflowError, |
| 464 |
space.call_function, w_app_g3_ll, w_huge) |
| 465 |
|
| 466 |
def test_interp2app_unwrap_spec_r_uint(self): |
| 467 |
space = self.space |
| 468 |
w = space.wrap |
| 469 |
def g3_ll(space, n): |
| 470 |
return space.wrap(n * 3) |
| 471 |
app_g3_ll = gateway.interp2app_temp(g3_ll, |
| 472 |
unwrap_spec=[gateway.ObjSpace, |
| 473 |
gateway.r_uint]) |
| 474 |
w_app_g3_ll = space.wrap(app_g3_ll) |
| 475 |
w_big = w(gateway.r_uint(sys.maxint+100)) |
| 476 |
assert space.eq_w( |
| 477 |
space.call_function(w_app_g3_ll, w_big), |
| 478 |
w(gateway.r_uint((sys.maxint+100)*3))) |
| 479 |
space.raises_w(space.w_OverflowError, |
| 480 |
space.call_function, w_app_g3_ll, w(10L**100)) |
| 481 |
space.raises_w(space.w_ValueError, |
| 482 |
space.call_function, w_app_g3_ll, w(-1)) |
| 483 |
|
| 484 |
def test_interp2app_unwrap_spec_r_ulonglong(self): |
| 485 |
space = self.space |
| 486 |
w = space.wrap |
| 487 |
def g3_ll(space, n): |
| 488 |
return space.wrap(n * 3) |
| 489 |
app_g3_ll = gateway.interp2app_temp(g3_ll, |
| 490 |
unwrap_spec=[gateway.ObjSpace, |
| 491 |
gateway.r_ulonglong]) |
| 492 |
w_app_g3_ll = space.wrap(app_g3_ll) |
| 493 |
w_big = w(gateway.r_ulonglong(-100)) |
| 494 |
assert space.eq_w( |
| 495 |
space.call_function(w_app_g3_ll, w_big), |
| 496 |
w(gateway.r_ulonglong(-300))) |
| 497 |
space.raises_w(space.w_OverflowError, |
| 498 |
space.call_function, w_app_g3_ll, w(10L**100)) |
| 499 |
space.raises_w(space.w_ValueError, |
| 500 |
space.call_function, w_app_g3_ll, w(-1)) |
| 501 |
space.raises_w(space.w_ValueError, |
| 502 |
space.call_function, w_app_g3_ll, w(-10L**99)) |
| 503 |
|
| 504 |
def test_interp2app_unwrap_spec_index(self): |
| 505 |
space = self.space |
| 506 |
w = space.wrap |
| 507 |
def g3_idx(space, idx0): |
| 508 |
return space.wrap(idx0 + 1) |
| 509 |
app_g3_idx = gateway.interp2app_temp(g3_idx, |
| 510 |
unwrap_spec=[gateway.ObjSpace, |
| 511 |
'index']) |
| 512 |
w_app_g3_idx = space.wrap(app_g3_idx) |
| 513 |
assert space.eq_w( |
| 514 |
space.call_function(w_app_g3_idx, w(123)), |
| 515 |
w(124)) |
| 516 |
space.raises_w(space.w_OverflowError, |
| 517 |
space.call_function, |
| 518 |
w_app_g3_idx, |
| 519 |
space.mul(space.wrap(sys.maxint), space.wrap(7))) |
| 520 |
space.raises_w(space.w_OverflowError, |
| 521 |
space.call_function, |
| 522 |
w_app_g3_idx, |
| 523 |
space.mul(space.wrap(sys.maxint), space.wrap(-7))) |
| 524 |
|
| 525 |
def test_interp2app_unwrap_spec_fsencode(self): |
| 526 |
import sys |
| 527 |
space = self.space |
| 528 |
w = space.wrap |
| 529 |
def f(filename): |
| 530 |
return space.newbytes(filename) |
| 531 |
app_f = gateway.interp2app_temp(f, unwrap_spec=['fsencode']) |
| 532 |
w_app_f = space.wrap(app_f) |
| 533 |
assert space.eq_w( |
| 534 |
space.call_function(w_app_f, w(u'\udc80')), |
| 535 |
space.newbytes('\x80')) |
| 536 |
|
| 537 |
def test_interp2app_unwrap_spec_typechecks(self): |
| 538 |
from rpython.rlib.rarithmetic import r_longlong |
| 539 |
|
| 540 |
space = self.space |
| 541 |
w = space.wrap |
| 542 |
def g3_id(space, x): |
| 543 |
return space.wrap(x) |
| 544 |
app_g3_i = gateway.interp2app_temp(g3_id, |
| 545 |
unwrap_spec=[gateway.ObjSpace, |
| 546 |
int]) |
| 547 |
w_app_g3_i = space.wrap(app_g3_i) |
| 548 |
assert space.eq_w(space.call_function(w_app_g3_i,w(1)),w(1)) |
| 549 |
assert space.eq_w(space.call_function(w_app_g3_i,w(1L)),w(1)) |
| 550 |
space.raises_w(space.w_OverflowError, space.call_function,w_app_g3_i,w(sys.maxint*2)) |
| 551 |
space.raises_w(space.w_TypeError, space.call_function,w_app_g3_i,w(None)) |
| 552 |
space.raises_w(space.w_TypeError, space.call_function,w_app_g3_i,w("foo")) |
| 553 |
space.raises_w(space.w_TypeError, space.call_function,w_app_g3_i,w(1.0)) |
| 554 |
|
| 555 |
app_g3_s = gateway.interp2app_temp(g3_id, |
| 556 |
unwrap_spec=[gateway.ObjSpace, |
| 557 |
'text']) |
| 558 |
w_app_g3_s = space.wrap(app_g3_s) |
| 559 |
assert space.eq_w(space.call_function(w_app_g3_s,w("foo")),w("foo")) |
| 560 |
space.raises_w(space.w_TypeError, space.call_function,w_app_g3_s,w(None)) |
| 561 |
space.raises_w(space.w_TypeError, space.call_function,w_app_g3_s,w(1)) |
| 562 |
space.raises_w(space.w_TypeError, space.call_function,w_app_g3_s,w(1.0)) |
| 563 |
|
| 564 |
app_g3_f = gateway.interp2app_temp(g3_id, |
| 565 |
unwrap_spec=[gateway.ObjSpace, |
| 566 |
float]) |
| 567 |
w_app_g3_f = space.wrap(app_g3_f) |
| 568 |
assert space.eq_w(space.call_function(w_app_g3_f,w(1.0)),w(1.0)) |
| 569 |
assert space.eq_w(space.call_function(w_app_g3_f,w(1)),w(1.0)) |
| 570 |
assert space.eq_w(space.call_function(w_app_g3_f,w(1L)),w(1.0)) |
| 571 |
space.raises_w(space.w_TypeError, space.call_function,w_app_g3_f,w(None)) |
| 572 |
space.raises_w(space.w_TypeError, space.call_function,w_app_g3_f,w("foo")) |
| 573 |
|
| 574 |
app_g3_r = gateway.interp2app_temp(g3_id, |
| 575 |
unwrap_spec=[gateway.ObjSpace, |
| 576 |
r_longlong]) |
| 577 |
w_app_g3_r = space.wrap(app_g3_r) |
| 578 |
space.raises_w(space.w_TypeError, space.call_function,w_app_g3_r,w(1.0)) |
| 579 |
|
| 580 |
def test_interp2app_unwrap_spec_utf8(self): |
| 581 |
space = self.space |
| 582 |
w = space.wrap |
| 583 |
def g3_u(space, utf8): |
| 584 |
return space.wrap(utf8) |
| 585 |
app_g3_u = gateway.interp2app_temp(g3_u, |
| 586 |
unwrap_spec=[gateway.ObjSpace, |
| 587 |
'utf8']) |
| 588 |
w_app_g3_u = space.wrap(app_g3_u) |
| 589 |
encoded = u"gęść".encode('utf8') |
| 590 |
assert self.space.eq_w( |
| 591 |
space.call_function(w_app_g3_u, w(u"gęść")), |
| 592 |
w(encoded)) |
| 593 |
assert self.space.eq_w( |
| 594 |
space.call_function(w_app_g3_u, w("foo")), |
| 595 |
w("foo")) |
| 596 |
space.raises_w(space.w_TypeError, space.call_function, w_app_g3_u, |
| 597 |
w(None)) |
| 598 |
space.raises_w(space.w_TypeError, space.call_function, w_app_g3_u, |
| 599 |
w(42)) |
| 600 |
w_ascii = space.appexec([], """(): |
| 601 |
import sys |
| 602 |
return sys.getdefaultencoding() == 'ascii'""") |
| 603 |
if space.is_true(w_ascii): |
| 604 |
raises(gateway.OperationError, space.call_function, w_app_g3_u, |
| 605 |
w("\x80")) |
| 606 |
|
| 607 |
def test_interp2app_unwrap_spec_unwrapper(self): |
| 608 |
space = self.space |
| 609 |
class Unwrapper(gateway.Unwrapper): |
| 610 |
def unwrap(self, space, w_value): |
| 611 |
return space.int_w(w_value) |
| 612 |
|
| 613 |
w = space.wrap |
| 614 |
def g3_u(space, value): |
| 615 |
return space.wrap(value + 1) |
| 616 |
app_g3_u = gateway.interp2app_temp(g3_u, |
| 617 |
unwrap_spec=[gateway.ObjSpace, |
| 618 |
Unwrapper]) |
| 619 |
assert self.space.eq_w( |
| 620 |
space.call_function(w(app_g3_u), w(42)), w(43)) |
| 621 |
space.raises_w(space.w_TypeError, space.call_function, |
| 622 |
w(app_g3_u), w(None)) |
| 623 |
|
| 624 |
def test_interp2app_classmethod(self): |
| 625 |
space = self.space |
| 626 |
w = space.wrap |
| 627 |
def g_run(space, w_type): |
| 628 |
assert space.is_w(w_type, space.w_text) |
| 629 |
return w(42) |
| 630 |
|
| 631 |
app_g_run = gateway.interp2app_temp(g_run, |
| 632 |
unwrap_spec=[gateway.ObjSpace, |
| 633 |
gateway.W_Root], |
| 634 |
as_classmethod=True) |
| 635 |
w_app_g_run = space.wrap(app_g_run) |
| 636 |
w_bound = space.get(w_app_g_run, w("hello"), space.w_text) |
| 637 |
assert space.eq_w(space.call_function(w_bound), w(42)) |
| 638 |
|
| 639 |
def test_interp2app_fastcall(self): |
| 640 |
space = self.space |
| 641 |
w = space.wrap |
| 642 |
w_3 = w(3) |
| 643 |
|
| 644 |
def f(space): |
| 645 |
return w_3 |
| 646 |
app_f = gateway.interp2app_temp(f, unwrap_spec=[gateway.ObjSpace]) |
| 647 |
w_app_f = w(app_f) |
| 648 |
|
| 649 |
|
| 650 |
assert isinstance(w_app_f.code, gateway.BuiltinCode0) |
| 651 |
|
| 652 |
called = [] |
| 653 |
fastcall_0 = w_app_f.code.fastcall_0 |
| 654 |
def witness_fastcall_0(space, w_func): |
| 655 |
called.append(w_func) |
| 656 |
return fastcall_0(space, w_func) |
| 657 |
|
| 658 |
w_app_f.code.fastcall_0 = witness_fastcall_0 |
| 659 |
|
| 660 |
w_3 = space.newint(3) |
| 661 |
w_res = space.call_function(w_app_f) |
| 662 |
|
| 663 |
assert w_res is w_3 |
| 664 |
assert called == [w_app_f] |
| 665 |
|
| 666 |
called = [] |
| 667 |
|
| 668 |
w_res = space.appexec([w_app_f], """(f): |
| 669 |
return f() |
| 670 |
""") |
| 671 |
|
| 672 |
assert w_res is w_3 |
| 673 |
assert called == [w_app_f] |
| 674 |
|
| 675 |
def test_interp2app_fastcall_method(self): |
| 676 |
space = self.space |
| 677 |
w = space.wrap |
| 678 |
w_3 = w(3) |
| 679 |
|
| 680 |
def f(space, w_self, w_x): |
| 681 |
return w_x |
| 682 |
app_f = gateway.interp2app_temp(f, unwrap_spec=[gateway.ObjSpace, |
| 683 |
gateway.W_Root, |
| 684 |
gateway.W_Root]) |
| 685 |
w_app_f = w(app_f) |
| 686 |
|
| 687 |
|
| 688 |
assert isinstance(w_app_f.code, gateway.BuiltinCode2) |
| 689 |
|
| 690 |
called = [] |
| 691 |
fastcall_2 = w_app_f.code.fastcall_2 |
| 692 |
def witness_fastcall_2(space, w_func, w_a, w_b): |
| 693 |
called.append(w_func) |
| 694 |
return fastcall_2(space, w_func, w_a, w_b) |
| 695 |
|
| 696 |
w_app_f.code.fastcall_2 = witness_fastcall_2 |
| 697 |
|
| 698 |
w_res = space.appexec([w_app_f, w_3], """(f, x): |
| 699 |
class A(object): |
| 700 |
m = f # not a builtin function, so works as method |
| 701 |
y = A().m(x) |
| 702 |
b = A().m |
| 703 |
z = b(x) |
| 704 |
return y is x and z is x |
| 705 |
""") |
| 706 |
|
| 707 |
assert space.is_true(w_res) |
| 708 |
assert called == [w_app_f, w_app_f] |
| 709 |
|
| 710 |
def test_interp2app_fastcall_method_with_space(self): |
| 711 |
class W_X(W_Root): |
| 712 |
def descr_f(self, space, w_x): |
| 713 |
return w_x |
| 714 |
|
| 715 |
app_f = gateway.interp2app_temp(W_X.descr_f, unwrap_spec=['self', |
| 716 |
gateway.ObjSpace, gateway.W_Root]) |
| 717 |
|
| 718 |
w_app_f = self.space.wrap(app_f) |
| 719 |
|
| 720 |
assert isinstance(w_app_f.code, gateway.BuiltinCode2) |
| 721 |
|
| 722 |
called = [] |
| 723 |
fastcall_2 = w_app_f.code.fastcall_2 |
| 724 |
def witness_fastcall_2(space, w_func, w_a, w_b): |
| 725 |
called.append(w_func) |
| 726 |
return fastcall_2(space, w_func, w_a, w_b) |
| 727 |
|
| 728 |
w_app_f.code.fastcall_2 = witness_fastcall_2 |
| 729 |
space = self.space |
| 730 |
|
| 731 |
w_res = space.call_function(w_app_f, W_X(), space.wrap(3)) |
| 732 |
|
| 733 |
assert space.is_true(w_res) |
| 734 |
assert called == [w_app_f] |
| 735 |
|
| 736 |
def test_plain(self): |
| 737 |
space = self.space |
| 738 |
|
| 739 |
def g(space, w_a, w_x): |
| 740 |
return space.newtuple([space.wrap('g'), w_a, w_x]) |
| 741 |
|
| 742 |
w_g = space.wrap(gateway.interp2app_temp(g, |
| 743 |
unwrap_spec=[gateway.ObjSpace, |
| 744 |
gateway.W_Root, |
| 745 |
gateway.W_Root])) |
| 746 |
|
| 747 |
args = argument.Arguments(space, [space.wrap(-1), space.wrap(0)]) |
| 748 |
|
| 749 |
w_res = space.call_args(w_g, args) |
| 750 |
assert space.is_true(space.eq(w_res, space.wrap(('g', -1, 0)))) |
| 751 |
|
| 752 |
w_self = space.wrap('self') |
| 753 |
|
| 754 |
args0 = argument.Arguments(space, [space.wrap(0)]) |
| 755 |
args = args0.prepend(w_self) |
| 756 |
|
| 757 |
w_res = space.call_args(w_g, args) |
| 758 |
assert space.is_true(space.eq(w_res, space.wrap(('g', 'self', 0)))) |
| 759 |
|
| 760 |
args3 = argument.Arguments(space, [space.wrap(3)]) |
| 761 |
w_res = space.call_obj_args(w_g, w_self, args3) |
| 762 |
assert space.is_true(space.eq(w_res, space.wrap(('g', 'self', 3)))) |
| 763 |
|
| 764 |
def test_unwrap_spec_decorator(self): |
| 765 |
space = self.space |
| 766 |
@gateway.unwrap_spec(gateway.ObjSpace, gateway.W_Root, int) |
| 767 |
def g(space, w_thing, i): |
| 768 |
return space.newtuple([w_thing, space.wrap(i)]) |
| 769 |
w_g = space.wrap(gateway.interp2app_temp(g)) |
| 770 |
args = argument.Arguments(space, [space.wrap(-1), space.wrap(0)]) |
| 771 |
w_res = space.call_args(w_g, args) |
| 772 |
assert space.eq_w(w_res, space.wrap((-1, 0))) |
| 773 |
|
| 774 |
def test_unwrap_spec_decorator_kwargs(self): |
| 775 |
space = self.space |
| 776 |
@gateway.unwrap_spec(i=int) |
| 777 |
def f(space, w_thing, i): |
| 778 |
return space.newtuple([w_thing, space.wrap(i)]) |
| 779 |
unwrap_spec = gateway.BuiltinCode(f)._unwrap_spec |
| 780 |
assert unwrap_spec == [ObjSpace, W_Root, int] |
| 781 |
|
| 782 |
def test_unwrap_spec_default_applevel(self): |
| 783 |
space = self.space |
| 784 |
@gateway.unwrap_spec(w_x = WrappedDefault(42)) |
| 785 |
def g(space, w_x): |
| 786 |
return w_x |
| 787 |
w_g = space.wrap(gateway.interp2app_temp(g)) |
| 788 |
args = argument.Arguments(space, []) |
| 789 |
w_res = space.call_args(w_g, args) |
| 790 |
assert space.eq_w(w_res, space.wrap(42)) |
| 791 |
|
| 792 |
args = argument.Arguments(space, [space.wrap(84)]) |
| 793 |
w_res = space.call_args(w_g, args) |
| 794 |
assert space.eq_w(w_res, space.wrap(84)) |
| 795 |
|
| 796 |
def test_unwrap_spec_default_applevel_2(self): |
| 797 |
space = self.space |
| 798 |
@gateway.unwrap_spec(w_x = (WrappedDefault(42)), y=int) |
| 799 |
def g(space, w_x, y=10): |
| 800 |
return space.add(w_x, space.wrap(y)) |
| 801 |
w_g = space.wrap(gateway.interp2app_temp(g)) |
| 802 |
args = argument.Arguments(space, []) |
| 803 |
w_res = space.call_args(w_g, args) |
| 804 |
assert space.eq_w(w_res, space.wrap(52)) |
| 805 |
|
| 806 |
args = argument.Arguments(space, [space.wrap(84)]) |
| 807 |
w_res = space.call_args(w_g, args) |
| 808 |
assert space.eq_w(w_res, space.wrap(94)) |
| 809 |
|
| 810 |
args = argument.Arguments(space, [space.wrap(84), space.wrap(-1)]) |
| 811 |
w_res = space.call_args(w_g, args) |
| 812 |
assert space.eq_w(w_res, space.wrap(83)) |
| 813 |
|
| 814 |
def test_unwrap_spec_default_applevel_bogus(self): |
| 815 |
space = self.space |
| 816 |
@gateway.unwrap_spec(w_x = WrappedDefault(42), y=int) |
| 817 |
def g(space, w_x, y): |
| 818 |
never_called |
| 819 |
py.test.raises(KeyError, space.wrap, gateway.interp2app_temp(g)) |
| 820 |
|
| 821 |
def test_unwrap_spec_default_applevel_bug2(self): |
| 822 |
space = self.space |
| 823 |
def g(space, w_x, w_y=None, __args__=None): |
| 824 |
return w_x |
| 825 |
w_g = space.wrap(gateway.interp2app_temp(g)) |
| 826 |
w_42 = space.call_function(w_g, space.wrap(42)) |
| 827 |
assert space.int_w(w_42) == 42 |
| 828 |
py.test.raises(gateway.OperationError, space.call_function, w_g) |
| 829 |
|
| 830 |
def g(space, w_x, w_y=None, args_w=None): |
| 831 |
return w_x |
| 832 |
w_g = space.wrap(gateway.interp2app_temp(g)) |
| 833 |
w_42 = space.call_function(w_g, space.wrap(42)) |
| 834 |
assert space.int_w(w_42) == 42 |
| 835 |
py.test.raises(gateway.OperationError, space.call_function, w_g) |
| 836 |
|
| 837 |
def test_interp2app_doc(self): |
| 838 |
space = self.space |
| 839 |
def f(space, w_x): |
| 840 |
"""foo""" |
| 841 |
w_f = space.wrap(gateway.interp2app_temp(f)) |
| 842 |
assert space.unwrap(space.getattr(w_f, space.wrap('__doc__'))) == 'foo' |
| 843 |
|
| 844 |
def g(space, w_x): |
| 845 |
never_called |
| 846 |
w_g = space.wrap(gateway.interp2app_temp(g, doc='bar')) |
| 847 |
assert space.unwrap(space.getattr(w_g, space.wrap('__doc__'))) == 'bar' |
| 848 |
|
| 849 |
def test_system_error(self): |
| 850 |
py.test.skip("we don't wrap a random exception inside SystemError " |
| 851 |
"when untranslated, because it makes testing harder") |
| 852 |
class UnexpectedException(Exception): |
| 853 |
pass |
| 854 |
space = self.space |
| 855 |
def g(space): |
| 856 |
raise UnexpectedException |
| 857 |
w_g = space.wrap(gateway.interp2app_temp(g)) |
| 858 |
e = py.test.raises(OperationError, space.appexec, [w_g], """(my_g): |
| 859 |
my_g() |
| 860 |
""") |
| 861 |
err = str(e.value) |
| 862 |
assert 'SystemError' in err |
| 863 |
assert ('unexpected internal exception (please ' |
| 864 |
'report a bug): UnexpectedException') in err |
| 865 |
|
| 866 |
def test_system_error_2(self): |
| 867 |
py.test.skip("we don't wrap a random exception inside SystemError " |
| 868 |
"when untranslated, because it makes testing harder") |
| 869 |
class UnexpectedException(Exception): |
| 870 |
pass |
| 871 |
space = self.space |
| 872 |
def g(space): |
| 873 |
raise UnexpectedException |
| 874 |
w_g = space.wrap(gateway.interp2app_temp(g)) |
| 875 |
w_msg = space.appexec([w_g], """(my_g): |
| 876 |
try: |
| 877 |
my_g() |
| 878 |
except SystemError as e: |
| 879 |
return str(e) |
| 880 |
""") |
| 881 |
err = space.text_w(w_msg) |
| 882 |
assert ('unexpected internal exception (please ' |
| 883 |
'report a bug): UnexpectedException') in err |
| 884 |
|
| 885 |
def test_bare_raise_in_app_helper(self): |
| 886 |
space = self.space |
| 887 |
w = space.wrap |
| 888 |
def app_g3(a): |
| 889 |
try: |
| 890 |
1 / a |
| 891 |
except ZeroDivisionError: |
| 892 |
raise |
| 893 |
g3 = gateway.app2interp(app_g3) |
| 894 |
space.raises_w(space.w_ZeroDivisionError, g3, space, w(0)) |
| 895 |
|
| 896 |
def test_unwrap_spec_default_bytes(self): |
| 897 |
space = self.space |
| 898 |
@gateway.unwrap_spec(s='bufferstr') |
| 899 |
def g(space, s=''): |
| 900 |
return space.wrap(type(s) is str) |
| 901 |
w_g = space.wrap(gateway.interp2app_temp(g)) |
| 902 |
args = argument.Arguments(space, []) |
| 903 |
w_res = space.call_args(w_g, args) |
| 904 |
assert space.eq_w(w_res, space.w_True) |
| 905 |
|
| 906 |
def test_unwrap_spec_default_applevel_bytes(self): |
| 907 |
space = self.space |
| 908 |
@gateway.unwrap_spec(w_x=WrappedDefault('foo')) |
| 909 |
def g(space, w_x): |
| 910 |
return w_x |
| 911 |
w_g = space.wrap(gateway.interp2app_temp(g)) |
| 912 |
args = argument.Arguments(space, []) |
| 913 |
w_res = space.call_args(w_g, args) |
| 914 |
assert space.eq_w(w_res, space.newbytes('foo')) |
| 915 |
|
| 916 |
def test_unwrap_spec_kwonly(self): |
| 917 |
space = self.space |
| 918 |
def g(space, w_x, __kwonly__, w_y): |
| 919 |
return space.sub(w_x, w_y) |
| 920 |
w_g = space.wrap(gateway.interp2app_temp(g)) |
| 921 |
w = space.wrap |
| 922 |
w1 = w(1) |
| 923 |
|
| 924 |
for i in range(4): |
| 925 |
a = argument.Arguments(space, [w1, w1, w1]) |
| 926 |
py.test.raises(gateway.OperationError, space.call_args, w_g, a) |
| 927 |
py.test.raises(gateway.OperationError, space.call_function, w_g, |
| 928 |
*(i * (w1,))) |
| 929 |
|
| 930 |
args = argument.Arguments(space, [w(1)], |
| 931 |
w_starstararg = w({'y': 10})) |
| 932 |
assert space.eq_w(space.call_args(w_g, args), w(-9)) |
| 933 |
args = argument.Arguments(space, [], |
| 934 |
w_starstararg = w({'x': 2, 'y': 10})) |
| 935 |
assert space.eq_w(space.call_args(w_g, args), w(-8)) |
| 936 |
|
| 937 |
def test_unwrap_spec_kwonly_default(self): |
| 938 |
space = self.space |
| 939 |
@gateway.unwrap_spec(w_x2=WrappedDefault(50), y2=int) |
| 940 |
def g(space, w_x1, w_x2, __kwonly__, w_y1, y2=200): |
| 941 |
return space.sub(space.sub(w_x1, w_x2), |
| 942 |
space.sub(w_y1, w(y2))) |
| 943 |
w_g = space.wrap(gateway.interp2app_temp(g)) |
| 944 |
w = space.wrap |
| 945 |
w1 = w(1) |
| 946 |
|
| 947 |
for i in range(6): |
| 948 |
py.test.raises(gateway.OperationError, space.call_function, w_g, |
| 949 |
*(i * (w1,))) |
| 950 |
|
| 951 |
def expected(x1, x2=50, y1="missing", y2=200): |
| 952 |
return (x1 - x2) - (y1 - y2) |
| 953 |
|
| 954 |
def check(*args, **kwds): |
| 955 |
a = argument.Arguments(space, [], w_stararg = w(args), |
| 956 |
w_starstararg = w(kwds)) |
| 957 |
w_res = space.call_args(w_g, a) |
| 958 |
assert space.eq_w(w_res, w(expected(*args, **kwds))) |
| 959 |
|
| 960 |
del kwds['y1'] |
| 961 |
a = argument.Arguments(space, [], w_stararg = w(args), |
| 962 |
w_starstararg = w(kwds)) |
| 963 |
py.test.raises(gateway.OperationError, space.call_args, w_g, a) |
| 964 |
|
| 965 |
args += (1234,) |
| 966 |
a = argument.Arguments(space, [], w_stararg = w(args), |
| 967 |
w_starstararg = w(kwds)) |
| 968 |
py.test.raises(gateway.OperationError, space.call_args, w_g, a) |
| 969 |
|
| 970 |
check(5, y1=1234) |
| 971 |
check(5, 1, y1=1234) |
| 972 |
check(5, x2=1, y1=1234) |
| 973 |
check(5, y1=1234, y2=343) |
| 974 |
check(5, 1, y1=1234, y2=343) |
| 975 |
check(5, x2=1, y1=1234, y2=343) |
| 976 |
check(x1=5, y1=1234, ) |
| 977 |
check(x1=5, x2=1, y1=1234, ) |
| 978 |
check(x1=5, y1=1234, y2=343) |
| 979 |
check(x1=5, x2=1, y1=1234, y2=343) |
| 980 |
|
| 981 |
def test_unwrap_spec_kwonly_default_2(self): |
| 982 |
space = self.space |
| 983 |
@gateway.unwrap_spec(w_x2=WrappedDefault(50)) |
| 984 |
def g(space, w_x2=None): |
| 985 |
return w_x2 |
| 986 |
w_g = space.wrap(gateway.interp2app_temp(g)) |
| 987 |
w_res = space.call_function(w_g) |
| 988 |
assert space.eq_w(w_res, space.wrap(50)) |
| 989 |
|
| 990 |
def test_unwrap_spec_kwonly_with_starargs_bug(self): |
| 991 |
space = self.space |
| 992 |
@gateway.unwrap_spec(w_name=WrappedDefault(None), w_obj=WrappedDefault(None)) |
| 993 |
def init(w_a, space, args_w, __kwonly__, w_obj=None, w_name=None): |
| 994 |
return space.newtuple([w_a, space.newtuple(args_w), w_obj, w_name]) |
| 995 |
w_g = space.wrap(gateway.interp2app_temp(init)) |
| 996 |
w_res = space.call_function(w_g, space.newint(1), space.newint(2)) |
| 997 |
assert space.eq_w(w_res, space.newtuple([space.newint(1), space.newtuple([space.newint(2)]), space.w_None, space.w_None])) |
| 998 |
|
| 999 |
def test_posonly_args(self): |
| 1000 |
space = self.space |
| 1001 |
@gateway.unwrap_spec(w_x2=WrappedDefault(50)) |
| 1002 |
def g(space, w_t, w_x2, __posonly__): |
| 1003 |
assert space.eq_w(w_t, space.newint(1)) |
| 1004 |
return w_x2 |
| 1005 |
w_g = space.wrap(gateway.interp2app_temp(g)) |
| 1006 |
w_res = space.call_function(w_g, space.wrap(1)) |
| 1007 |
assert space.eq_w(w_res, space.wrap(50)) |
| 1008 |
|
| 1009 |
def test_text_signature_module_function(self): |
| 1010 |
space = self.space |
| 1011 |
def f(space, w_x, w_y): |
| 1012 |
return w_x |
| 1013 |
w_f = space.wrap(gateway.interp2app_temp(f)) |
| 1014 |
assert w_f.w_text_signature is not None |
| 1015 |
assert space.text_w(w_f.w_text_signature) == '($module, x, y, /)' |
| 1016 |
|
| 1017 |
def test_text_signature_method(self): |
| 1018 |
space = self.space |
| 1019 |
class W_MyObj(W_Root): |
| 1020 |
def descr_m(self, space, w_x): |
| 1021 |
return w_x |
| 1022 |
app_m = gateway.interp2app_temp(W_MyObj.descr_m.im_func, |
| 1023 |
self_type=W_MyObj) |
| 1024 |
w_m = space.wrap(app_m) |
| 1025 |
assert w_m.w_text_signature is not None |
| 1026 |
assert space.text_w(w_m.w_text_signature) == '($self, x, /)' |
| 1027 |
|
| 1028 |
def test_text_signature_type_method(self): |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
from pypy.interpreter.typedef import TypeDef |
| 1036 |
space = self.space |
| 1037 |
def descr_method(space, w_obj): |
| 1038 |
return w_obj |
| 1039 |
ia = gateway.interp2app_temp(descr_method) |
| 1040 |
|
| 1041 |
ia._is_type_method = True |
| 1042 |
w_f = space.wrap(ia) |
| 1043 |
assert w_f.w_text_signature is not None |
| 1044 |
assert space.text_w(w_f.w_text_signature) == '($obj, /)' |
| 1045 |
|
| 1046 |
|
| 1047 |
def descr_module_fn(space, w_obj): |
| 1048 |
return w_obj |
| 1049 |
ia2 = gateway.interp2app_temp(descr_module_fn) |
| 1050 |
w_f2 = space.wrap(ia2) |
| 1051 |
assert space.text_w(w_f2.w_text_signature) == '($module, obj, /)' |
| 1052 |
|
| 1053 |
|
| 1054 |
def descr_method2(space, w_obj, w_proto): |
| 1055 |
return w_obj |
| 1056 |
ia3 = gateway.interp2app_temp(descr_method2) |
| 1057 |
ia3._is_type_method = True |
| 1058 |
w_f3 = space.wrap(ia3) |
| 1059 |
assert space.text_w(w_f3.w_text_signature) == '($obj, proto, /)' |
| 1060 |
|
| 1061 |
def test_text_signature_text_default(self): |
| 1062 |
space = self.space |
| 1063 |
from pypy.interpreter.gateway import Unwrapper |
| 1064 |
class MyFD(Unwrapper): |
| 1065 |
text_default = None |
| 1066 |
def unwrap(self, space, w_value): |
| 1067 |
if space.is_none(w_value): |
| 1068 |
return -100 |
| 1069 |
return space.int_w(w_value) |
| 1070 |
@gateway.unwrap_spec(fd=MyFD) |
| 1071 |
def f(space, __kwonly__, fd=-100): |
| 1072 |
pass |
| 1073 |
w_f = space.wrap(gateway.interp2app_temp(f)) |
| 1074 |
assert w_f.w_text_signature is not None |
| 1075 |
sig = space.text_w(w_f.w_text_signature) |
| 1076 |
assert 'fd=None' in sig |
| 1077 |
assert '-100' not in sig |
| 1078 |
|
| 1079 |
def test_text_signature_posonly(self): |
| 1080 |
space = self.space |
| 1081 |
@gateway.unwrap_spec(w_x=WrappedDefault(42)) |
| 1082 |
def f(space, w_self, w_x, __posonly__): |
| 1083 |
return w_x |
| 1084 |
w_f = space.wrap(gateway.interp2app_temp(f)) |
| 1085 |
assert w_f.w_text_signature is not None |
| 1086 |
sig = space.text_w(w_f.w_text_signature) |
| 1087 |
assert '/' in sig |
| 1088 |
assert '$module' in sig |
| 1089 |
|
| 1090 |
class AppTestPyTestMark: |
| 1091 |
@py.test.mark.unlikely_to_exist |
| 1092 |
def test_anything(self): |
| 1093 |
pass |
| 1094 |
|
| 1095 |
|
| 1096 |
class TestPassThroughArguments: |
| 1097 |
def test_pass_trough_arguments0(self): |
| 1098 |
space = self.space |
| 1099 |
|
| 1100 |
called = [] |
| 1101 |
|
| 1102 |
def f(space, __args__): |
| 1103 |
called.append(__args__) |
| 1104 |
a_w, _ = __args__.unpack() |
| 1105 |
return space.newtuple([space.wrap('f')]+a_w) |
| 1106 |
|
| 1107 |
w_f = space.wrap(gateway.interp2app_temp(f, |
| 1108 |
unwrap_spec=[gateway.ObjSpace, |
| 1109 |
gateway.Arguments])) |
| 1110 |
|
| 1111 |
args = argument.Arguments(space, [space.wrap(7)]) |
| 1112 |
|
| 1113 |
w_res = space.call_args(w_f, args) |
| 1114 |
assert space.is_true(space.eq(w_res, space.wrap(('f', 7)))) |
| 1115 |
|
| 1116 |
|
| 1117 |
assert called[0] is args |
| 1118 |
|
| 1119 |
def test_pass_trough_arguments1(self): |
| 1120 |
space = self.space |
| 1121 |
|
| 1122 |
called = [] |
| 1123 |
|
| 1124 |
def g(space, w_self, __args__): |
| 1125 |
called.append(__args__) |
| 1126 |
a_w, _ = __args__.unpack() |
| 1127 |
return space.newtuple([space.wrap('g'), w_self, ]+a_w) |
| 1128 |
|
| 1129 |
w_g = space.wrap(gateway.interp2app_temp(g, |
| 1130 |
unwrap_spec=[gateway.ObjSpace, |
| 1131 |
gateway.W_Root, |
| 1132 |
gateway.Arguments])) |
| 1133 |
|
| 1134 |
old_funcrun = w_g.code.funcrun |
| 1135 |
def funcrun_witness(func, args): |
| 1136 |
called.append('funcrun') |
| 1137 |
return old_funcrun(func, args) |
| 1138 |
|
| 1139 |
w_g.code.funcrun = funcrun_witness |
| 1140 |
|
| 1141 |
w_self = space.wrap('self') |
| 1142 |
|
| 1143 |
args3 = argument.Arguments(space, [space.wrap(3)]) |
| 1144 |
w_res = space.call_obj_args(w_g, w_self, args3) |
| 1145 |
assert space.is_true(space.eq(w_res, space.wrap(('g', 'self', 3)))) |
| 1146 |
|
| 1147 |
assert len(called) == 1 |
| 1148 |
assert called[0] is args3 |
| 1149 |
|
| 1150 |
called = [] |
| 1151 |
args0 = argument.Arguments(space, [space.wrap(0)]) |
| 1152 |
args = args0.prepend(w_self) |
| 1153 |
|
| 1154 |
w_res = space.call_args(w_g, args) |
| 1155 |
assert space.is_true(space.eq(w_res, space.wrap(('g', 'self', 0)))) |
| 1156 |
|
| 1157 |
assert len(called) == 2 |
| 1158 |
assert called[0] == 'funcrun' |
| 1159 |
called = [] |
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
w_res = space.call_function(w_g, w_self) |
| 1164 |
assert space.is_true(space.eq(w_res, space.wrap(('g', 'self')))) |
| 1165 |
assert len(called) == 1 |
| 1166 |
assert isinstance(called[0], argument.Arguments) |
| 1167 |
called = [] |
| 1168 |
|
| 1169 |
w_res = space.appexec([w_g], """(g): |
| 1170 |
return g('self', 11) |
| 1171 |
""") |
| 1172 |
assert space.is_true(space.eq(w_res, space.wrap(('g', 'self', 11)))) |
| 1173 |
assert len(called) == 1 |
| 1174 |
assert isinstance(called[0], argument.Arguments) |
| 1175 |
called = [] |
| 1176 |
|
| 1177 |
w_res = space.appexec([w_g], """(g): |
| 1178 |
class A(object): |
| 1179 |
m = g # not a builtin function, so works as method |
| 1180 |
d = {'A': A} |
| 1181 |
exec(\"\"\" |
| 1182 |
# own compiler |
| 1183 |
a = A() |
| 1184 |
y = a.m(33) |
| 1185 |
\"\"\", d) |
| 1186 |
return d['y'] == ('g', d['a'], 33) |
| 1187 |
""") |
| 1188 |
assert space.is_true(w_res) |
| 1189 |
assert len(called) == 1 |
| 1190 |
assert isinstance(called[0], argument.Arguments) |
| 1191 |
|
| 1192 |
def test_pass_trough_arguments_method(self): |
| 1193 |
space = self.space |
| 1194 |
|
| 1195 |
called = [] |
| 1196 |
|
| 1197 |
class W_Something(W_Root): |
| 1198 |
def f(self, space, __args__): |
| 1199 |
called.append(__args__) |
| 1200 |
a_w, _ = __args__.unpack() |
| 1201 |
return space.newtuple([space.wrap('f')]+a_w) |
| 1202 |
|
| 1203 |
w_f = space.wrap(gateway.interp2app_temp(W_Something.f)) |
| 1204 |
|
| 1205 |
w_self = space.wrap(W_Something()) |
| 1206 |
args = argument.Arguments(space, [space.wrap(7)]) |
| 1207 |
|
| 1208 |
w_res = space.call_obj_args(w_f, w_self, args) |
| 1209 |
assert space.is_true(space.eq(w_res, space.wrap(('f', 7)))) |
| 1210 |
|
| 1211 |
|
| 1212 |
assert called[0] is args |
| 1213 |
|
| 1214 |
def test_base_regular_descr_mismatch(self): |
| 1215 |
space = self.space |
| 1216 |
|
| 1217 |
def f(): |
| 1218 |
raise gateway.DescrMismatch |
| 1219 |
|
| 1220 |
w_f = space.wrap(gateway.interp2app_temp(f, |
| 1221 |
unwrap_spec=[])) |
| 1222 |
args = argument.Arguments(space, []) |
| 1223 |
space.raises_w(space.w_SystemError, space.call_args, w_f, args) |
| 1224 |
|
| 1225 |
def test_pass_trough_arguments0_descr_mismatch(self): |
| 1226 |
space = self.space |
| 1227 |
|
| 1228 |
def f(space, __args__): |
| 1229 |
raise gateway.DescrMismatch |
| 1230 |
|
| 1231 |
w_f = space.wrap(gateway.interp2app_temp(f, |
| 1232 |
unwrap_spec=[gateway.ObjSpace, |
| 1233 |
gateway.Arguments])) |
| 1234 |
args = argument.Arguments(space, []) |
| 1235 |
space.raises_w(space.w_SystemError, space.call_args, w_f, args) |
| 1236 |
|
| 1237 |
|
| 1238 |
class AppTestKeywordsToBuiltinSanity(object): |
| 1239 |
def test_type(self): |
| 1240 |
class X(object): |
| 1241 |
def __init__(myself, **kw): |
| 1242 |
pass |
| 1243 |
clash = type.__call__.__code__.co_varnames[0] |
| 1244 |
|
| 1245 |
X(**{clash: 33}) |
| 1246 |
type.__call__(X, **{clash: 33}) |
| 1247 |
|
| 1248 |
def test_object_new(self): |
| 1249 |
class X(object): |
| 1250 |
def __init__(self, **kw): |
| 1251 |
pass |
| 1252 |
clash = object.__new__.__code__.co_varnames[0] |
| 1253 |
|
| 1254 |
X(**{clash: 33}) |
| 1255 |
object.__new__(X, **{clash: 33}) |
| 1256 |
|
| 1257 |
def test_dict_new(self): |
| 1258 |
clash = dict.__new__.__code__.co_varnames[0] |
| 1259 |
|
| 1260 |
dict(**{clash: 33}) |
| 1261 |
dict.__new__(dict, **{clash: 33}) |
| 1262 |
|
| 1263 |
def test_dict_init(self): |
| 1264 |
d = {} |
| 1265 |
clash = dict.__init__.__code__.co_varnames[0] |
| 1266 |
|
| 1267 |
d.__init__(**{clash: 33}) |
| 1268 |
dict.__init__(d, **{clash: 33}) |
| 1269 |
|
| 1270 |
def test_dict_update(self): |
| 1271 |
d = {} |
| 1272 |
clash = dict.update.__code__.co_varnames[0] |
| 1273 |
|
| 1274 |
d.update(**{clash: 33}) |
| 1275 |
dict.update(d, **{clash: 33}) |
| 1276 |
|
| 1277 |
|
| 1278 |
class AppTestFastPathCrash(object): |
| 1279 |
def test_fast_path_crash(self): |
| 1280 |
|
| 1281 |
import sys |
| 1282 |
if '__pypy__' in sys.modules: |
| 1283 |
msg_fmt = "'%s' object expected, got '%s'" |
| 1284 |
else: |
| 1285 |
msg_fmt = "'%s' object but received a '%s'" |
| 1286 |
for obj in (dict, set): |
| 1287 |
with raises(TypeError) as excinfo: |
| 1288 |
obj.__init__(0) |
| 1289 |
msg = msg_fmt % (obj.__name__, 'int') |
| 1290 |
assert msg in str(excinfo.value) |