1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
import sys
import threading
import types
import zlib
import unittest
try:
from unittest import mock
except ImportError:
import mock
import mitogen.core
import mitogen.utils
from mitogen.core import b
import testlib
import simple_pkg.imports_replaces_self
class ImporterMixin(testlib.RouterMixin):
modname = None
def setUp(self):
super(ImporterMixin, self).setUp()
self.context = mock.Mock()
self.importer = mitogen.core.Importer(self.router, self.context, '')
# TODO: this is a horrendous hack. Without it, we can't deliver a
# response to find_module() via _on_load_module() since find_module()
# is still holding the lock. The tests need a nicer abstraction for
# soemthing like "fake participant" that lets us have a mock master
# that respects the execution model expected by the code -- probably
# (grmph) including multiplexer thread and all.
self.importer._lock = threading.RLock()
def set_get_module_response(self, resp):
def on_context_send(msg):
self.context_send_msg = msg
self.importer._on_load_module(
mitogen.core.Message.pickled(resp)
)
self.context.send = on_context_send
def tearDown(self):
sys.modules.pop(self.modname, None)
super(ImporterMixin, self).tearDown()
class InvalidNameTest(ImporterMixin, testlib.TestCase):
modname = 'trailingdot.'
# 0:fullname 1:pkg_present 2:path 3:compressed 4:related
response = (modname, None, None, None, None)
@unittest.skipIf(sys.version_info < (3, 4), 'Requires ModuleSpec, Python 3.4+')
def test_find_spec_invalid(self):
self.set_get_module_response(self.response)
self.assertEqual(self.importer.find_spec(self.modname, path=None), None)
class MissingModuleTest(ImporterMixin, testlib.TestCase):
modname = 'missing'
# 0:fullname 1:pkg_present 2:path 3:compressed 4:related
response = (modname, None, None, None, None)
@unittest.skipIf(sys.version_info >= (3, 4), 'Superceded in Python 3.4+')
def test_load_module_missing(self):
self.set_get_module_response(self.response)
self.assertRaises(ImportError, self.importer.load_module, self.modname)
@unittest.skipIf(sys.version_info < (3, 4), 'Requires ModuleSpec, Python 3.4+')
def test_find_spec_missing(self):
"""
Importer should optimistically offer itself as a module loader
when there are no disqualifying criteria.
"""
import importlib.machinery
self.set_get_module_response(self.response)
spec = self.importer.find_spec(self.modname, path=None)
self.assertIsInstance(spec, importlib.machinery.ModuleSpec)
self.assertEqual(spec.name, self.modname)
self.assertEqual(spec.loader, self.importer)
@unittest.skipIf(sys.version_info < (3, 4), 'Requires ModuleSpec, Python 3.4+')
def test_create_module_missing(self):
import importlib.machinery
self.set_get_module_response(self.response)
spec = importlib.machinery.ModuleSpec(self.modname, self.importer)
self.assertRaises(ImportError, self.importer.create_module, spec)
@unittest.skipIf(sys.version_info >= (3, 4), 'Superceded in Python 3.4+')
class LoadModuleTest(ImporterMixin, testlib.TestCase):
data = zlib.compress(b("data = 1\n\n"))
path = 'fake_module.py'
modname = 'fake_module'
# 0:fullname 1:pkg_present 2:path 3:compressed 4:related
response = (modname, None, path, data, [])
def test_module_added_to_sys_modules(self):
self.set_get_module_response(self.response)
mod = self.importer.load_module(self.modname)
self.assertIs(sys.modules[self.modname], mod)
self.assertIsInstance(mod, types.ModuleType)
def test_module_file_set(self):
self.set_get_module_response(self.response)
mod = self.importer.load_module(self.modname)
self.assertEqual(mod.__file__, 'master:' + self.path)
def test_module_loader_set(self):
self.set_get_module_response(self.response)
mod = self.importer.load_module(self.modname)
self.assertIs(mod.__loader__, self.importer)
def test_module_package_unset(self):
self.set_get_module_response(self.response)
mod = self.importer.load_module(self.modname)
self.assertIsNone(mod.__package__)
@unittest.skipIf(sys.version_info < (3, 4), 'Requires ModuleSpec, Python 3.4+')
class ModuleSpecTest(ImporterMixin, testlib.TestCase):
data = zlib.compress(b("data = 1\n\n"))
path = 'fake_module.py'
modname = 'fake_module'
# 0:fullname 1:pkg_present 2:path 3:compressed 4:related
response = (modname, None, path, data, [])
def test_module_attributes(self):
import importlib.machinery
self.set_get_module_response(self.response)
spec = importlib.machinery.ModuleSpec(self.modname, self.importer)
mod = self.importer.create_module(spec)
self.assertIsInstance(mod, types.ModuleType)
self.assertEqual(mod.__name__, 'fake_module')
#self.assertFalse(hasattr(mod, '__file__'))
@unittest.skipIf(sys.version_info >= (3, 4), 'Superceded in Python 3.4+')
class LoadSubmoduleTest(ImporterMixin, testlib.TestCase):
data = zlib.compress(b("data = 1\n\n"))
path = 'fake_module.py'
modname = 'mypkg.fake_module'
# 0:fullname 1:pkg_present 2:path 3:compressed 4:related
response = (modname, None, path, data, [])
def test_module_package_unset(self):
self.set_get_module_response(self.response)
mod = self.importer.load_module(self.modname)
self.assertEqual(mod.__package__, 'mypkg')
@unittest.skipIf(sys.version_info < (3, 4), 'Requires ModuleSpec, Python 3.4+')
class SubmoduleSpecTest(ImporterMixin, testlib.TestCase):
data = zlib.compress(b("data = 1\n\n"))
path = 'fake_module.py'
modname = 'mypkg.fake_module'
# 0:fullname 1:pkg_present 2:path 3:compressed 4:related
response = (modname, None, path, data, [])
def test_module_attributes(self):
import importlib.machinery
self.set_get_module_response(self.response)
spec = importlib.machinery.ModuleSpec(self.modname, self.importer)
mod = self.importer.create_module(spec)
self.assertIsInstance(mod, types.ModuleType)
self.assertEqual(mod.__name__, 'mypkg.fake_module')
#self.assertFalse(hasattr(mod, '__file__'))
@unittest.skipIf(sys.version_info >= (3, 4), 'Superceded in Python 3.4+')
class LoadModulePackageTest(ImporterMixin, testlib.TestCase):
data = zlib.compress(b("func = lambda: 1\n\n"))
path = 'fake_pkg/__init__.py'
modname = 'fake_pkg'
# 0:fullname 1:pkg_present 2:path 3:compressed 4:related
response = (modname, [], path, data, [])
def test_module_file_set(self):
self.set_get_module_response(self.response)
mod = self.importer.load_module(self.modname)
self.assertEqual(mod.__file__, 'master:' + self.path)
def test_get_filename(self):
self.set_get_module_response(self.response)
mod = self.importer.load_module(self.modname)
filename = mod.__loader__.get_filename(self.modname)
self.assertEqual('master:fake_pkg/__init__.py', filename)
def test_get_source(self):
self.set_get_module_response(self.response)
mod = self.importer.load_module(self.modname)
source = mod.__loader__.get_source(self.modname)
self.assertEqual(source,
mitogen.core.to_text(zlib.decompress(self.data)))
def test_module_loader_set(self):
self.set_get_module_response(self.response)
mod = self.importer.load_module(self.modname)
self.assertIs(mod.__loader__, self.importer)
def test_module_path_present(self):
self.set_get_module_response(self.response)
mod = self.importer.load_module(self.modname)
self.assertEqual(mod.__path__, [])
def test_module_package_set(self):
self.set_get_module_response(self.response)
mod = self.importer.load_module(self.modname)
self.assertEqual(mod.__package__, self.modname)
def test_module_data(self):
self.set_get_module_response(self.response)
mod = self.importer.load_module(self.modname)
self.assertIsInstance(mod.func, types.FunctionType)
self.assertEqual(mod.func.__module__, self.modname)
@unittest.skipIf(sys.version_info < (3, 4), 'Requires ModuleSpec, Python 3.4+')
class PackageSpecTest(ImporterMixin, testlib.TestCase):
data = zlib.compress(b("func = lambda: 1\n\n"))
path = 'fake_pkg/__init__.py'
modname = 'fake_pkg'
# 0:fullname 1:pkg_present 2:path 3:compressed 4:related
response = (modname, [], path, data, [])
def test_module_attributes(self):
import importlib.machinery
self.set_get_module_response(self.response)
spec = importlib.machinery.ModuleSpec(self.modname, self.importer)
mod = self.importer.create_module(spec)
self.assertIsInstance(mod, types.ModuleType)
self.assertEqual(mod.__name__, 'fake_pkg')
#self.assertFalse(hasattr(mod, '__file__'))
def test_get_filename(self):
import importlib.machinery
self.set_get_module_response(self.response)
spec = importlib.machinery.ModuleSpec(self.modname, self.importer)
_ = self.importer.create_module(spec)
filename = self.importer.get_filename(self.modname)
self.assertEqual('master:fake_pkg/__init__.py', filename)
def test_get_source(self):
import importlib.machinery
self.set_get_module_response(self.response)
spec = importlib.machinery.ModuleSpec(self.modname, self.importer)
_ = self.importer.create_module(spec)
source = self.importer.get_source(self.modname)
self.assertEqual(source,
mitogen.core.to_text(zlib.decompress(self.data)))
class EmailParseAddrSysTest(testlib.RouterMixin, testlib.TestCase):
def initdir(self, caplog):
self.caplog = caplog
def test_sys_module_not_fetched(self):
# An old version of core.Importer would request the email.sys module
# while executing email.utils.parseaddr(). Ensure this needless
# roundtrip has not reappeared.
pass
class ImporterBlacklistTest(testlib.TestCase):
def test_is_blacklisted_import_default(self):
importer = mitogen.core.Importer(
router=mock.Mock(), context=None, core_src='',
)
self.assertIsInstance(importer.whitelist, list)
self.assertIsInstance(importer.blacklist, list)
self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'mypkg'))
self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'mypkg.mod'))
self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'otherpkg'))
self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'otherpkg.mod'))
self.assertTrue(mitogen.core.is_blacklisted_import(importer, '__builtin__'))
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'builtins'))
def test_is_blacklisted_import_just_whitelist(self):
importer = mitogen.core.Importer(
router=mock.Mock(), context=None, core_src='',
whitelist=('mypkg',),
)
self.assertIsInstance(importer.whitelist, list)
self.assertIsInstance(importer.blacklist, list)
self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'mypkg'))
self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'mypkg.mod'))
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'otherpkg'))
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'otherpkg.mod'))
self.assertTrue(mitogen.core.is_blacklisted_import(importer, '__builtin__'))
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'builtins'))
def test_is_blacklisted_import_just_blacklist(self):
importer = mitogen.core.Importer(
router=mock.Mock(), context=None, core_src='',
blacklist=('mypkg',),
)
self.assertIsInstance(importer.whitelist, list)
self.assertIsInstance(importer.blacklist, list)
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'mypkg'))
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'mypkg.mod'))
self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'otherpkg'))
self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'otherpkg.mod'))
self.assertTrue(mitogen.core.is_blacklisted_import(importer, '__builtin__'))
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'builtins'))
def test_is_blacklisted_import_whitelist_and_blacklist(self):
importer = mitogen.core.Importer(
router=mock.Mock(), context=None, core_src='',
whitelist=('mypkg',), blacklist=('mypkg',),
)
self.assertIsInstance(importer.whitelist, list)
self.assertIsInstance(importer.blacklist, list)
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'mypkg'))
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'mypkg.mod'))
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'otherpkg'))
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'otherpkg.mod'))
self.assertTrue(mitogen.core.is_blacklisted_import(importer, '__builtin__'))
self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'builtins'))
class Python24LineCacheTest(testlib.TestCase):
# TODO: mitogen.core.Importer._update_linecache()
pass
class SelfReplacingModuleTest(testlib.RouterMixin, testlib.TestCase):
# issue #590
def test_importer_handles_self_replacement(self):
c = self.router.local()
self.assertEqual(0,
c.call(simple_pkg.imports_replaces_self.subtract_one, 1))
|