| 1 |
|
| 2 |
from py.test import raises |
| 3 |
from pypy.interpreter.error import OperationError |
| 4 |
from pypy.interpreter.function import Function |
| 5 |
from pypy.interpreter.pycode import PyCode |
| 6 |
from rpython.rlib.rarithmetic import r_longlong, r_ulonglong |
| 7 |
import sys |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
INT32_MAX = 2147483648 |
| 14 |
|
| 15 |
|
| 16 |
class TestObjSpace: |
| 17 |
|
| 18 |
def test_isinstance(self): |
| 19 |
space = self.space |
| 20 |
w_i = space.wrap(4) |
| 21 |
w_result = space.isinstance(w_i, space.w_int) |
| 22 |
assert space.is_true(w_result) |
| 23 |
assert space.isinstance_w(w_i, space.w_int) |
| 24 |
w_result = space.isinstance(w_i, space.w_bytes) |
| 25 |
assert not space.is_true(w_result) |
| 26 |
assert not space.isinstance_w(w_i, space.w_bytes) |
| 27 |
|
| 28 |
def test_newlist(self): |
| 29 |
w = self.space.wrap |
| 30 |
l = range(10) |
| 31 |
w_l = self.space.newlist([w(i) for i in l]) |
| 32 |
assert self.space.eq_w(w_l, w(l)) |
| 33 |
|
| 34 |
def test_newdict(self): |
| 35 |
d = {} |
| 36 |
w_d = self.space.newdict() |
| 37 |
assert self.space.eq_w(w_d, self.space.wrap(d)) |
| 38 |
|
| 39 |
def test_newtuple(self): |
| 40 |
w = self.space.wrap |
| 41 |
t = tuple(range(10)) |
| 42 |
w_t = self.space.newtuple([w(i) for i in t]) |
| 43 |
assert self.space.eq_w(w_t, w(t)) |
| 44 |
|
| 45 |
def test_is_true(self): |
| 46 |
w = self.space.wrap |
| 47 |
true = (1==1) |
| 48 |
false = (1==0) |
| 49 |
w_true = w(true) |
| 50 |
w_false = w(false) |
| 51 |
assert self.space.is_true(w_true) |
| 52 |
assert not self.space.is_true(w_false) |
| 53 |
|
| 54 |
def test_is_(self): |
| 55 |
w_l = self.space.newlist([]) |
| 56 |
w_m = self.space.newlist([]) |
| 57 |
assert self.space.is_(w_l, w_l) == self.space.w_True |
| 58 |
assert self.space.is_(w_l, w_m) == self.space.w_False |
| 59 |
|
| 60 |
def test_newbool(self): |
| 61 |
assert self.space.newbool(0) == self.space.w_False |
| 62 |
assert self.space.newbool(1) == self.space.w_True |
| 63 |
|
| 64 |
def test_unpackiterable(self): |
| 65 |
space = self.space |
| 66 |
w = space.wrap |
| 67 |
l = [space.newlist([]) for l in range(4)] |
| 68 |
w_l = space.newlist(l) |
| 69 |
l1 = space.unpackiterable(w_l) |
| 70 |
l2 = space.unpackiterable(w_l, 4) |
| 71 |
for i in range(4): |
| 72 |
assert space.is_w(l1[i], l[i]) |
| 73 |
assert space.is_w(l2[i], l[i]) |
| 74 |
err = raises(OperationError, space.unpackiterable, w_l, 3) |
| 75 |
assert err.value.match(space, space.w_ValueError) |
| 76 |
err = raises(OperationError, space.unpackiterable, w_l, 5) |
| 77 |
assert err.value.match(space, space.w_ValueError) |
| 78 |
w_a = space.appexec((), """(): |
| 79 |
class A(object): |
| 80 |
def __iter__(self): |
| 81 |
return self |
| 82 |
def __next__(self): |
| 83 |
raise StopIteration |
| 84 |
def __len__(self): |
| 85 |
1/0 |
| 86 |
return A() |
| 87 |
""") |
| 88 |
try: |
| 89 |
space.unpackiterable(w_a) |
| 90 |
except OperationError as o: |
| 91 |
if not o.match(space, space.w_ZeroDivisionError): |
| 92 |
raise |
| 93 |
else: |
| 94 |
raise Exception("DID NOT RAISE") |
| 95 |
|
| 96 |
def test_fixedview(self): |
| 97 |
space = self.space |
| 98 |
w = space.wrap |
| 99 |
l = [w(1), w(2), w(3), w(4)] |
| 100 |
w_l = space.newtuple(l) |
| 101 |
assert space.fixedview(w_l) == l |
| 102 |
assert space.fixedview(w_l, 4) == l |
| 103 |
err = raises(OperationError, space.fixedview, w_l, 3) |
| 104 |
assert err.value.match(space, space.w_ValueError) |
| 105 |
err = raises(OperationError, space.fixedview, w_l, 5) |
| 106 |
assert err.value.match(space, space.w_ValueError) |
| 107 |
|
| 108 |
def test_listview(self): |
| 109 |
space = self.space |
| 110 |
w = space.wrap |
| 111 |
l = [w(1), w(2), w(3), w(4)] |
| 112 |
w_l = space.newtuple(l) |
| 113 |
assert space.listview(w_l) == l |
| 114 |
assert space.listview(w_l, 4) == l |
| 115 |
err = raises(OperationError, space.listview, w_l, 3) |
| 116 |
assert err.value.match(space, space.w_ValueError) |
| 117 |
err = raises(OperationError, space.listview, w_l, 5) |
| 118 |
assert err.value.match(space, space.w_ValueError) |
| 119 |
|
| 120 |
def test_exception_match(self): |
| 121 |
assert self.space.exception_match(self.space.w_ValueError, |
| 122 |
self.space.w_ValueError) |
| 123 |
assert self.space.exception_match(self.space.w_IndexError, |
| 124 |
self.space.w_LookupError) |
| 125 |
assert not self.space.exception_match(self.space.w_ValueError, |
| 126 |
self.space.w_LookupError) |
| 127 |
|
| 128 |
def test_lookup(self): |
| 129 |
w = self.space.wrap |
| 130 |
w_object_doc = self.space.getattr(self.space.w_object, w("__doc__")) |
| 131 |
w_instance = self.space.appexec([], "(): return object()") |
| 132 |
w_doc = self.space.lookup(w_instance, "__doc__") |
| 133 |
assert self.space.text_w(w_doc) == self.space.text_w(w_object_doc) |
| 134 |
assert self.space.lookup(w_instance, "gobbledygook") is None |
| 135 |
w_instance = self.space.appexec([], """(): |
| 136 |
class Lookup(object): |
| 137 |
"bla" |
| 138 |
return Lookup()""") |
| 139 |
assert self.space.text_w(self.space.lookup(w_instance, "__doc__")) == "bla" |
| 140 |
|
| 141 |
def test_callable(self): |
| 142 |
def is_callable(w_obj): |
| 143 |
return self.space.is_true(self.space.callable(w_obj)) |
| 144 |
|
| 145 |
assert is_callable(self.space.w_int) |
| 146 |
assert not is_callable(self.space.wrap(1)) |
| 147 |
w_func = self.space.appexec([], "():\n def f(): pass\n return f") |
| 148 |
assert is_callable(w_func) |
| 149 |
w_lambda_func = self.space.appexec([], "(): return lambda: True") |
| 150 |
assert is_callable(w_lambda_func) |
| 151 |
|
| 152 |
w_instance = self.space.appexec([], """(): |
| 153 |
class Call(object): |
| 154 |
def __call__(self): pass |
| 155 |
return Call()""") |
| 156 |
assert is_callable(w_instance) |
| 157 |
|
| 158 |
w_instance = self.space.appexec([], |
| 159 |
"():\n class NoCall(object): pass\n return NoCall()") |
| 160 |
assert not is_callable(w_instance) |
| 161 |
self.space.setattr(w_instance, self.space.wrap("__call__"), w_func) |
| 162 |
assert not is_callable(w_instance) |
| 163 |
|
| 164 |
|
| 165 |
def test_int_w(self): |
| 166 |
space = self.space |
| 167 |
w_x = space.wrap(42) |
| 168 |
assert space.int_w(w_x) == 42 |
| 169 |
assert space.int_w(w_x, allow_conversion=False) == 42 |
| 170 |
|
| 171 |
w_x = space.wrap(44.0) |
| 172 |
space.raises_w(space.w_TypeError, space.int_w, w_x) |
| 173 |
space.raises_w(space.w_TypeError, space.int_w, w_x, allow_conversion=False) |
| 174 |
|
| 175 |
w_instance = self.space.appexec([], """(): |
| 176 |
class MyInt(object): |
| 177 |
def __int__(self): |
| 178 |
return 43 |
| 179 |
return MyInt() |
| 180 |
""") |
| 181 |
assert space.int_w(w_instance) == 43 |
| 182 |
space.raises_w(space.w_TypeError, space.int_w, w_instance, allow_conversion=False) |
| 183 |
|
| 184 |
w_instance = self.space.appexec([], """(): |
| 185 |
class MyInt(object): |
| 186 |
def __int__(self): |
| 187 |
return 43 |
| 188 |
|
| 189 |
class AnotherInt(object): |
| 190 |
def __int__(self): |
| 191 |
return MyInt() |
| 192 |
|
| 193 |
return AnotherInt() |
| 194 |
""") |
| 195 |
space.raises_w(space.w_TypeError, space.int_w, w_instance) |
| 196 |
space.raises_w(space.w_TypeError, space.int_w, w_instance, allow_conversion=False) |
| 197 |
|
| 198 |
def test_interp_w(self): |
| 199 |
w = self.space.wrap |
| 200 |
w_bltinfunction = self.space.builtin.get('len') |
| 201 |
res = self.space.interp_w(Function, w_bltinfunction) |
| 202 |
assert res is w_bltinfunction |
| 203 |
self.space.raises_w(self.space.w_TypeError, self.space.interp_w, PyCode, w_bltinfunction) |
| 204 |
self.space.raises_w(self.space.w_TypeError, self.space.interp_w, Function, w(42)) |
| 205 |
self.space.raises_w(self.space.w_TypeError, self.space.interp_w, Function, w(None)) |
| 206 |
res = self.space.interp_w(Function, w(None), can_be_None=True) |
| 207 |
assert res is None |
| 208 |
|
| 209 |
def test_text0_w(self): |
| 210 |
space = self.space |
| 211 |
w = space.wrap |
| 212 |
assert space.text0_w(w("123")) == "123" |
| 213 |
space.raises_w(space.w_ValueError, space.text0_w, w("123\x004")) |
| 214 |
|
| 215 |
def test_text_w(self): |
| 216 |
space = self.space |
| 217 |
x = u'àèì' |
| 218 |
w_name = space.wrap(x) |
| 219 |
assert space.text_w(w_name) == x.encode('utf-8') |
| 220 |
|
| 221 |
def test_getindex_w(self): |
| 222 |
w_instance1 = self.space.appexec([], """(): |
| 223 |
class X(object): |
| 224 |
def __index__(self): return 42 |
| 225 |
return X()""") |
| 226 |
w_instance2 = self.space.appexec([], """(): |
| 227 |
class Y(object): |
| 228 |
def __index__(self): return 2**70 |
| 229 |
return Y()""") |
| 230 |
first = self.space.getindex_w(w_instance1, None) |
| 231 |
assert first == 42 |
| 232 |
second = self.space.getindex_w(w_instance2, None) |
| 233 |
assert second == sys.maxint |
| 234 |
self.space.raises_w(self.space.w_IndexError, |
| 235 |
self.space.getindex_w, w_instance2, self.space.w_IndexError) |
| 236 |
try: |
| 237 |
self.space.getindex_w(self.space.w_tuple, None, "foobar") |
| 238 |
except OperationError as e: |
| 239 |
assert e.errorstr(self.space) == ( |
| 240 |
"TypeError: foobar indices must be integers or slices, not type") |
| 241 |
else: |
| 242 |
assert 0, "should have raised" |
| 243 |
|
| 244 |
def test_r_longlong_w(self): |
| 245 |
space = self.space |
| 246 |
w_value = space.wrap(12) |
| 247 |
res = space.r_longlong_w(w_value) |
| 248 |
assert res == 12 |
| 249 |
assert type(res) is r_longlong |
| 250 |
w_value = space.wrap(r_longlong(-INT32_MAX * 42)) |
| 251 |
res = space.r_longlong_w(w_value) |
| 252 |
assert res == -INT32_MAX * 42 |
| 253 |
assert type(res) is r_longlong |
| 254 |
w_obj = space.wrap("hello world") |
| 255 |
space.raises_w(space.w_TypeError, space.r_longlong_w, w_obj) |
| 256 |
w_obj = space.wrap(-12.34) |
| 257 |
space.raises_w(space.w_TypeError, space.r_longlong_w, w_obj) |
| 258 |
|
| 259 |
def test_r_ulonglong_w(self): |
| 260 |
space = self.space |
| 261 |
w_value = space.wrap(12) |
| 262 |
res = space.r_ulonglong_w(w_value) |
| 263 |
assert res == 12 |
| 264 |
assert type(res) is r_ulonglong |
| 265 |
w_value = space.wrap(r_ulonglong(INT32_MAX * 42)) |
| 266 |
res = space.r_ulonglong_w(w_value) |
| 267 |
assert res == INT32_MAX * 42 |
| 268 |
assert type(res) is r_ulonglong |
| 269 |
w_obj = space.wrap("hello world") |
| 270 |
space.raises_w(space.w_TypeError, space.r_ulonglong_w, w_obj) |
| 271 |
w_obj = space.wrap(-12.34) |
| 272 |
space.raises_w(space.w_TypeError, space.r_ulonglong_w, w_obj) |
| 273 |
w_obj = space.wrap(-12) |
| 274 |
space.raises_w(space.w_ValueError, space.r_ulonglong_w, w_obj) |
| 275 |
|
| 276 |
def test_truncatedint_w(self): |
| 277 |
space = self.space |
| 278 |
assert space.truncatedint_w(space.wrap(42)) == 42 |
| 279 |
assert space.truncatedint_w(space.wrap(sys.maxint)) == sys.maxint |
| 280 |
assert space.truncatedint_w(space.wrap(sys.maxint+1)) == -sys.maxint-1 |
| 281 |
assert space.truncatedint_w(space.wrap(-1)) == -1 |
| 282 |
assert space.truncatedint_w(space.wrap(-sys.maxint-2)) == sys.maxint |
| 283 |
|
| 284 |
def test_truncatedlonglong_w(self): |
| 285 |
space = self.space |
| 286 |
w_value = space.wrap(12) |
| 287 |
res = space.truncatedlonglong_w(w_value) |
| 288 |
assert res == 12 |
| 289 |
assert type(res) is r_longlong |
| 290 |
|
| 291 |
w_value = space.wrap(r_ulonglong(9223372036854775808)) |
| 292 |
res = space.truncatedlonglong_w(w_value) |
| 293 |
assert res == -9223372036854775808 |
| 294 |
assert type(res) is r_longlong |
| 295 |
|
| 296 |
w_value = space.wrap(r_ulonglong(18446744073709551615)) |
| 297 |
res = space.truncatedlonglong_w(w_value) |
| 298 |
assert res == -1 |
| 299 |
assert type(res) is r_longlong |
| 300 |
|
| 301 |
w_value = space.wrap(r_ulonglong(18446744073709551616)) |
| 302 |
res = space.truncatedlonglong_w(w_value) |
| 303 |
assert res == 0 |
| 304 |
assert type(res) is r_longlong |
| 305 |
|
| 306 |
|
| 307 |
def test_call_obj_args(self): |
| 308 |
from pypy.interpreter.argument import Arguments |
| 309 |
|
| 310 |
space = self.space |
| 311 |
|
| 312 |
w_f = space.appexec([], """(): |
| 313 |
def f(x, y): |
| 314 |
return (x, y) |
| 315 |
return f |
| 316 |
""") |
| 317 |
|
| 318 |
w_a = space.appexec([], """(): |
| 319 |
class A(object): |
| 320 |
def __call__(self, x): |
| 321 |
return x |
| 322 |
return A() |
| 323 |
""") |
| 324 |
|
| 325 |
w_9 = space.wrap(9) |
| 326 |
w_1 = space.wrap(1) |
| 327 |
|
| 328 |
w_res = space.call_obj_args(w_f, w_9, Arguments(space, [w_1])) |
| 329 |
|
| 330 |
w_x, w_y = space.fixedview(w_res, 2) |
| 331 |
assert w_x is w_9 |
| 332 |
assert w_y is w_1 |
| 333 |
|
| 334 |
w_res = space.call_obj_args(w_a, w_9, Arguments(space, [])) |
| 335 |
assert w_res is w_9 |
| 336 |
|
| 337 |
def test_compare_by_iteration(self): |
| 338 |
import operator |
| 339 |
space = self.space |
| 340 |
for op in ['eq', 'ne', 'lt', 'le', 'gt', 'ge']: |
| 341 |
comparer = getattr(operator, op) |
| 342 |
for x in [[], [0], [0, 1], [0, 1, 2]]: |
| 343 |
for y in [[], [-1], [0], [1], [-1, 0], |
| 344 |
[0, 0], [0, 1], [0, 2], |
| 345 |
[0, 1, 1], [0, 1, 2], [0, 1, 3]]: |
| 346 |
w_res = space.compare_by_iteration(space.wrap(x), |
| 347 |
space.wrap(y), op) |
| 348 |
if comparer(x, y): |
| 349 |
assert w_res is space.w_True |
| 350 |
else: |
| 351 |
assert w_res is space.w_False |
| 352 |
|
| 353 |
def test_isabstract(self, space): |
| 354 |
w_A = space.appexec([], """(): |
| 355 |
class A: |
| 356 |
def f(): pass |
| 357 |
f.__isabstractmethod__ = True |
| 358 |
|
| 359 |
def g(): pass |
| 360 |
g.__isabstractmethod__ = True |
| 361 |
g = classmethod(g) |
| 362 |
|
| 363 |
def h(): pass |
| 364 |
h.__isabstractmethod__ = True |
| 365 |
h = staticmethod(h) |
| 366 |
return A""") |
| 367 |
w_B = space.appexec([], """(): |
| 368 |
class B: |
| 369 |
def f(): pass |
| 370 |
|
| 371 |
@classmethod |
| 372 |
def g(): pass |
| 373 |
|
| 374 |
@staticmethod |
| 375 |
def h(): pass |
| 376 |
return B""") |
| 377 |
assert space.isabstractmethod_w(space.getattr(w_A, space.wrap('f'))) |
| 378 |
assert space.isabstractmethod_w(space.getattr(w_A, space.wrap('g'))) |
| 379 |
assert space.isabstractmethod_w(space.getattr(w_A, space.wrap('h'))) |
| 380 |
assert not space.isabstractmethod_w(space.getattr(w_B, space.wrap('f'))) |
| 381 |
assert not space.isabstractmethod_w(space.getattr(w_B, space.wrap('g'))) |
| 382 |
assert not space.isabstractmethod_w(space.getattr(w_B, space.wrap('h'))) |
| 383 |
|
| 384 |
def test_unicode_not_in_typed_unwrap_errors(self): |
| 385 |
space = self.space |
| 386 |
for meth in space.text_w, space.utf8_w, space.convert_to_w_unicode: |
| 387 |
with raises(OperationError) as excinfo: |
| 388 |
meth(space.newint(1)) |
| 389 |
exc = excinfo.value |
| 390 |
exc.normalize_exception(space) |
| 391 |
w_exc = exc.get_w_value(space) |
| 392 |
assert space.text_w(space.str(excinfo.value.get_w_value(space))) == \ |
| 393 |
'expected str, got int object' |
| 394 |
|
| 395 |
|
| 396 |
class TestModuleMinimal: |
| 397 |
def test_sys_exists(self): |
| 398 |
assert self.space.sys |
| 399 |
|
| 400 |
def test_import_exists(self): |
| 401 |
space = self.space |
| 402 |
assert space.builtin |
| 403 |
w_name = space.wrap('__import__') |
| 404 |
w_builtin = space.sys.getmodule('builtins') |
| 405 |
w_import = self.space.getattr(w_builtin, w_name) |
| 406 |
assert space.is_true(w_import) |
| 407 |
|
| 408 |
def test_stdlib_module_names(self): |
| 409 |
space = self.space |
| 410 |
w_stdlib_module_names = space.getattr(space.sys, space.newtext("stdlib_module_names")) |
| 411 |
assert space.is_true(w_stdlib_module_names) |
| 412 |
|
| 413 |
def test_sys_import(self): |
| 414 |
from pypy.interpreter.main import run_string |
| 415 |
run_string('import sys', space=self.space) |
| 416 |
|
| 417 |
def test_dont_reload_builtin_mods_on_startup(self): |
| 418 |
from pypy.tool.option import make_config, make_objspace |
| 419 |
config = make_config(None) |
| 420 |
space = make_objspace(config) |
| 421 |
w_executable = space.wrap('executable') |
| 422 |
assert space.findattr(space.sys, w_executable) is None |
| 423 |
space.setattr(space.sys, w_executable, space.wrap('foobar')) |
| 424 |
assert space.text_w(space.getattr(space.sys, w_executable)) == 'foobar' |
| 425 |
space.startup() |
| 426 |
assert space.text_w(space.getattr(space.sys, w_executable)) == 'foobar' |
| 427 |
|
| 428 |
def test_interned_strings_are_weak(self): |
| 429 |
import weakref, gc, random |
| 430 |
space = self.space |
| 431 |
assert space.config.translation.rweakref |
| 432 |
w1 = space.new_interned_str("abcdef") |
| 433 |
w2 = space.new_interned_str("abcdef") |
| 434 |
assert w2 is w1 |
| 435 |
|
| 436 |
|
| 437 |
rw1 = weakref.ref(w1) |
| 438 |
del w1, w2 |
| 439 |
i = 10 |
| 440 |
while rw1() is not None: |
| 441 |
i -= 1 |
| 442 |
assert i >= 0 |
| 443 |
gc.collect() |
| 444 |
|
| 445 |
s = "foobar%r" % random.random() |
| 446 |
w0 = space.wrap(s) |
| 447 |
w1 = space.new_interned_w_str(w0) |
| 448 |
assert w1 is w0 |
| 449 |
w2 = space.new_interned_w_str(w0) |
| 450 |
assert w2 is w0 |
| 451 |
|
| 452 |
def test_atexit_catches_exceptions(self): |
| 453 |
from pypy.tool.pytest.objspace import maketestobjspace |
| 454 |
space = maketestobjspace() |
| 455 |
space.appexec([], """(): |
| 456 |
import atexit |
| 457 |
atexit.register(lambda: this_is_an_unknown_name) |
| 458 |
""") |
| 459 |
ret = space.finish() |
| 460 |
assert ret == 0 |
| 461 |
|
| 462 |
|
| 463 |
def test_exit_closed_std(self): |
| 464 |
from pypy.tool.pytest.objspace import maketestobjspace |
| 465 |
space = maketestobjspace() |
| 466 |
space.appexec([], """(): |
| 467 |
import sys, os |
| 468 |
sys.stdout.write('x') |
| 469 |
os.close(sys.stdout.fileno()) |
| 470 |
""") |
| 471 |
ret = space.finish() |
| 472 |
assert ret < 0 |