Should you also insert None into sys.path_importer_cache to signify there is no finder for the path entry? I guess the real problem with that is there is no guarantee the path entry is hashable, so that probably won't work. So nevermind. =)<div class="gmail_extra">
<br><br><div class="gmail_quote">On Tue, Nov 20, 2012 at 3:35 PM, barry.warsaw <span dir="ltr"><<a href="mailto:python-checkins@python.org" target="_blank">python-checkins@python.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<a href="http://hg.python.org/cpython/rev/291406748217" target="_blank">http://hg.python.org/cpython/rev/291406748217</a><br>
changeset: 80529:291406748217<br>
branch: 3.3<br>
parent: 80527:4537dd27b2dc<br>
user: Barry Warsaw <<a href="mailto:barry@python.org">barry@python.org</a>><br>
date: Tue Nov 20 15:22:51 2012 -0500<br>
summary:<br>
- Issue #16514: Fix regression causing a traceback when sys.path[0] is None<br>
(actually, any non-string or non-bytes type).<br>
<br>
files:<br>
Doc/library/sys.rst | 4 +-<br>
Doc/reference/import.rst | 24 +-<br>
Lib/importlib/_bootstrap.py | 2 +<br>
Lib/test/test_importlib/import_/test_path.py | 25 +-<br>
Misc/NEWS | 3 +<br>
Python/importlib.h | 130 +++++----<br>
6 files changed, 111 insertions(+), 77 deletions(-)<br>
<br>
<br>
diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst<br>
--- a/Doc/library/sys.rst<br>
+++ b/Doc/library/sys.rst<br>
@@ -783,7 +783,9 @@<br>
current directory first. Notice that the script directory is inserted *before*<br>
the entries inserted as a result of :envvar:`PYTHONPATH`.<br>
<br>
- A program is free to modify this list for its own purposes.<br>
+ A program is free to modify this list for its own purposes. Only strings<br>
+ and bytes should be added to :data:`sys.path`; all other data types are<br>
+ ignored during import.<br>
<br>
<br>
.. seealso::<br>
diff --git a/Doc/reference/import.rst b/Doc/reference/import.rst<br>
--- a/Doc/reference/import.rst<br>
+++ b/Doc/reference/import.rst<br>
@@ -540,7 +540,10 @@<br>
implementation-specific defaults. Entries in :data:`sys.path` can name<br>
directories on the file system, zip files, and potentially other "locations"<br>
(see the :mod:`site` module) that should be searched for modules, such as<br>
-URLs, or database queries.<br>
+URLs, or database queries. Only strings and bytes should be present on<br>
+:data:`sys.path`; all other data types are ignored. The encoding of bytes<br>
+entries is determined by the individual :term:`path entry finders <path entry<br>
+finder>`.<br>
<br>
The :term:`path based finder` is a :term:`meta path finder`, so the import<br>
machinery begins the :term:`import path` search by calling the path<br>
@@ -563,14 +566,17 @@<br>
the path based finder to perform the path entry search again [#fnpic]_.<br>
<br>
If the path entry is not present in the cache, the path based finder iterates<br>
-over every callable in :data:`sys.path_hooks`. Each of the<br>
-:term:`path entry hooks <path entry hook>` in this list is called with a<br>
-single argument, the path entry to be searched. This callable may either<br>
-return a :term:`path entry finder` that can handle the path entry, or it may<br>
-raise :exc:`ImportError`.<br>
-An :exc:`ImportError` is used by the path based finder to signal that the hook<br>
-cannot find a :term:`path entry finder` for that :term:`path entry`. The<br>
-exception is ignored and :term:`import path` iteration continues.<br>
+over every callable in :data:`sys.path_hooks`. Each of the :term:`path entry<br>
+hooks <path entry hook>` in this list is called with a single argument, the<br>
+path entry to be searched. This callable may either return a :term:`path<br>
+entry finder` that can handle the path entry, or it may raise<br>
+:exc:`ImportError`. An :exc:`ImportError` is used by the path based finder to<br>
+signal that the hook cannot find a :term:`path entry finder` for that<br>
+:term:`path entry`. The exception is ignored and :term:`import path`<br>
+iteration continues. The hook should expect either a string or bytes object;<br>
+the encoding of bytes objects is up to the hook (e.g. it may be a file system<br>
+encoding, UTF-8, or something else), and if the hook cannot decode the<br>
+argument, it should raise :exc:`ImportError`.<br>
<br>
If :data:`sys.path_hooks` iteration ends with no :term:`path entry finder`<br>
being returned, then the path based finder's :meth:`find_module()` method<br>
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py<br>
--- a/Lib/importlib/_bootstrap.py<br>
+++ b/Lib/importlib/_bootstrap.py<br>
@@ -1281,6 +1281,8 @@<br>
# the list of paths that will become its __path__<br>
namespace_path = []<br>
for entry in path:<br>
+ if not isinstance(entry, (str, bytes)):<br>
+ continue<br>
finder = cls._path_importer_cache(entry)<br>
if finder is not None:<br>
if hasattr(finder, 'find_loader'):<br>
diff --git a/Lib/test/test_importlib/import_/test_path.py b/Lib/test/test_importlib/import_/test_path.py<br>
--- a/Lib/test/test_importlib/import_/test_path.py<br>
+++ b/Lib/test/test_importlib/import_/test_path.py<br>
@@ -1,15 +1,14 @@<br>
from importlib import _bootstrap<br>
from importlib import machinery<br>
+from importlib import import_module<br>
from .. import util<br>
from . import util as import_util<br>
-import imp<br>
import os<br>
import sys<br>
-import tempfile<br>
-from test import support<br>
-from types import MethodType<br>
+from types import ModuleType<br>
import unittest<br>
import warnings<br>
+import zipimport<br>
<br>
<br>
class FinderTests(unittest.TestCase):<br>
@@ -89,6 +88,24 @@<br>
self.assertIs(loader, importer)<br>
self.assertIn(os.curdir, sys.path_importer_cache)<br>
<br>
+ def test_None_on_sys_path(self):<br>
+ # Putting None in sys.path[0] caused an import regression from Python<br>
+ # 3.2: <a href="http://bugs.python.org/issue16514" target="_blank">http://bugs.python.org/issue16514</a><br>
+ new_path = sys.path[:]<br>
+ new_path.insert(0, None)<br>
+ new_path_importer_cache = sys.path_importer_cache.copy()<br>
+ new_path_importer_cache.pop(None, None)<br>
+ new_path_hooks = [zipimport.zipimporter,<br>
+ _bootstrap.FileFinder.path_hook(<br>
+ *_bootstrap._get_supported_file_loaders())]<br>
+ with util.uncache('email'):<br>
+ with util.import_state(meta_path=sys.meta_path[:],<br>
+ path=new_path,<br>
+ path_importer_cache=new_path_importer_cache,<br>
+ path_hooks=new_path_hooks):<br>
+ module = import_module('email')<br>
+ self.assertIsInstance(module, ModuleType)<br>
+<br>
<br>
def test_main():<br>
from test.support import run_unittest<br>
diff --git a/Misc/NEWS b/Misc/NEWS<br>
--- a/Misc/NEWS<br>
+++ b/Misc/NEWS<br>
@@ -12,6 +12,9 @@<br>
Core and Builtins<br>
-----------------<br>
<br>
+- Issue #16514: Fix regression causing a traceback when sys.path[0] is None<br>
+ (actually, any non-string or non-bytes type).<br>
+<br>
- Issue #16306: Fix multiple error messages when unknown command line<br>
parameters where passed to the interpreter. Patch by Hieu Nguyen.<br>
<br>
diff --git a/Python/importlib.h b/Python/importlib.h<br>
--- a/Python/importlib.h<br>
+++ b/Python/importlib.h<br>
[stripped]<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Repository URL: <a href="http://hg.python.org/cpython" target="_blank">http://hg.python.org/cpython</a><br>
</font></span><br>_______________________________________________<br>
Python-checkins mailing list<br>
<a href="mailto:Python-checkins@python.org">Python-checkins@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-checkins" target="_blank">http://mail.python.org/mailman/listinfo/python-checkins</a><br>
<br></blockquote></div><br></div>