By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

where's "import" in the C sources?

P: n/a
Hi,

i tried to find the file and line in the C sources of python
where the command "import" is implemented. Can anybody give
me some hint on this?
Thanks,
Torsten.

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Torsten Mohr <tm***@s.netic.de> writes:
i tried to find the file and line in the C sources of python
where the command "import" is implemented. Can anybody give
me some hint on this?


Well, there are several levels, depending on what you are looking for.
The literal "import" syntax in a source module is translated by the
Python compiler to various IMPORT_* bytecodes, which are processed in
the main interpreter loop (see ceval.c).

They all basically bubble down to making use of the builtin __import__
method, which is obtained from the builtin module defined in
bltinmodule.c.

That in turn makes use of the import processing module whose code can
be found in import.c - which is the same source that also implements
the "imp" module to provide lower layer access to to the import
internals.

Now, when it comes to physically loading in a module, Python source
and compiled modules are handled by import (well, not the compiling
part), but dynamically loaded extension modules are OS specific. You
can find the handling of such extension modules in OS-specific source
files dynload_*.c (e.g., dynload_win.c for Windows).

All of these files can be found in the dist/src/Python directory in
the Python source tree.

-- David
Jul 18 '05 #2

P: n/a
Hi David,

thanks for the explanation. That is very helpful to me.
Best regards,
Torsten.
David Bolen wrote:
Torsten Mohr <tm***@s.netic.de> writes:
i tried to find the file and line in the C sources of python
where the command "import" is implemented. Can anybody give
me some hint on this?


Well, there are several levels, depending on what you are looking for.
The literal "import" syntax in a source module is translated by the
Python compiler to various IMPORT_* bytecodes, which are processed in
the main interpreter loop (see ceval.c).

They all basically bubble down to making use of the builtin __import__
method, which is obtained from the builtin module defined in
bltinmodule.c.

That in turn makes use of the import processing module whose code can
be found in import.c - which is the same source that also implements
the "imp" module to provide lower layer access to to the import
internals.

Now, when it comes to physically loading in a module, Python source
and compiled modules are handled by import (well, not the compiling
part), but dynamically loaded extension modules are OS specific. You
can find the handling of such extension modules in OS-specific source
files dynload_*.c (e.g., dynload_win.c for Windows).

All of these files can be found in the dist/src/Python directory in
the Python source tree.

-- David


Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.