| 1 |
|
| 2 |
import py |
| 3 |
from pypy.interpreter.error import OperationError |
| 4 |
from pypy.interpreter.module import Module |
| 5 |
|
| 6 |
class TestModule: |
| 7 |
def test_name(self, space): |
| 8 |
w = space.wrap |
| 9 |
m = Module(space, space.wrap('m')) |
| 10 |
w_m = w(m) |
| 11 |
assert space.eq_w(space.getattr(w_m, w('__name__')), w('m')) |
| 12 |
|
| 13 |
def test_attr(self, space): |
| 14 |
w = space.wrap |
| 15 |
w_m = w(Module(space, space.wrap('m'))) |
| 16 |
self.space.setattr(w_m, w('x'), w(15)) |
| 17 |
assert space.eq_w(space.getattr(w_m, w('x')), w(15)) |
| 18 |
space.delattr(w_m, w('x')) |
| 19 |
space.raises_w(space.w_AttributeError, |
| 20 |
space.delattr, w_m, w('x')) |
| 21 |
|
| 22 |
def test___file__(self, space): |
| 23 |
w = space.wrap |
| 24 |
m = Module(space, space.wrap('m')) |
| 25 |
py.test.raises(OperationError, space.getattr, w(m), w('__file__')) |
| 26 |
m._cleanup_() |
| 27 |
py.test.raises(OperationError, space.getattr, w(m), w('__file__')) |
| 28 |
space.setattr(w(m), w('__file__'), w('m.py')) |
| 29 |
space.getattr(w(m), w('__file__')) |
| 30 |
m._cleanup_() |
| 31 |
py.test.raises(OperationError, space.getattr, w(m), w('__file__')) |
| 32 |
|
| 33 |
|
| 34 |
class AppTest_ModuleObject: |
| 35 |
def test_attr(self): |
| 36 |
m = __import__('builtins') |
| 37 |
m.x = 15 |
| 38 |
assert m.x == 15 |
| 39 |
assert getattr(m, 'x') == 15 |
| 40 |
setattr(m, 'x', 23) |
| 41 |
assert m.x == 23 |
| 42 |
assert getattr(m, 'x') == 23 |
| 43 |
del m.x |
| 44 |
raises(AttributeError, getattr, m, 'x') |
| 45 |
m.x = 15 |
| 46 |
delattr(m, 'x') |
| 47 |
raises(AttributeError, getattr, m, 'x') |
| 48 |
raises(AttributeError, delattr, m, 'x') |
| 49 |
raises(AttributeError, setattr, m, '__dict__', {}) |
| 50 |
|
| 51 |
def test_docstring(self): |
| 52 |
import sys |
| 53 |
foo = type(sys)('foo') |
| 54 |
assert foo.__name__ == 'foo' |
| 55 |
assert foo.__doc__ is None |
| 56 |
bar = type(sys)('bar','docstring') |
| 57 |
assert bar.__doc__ == 'docstring' |
| 58 |
|
| 59 |
def test___file__(self): |
| 60 |
import sys |
| 61 |
assert not hasattr(sys, '__file__') |
| 62 |
|
| 63 |
def test_repr(self): |
| 64 |
import sys |
| 65 |
if not hasattr(sys, "pypy_objspaceclass"): |
| 66 |
skip("need PyPy for _pypy_interact") |
| 67 |
r = repr(sys) |
| 68 |
assert r == "<module 'sys' (built-in)>" |
| 69 |
|
| 70 |
import _pypy_interact |
| 71 |
r = repr(_pypy_interact) |
| 72 |
assert (r.startswith("<module '_pypy_interact' from ") and |
| 73 |
('lib_pypy/_pypy_interact.py' in r or |
| 74 |
r'lib_pypy\\_pypy_interact.py' in r.lower()) and |
| 75 |
r.endswith('>')) |
| 76 |
nofile = type(_pypy_interact)('nofile', 'foo') |
| 77 |
assert repr(nofile) == "<module 'nofile'>" |
| 78 |
|
| 79 |
m = type(_pypy_interact).__new__(type(_pypy_interact)) |
| 80 |
assert repr(m).startswith("<module '?'") |
| 81 |
|
| 82 |
def test_repr_with_loader_with_valid_module_repr(self): |
| 83 |
import sys |
| 84 |
test_module = type(sys)("test_module", "doc") |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
class CustomLoader: |
| 90 |
@classmethod |
| 91 |
def module_repr(cls, module): |
| 92 |
mod_repr = ("<module {mod_name}: " |
| 93 |
"{cls} Test>".format(mod_name=repr(module.__name__), |
| 94 |
cls=repr(cls.__name__))) |
| 95 |
return mod_repr |
| 96 |
test_module.__loader__ = CustomLoader |
| 97 |
assert repr(test_module) == "<module 'test_module': 'CustomLoader' Test>" |
| 98 |
|
| 99 |
def test_repr_with_loader_with_module_repr_wrong_type(self): |
| 100 |
import sys |
| 101 |
test_module = type(sys)("test_module", "doc") |
| 102 |
|
| 103 |
|
| 104 |
class BuggyCustomLoader: |
| 105 |
@classmethod |
| 106 |
def module_repr(cls, module): |
| 107 |
return 5 |
| 108 |
|
| 109 |
test_module.__loader__ = BuggyCustomLoader |
| 110 |
raises(TypeError, repr, test_module) |
| 111 |
|
| 112 |
def test_repr_with_loader_with_raising_module_repr(self): |
| 113 |
import sys |
| 114 |
test_module = type(sys)("test_module", "doc") |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
class CustomLoaderWithRaisingRepr: |
| 119 |
@classmethod |
| 120 |
def module_repr(cls, module): |
| 121 |
return repr(1/0) |
| 122 |
|
| 123 |
test_module.__loader__ = CustomLoaderWithRaisingRepr |
| 124 |
mod_repr = repr(test_module) |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
loader_repr = repr(test_module.__loader__) |
| 129 |
expected_repr = "<module 'test_module' ({})>".format(loader_repr) |
| 130 |
assert mod_repr == expected_repr |
| 131 |
|
| 132 |
def test_repr_with_loader_with_raising_module_repr2(self): |
| 133 |
import sys |
| 134 |
test_module = type(sys)("test_module", "doc") |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
class CustomLoaderWithRaisingRepr: |
| 139 |
@classmethod |
| 140 |
def module_repr(cls, module): |
| 141 |
raise KeyboardInterrupt |
| 142 |
|
| 143 |
test_module.__loader__ = CustomLoaderWithRaisingRepr |
| 144 |
raises(KeyboardInterrupt, 'repr(test_module)') |
| 145 |
|
| 146 |
def test_repr_with_raising_loader_and___file__(self): |
| 147 |
import sys |
| 148 |
test_module = type(sys)("test_module", "doc") |
| 149 |
test_module.__file__ = "/fake_dir/test_module.py" |
| 150 |
class CustomLoaderWithRaisingRepr: |
| 151 |
"""Operates just like the builtin importer, but implements a |
| 152 |
module_repr method that raises an exception.""" |
| 153 |
@classmethod |
| 154 |
def module_repr(cls, module): |
| 155 |
return repr(1/0) |
| 156 |
|
| 157 |
test_module.__loader__ = CustomLoaderWithRaisingRepr |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
expected_repr = "<module 'test_module' from '/fake_dir/test_module.py'>" |
| 164 |
assert repr(test_module) == expected_repr |
| 165 |
|
| 166 |
def test_repr_with_missing_name(self): |
| 167 |
import sys |
| 168 |
test_module = type(sys)("test_module", "doc") |
| 169 |
del test_module.__name__ |
| 170 |
mod_repr = repr(test_module) |
| 171 |
assert mod_repr == "<module '?'>" |
| 172 |
|
| 173 |
def test_dir(self): |
| 174 |
import sys |
| 175 |
items = sys.__dir__() |
| 176 |
assert sorted(items) == dir(sys) |
| 177 |
|
| 178 |
def test_package(self): |
| 179 |
import sys |
| 180 |
import os |
| 181 |
|
| 182 |
assert sys.__package__ == '' |
| 183 |
assert os.__package__ == '' |
| 184 |
assert type(sys)('foo').__package__ is None |
| 185 |
|
| 186 |
def test_name_nonascii(self): |
| 187 |
import sys |
| 188 |
m = type(sys)('日本') |
| 189 |
assert m.__name__ == '日本' |
| 190 |
assert repr(m).startswith("<module '日本'") |
| 191 |
|
| 192 |
def test_AttributeError_message(self): |
| 193 |
import sys |
| 194 |
test_module = type(sys)("test_module", "doc") |
| 195 |
excinfo = raises(AttributeError, 'test_module.does_not_exist') |
| 196 |
assert (excinfo.value.args[0] == |
| 197 |
"module 'test_module' has no attribute 'does_not_exist'") |
| 198 |
|
| 199 |
nameless = type(sys)("nameless", "doc") |
| 200 |
del nameless.__name__ |
| 201 |
assert not hasattr(nameless, '__name__') |
| 202 |
excinfo = raises(AttributeError, 'nameless.does_not_exist') |
| 203 |
assert (excinfo.value.args[0] == |
| 204 |
"module has no attribute 'does_not_exist'") |
| 205 |
|
| 206 |
def test_weakrefable(self): |
| 207 |
import weakref |
| 208 |
weakref.ref(weakref) |
| 209 |
|
| 210 |
def test_all_dict_content(self): |
| 211 |
import sys |
| 212 |
m = type(sys)('foo') |
| 213 |
assert m.__dict__ == {'__name__': 'foo', |
| 214 |
'__doc__': None, |
| 215 |
'__package__': None, |
| 216 |
'__loader__': None, |
| 217 |
'__spec__': None} |
| 218 |
|
| 219 |
def test_module_new_makes_empty_dict(self): |
| 220 |
import sys |
| 221 |
m = type(sys).__new__(type(sys)) |
| 222 |
assert not m.__dict__ |
| 223 |
|
| 224 |
def test_class_assignment_for_module(self): |
| 225 |
import sys |
| 226 |
modtype = type(sys) |
| 227 |
class X(modtype): |
| 228 |
_foobar_ = 42 |
| 229 |
|
| 230 |
m = X("yytest_moduleyy") |
| 231 |
assert type(m) is m.__class__ is X |
| 232 |
assert m._foobar_ == 42 |
| 233 |
m.__class__ = modtype |
| 234 |
assert type(m) is m.__class__ is modtype |
| 235 |
assert not hasattr(m, '_foobar_') |
| 236 |
|
| 237 |
m = modtype("xxtest_modulexx") |
| 238 |
assert type(m) is m.__class__ is modtype |
| 239 |
m.__class__ = X |
| 240 |
assert m._foobar_ == 42 |
| 241 |
assert type(m) is m.__class__ is X |
| 242 |
|
| 243 |
sys.__class__ = modtype |
| 244 |
assert type(sys) is sys.__class__ is modtype |
| 245 |
sys.__class__ = X |
| 246 |
assert sys._foobar_ == 42 |
| 247 |
sys.__class__ = modtype |
| 248 |
|
| 249 |
class XX(modtype): |
| 250 |
__slots__ = ['a', 'b'] |
| 251 |
|
| 252 |
x = XX("zztest_modulezz") |
| 253 |
assert x.__class__ is XX |
| 254 |
raises(AttributeError, "x.a") |
| 255 |
x.a = 42 |
| 256 |
assert x.a == 42 |
| 257 |
x.a = 43 |
| 258 |
assert x.a == 43 |
| 259 |
assert 'a' not in x.__dict__ |
| 260 |
del x.a |
| 261 |
raises(AttributeError, "x.a") |
| 262 |
raises(AttributeError, "del x.a") |
| 263 |
raises(TypeError, "x.__class__ = X") |
| 264 |
raises(TypeError, "sys.__class__ = XX") |
| 265 |
|
| 266 |
def test_class_assignment_for_module_getattr(self): |
| 267 |
import sys |
| 268 |
modtype = type(sys) |
| 269 |
class X(modtype): |
| 270 |
def __getattr__(self, n): |
| 271 |
if n == "m": |
| 272 |
return 123 |
| 273 |
raise AttributeError |
| 274 |
mod = modtype("mymod") |
| 275 |
assert not hasattr(mod, "m") |
| 276 |
mod.x = 456 |
| 277 |
assert mod.x == 456 |
| 278 |
|
| 279 |
mod.__class__ = X |
| 280 |
assert mod.m == 123 |
| 281 |
assert mod.x == 456 |
| 282 |
|
| 283 |
def test_getattr_dir(self): |
| 284 |
def __getattr__(name): |
| 285 |
if name == 'y': |
| 286 |
return 42 |
| 287 |
elif name == 'z': |
| 288 |
return |
| 289 |
raise AttributeError("No attribute '{}'".format(name)) |
| 290 |
|
| 291 |
def __dir__(): |
| 292 |
return ['x', 'y', 'z', 'w'] |
| 293 |
|
| 294 |
import sys |
| 295 |
m = type(sys)('foo') |
| 296 |
m.x = 'x' |
| 297 |
m.__getattr__ = __getattr__ |
| 298 |
print(m.__dir__) |
| 299 |
m.__dir__ = __dir__ |
| 300 |
|
| 301 |
assert m.x == 'x' |
| 302 |
assert m.y == 42 |
| 303 |
assert m.z == None |
| 304 |
excinfo = raises(AttributeError, 'm.w') |
| 305 |
assert str(excinfo.value) == "No attribute 'w'" |
| 306 |
print(dir(m)) |
| 307 |
assert dir(m) == sorted(['x', 'y', 'z', 'w']) |