[Python-Dev] import.c:load_source_module() confusion

Guido van Rossum guido@python.org
Fri, 31 Jan 2003 10:15:06 -0500


> I'm working on a patch implementing PEP 304 behavior and am a little
> confused about how import.c:load_source_module() works.  When a source
> module is found, it's called.  The first thing it does is check for and try
> to read a .pyc/.pyo file:
> 
>         cpathname = make_compiled_pathname(pathname, buf,
>                                            (size_t)MAXPATHLEN + 1);
>         if (cpathname != NULL &&
>             (fpc = check_compiled_module(pathname, mtime, cpathname))) {
>                 co = read_compiled_module(cpathname, fpc);
>                 fclose(fpc);
>                 if (co == NULL)
>                         return NULL;
>                 if (Py_VerboseFlag)
>                         PySys_WriteStderr("import %s # precompiled from %s\n",
>                                 name, cpathname);
>                 pathname = cpathname;
>         }
> 
> I don't understand why it returns when co is NULL.  Shouldn't it compile the
> source code (later on in the function) and return the code object generated
> from that?  It seems like it would be rare in most circumstances (unless
> your name is Paul DuBois ;-) for read_compiled_module() to return NULL
> (corrupt marshal data or a .pyc containing something other than a code
> object).

Unlike a bad magic number, getting an error from
read_compiled_module() is indicative of a problem that shouldn't be
ignored.

--Guido van Rossum (home page: http://www.python.org/~guido/)