Commit Graph

279 Commits

Author SHA1 Message Date
Brett Cannon b428f47cf6 Don't overwrite a __path__ value from extension modules if already
set.
2012-08-11 19:43:29 -04:00
Philip Jenvey 688a551ca0 fix docstring wording 2012-08-10 16:21:35 -07:00
Brett Cannon f410ce8c09 Issue #15502: Refactor some code. 2012-08-10 17:41:23 -04:00
Philip Jenvey 731d48a65f update docstring per the extension package fix, refactor 2012-08-10 11:53:54 -07:00
Brett Cannon ac9f2f3de3 Issue #15576: Allow extension modules to be a package's __init__
module again. Also took the opportunity to stop accidentally exporting
_imp.extension_suffixes() as public.
2012-08-10 13:47:54 -04:00
Brett Cannon f4dc9204cc Issue #15502: Finish bringing importlib.abc in line with the current
state of the import system. Also make importlib.invalidate_caches()
work with sys.meta_path instead of sys.path_importer_cache to
completely separate the path-based import system from the overall
import system.

Patch by Eric Snow.
2012-08-10 12:21:12 -04:00
Brett Cannon cb4996afe4 Issue #15471: Don't use mutable object as default values for the
parameters of importlib.__import__().
2012-08-06 16:34:44 -04:00
Nick Coghlan 4941774f59 Issue #15502: Bring the importlib.PathFinder docs and docstring more in line with the new import system documentation, and fix various parts of the new docs that weren't quite right given PEP 420 or were otherwise a bit misleading. Also note the key terminology problem still being discussed in the issue 2012-08-02 23:03:58 +10:00
Nick Coghlan ff79486bb5 Close #15519: Properly expose WindowsRegistryFinder in importlib and bring the name into line with normal import terminology. Original patch by Eric Snow 2012-08-02 21:45:24 +10:00
Nick Coghlan 42c0766a53 Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise 2012-07-31 21:14:18 +10:00
Nick Coghlan 5ee9892406 Close #15425: Eliminate more importlib related traceback noise 2012-07-29 20:30:36 +10:00
Martin v. Löwis e3010a8d12 Issue #14578: Support modules registered in the Windows registry again.
Patch by Amaury Forgeot d'Arc.
2012-07-28 21:33:05 +02:00
Nick Coghlan 2824cb507d Issue #15343: A lot more than just unicode decoding can go wrong when retrieving a source file 2012-07-15 22:12:14 +10:00
Brett Cannon a6473f9cfd Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Lib/imp.py for imp.source_from_cache() instead of its own C version.

Also change PyImport_ExecCodeModuleObject() to not infer the source
path from the bytecode path like
PyImport_ExecCodeModuleWithPathnames() does. This makes the function
less magical.

This also has the side-effect of removing all uses of MAXPATHLEN in
Python/import.c which can cause failures on really long filenames.
2012-07-13 13:57:03 -04:00
Brett Cannon 461c813164 Issue #15111: When a module was imported using a 'from import'
statement (e.g. ``from distutils import msvc9compiler``) that triggers
an ImportError of its own (e.g. the non-existence of winreg), let that
exception propagate instead of raising a generic ImportError for the
module being requested (e.g. msvc9compiler).
2012-07-10 10:05:00 -04:00
Brett Cannon 77b2abd094 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Lib/imp.py.
2012-07-09 16:09:00 -04:00
Brett Cannon 19a2f5961c Issue #15056: imp.cache_from_source() and source_from_cache() raise
NotimplementedError when sys.implementation.cache_tag is None.

Thanks to Pranav Ravichandran for taking an initial stab at the patch.
2012-07-09 13:58:07 -04:00
Amaury Forgeot d'Arc 1ced17dfe9 Issue #15110: Copy same docstring as other '_exec_module' methods. 2012-07-08 21:03:01 +02:00
Amaury Forgeot d'Arc ae7b8f07c1 Issue #15110: Also hide importlib frames when importing a builtin module fails. 2012-07-08 20:52:38 +02:00
Antoine Pitrou bc07a5c913 Issue #15110: Fix the tracebacks generated by "import xxx" to not show the importlib stack frames. 2012-07-08 12:01:27 +02:00
Florent Xicluna 79d79a0f29 Minor refactoring in importlib._bootstrap, and fix the '_wrap' docstring. 2012-07-07 13:16:44 +02:00
Brett Cannon 98979b85e7 Issue #15166: Re-implement imp.get_tag() using sys.implementation.
Also eliminates some C code in Python/import.c as well.

Patch by Eric Snow with verification by comparing against another
patch from Jeff Knupp.
2012-07-02 15:13:11 -04:00
Eric V. Smith e51a36922f Fixes issue 15039: namespace packages are no longer imported in preference to modules of the same name. 2012-06-24 19:13:55 -04:00
Antoine Pitrou 310f95b04d A better repr() for FileFinder 2012-06-23 02:12:56 +02:00
Brett Cannon ea0b823940 Issue #14938: importlib.abc.SourceLoader.is_package() now takes the
module name into consideration when determining whether a module is a
package or not. This prevents importing a module's __init__ module
directly and having it considered a package, which can lead to
duplicate sub-modules.

Thanks to Ronan Lamy for reporting the bug.
2012-06-15 20:00:53 -04:00
Nick Coghlan 5c6eba3a93 Tweak importlib._bootstrap to avoid zero-argument super so I can work on issue #14857 without breaking imports 2012-05-27 17:49:58 +10:00
Brett Cannon d785cb3955 Remove some redundant decorators. 2012-05-26 14:28:21 -04:00
Eric V. Smith 984b11f88f issue 14660: Implement PEP 420, namespace packages. 2012-05-24 20:21:04 -04:00
Antoine Pitrou ea3eb88bca Issue #9260: A finer-grained import lock.
Most of the import sequence now uses per-module locks rather than the
global import lock, eliminating well-known issues with threads and imports.
2012-05-17 18:55:59 +02:00
Brett Cannon c049952de7 Issue #13959: Have
importlib.abc.FileLoader.load_module()/get_filename() and
importlib.machinery.ExtensionFileLoader.load_module() have their
single argument be optional as the loader's constructor has all the
ncessary information.

This allows for the deprecation of
imp.load_source()/load_compile()/load_package().
2012-05-11 14:48:41 -04:00
Brett Cannon cb66eb0dec Issue #13959: Deprecate imp.get_suffixes() for new attributes on
importlib.machinery that provide the suffix details for import.
The attributes were not put on imp so as to compartmentalize
everything importlib needs for setting up imports in
importlib.machinery.

This also led to an indirect deprecation of inspect.getmoduleinfo() as
it directly returned imp.get_suffix's returned tuple which no longer
makes sense.
2012-05-11 12:58:42 -04:00
Antoine Pitrou 6efa50a384 Issue #14583: Fix importlib bug when a package's __init__.py would first import one of its modules then raise an error. 2012-05-07 21:41:59 +02:00
Brett Cannon feccc09952 Clean up a docstring. 2012-05-04 16:47:54 -04:00
Brett Cannon f19c191067 Jython-friendly tweak. 2012-05-04 15:46:04 -04:00
Brett Cannon 2657df4744 Issue #13959: Re-implement imp.get_suffixes() in Lib/imp.py.
This introduces a new function, imp.extension_suffixes(), which is
currently undocumented. That is forthcoming once issue #14657 is
resolved and how to expose file suffixes is decided.
2012-05-04 15:20:40 -04:00
Brett Cannon 17098a5447 Properly mark names in importlib._bootstrap as private. 2012-05-04 13:52:49 -04:00
Brett Cannon efad00d520 Issue #14646: __import__() now sets __loader__ if need be.
importlib.util.module_for_loader also will set __loader__ along with
__package__. This is in conjunction to a forthcoming update to PEP 302
which will make these two attributes required for loaders to set.
2012-04-27 17:27:14 -04:00
Brett Cannon aa93642a35 Issue #14605: Use None in sys.path_importer_cache to represent no
finder instead of using some (now non-existent) implicit finder.
2012-04-27 15:30:58 -04:00
Brett Cannon ce418b448f Issue #14605: Stop having implicit entries for sys.meta_path.
ImportWarning is raised if sys.meta_path is found to be empty.
2012-04-27 14:01:58 -04:00
Brett Cannon e0d88a173c Issue #14605: Make explicit the entries on sys.path_hooks that used to
be implicit.

Added a warning for when sys.path_hooks is found to be empty. Also
changed the meaning of None in sys.path_importer_cache to represent
trying sys.path_hooks again (an interpretation of previous semantics).
Also added a warning for when None was found.

The long-term goal is for None in sys.path_importer_cache to represent
the same as imp.NullImporter: no finder found for that sys.path entry.
2012-04-25 20:54:04 -04:00
Marc-Andre Lemburg 7541c8ea37 Issue #14605 and #14642:
Issue a warning in case Python\importlib.h needs to be rebuilt,
but there's no Python interpreter around to freeze the bootstrap
script.
2012-04-25 10:54:48 +02:00
Marc-Andre Lemburg 4fe29c9657 Issue #14605: Rename _SourcelessFileLoader to SourcelessFileLoader.
This time also recreating the Python/importlib.h file to make
make happy. See the ticket for details.
2012-04-25 02:31:37 +02:00
Marc-Andre Lemburg ac8805a01a Issue #14605: Revert renaming of _SourcelessFileLoader, since it caused
the buildbots to fail.
2012-04-25 02:11:07 +02:00
Marc-Andre Lemburg 2945e78b05 Issue #14605: Rename _SourcelessFileLoader to SourcelessFileLoader 2012-04-25 01:36:48 +02:00
Brett Cannon 938d44d59c Issue #14605: Expose importlib.abc.FileLoader and
importlib.machinery.(FileFinder, SourceFileLoader,
_SourcelessFileLoader, ExtensionFileLoader).

This exposes all of importlib's mechanisms that will become public on
the sys module.
2012-04-22 19:58:33 -04:00
Brett Cannon 5c903e6ee1 Issue #13959: Continue to try to accomodate altsep in importlib by not
ignoring altsep if it already exists on a path when doing a join.
2012-04-22 11:45:07 -04:00
Brett Cannon cf649958f7 Revert to os.path.join() semantics for path manipulation in importlib
which is different than what imp.cache_from_source() operates on.
2012-04-22 02:06:23 -04:00
Brett Cannon a846236855 Continue the good fight to get Windows to like importlib by fixing a
variable name.
2012-04-21 21:46:32 -04:00
Brett Cannon 0d05a7698b Have importlib look for pre-existing path separators when joining
paths.
2012-04-21 21:21:27 -04:00
Brett Cannon 2f92389d5c Don't worry about moving imp.get_tag() over to Lib/imp.py. 2012-04-21 18:55:51 -04:00
Brett Cannon a64faf0771 Issue #13959: Re-implement imp.source_from_cache() in Lib/imp.py. 2012-04-21 18:52:52 -04:00
Brett Cannon ea59dbff16 Issue #13959: Re-implement imp.cache_from_source() in Lib/imp.py. 2012-04-20 21:44:46 -04:00
Brett Cannon ed672d6872 Make path manipulation more robust for platforms with alternative path
separators.
2012-04-20 21:19:53 -04:00
Brett Cannon 24117a748b Issue #13959: Keep imp.get_magic() in C code, but cache in importlib
for performance. While get_magic() could move to Lib/imp.py, having to
support PyImport_GetMagicNumber() would lead to equal, if not more, C
code than sticking with the status quo.
2012-04-20 18:04:03 -04:00
Brett Cannon 8ff6baf25b Issue #14581: Windows users are allowed to import modules w/o taking
the file suffix's case into account, even when doing a case-sensitive
import.
2012-04-20 12:53:14 -04:00
Benjamin Peterson 2a481e58ff don't bother keeping a set we'll never use 2012-04-18 15:25:50 -04:00
Benjamin Peterson d76bc7abac rollback 005fd1fe31ab (see #14609 and #14582)
Being able to overload a sys.module entry during import of a module was broken
by this changeset.
2012-04-18 10:55:43 -04:00
Brett Cannon 7bd329d800 Issue #12599: Be more strict in accepting None vs. a false-like object
in importlib.

Thanks to PJE for pointing out the issue and Nick Coghlan for filing
the bug.
2012-04-17 21:41:35 -04:00
Brett Cannon 6f44d66bc4 Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
rewriting functionality in pure Python.

To start, imp.new_module() has been rewritten in pure Python, put into
importlib (privately) and then publicly exposed in imp.
2012-04-15 16:08:47 -04:00
Brett Cannon 7788838473 merge 2012-04-15 15:25:10 -04:00
Brett Cannon 881535b726 Issue #14582: Import returns the module returned by a loader instead
of sys.modules when possible.

This is being done for two reasons. One is to gain a little bit of
performance by skipping an unnecessary dict lookup in sys.modules. But
the other (and main) reason is to be a little bit more clear in how
things should work from the perspective of import's interactions with
loaders. Otherwise loaders can easily forget to return the module even
though PEP 302 explicitly states they are expected to return the module
they loaded.
2012-04-15 15:24:04 -04:00
Philip Jenvey f8f3190d32 utilize startswith(tuple) 2012-04-15 12:21:32 -07:00
Brett Cannon 49f8d8b016 Handle importing pkg.mod by executing
__import__('mod', {'__packaging__': 'pkg', level=1) w/o properly (and
thus not segfaulting).
2012-04-14 21:50:00 -04:00
Brett Cannon 44590e4786 Add some comments. 2012-04-14 18:37:07 -04:00
Brett Cannon fd0741555b Issue #2377: Make importlib the implementation of __import__().
importlib._bootstrap is now frozen into Python/importlib.h and stored
as _frozen_importlib in sys.modules. Py_Initialize() loads the frozen
code along with sys and imp and then uses _frozen_importlib._install()
to set builtins.__import__() w/ _frozen_importlib.__import__().
2012-04-14 14:10:13 -04:00
Brett Cannon bbb6680ee5 Have importlib take advantage of ImportError's new 'name' and 'path'
attributes.
2012-04-12 21:09:01 -04:00
Brett Cannon 927d87470a If a module injects something into sys.modules as a side-effect of
importation, then respect that injection.

Discovered thanks to Lib/xml/parsers/expat.py injecting
xml.parsers.expat.errors and etree now importing that directly as a
module.
2012-04-02 20:33:56 -04:00
Brett Cannon 0d4d410b2d Remove a dead docstring. 2012-03-02 12:32:14 -05:00
Brett Cannon b46a1793a7 Update importlib.invalidate_caches() to be more general. 2012-02-27 18:15:42 -05:00
Philip Jenvey 4b42ff609d unused imports, pep8 2012-02-24 21:48:17 -08:00
Brett Cannon 625cd23da4 Simplify importib._resolve_name(). 2012-02-24 11:20:54 -05:00
Brett Cannon 4b03b68635 Turn _return_module() into _handle_fromlist(). 2012-02-23 20:47:57 -05:00
Brett Cannon f500778f65 Improper type for __package__ should raise TypeError, not ValueError. 2012-02-23 18:29:12 -05:00
Brett Cannon 068915cc8b Do a type check instead of an interface check. 2012-02-23 18:18:48 -05:00
Brett Cannon 34d8e41a47 Refactor importlib to make it easier to re-implement in C. 2012-02-22 18:33:05 -05:00
Charles-François Natali 6db1c40b37 Issue #14077: importlib: Fix regression introduced by de6703671386. 2012-02-22 21:03:09 +01:00
Antoine Pitrou b5c793a0b3 Issue #14063: fix test_importlib failure under OS X case-insensitive filesystems
(regression)
2012-02-20 22:06:59 +01:00
Benjamin Peterson 6ddac006be put docstrings on functions 2012-02-20 15:06:35 -05:00
Antoine Pitrou b67075beb5 _relax_case -> _make_relax_case 2012-02-20 13:52:47 +01:00
Antoine Pitrou c541f8ef40 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
importlib is now often faster than imp.find_module() at finding modules.
2012-02-20 01:48:16 +01:00
Brett Cannon 336b2f45e5 Fix a failing importlib test under Windows.
Closes issue #14054.
2012-02-19 19:36:44 -05:00
Brett Cannon f2e86751cc Optimize importlib's case-sensitivity check by wasting as little time as possible under case-sensitive OSs. 2012-02-17 09:46:48 -05:00
Brett Cannon ba17fe256e Have importlib use os.replace() for atomic renaming.
Closes issue #13961. Thanks to Charles-François Natali for the patch.
2012-02-17 09:26:53 -05:00
Brett Cannon f58d45c649 Tweak the handling of the empty string in sys.path for importlib.
It seems better to cache the finder for the cwd under its full path
insetad of '' in case the cwd changes. Otherwise FileFinder needs to
dynamically change itself based on whether it is given '' instead of
caching a finder for every change to the cwd.
2012-02-16 18:12:00 -05:00
Brett Cannon 3b1a06c1ea importlib.__import__() now raises ValueError when level < 0.
This is to bring it more in line with what PEP 328 set out to do with
removing ambiguous absolute/relative import semantics.
2012-02-16 17:47:48 -05:00
Brett Cannon 7fab676e87 Refactor importlib.__import__() and _gcd_import() to facilitate using
an __import__ implementation that takes care of basics in C and punts
to importlib for more complicated code.
2012-02-16 13:43:41 -05:00
Brett Cannon 0568d6fd4e Bring importlib in line w/ changes made in my personal bootstrap branch in the sandbox. 2012-02-14 18:38:11 -05:00
Philip Jenvey 353c10772a simplify 2012-02-10 11:45:03 -08:00
Brett Cannon 4a2e1a0da7 Undo a bad mq management thingy. 2012-02-08 19:11:53 -05:00
Brett Cannon 97771096ab Whitespace normalization. 2012-02-08 18:55:37 -05:00
Brett Cannon b4e63b3177 Use the cwd when the empty string is found in sys.path. This leads to
__file__ being an absolute path when the module is found in the
current directory.
2012-02-08 18:52:56 -05:00
Brett Cannon 354c26ecd6 Move setup code from importlib.__init__ to
importlib._bootstrap._setup().
2012-02-08 18:50:22 -05:00
Brett Cannon 8490fab4ad Don't fail in the face of a lacking attribute when wrapping a
function.
2012-02-08 18:44:14 -05:00
Brett Cannon 51d14f8e56 Relocate importlib._case_ok to importlib._bootstrap.
This required updating the code to use posix instead of os. This is
all being done to make bootstrapping easier to removing dependencies
that are kept in importlib.__init__ and thus outside of the single
file to bootstrap from.
2012-01-26 19:03:52 -05:00
Brett Cannon c264e3ee20 Move some code from importlib.__init__ to importlib._bootstrap that
does not need to be exposed from C code for bootstrapping reasons.
2012-01-25 18:58:03 -05:00
Brett Cannon f522aea7a1 Issue #13588: Rename decorators in importlib.
More descriptive names are now used in order to make tracebacks
more readable.
2012-01-16 11:46:22 -05:00
Antoine Pitrou 5136ac0ca2 Issue #13645: pyc files now contain the size of the corresponding source
code, to avoid timestamp collisions (especially on filesystems with a low
timestamp resolution) when checking for freshness of the bytecode.
2012-01-13 18:52:16 +01:00
Meador Inge 96ff0840b6 Issue #13593: updating the importlib utility decorators for __qualname__. 2011-12-14 22:53:13 -06:00
Meador Inge d7afeeeb8d Issue #13591: import_module potentially imports a module twice. 2011-12-14 22:27:28 -06:00
Meador Inge 416f12ddb3 Issue #13591: import_module potentially imports a module twice. 2011-12-14 22:23:46 -06:00
Antoine Pitrou 28e401e717 Issue #13392: Writing a pyc file should now be atomic under Windows as well. 2011-11-15 19:15:19 +01:00
Charles-François Natali 0c929d9d39 Issue #13303: Fix bytecode file default permission. 2011-11-10 19:12:29 +01:00
Charles-François Natali e695eec24a Issue #13303: Fix a race condition in the bytecode file creation. 2011-10-31 20:47:31 +01:00
Florent Xicluna 68f71a34f4 Simplify and remove few dependencies on 'errno', thanks to PEP 3151. 2011-10-28 16:06:23 +02:00
Antoine Pitrou daaaec9ee7 Silence the FileExistsError which can be raised because of the O_EXCL flag
(as in import.c)
2011-10-19 23:28:40 +02:00
Antoine Pitrou 707033a694 Issue #13146: Writing a pyc file is now atomic under POSIX. 2011-10-17 19:28:44 +02:00
Brett Cannon 442c9b92d8 Make importlib compatible with __import__ by "fixing" code.co_filename
paths.

__import__ does a little trick when importing from bytecode by
back-patching the co_filename paths to point to the file location
where the code object was loaded from, *not* where the code object was
originally created. This allows co_filename to point to a valid path.
Problem is that co_filename is immutable from Python, so a private
function -- imp._fix_co_filename() -- had to be introduced in order to
get things working properly. Originally the plan was to add a file
argument to marshal.loads(), but that failed as the algorithm used by
__import__ is not fully recursive as one might expect, so to be fully
backwards-compatible the code used by __import__ needed to be exposed.

This closes issue #6811 by taking a different approach than outlined
in the issue.
2011-03-23 16:14:42 -07:00
Brett Cannon a7468bc5c6 Have importlib use the repr of a module name in error messages.
This makes it obvious that an import failed because of some extraneous
whitespace (e.g., a newline).

This is a partial fix for issue #8754.
2011-03-23 16:06:00 -07:00
Ezio Melotti 4969f709cc #11515: Merge with 3.1. 2011-03-15 05:59:46 +02:00
Ezio Melotti 42da663e6f #11515: fix several typos. Patch by Piotr Kasprzyk. 2011-03-15 05:18:48 +02:00
Ezio Melotti 373089239b #11515: Merge with 3.2. 2011-03-15 06:03:08 +02:00
Eric V. Smith 91f0359ee4 Skip test if zlib not present. Closes #11498. Patch by Natalia B. Bidart. 2011-03-14 11:57:16 -04:00
Eric V. Smith 4169826a00 Typos. 2011-03-14 10:56:33 -04:00
Raymond Hettinger d958ea70bc Issue 10899: Remove function type annotations from the stdlib 2011-01-13 19:08:04 +00:00
Raymond Hettinger cd92f37582 Issue 10899: Remove function type annotations from the stdlib 2011-01-13 02:31:25 +00:00
Brett Cannon 0ffe6a9760 Fix a minor inconsistency in capitalization for the 'No module named' exception
message in importlib.

Thanks to Éric Araujo for spotting the inconsistency.
2010-11-18 03:03:04 +00:00
Florent Xicluna 764d612f5e Remove redundant context manager. 2010-09-03 19:55:26 +00:00
Brett Cannon a7ceeb335f OSError is the exception raised when one tries to create a directory that
already exists, not IOError.

Part of the continuing saga of issue #9572.
2010-08-26 21:07:13 +00:00
Brett Cannon 1d6569cfb9 Fix a bug where an attribute was lacking an object to work off of.
Related to the fix for issue #9572. Thanks to Łukasz Czuja for catching the
bug.
2010-08-24 21:04:05 +00:00
Brett Cannon ee6d64773b One of the joys of having test_multiprocessing occasionally execute after
test_importlib is that it discovers special little race conditions. For
instance, it turns out that importlib would throw an exception if two different
Python processes both tried to create the __pycache__ directory as one process
would succeed, causing the other process to fail as it didn't expect to get any
"help". So now importlib simply stays calm and just accepts someone else did
the work of creating the __pycache__ directory for it, moving on with life.

Closes issue #9572.
2010-08-22 22:19:11 +00:00
Brett Cannon 418182e18b Make importlib.abc.SourceLoader the primary mechanism for importlib.
This required moving the class from importlib/abc.py into
importlib/_bootstrap.py and jiggering some code to work better with the class.
This included changing how the file finder worked to better meet import
semantics. This also led to fixing importlib to handle the empty string from
sys.path as import currently does (and making me wish we didn't support that
instead just required people to insert '.' instead to represent cwd).

It also required making the new set_data abstractmethod create
any needed subdirectories implicitly thanks to __pycache__ (it was either this
or grow the SourceLoader ABC to gain an 'exists' method and either a mkdir
method or have set_data with no data arg mean to create a directory).

Lastly, as an optimization the file loaders cache the file path where the
finder found something to use for loading (this is thanks to having a
sourceless loader separate from the source loader to simplify the code and
cut out stat calls).
Unfortunately test_runpy assumed a loader would always work for a module, even
if you changed from underneath it what it was expected to work with. By simply
dropping the previous loader in test_runpy so the proper loader can be returned
by the finder fixed the failure.

At this point importlib deviates from import on two points:

1. The exception raised when trying to import a file is different (import does
an explicit file check to print a special message, importlib just says the path
cannot be imported as if it was just some module name).

2. the co_filename on a code object is not being set to where bytecode was
actually loaded from instead of where the marshalled code object originally
came from (a solution for this has already been agreed upon on python-dev but has
not been implemented yet; issue8611).
2010-07-03 22:32:41 +00:00
Brett Cannon 61b14251d3 Make importlib.abc.SourceLoader the primary mechanism for importlib.
This required moving the class from importlib/abc.py into
importlib/_bootstrap.py and jiggering some code to work better with the class.
This included changing how the file finder worked to better meet import
semantics. This also led to fixing importlib to handle the empty string from
sys.path as import currently does (and making me wish we didn't support that
instead just required people to insert '.' instead to represent cwd).

It also required making the new set_data abstractmethod create
any needed subdirectories implicitly thanks to __pycache__ (it was either this
or grow the SourceLoader ABC to gain an 'exists' method and either a mkdir
method or have set_data with no data arg mean to create a directory).

Lastly, as an optimization the file loaders cache the file path where the
finder found something to use for loading (this is thanks to having a
sourceless loader separate from the source loader to simplify the code and
cut out stat calls).
Unfortunately test_runpy assumed a loader would always work for a module, even
if you changed from underneath it what it was expected to work with. By simply
dropping the previous loader in test_runpy so the proper loader can be returned
by the finder fixed the failure.

At this point importlib deviates from import on two points:

1. The exception raised when trying to import a file is different (import does
an explicit file check to print a special message, importlib just says the path
cannot be imported as if it was just some module name).

2. the co_filename on a code object is not being set to where bytecode was
actually loaded from instead of where the marshalled code object originally
came from (a solution for this has already been agreed upon on python-dev but has
not been implemented yet; issue8611).
2010-07-03 21:48:25 +00:00
Brett Cannon 0cf9e6a621 Move importlib.abc.SourceLoader to _bootstrap.
Required updating code relying on other modules to switch to _bootstrap's
unique module requirements. This led to the realization that
get_code was being too liberal in its exception catching when calling set_data
by blindly grabbing IOError. Shifted the responsibility of safely ignoring
writes to a read-only path to set_data.

Importlib is still not relying on SourceLoader yet; requires creating a
SourcelessLoader and updating the source finder.
2010-06-28 04:57:24 +00:00
Barry Warsaw 04b5684d00 Repair test failure. Bug 8727. 2010-05-18 14:15:20 +00:00
Barry Warsaw 28a691b7fd PEP 3147 2010-04-17 00:19:56 +00:00
Brett Cannon 39440b14cd Merged revisions 78242 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r78242 | brett.cannon | 2010-02-19 11:01:06 -0500 (Fri, 19 Feb 2010) | 5 lines

  Importlib was not matching import's handling of .pyc files where it had less
  then 8 bytes total in the file.

  Fixes issues 7361 & 7875.
........
2010-02-19 16:05:28 +00:00
Brett Cannon 9b3e15fbc4 Importlib was not matching import's handling of .pyc files where it had less
then 8 bytes total in the file.

Fixes issues 7361 & 7875.
2010-02-19 16:01:06 +00:00
Benjamin Peterson b4b929ec86 no need to translate newlines in python code anymore 2009-11-13 00:45:32 +00:00
Brett Cannon 3d26b95ca7 Merged revisions 76146 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r76146 | brett.cannon | 2009-11-07 15:55:05 -0800 (Sat, 07 Nov 2009) | 6 lines

  When trying to write new bytecode, importlib was not catching the IOError
  thrown if the file happened to be read-only to keep the failure silent.

  Fixes issue #7187. Thanks, Dave Malcolm for the report and analysis of the
  problem.
........
2009-11-07 23:57:20 +00:00
Brett Cannon e52c919d67 When trying to write new bytecode, importlib was not catching the IOError
thrown if the file happened to be read-only to keep the failure silent.

Fixes issue #7187. Thanks, Dave Malcolm for the report and analysis of the
problem.
2009-11-07 23:55:05 +00:00
Brett Cannon 1c1dcbfd5d Trying to import a submodule from another module and not a package was raising
AttributeError in importlib when it should be an ImportError.

Found when running importlib against test_runpy.
2009-08-30 20:22:21 +00:00
Brett Cannon de4ebfe559 When the globals argument to importlib.__import__() contained any value for
__package__, it was used. This was incorrect since it could be set to None to
represent the fact that a proper value was unknown. Now None will trigger the
calculation for __package__.

Discovered when running importlib against test_importhooks.
2009-08-30 19:53:48 +00:00
Brett Cannon 6afbaef2fd Raise TypeError if the name given to importlib.__import__() lacks an rpartition
attribute. Was throwing AttributeError before. Discovered when running
test_builtin against importlib.

This exception change is specific to importlib.__import__() and does not apply to
import_module() as it is being done for compatibility reasons only.
2009-08-30 19:08:58 +00:00
Brett Cannon 9e0e1a63c8 Allow importlib.__import__ to accept any iterable for fromlist. Discovered when
running importlib against test___all__.
2009-08-30 18:28:46 +00:00
Brett Cannon 8a1a59f0ec Merged revisions 74584 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r74584 | brett.cannon | 2009-08-29 20:47:36 -0700 (Sat, 29 Aug 2009) | 3 lines

  Have importlib raise ImportError if None is found in sys.modules. This matches
  current import semantics.
........
2009-08-30 04:29:47 +00:00
Brett Cannon 4d75fc1ce9 Have importlib raise ImportError if None is found in sys.modules. This matches
current import semantics.
2009-08-30 03:47:36 +00:00
Brett Cannon 6919427e94 Implement the PEP 302 protocol for get_filename() as
importlib.abc.ExecutionLoader. PyLoader now inherits from this ABC instead of
InspectLoader directly. Both PyLoader and PyPycLoader provide concrete
implementations of get_filename in terms of source_path and bytecode_path.
2009-07-20 04:23:48 +00:00
Brett Cannon 12e533d7bf Rip out a useless method that the superclass implements properly. 2009-04-01 03:35:20 +00:00
Brett Cannon 8593a75688 Fix importlib.machinery.PathFinder.find_module() to essentially skip over None
entries in sys.path_importer_cache. While this differs from semantics in how
__import__ works, it prevents any implicit semantics from taking hold with
users.
2009-03-30 19:57:15 +00:00
Brett Cannon 8d11013169 Implement InspectLoader for FrozenImporter. 2009-03-15 02:20:16 +00:00
Brett Cannon a113ac58be Implement InspectLoader for BuiltinImporter. 2009-03-15 01:41:33 +00:00
Brett Cannon 7aa21f75c1 A few more docstring/API cleanups for importlib. 2009-03-15 00:53:05 +00:00
Brett Cannon 0e0d8a63b1 Clean up docstring from importlib.util.module_for_loader. 2009-03-15 00:00:19 +00:00
Brett Cannon f87e04d339 Finish properly hiding importlib implementation code. 2009-03-12 22:47:53 +00:00
Brett Cannon e9103d2619 Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now. 2009-03-12 22:37:06 +00:00
Brett Cannon ce43ddfee5 Do a little bit of reorganization on importlib._bootstrap. 2009-03-12 22:28:55 +00:00
Brett Cannon 3eeaa0a821 Make utility code in importlib._bootstrap private. 2009-03-12 22:07:17 +00:00
Brett Cannon 2cf03a8204 Implement importlib.util.set_loader: a decorator to automatically set
__loader__ on modules.
2009-03-10 05:17:37 +00:00
Brett Cannon d43b30b046 Implement get_source for importlib.abc.PyLoader using source_path and get_data. 2009-03-10 03:29:23 +00:00
Brett Cannon 2a922ed6ad Introduce importlib.abc. The module contains various ABCs related to imports
(mostly stuff specified by PEP 302). There are two ABCs, PyLoader and
PyPycLoader, which help with implementing source and source/bytecode loaders by
implementing load_module in terms of other methods. This removes a lot of
gritty details loaders typically have to worry about.
2009-03-09 03:35:50 +00:00