How to use find_module method in Slash

Best Python code snippet using slash

sandbox_test.py

Source:sandbox_test.py Github

copy

Full Screen

...123 class DummyPathHook(object):124 def __init__(self, path):125 if path != 'dummy/path':126 raise ImportError127 def find_module(self, unused_fullname):128 return self129 def load_module(self, fullname):130 return imp.new_module('fake name: %s' % fullname)131 self.test_policies['distutils.util'] = sandbox.ModuleOverridePolicy(132 None, [], {}, default_pass_through=True)133 sys.path_hooks = [DummyPathHook]134 util = self.hook.load_module('distutils.util')135 self.assertEqual('fake name: distutils.util', util.__name__)136 def test_load_with_path_hook_cant_find(self):137 class DummyPathHook(object):138 def __init__(self, path):139 if path != 'dummy/path':140 raise ImportError141 def find_module(self, unused_fullname):142 return None143 def load_module(self, fullname):144 raise ImportError145 self.test_policies['distutils.util'] = sandbox.ModuleOverridePolicy(146 None, [], {}, default_pass_through=True)147 sys.path_hooks = [DummyPathHook]148 util = self.hook.load_module('distutils.util')149 self.assertEqual('distutils.util', util.__name__)150 def test_load_without_path_hook(self):151 self.test_policies['urllib'] = sandbox.ModuleOverridePolicy(152 None, [], {}, default_pass_through=True)153 urllib = self.hook.load_module('urllib')154 self.assertIn('urlopen', urllib.__dict__)155 self.assertEqual('urllib', urllib.__name__)156 def test_load_without_path_hook_not_found(self):157 self.test_policies['urllib'] = sandbox.ModuleOverridePolicy(158 None, [], {}, default_pass_through=True)159 self.assertRaises(ImportError, self.hook.load_module, 'fake_module')160 def test_load_already_in_sys_modules(self):161 module = imp.new_module('foo')162 sys.modules['foo'] = module163 self.assertEqual(module, self.hook.load_module('foo'))164 def test_is_package(self):165 self.assertTrue(self.hook.is_package('distutils'))166 def test_is_package_standard_lib(self):167 self.assertTrue(self.hook.is_package('email'))168 def test_is_package_not_package_standard_lib(self):169 self.assertFalse(self.hook.is_package('urllib'))170 def test_is_package_not_package(self):171 self.assertFalse(self.hook.is_package('distutils.util'))172 def test_is_package_does_not_exist(self):173 self.assertRaises(ImportError, self.hook.is_package, 'foo.bar')174 def test_get_source(self):175 with open(__import__('distutils').__file__.replace('.pyc', '.py')) as f:176 source = f.read()177 self.assertEqual(source, self.hook.get_source('distutils'))178 def test_get_source_does_not_exist(self):179 self.assertRaises(ImportError, self.hook.get_source, 'foo.bar')180 def test_get_source_standard_library(self):181 # The expected value is hard to find if the standard library might or might182 # not be zipped so just check that a value is found.183 self.assertTrue(self.hook.get_source('urllib'))184 def test_get_code(self):185 filename = __import__('distutils').__file__.replace('.pyc', '.py')186 with open(filename) as f:187 expected_code = compile(f.read(), filename, 'exec')188 self.assertEqual(expected_code, self.hook.get_code('distutils'))189 def test_get_code_does_not_exist(self):190 self.assertRaises(ImportError, self.hook.get_code, 'foo.bar')191 def test_get_code_standard_library(self):192 # The expected value is hard to find if the standard library might or might193 # not be zipped so just check that a value is found.194 self.assertTrue(self.hook.get_code('urllib'))195 def test_os_module_policy(self):196 hooked_os = imp.new_module('os')197 hooked_os.__dict__.update(os.__dict__)198 sandbox._MODULE_OVERRIDE_POLICIES['os'].apply_policy(hooked_os.__dict__)199 self.assertEqual(stubs.return_minus_one, hooked_os.getpid)200 self.assertNotIn('execv', hooked_os.__dict__)201 self.assertEqual(stubs.os_error_not_implemented, hooked_os.unlink)202 self.assertEqual(os.walk, hooked_os.walk)203class CModuleImportHookTest(unittest.TestCase):204 def test_find_module_enabled_module(self):205 hook = sandbox.CModuleImportHook([re.compile(r'lxml\.')])206 self.assertIsNone(hook.find_module('lxml'))207 lxml = __import__('lxml')208 self.assertIsNone(hook.find_module('lxml.etree', lxml.__path__))209 def test_find_module_disabled_module(self):210 hook = sandbox.CModuleImportHook([re.compile(r'numpy\.')])211 self.assertIsNone(hook.find_module('lxml'))212 lxml = __import__('lxml')213 self.assertEqual(hook, hook.find_module('lxml.etree', lxml.__path__))214 def test_find_module_not_c_module(self):215 hook = sandbox.CModuleImportHook([])216 self.assertIsNone(hook.find_module('httplib'))217 def test_find_module_whitelisted(self):218 hook = sandbox.CModuleImportHook([])219 for name in sandbox._WHITE_LIST_C_MODULES:220 self.assertIsNone(hook.find_module(name))221 def test_find_module_not_whitelisted(self):222 hook = sandbox.CModuleImportHook([])223 self.assertEqual(hook, hook.find_module('__builtin__'))224 def test_find_module_not_whitelisted_enabled_via_libaries(self):225 hook = sandbox.CModuleImportHook([re.compile(r'__builtin__')])226 self.assertIsNone(hook.find_module('__builtin__'))227 def test_load_module(self):228 hook = sandbox.CModuleImportHook([])229 self.assertRaises(ImportError, hook.load_module, 'lxml')230class PathOverrideImportHookTest(unittest.TestCase):231 def setUp(self):232 self.saved_lxml = lxml233 self.saved_pil = PIL234 self.saved_urllib = urllib235 def tearDown(self):236 sys.modules['urllib'] = self.saved_urllib237 sys.modules['PIL'] = self.saved_pil238 sys.modules['lxml'] = self.saved_lxml239 def test_package_success(self):240 hook = sandbox.PathOverrideImportHook(['lxml'])241 self.assertEqual(hook, hook.find_module('lxml'))242 del sys.modules['lxml']243 hooked_lxml = hook.load_module('lxml')244 self.assertEqual(hooked_lxml.__file__, lxml.__file__)245 self.assertEqual(hooked_lxml.__path__, lxml.__path__)246 self.assertEqual(hooked_lxml.__loader__, hook)247 self.assertEqual([os.path.dirname(self.saved_lxml.__file__)],248 hook.extra_accessible_paths)249 self.assertFalse(hook.extra_sys_paths)250 def test_package_success_pil_in_sys_path(self):251 hook = sandbox.PathOverrideImportHook(['PIL'])252 self.assertEqual(hook, hook.find_module('PIL'))253 del sys.modules['PIL']254 hooked_pil = hook.load_module('PIL')255 self.assertEqual(hooked_pil.__file__, PIL.__file__)256 self.assertEqual(hooked_pil.__path__, PIL.__path__)257 self.assertEqual(hooked_pil.__loader__, hook)258 self.assertFalse(hook.extra_accessible_paths)259 self.assertEqual([os.path.dirname(self.saved_pil.__file__)],260 hook.extra_sys_paths)261 def test_module_success(self):262 hook = sandbox.PathOverrideImportHook(['urllib'])263 self.assertEqual(hook, hook.find_module('urllib'))264 del sys.modules['urllib']265 hooked_urllib = hook.load_module('urllib')266 self.assertEqual(hooked_urllib.__file__.replace('.pyc', '.py'),267 urllib.__file__.replace('.pyc', '.py'))268 self.assertEqual(hooked_urllib.__loader__, hook)269 self.assertNotIn('__path__', hooked_urllib.__dict__)270 self.assertFalse(hook.extra_accessible_paths)271 self.assertFalse(hook.extra_sys_paths)272 def test_disabled_modules(self):273 hook = sandbox.PathOverrideImportHook(['lxml'])274 self.assertFalse(hook.find_module('lxml.foo'))275 self.assertFalse(hook.find_module('numpy'))276 self.assertFalse(hook.find_module('os'))277 def test_module_not_installed(self):278 hook = sandbox.PathOverrideImportHook(['foo'])279 self.assertFalse(hook.find_module('foo'))280 self.assertFalse(hook.extra_accessible_paths)281 self.assertFalse(hook.extra_sys_paths)282 def test_import_alread_in_sys_modules(self):283 hook = sandbox.PathOverrideImportHook(['lxml'])284 self.assertEqual(os, hook.load_module('os'))285class PathRestrictingImportHookTest(unittest.TestCase):286 def setUp(self):287 self.mox = mox.Mox()288 self.mox.StubOutWithMock(imp, 'find_module')289 self.mox.StubOutWithMock(stubs.FakeFile, 'is_file_accessible')290 self.hook = sandbox.PathRestrictingImportHook([re.compile(r'lxml(\..*)?$')])291 def tearDown(self):292 self.mox.UnsetStubs()293 def test_accessible(self):294 imp.find_module('bar', ['foo']).AndReturn((None, 'foo/bar.py',295 (None, None, imp.PY_SOURCE)))296 stubs.FakeFile.is_file_accessible('foo/bar.py').AndReturn(True)297 self.mox.ReplayAll()298 self.assertIsNone(self.hook.find_module('foo.bar', ['foo']))299 def test_not_accessible(self):300 imp.find_module('bar', ['foo']).AndReturn((None, 'foo/bar.py',301 (None, None, imp.PY_SOURCE)))302 stubs.FakeFile.is_file_accessible('foo/bar.py').AndReturn(False)303 self.mox.ReplayAll()304 self.assertEqual(self.hook, self.hook.find_module('foo.bar', ['foo']))305 def test_c_module_accessible(self):306 imp.find_module('bar', ['foo']).AndReturn((None, 'foo/bar.so',307 (None, None, imp.C_EXTENSION)))308 stubs.FakeFile.is_file_accessible('foo/bar.so').AndReturn(True)309 self.mox.ReplayAll()310 self.assertIsNone(self.hook.find_module('foo.bar', ['foo']))311 def test_c_module_not_accessible(self):312 imp.find_module('bar', ['foo']).AndReturn((None, 'foo/bar.so',313 (None, None, imp.C_EXTENSION)))314 stubs.FakeFile.is_file_accessible('foo/bar.so').AndReturn(False)315 self.mox.ReplayAll()316 self.assertEqual(self.hook, self.hook.find_module('foo.bar', ['foo']))317 def test_compiled_python_accessible(self):318 imp.find_module('bar', ['foo']).AndReturn((None, 'foo/bar.pyc',319 (None, None, imp.PY_COMPILED)))320 stubs.FakeFile.is_file_accessible('foo/bar.pyc').AndReturn(True)321 self.mox.ReplayAll()322 self.assertIsNone(self.hook.find_module('foo.bar', ['foo']))323 def test_compiled_python_not_accessible(self):324 imp.find_module('bar', ['foo']).AndReturn((None, 'foo/bar.pyc',325 (None, None, imp.PY_COMPILED)))326 stubs.FakeFile.is_file_accessible('foo/bar.pyc').AndReturn(False)327 self.mox.ReplayAll()328 self.assertEqual(self.hook, self.hook.find_module('foo.bar', ['foo']))329 def test_c_builtin(self):330 imp.find_module('bar', ['foo']).AndReturn((None, 'bar',331 (None, None, imp.C_BUILTIN)))332 self.mox.ReplayAll()333 self.assertIsNone(self.hook.find_module('foo.bar', ['foo']))334 def test_py_frozen(self):335 imp.find_module('bar', ['foo']).AndReturn((None, 'bar',336 (None, None, imp.PY_FROZEN)))337 self.mox.ReplayAll()338 self.assertIsNone(self.hook.find_module('foo.bar', ['foo']))339 def test_enabled_c_library(self):340 imp.find_module('lxmla', ['foo']).AndReturn((None, 'lxmla.py',341 (None, None, imp.PY_SOURCE)))342 stubs.FakeFile.is_file_accessible('lxmla.py').AndReturn(False)343 self.mox.ReplayAll()344 self.assertEqual(self.hook, self.hook.find_module('lxmla', ['foo']))345 self.assertIsNone(self.hook.find_module('lxml', None))346 self.assertIsNone(self.hook.find_module('lxml.html', None))347 def test_load_module(self):348 self.assertRaises(ImportError, self.hook.load_module, 'os')349class PyCryptoRandomImportHookTest(unittest.TestCase):350 def test_find_module(self):351 self.assertIsInstance(352 sandbox.PyCryptoRandomImportHook.find_module(353 'Crypto.Random.OSRNG.posix'),354 sandbox.PyCryptoRandomImportHook)355 self.assertIsNone(356 sandbox.PyCryptoRandomImportHook.find_module('Crypto.Random.OSRNG.nt'))357if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Slash automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful