Skip to content

Commit

Permalink
Fixing issue with import order when using in Python 3
Browse files Browse the repository at this point in the history
In Python versions >= 3.1, other importers get added to the
sys.meta_path. Unfortunately, this causes installed packages
to override Pynsive loaded plugins. Causing things like
coverage information to be improperly reported on Pynsive loaded
modules in Python 3. This also resolves:
jmvrbanac/Specter#49
  • Loading branch information
jmvrbanac committed Mar 30, 2015
1 parent e641e52 commit 578a1e9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pynsive/plugin/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ class PluginManager(object):
"""
def __init__(self):
self.finder = ModuleFinder()
sys.meta_path.append(self.finder)

# Newer Python versions add other importers which can override our
# modules. Making sure we're first on newer Python version.
if sys.version_info >= (3, 1, 0):
sys.meta_path.insert(0, self.finder)
else:
sys.meta_path.append(self.finder)

def destroy(self):
"""
Expand Down
19 changes: 19 additions & 0 deletions tests/manager_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import sys
import unittest

import pynsive


class WhenCreatingThePluginManager(unittest.TestCase):
def setUp(self):
self.manager = pynsive.PluginManager()

def tearDown(self):
self.manager.destroy()

def test_correct_meta_path_insertion(self):
finder_index = sys.meta_path.index(self.manager.finder)
if sys.version_info >= (3, 1, 0):
self.assertEqual(0, finder_index)
else:
self.assertEqual(len(sys.meta_path) - 1, finder_index)

0 comments on commit 578a1e9

Please sign in to comment.