Skip to content

Commit aee0bad

Browse files
committed
First part of package support.
This doesn't yet support "import a.b.c" or "from a.b.c import x", but it does recognize directories. When importing a directory, it initializes __path__ to a list containing the directory name, and loads the __init__ module if found. The (internal) find_module() and load_module() functions are restructured so that they both also handle built-in and frozen modules and Mac resources (and directories of course). The imp module's find_module() and (new) load_module() also have this functionality. Moreover, imp unconditionally defines constants for all module types, and has two more new functions: find_module_in_package() and find_module_in_directory(). There's also a new API function, PyImport_ImportModuleEx(), which takes all four __import__ arguments (name, globals, locals, fromlist). The last three may be NULL. This is currently the same as PyImport_ImportModule() but in the future it will be able to do relative dotted-path imports. Other changes: - bltinmodule.c: in __import__, call PyImport_ImportModuleEx(). - ceval.c: always pass the fromlist to __import__, even if it is a C function, so PyImport_ImportModuleEx() is useful. - getmtime.c: the function has a second argument, the FILE*, on which it applies fstat(). According to Sjoerd this is much faster. The first (pathname) argument is ignored, but remains for backward compatibility (so the Mac version still works without changes). By cleverly combining the new imp functionality, the full support for dotted names in Python (mini.py, not checked in) is now about 7K, lavishly commented (vs. 14K for ni plus 11K for ihooks, also lavishly commented). Good night!
1 parent 026de19 commit aee0bad

File tree

5 files changed

+460
-148
lines changed

5 files changed

+460
-148
lines changed

Python/bltinmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ builtin___import__(self, args)
6565
if (!PyArg_ParseTuple(args, "s|OOO:__import__",
6666
&name, &globals, &locals, &fromlist))
6767
return NULL;
68-
return PyImport_ImportModule(name);
68+
return PyImport_ImportModuleEx(name, globals, locals, fromlist);
6969
}
7070

7171

Python/ceval.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,16 +1408,10 @@ eval_code2(co, globals, locals,
14081408
"__import__ not found");
14091409
break;
14101410
}
1411-
if (PyCFunction_Check(x)) {
1412-
u = Py_None;
1413-
Py_INCREF(u);
1414-
}
1415-
else {
1416-
u = find_from_args(f, INSTR_OFFSET());
1417-
if (u == NULL) {
1418-
x = u;
1419-
break;
1420-
}
1411+
u = find_from_args(f, INSTR_OFFSET());
1412+
if (u == NULL) {
1413+
x = u;
1414+
break;
14211415
}
14221416
w = Py_BuildValue("(OOOO)",
14231417
w,

Python/getmtime.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@ PERFORMANCE OF THIS SOFTWARE.
3535

3636
#include "config.h"
3737

38+
#include <stdio.h>
3839
#include <sys/types.h>
3940
#include <sys/stat.h>
4041

4142
long
42-
PyOS_GetLastModificationTime(path)
43+
PyOS_GetLastModificationTime(path, fp)
4344
char *path;
45+
FILE *fp;
4446
{
4547
struct stat st;
46-
if (stat(path, &st) != 0)
48+
if (fstat(fileno(fp), &st) != 0)
4749
return -1;
4850
else
4951
return st.st_mtime;

0 commit comments

Comments
 (0)