Commit Graph

438 Commits

Author SHA1 Message Date
Brett Cannon 2b8fc303ad Abstract out stat calls in importlib for easier experimentation. 2013-11-01 14:04:24 -04:00
Brett Cannon f6901c8baa Issue #19410: Put back in special-casing of '' for
importlib.machinery.FileFinder.

While originally moved to stop special-casing '' as PathFinder farther
up the typical call chain now uses the cwd in the instance of '', it
was deemed an unnecessary risk to breaking subclasses of FileFinder to
take the special-casing out.
2013-11-01 10:37:57 -04:00
Eric Snow cdf601281f Issue #19413: Restore pre-3.3 reload() semantics of re-finding modules. 2013-10-31 22:22:15 -06:00
Benjamin Peterson 6b4f7803f8 cleanup the construction of __qualname__ (closes #19301 again) 2013-10-20 17:50:28 -04:00
Benjamin Peterson 3d9e481ece give explicitly global functions and classes a global __qualname__ (closes #19301) 2013-10-19 16:01:13 -04:00
Brett Cannon 58f5680462 Issue #18810: Be optimistic with stat calls when seeing if a directory
exists when checking for a package.

Before there was an isdir check and then various isfile checks for
possible __init__ files when looking for a package.
This change drops the isdir check by leaning
on the assumption that a directory will not contain something named
after the module being imported which is not a directory. If the module
is a package then it saves a stat call. If there is nothing in the
directory with the potential package name it also saves a stat call.
Only if there is something in the directory named the same thing as
the potential package will the number of stat calls increase
(due to more wasteful __init__ checks).

Semantically there is no change as the isdir check moved
down so that namespace packages continue to have no chance of
accidentally collecting non-existent directories.
2013-10-18 13:24:13 -04:00
Brett Cannon bb9e481efa Issue #18416: Fix various os calls in importlib.machinery.FileFinder
now that self.path is no longer forced to '.'.
2013-10-18 12:01:06 -04:00
Brett Cannon 27e27f7ee1 Issue #18416: Have importlib.machinery.PathFinder treat '' as the cwd
and stop importlib.machinery.FileFinder treating '' as '.'.

Previous PathFinder transformed '' into '.' which led to __file__ for
modules imported from the cwd to always be relative paths. This meant
the values of the attribute were wrong as soon as the cwd changed.
This change now means that as long as the site module is run (which
makes all entries in sys.path absolute) then all values for __file__
will also be absolute unless it's for __main__ when specified by file
path in a relative way (modules imported by runpy will have an
absolute path).

Now that PathFinder is no longer treating '' as '.' it only makes
sense for FileFinder to stop doing so as well. Now no transformation
is performed for the directory given to the __init__ method.

Thanks to Madison May for the initial patch.
2013-10-18 11:39:04 -04:00
Eric Snow 7e70fa5314 [issue 19152] Ensure we have actually registered ExtensionFileLoader as an ExecutionLoader. 2013-10-04 20:28:52 -06:00
Eric Snow 5179445520 [issue19152] Add ExtensionFileLoader.get_filename(). 2013-10-03 12:08:55 -06:00
Eric Snow e8bbfebee0 [issue19951] Fix docstring and use of _get_suppported_file_loaders() to reflect 2-tuples. 2013-10-03 12:08:55 -06:00
Meador Inge 3c56145638 Issue #16826: Revert fix while Windows issues are being worked out. 2013-09-03 19:54:40 -05:00
Meador Inge c9e1dcdd53 Issue #16826: Revert fix while Windows issues are being worked out. 2013-09-03 19:43:49 -05:00
Meador Inge 9ab358ad7c Issue #16826: Don't check for PYTHONCASEOK when using -E.
This commit fixes a regression that sneaked into Python 3.3 where importlib
was not respecting -E when checking for the PYTHONCASEOK environment variable.
2013-09-03 16:53:22 -05:00
Meador Inge d151da9ef7 Issue #16826: Don't check for PYTHONCASEOK when using -E.
This commit fixes a regression that sneaked into Python 3.3 where importlib
was not respecting -E when checking for the PYTHONCASEOK environment variable.
2013-09-03 16:37:26 -05:00
Eric Snow 8e4554027b Closes issue #18698: ensure importlib.reload() returns the module out of sys.modules. 2013-08-14 18:11:09 -06:00
Brett Cannon e1f159722e Closes issue #18598: Have the exception message for
importlib.import_module() include the name of the module when the
'package' argument is missing but needed.
2013-08-12 13:29:11 -04:00
Ezio Melotti fbcf4d78c8 #18681: merge with 3.3. 2013-08-10 20:01:43 +03:00
Brett Cannon f0cb69274c Issue #18415: Normalize what type of quotes are used with string
constants in importlib._bootstrap. Along the way clean up from string
interpolation to use the repr explicitly.

Initial patch by Madison May.
2013-07-12 11:04:23 -04:00
Brett Cannon 7e5d55705c merge for issue #18351. 2013-07-06 18:04:41 -04:00
Brett Cannon a53cca3fea Issue #18351: Fix various issues with
importlib._bootstrap._get_sourcefile().

Thanks to its only use by the C API, it was never properly tested
until now.

Thanks to Neal Norwitz for discovering the bug and Madison May for the patch.
2013-07-06 17:56:43 -04:00
Brett Cannon 0075110ab2 Issue #18364: Stop using the ImportError._not_found hack.
The private attribute was leaking out of importlib and led to at least
one person noticing it. Switch to another hack which won't leak
outside of importlib and is nearly as robust.
2013-07-06 14:48:18 -04:00
Brett Cannon 679ecb565b Issue #15767: back out 8a0ed9f63c6e, finishing the removal of
ModuleNotFoundError.
2013-07-04 17:51:50 -04:00
Brett Cannon 82da8886cc Issue #15767: Revert 3a50025f1900 for ModuleNotFoundError 2013-07-04 17:48:16 -04:00
Brett Cannon cd171c8e92 Issue #18200: Back out usage of ModuleNotFoundError (8d28d44f3a9a) 2013-07-04 17:43:24 -04:00
Brett Cannon 390f6c1bf7 Merge doc/argument name fix 2013-06-16 19:09:46 -04:00
Brett Cannon 1d75382e81 Fix a misnaming of a method and an argument 2013-06-16 19:06:55 -04:00
Brett Cannon f24fecd4ac Issue #18076: Introduce imoportlib.util.decode_source().
The helper function makes it easier to implement
imoprtlib.abc.InspectLoader.get_source() by making that function
require just the raw bytes for source code and handling all other
details.
2013-06-16 18:37:53 -04:00
Brett Cannon f4375ef4d4 importlib.abc.SourceLoader.get_source() was re-raising SyntaxError and
UnicodeDecodeError as ImportError. That was over-reaching the point of
raising ImportError in get_source() (which is to signal the source
code was not found when it should have). Conflating the two exceptions
with ImportError could lead to masking errors with the source which
should be known outside of whether there was an error simply getting
the source to begin with.
2013-06-16 18:05:54 -04:00
Brett Cannon 01b0475b08 Issue #18115: Abstract out managing the cleanup of modules to use in
loaders where C code provides the loaded module.
2013-06-16 17:23:06 -04:00
Brett Cannon 13d8ff9c5b Issues #18058, 18057: Make importlib._bootstrap.NamespaceLoader
conform the the InspectLoader ABC. Perk of this is that runpy/-m can
now work with namespace packages.
2013-06-16 14:56:58 -04:00
Brett Cannon a3c96154d2 Issue #17907: touch up the code for imp.new_module(). 2013-06-14 22:26:30 -04:00
Brett Cannon 05a647deed Issue #18192: Introduce importlib.util.MAGIC_NUMBER and document the
deprecation of imp.get_magic().
2013-06-14 19:02:34 -04:00
Brett Cannon 3fe35e6503 Issue #18193: Add importlib.reload(), documenting (but not
implementing in code) the deprecation of imp.reload().

Thanks to Berker Peksag for the patch.
2013-06-14 15:04:26 -04:00
Brett Cannon 0a140668fa Issue #18200: Update the stdlib (except tests) to use
ModuleNotFoundError.
2013-06-13 20:57:26 -04:00
Brett Cannon 8f5ac5106e Issue #15767: Touch up ModuleNotFoundError usage by import.
Forgot to raise ModuleNotFoundError when None is found in sys.modules.
This led to introducing the C function PyErr_SetImportErrorSubclass()
to make setting ModuleNotFoundError easier.

Also updated the reference docs to mention ModuleNotFoundError
appropriately. Updated the docs for ModuleNotFoundError to mention the
None in sys.modules case.

Lastly, it was noticed that PyErr_SetImportError() was not setting an
exception when returning None in one case. That issue is now fixed.
2013-06-12 23:29:18 -04:00
Brett Cannon b1611e2772 Issue #15767: Introduce ModuleNotFoundError, a subclass of
ImportError.

The exception is raised by import when a module could not be found.
Technically this is defined as no viable loader could be found for the
specified module. This includes ``from ... import`` statements so that
the module usage is consistent for all situations where import
couldn't find what was requested.

This should allow for the common idiom of::

  try:
    import something
  except ImportError:
    pass

to be updated to using ModuleNotFoundError and not accidentally mask
ImportError messages that should propagate (e.g. issues with a
loader).

This work was driven by the fact that the ``from ... import``
statement needed to be able to tell the difference between an
ImportError that simply couldn't find a module (and thus silence the
exception so that ceval can raise it) and an ImportError that
represented an actual problem.
2013-06-12 16:59:46 -04:00
Brett Cannon 12d400db65 explanatory comment 2013-06-11 17:34:04 -04:00
Brett Cannon 865b2925dd typo fix 2013-06-11 17:22:39 -04:00
Brett Cannon 4e694d6fa9 tweak exception message (again) 2013-06-05 18:37:50 -04:00
Brett Cannon af38f5a503 Tweak at the suggestion of Ezio Melotti for exception messages when
EOF is hit while trying to read the header of a bytecode file.
2013-06-04 17:34:49 -04:00
Brett Cannon 0e75c06886 fix whitespace 2013-05-31 18:57:45 -04:00
Brett Cannon 0dbb4c7f13 Issues #18088, 18089: Introduce
importlib.abc.Loader.init_module_attrs() and implement
importlib.abc.InspectLoader.load_module().

The importlib.abc.Loader.init_module_attrs() method sets the various
attributes on the module being loaded. It is done unconditionally to
support reloading. Typically people used
importlib.util.module_for_loader, but since that's a decorator there
was no way to override it's actions, so init_module_attrs() came into
existence to allow for overriding. This is also why module_for_loader
is now pending deprecation (having its other use replaced by
importlib.util.module_to_load).

All of this allowed for importlib.abc.InspectLoader.load_module() to
be implemented. At this point you can now implement a loader with
nothing more than get_code() (which only requires get_source();
package support requires is_package()). Thanks to init_module_attrs()
the implementation of load_module() is basically a context manager
containing 2 methods calls, a call to exec(), and a return statement.
2013-05-31 18:56:47 -04:00
Brett Cannon f1d7b11db9 Docstring cleanup 2013-05-31 18:39:07 -04:00
Brett Cannon c29cb41822 Fix for last commit on adding reset_name to module_to_load 2013-05-31 18:37:44 -04:00
Brett Cannon b60a43eabf Add a reset_name argument to importlib.util.module_to_load in order to
control whether to reset the module's __name__ attribute in case a
reload is being done.
2013-05-31 18:11:17 -04:00
Brett Cannon 357c9fb055 Rename importlib.util.ModuleManager to module_to_load so that the name
explains better what the context manager is providing.
2013-05-30 17:31:47 -04:00
Brett Cannon 3dc48d6f69 Issue #18070: importlib.util.module_for_loader() now sets __loader__
and __package__ unconditionally in order to do the right thing for
reloading.
2013-05-28 18:35:54 -04:00
Brett Cannon a3687f0d68 Introduce importlib.util.ModuleManager which is a context manager to
handle providing (and cleaning up if needed) the module to be loaded.

A future commit will use the context manager in
Lib/importlib/_bootstrap.py and thus why the code is placed there
instead of in Lib/importlib/util.py.
2013-05-28 17:29:34 -04:00
Brett Cannon 3b62ca88e4 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
ExecutionLoader.
2013-05-27 21:11:04 -04:00