0

From https://docs.python.org/3/library/importlib.html

The purpose of the importlib package is two-fold.

One is to provide the implementation of the import statement (and thus, by extension, the __import__() function) in Python source code. This provides an implementation of import which is portable to any Python interpreter. This also provides an implementation which is easier to comprehend than one implemented in a programming language other than Python.

Two, the components to implement import are exposed in this package, making it easier for users to create their own custom objects (known generically as an importer) to participate in the import process.

Does it mean that both the import statement and builtin.__import__() function are by default implemented based on importlib.__import() function?

But https://stackoverflow.com/a/44655619/156458 implies that builtins.__import__ is not implemented based on importlib.__import__ by default.

https://docs.python.org/3/library/functions.html#import says that buitlins.__import__ function is invoked by the import statement. so if builtins.__import__ is not implemented based on importlib.__import__ by default, the import statement is not implemented based on importlib.__import__ by default either.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Tim
  • 1
  • 141
  • 372
  • 590

1 Answers1

4

No, the actual import code is a port of the Python code. See the implementation of the PyImport_ImportModuleLevelObject() function (which builtins.__import__ is basically a light Python->C wrapper for), which contains the comment:

/* The below code is importlib.__import__() & _gcd_import(), ported to C
   for added performance. */

So for performance reasons, import uses C-optimised code, not the Python implementation of importlib. The two implementations are kept in sync, however, if you were to create a pull request touching one, the Python core developers will ask you to update the other too before accepting your changes.

Your use of emphasis leads me to believe you are misreading the documentation; importlib is not the implementation of the import statement. This is the implementation of the import statement in Python source code, in contrast to the implementation of the import statement in C code.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks. What does "One is to provide the implementation of the import statement (and thus, by extension, the __import__() function) in Python source code " mean about the relation between the three? – Tim Jun 20 '17 at 15:20
  • @Tim: Python code is understood by more people; by providing a Python implementation more people can try to understand how this all works and, perhaps, provide improvement suggestions. – Martijn Pieters Jun 20 '17 at 15:22
  • I see, thanks. Is it correct that `buitlins.__import__` attribute is binded to `buitlins.__import__` function by default, and can be rebinded to `importlib.__import` function or any custom function by `buitlins.__import__=`? [So are ``buitlins.__import__` attribute and ``buitlins.__import__` function two distinct things defined in the module `builtins`?](https://stackoverflow.com/q/44656019/156458) – Tim Jun 20 '17 at 15:38
  • @Tim: yes, you can do `builtins.__import__ = importlib.__import__`, letting you test the Python implementation. – Martijn Pieters Jun 20 '17 at 15:40
  • Is the `import` statement implemented based on `builtins.__import__` function or `builtins.__import__` attribute? I am asking this, because if it is implemented based on the attribute, and when I rebind `builtins.__import__` attribute to a function different from `builtins.__import__` function, the `import` statement will not be implemented based on `builtins.__import__` function but on the different function I rebind to `builtins.__import__` attribute, correct? – Tim Jun 20 '17 at 16:00
  • But the `import` statement is used for statically importing only, so it shouldn't be affected by me rebinding `builtins.__import__` attribute to a different function (which if I am correct happens only at run time). So what is the `import` statement implemented based on? Is it implemented based on `builtins.__import__` attribute or function? – Tim Jun 20 '17 at 16:03
  • @Tim: `import` the statement uses whatever is assigned to the `builtins.__import__` attribute. The default implementation assigned to that attribute is the C function `builtin__import__`. So yes, you can assign a different callable to that attribute and it'll be used. That's the point of that attribute existing in the first place. – Martijn Pieters Jun 20 '17 at 17:44
  • @Tim: The `import` statement is therefor *hookable*, just like attribute access on instances is hookable by implementing a `__getattribute__` or `__getattr__` method on a class. – Martijn Pieters Jun 20 '17 at 17:44
  • @Tim: https://en.wikipedia.org/wiki/Porting; the C code is a 'translation' from Python to C code. – Martijn Pieters Jun 22 '17 at 21:27