From 621f2533f73264877f5c7ceec5a22ef9bdc6031d Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Mon, 26 Feb 2018 23:34:02 +0000 Subject: [PATCH 1/3] tests: Add mitogen.core.is_blacklisted_import() tests These test the current behviour, which may not be exactly the intended behaviour. Refs #98 --- tests/importer_test.py | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/importer_test.py b/tests/importer_test.py index 5fe65f45..9d533752 100644 --- a/tests/importer_test.py +++ b/tests/importer_test.py @@ -9,6 +9,8 @@ import pytest import unittest2 import mitogen.core +import mitogen.utils + import testlib @@ -25,6 +27,55 @@ class ImporterMixin(testlib.RouterMixin): super(ImporterMixin, self).tearDown() +class ImporterBlacklist(testlib.TestCase): + def test_is_blacklisted_import_default(self): + importer = mitogen.core.Importer( + router=mock.Mock(), context=None, core_src='', + ) + 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.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.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.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 LoadModuleTest(ImporterMixin, testlib.TestCase): data = zlib.compress("data = 1\n\n") path = 'fake_module.py' From 4178c4061712cadfd0fb8cbc7bd97a4df4f5a4ea Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Mon, 26 Feb 2018 23:35:26 +0000 Subject: [PATCH 2/3] core: Add docstring to is_blacklisted_import() This documents the existing behaviour, which may not be the intended. --- mitogen/core.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mitogen/core.py b/mitogen/core.py index d8ffbb13..0bd75b6b 100644 --- a/mitogen/core.py +++ b/mitogen/core.py @@ -168,6 +168,14 @@ def restart(func, *args): def is_blacklisted_import(importer, fullname): + """Return ``True`` if `fullname` is part of a blacklisted package, or if + any packages have been whitelisted and `fullname` is not part of one. + + NB: + - If a package is on both lists, then it is treated as blacklisted. + - If any package is whitelisted, then all non-whitelisted packages are + treated as blacklisted. + """ return ((not any(fullname.startswith(s) for s in importer.whitelist)) or (any(fullname.startswith(s) for s in importer.blacklist))) From ba3bc0f7f19cf0e43632a79c840b6e1545613676 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Mon, 26 Feb 2018 23:38:05 +0000 Subject: [PATCH 3/3] core: Standardise type of Importer.whitelist This seemed a reasonable streamlining, but I'm happy to be overruled. --- mitogen/core.py | 2 +- tests/importer_test.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/mitogen/core.py b/mitogen/core.py index 0bd75b6b..5c028445 100644 --- a/mitogen/core.py +++ b/mitogen/core.py @@ -420,7 +420,7 @@ class Importer(object): 'utils', ]} self._lock = threading.Lock() - self.whitelist = whitelist or [''] + self.whitelist = list(whitelist) or [''] self.blacklist = list(blacklist) + [ # 2.x generates needless imports for 'builtins', while 3.x does the # same for '__builtin__'. The correct one is built-in, the other diff --git a/tests/importer_test.py b/tests/importer_test.py index 9d533752..55cad182 100644 --- a/tests/importer_test.py +++ b/tests/importer_test.py @@ -32,6 +32,8 @@ class ImporterBlacklist(testlib.TestCase): 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')) @@ -44,6 +46,8 @@ class ImporterBlacklist(testlib.TestCase): 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')) @@ -56,6 +60,8 @@ class ImporterBlacklist(testlib.TestCase): 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')) @@ -68,6 +74,8 @@ class ImporterBlacklist(testlib.TestCase): 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'))