# encoding: utf-8 import py from pypy.interpreter.error import OperationError from pypy.interpreter.module import Module class TestModule: def test_name(self, space): w = space.wrap m = Module(space, space.wrap('m')) w_m = w(m) assert space.eq_w(space.getattr(w_m, w('__name__')), w('m')) def test_attr(self, space): w = space.wrap w_m = w(Module(space, space.wrap('m'))) self.space.setattr(w_m, w('x'), w(15)) assert space.eq_w(space.getattr(w_m, w('x')), w(15)) space.delattr(w_m, w('x')) space.raises_w(space.w_AttributeError, space.delattr, w_m, w('x')) def test___file__(self, space): w = space.wrap m = Module(space, space.wrap('m')) py.test.raises(OperationError, space.getattr, w(m), w('__file__')) m._cleanup_() py.test.raises(OperationError, space.getattr, w(m), w('__file__')) space.setattr(w(m), w('__file__'), w('m.py')) space.getattr(w(m), w('__file__')) # does not raise m._cleanup_() py.test.raises(OperationError, space.getattr, w(m), w('__file__')) class AppTest_ModuleObject: def test_attr(self): m = __import__('builtins') m.x = 15 assert m.x == 15 assert getattr(m, 'x') == 15 setattr(m, 'x', 23) assert m.x == 23 assert getattr(m, 'x') == 23 del m.x raises(AttributeError, getattr, m, 'x') m.x = 15 delattr(m, 'x') raises(AttributeError, getattr, m, 'x') raises(AttributeError, delattr, m, 'x') raises(AttributeError, setattr, m, '__dict__', {}) def test_docstring(self): import sys foo = type(sys)('foo') assert foo.__name__ == 'foo' assert foo.__doc__ is None bar = type(sys)('bar','docstring') assert bar.__doc__ == 'docstring' def test___file__(self): import sys assert not hasattr(sys, '__file__') def test_repr(self): import sys if not hasattr(sys, "pypy_objspaceclass"): skip("need PyPy for _pypy_interact") r = repr(sys) assert r == "" import _pypy_interact # known to be in lib_pypy r = repr(_pypy_interact) assert (r.startswith("')) nofile = type(_pypy_interact)('nofile', 'foo') assert repr(nofile) == "" m = type(_pypy_interact).__new__(type(_pypy_interact)) assert repr(m).startswith("".format(mod_name=repr(module.__name__), cls=repr(cls.__name__))) return mod_repr test_module.__loader__ = CustomLoader assert repr(test_module) == "" def test_repr_with_loader_with_module_repr_wrong_type(self): import sys test_module = type(sys)("test_module", "doc") # This return value must be a string. class BuggyCustomLoader: @classmethod def module_repr(cls, module): return 5 test_module.__loader__ = BuggyCustomLoader raises(TypeError, repr, test_module) def test_repr_with_loader_with_raising_module_repr(self): import sys test_module = type(sys)("test_module", "doc") # If an exception occurs in module_repr(), the exception is caught # and discarded, and the calculation of the module's repr continues # as if module_repr() did not exist. class CustomLoaderWithRaisingRepr: @classmethod def module_repr(cls, module): return repr(1/0) test_module.__loader__ = CustomLoaderWithRaisingRepr mod_repr = repr(test_module) # The module has no __file__ attribute, so the repr should use # the loader and name loader_repr = repr(test_module.__loader__) expected_repr = "".format(loader_repr) assert mod_repr == expected_repr def test_repr_with_loader_with_raising_module_repr2(self): import sys test_module = type(sys)("test_module", "doc") # If an exception occurs in module_repr(), the exception is caught # and discarded, and the calculation of the module's repr continues # as if module_repr() did not exist. class CustomLoaderWithRaisingRepr: @classmethod def module_repr(cls, module): raise KeyboardInterrupt test_module.__loader__ = CustomLoaderWithRaisingRepr raises(KeyboardInterrupt, 'repr(test_module)') def test_repr_with_raising_loader_and___file__(self): import sys test_module = type(sys)("test_module", "doc") test_module.__file__ = "/fake_dir/test_module.py" class CustomLoaderWithRaisingRepr: """Operates just like the builtin importer, but implements a module_repr method that raises an exception.""" @classmethod def module_repr(cls, module): return repr(1/0) test_module.__loader__ = CustomLoaderWithRaisingRepr # If the module has an __file__ attribute, this is used as part # of the module's repr. # (If we have a loader that doesn't correctly implement module_repr, # if we have a path, we always just use name and path. expected_repr = "" assert repr(test_module) == expected_repr def test_repr_with_missing_name(self): import sys test_module = type(sys)("test_module", "doc") del test_module.__name__ mod_repr = repr(test_module) assert mod_repr == "" def test_dir(self): import sys items = sys.__dir__() assert sorted(items) == dir(sys) def test_package(self): import sys import os assert sys.__package__ == '' assert os.__package__ == '' assert type(sys)('foo').__package__ is None def test_name_nonascii(self): import sys m = type(sys)('日本') assert m.__name__ == '日本' assert repr(m).startswith("