revised import.c

lance@markv.com
Thu, 29 Jul 93 17:20:04 PDT

I have merged my changes to import.c into the 0.9.9 version.
Here are the diffs for the 0.9.9 version that allow you to have
a .pyc file without the .py file being present. Again, this still
needs to be tested by someone that uses dynamic linking as I don't.

Enjoy!

*** import.c Tue May 25 02:38:24 1993
--- import.c.new Thu Jul 29 17:10:14 1993
***************
*** 38,45 ****
--- 38,56 ----
#include "eval.h"
#include "osdefs.h"

+ #include <unistd.h>
+ #include <sys/stat.h>
+
extern int verbose; /* Defined in pythonmain.c */

+ #ifndef FALSE
+ #define FALSE 0
+ #endif
+
+ #ifndef TRUE
+ #define TRUE 1
+ #endif
+
#ifdef DEBUG
#define D(x) x
#else
***************
*** 97,137 ****
/* Suffixes used by open_module: */

#define PY_SUFFIX ".py"
#ifdef USE_DL
#define O_SUFFIX "module.o"
#endif

! /* Find and open a module file, using sys.path.
! Return a NULL pointer if no module file is found.
! When dynamic loading is enabled, the contents of namebuf
! is important when NULL is returned: if namebuf[0] != '\0'
! a dl-able object file was found and namebuf is its pathname. */
!
! static FILE *
! open_module(name, namebuf)
char *name;
! char *namebuf; /* XXX No buffer overflow checks! */
{
object *path;
! FILE *fp;
!
path = sysget("path");
if (path == NULL || !is_listobject(path)) {
/* No path -- at least try current directory */
- #ifdef USE_DL
- strcpy(namebuf, name);
- strcat(namebuf, O_SUFFIX);
- if (getmtime(namebuf) > 0)
- return NULL;
- #endif
strcpy(namebuf, name);
! strcat(namebuf, PY_SUFFIX);
! fp = fopen(namebuf, "r");
! }
! else {
int npath = getlistsize(path);
int i;
- fp = NULL;
for (i = 0; i < npath; i++) {
object *v = getlistitem(path, i);
int len;
--- 108,146 ----
/* Suffixes used by open_module: */

#define PY_SUFFIX ".py"
+ #define PYC_SUFFIX ".pyc"
#ifdef USE_DL
#define O_SUFFIX "module.o"
#endif

! /* This will search for a module named 'name' with the extension 'ext'
! and return it in 'namebuf' and return the mtime of each in 'mtime'
! it will return TRUE if it found it, FALSE if it does not.
! */
! static int
! find_module(name, ext, namebuf, mtime)
char *name;
! char *ext;
! char *namebuf;
! time_t *mtime;
{
object *path;
! struct stat s;
!
path = sysget("path");
if (path == NULL || !is_listobject(path)) {
/* No path -- at least try current directory */
strcpy(namebuf, name);
! strcat(namebuf, ext);
! if (stat(namebuf,&s) == -1)
! return FALSE;
! if (access(namebuf,R_OK) == -1)
! return FALSE;
! *mtime = s.st_mtime;
! return TRUE;
! } else {
int npath = getlistsize(path);
int i;
for (i = 0; i < npath; i++) {
object *v = getlistitem(path, i);
int len;
***************
*** 141,162 ****
len = getstringsize(v);
if (len > 0 && namebuf[len-1] != SEP)
namebuf[len++] = SEP;
- #ifdef USE_DL
strcpy(namebuf+len, name);
! strcat(namebuf, O_SUFFIX);
! if (getmtime(namebuf) > 0)
! return NULL;
! #endif
! strcpy(namebuf+len, name);
! strcat(namebuf, PY_SUFFIX);
! fp = fopen(namebuf, "r");
! if (fp != NULL)
! break;
}
}
! if (fp == NULL)
! namebuf[0] = '\0';
! return fp;
}

static object *
--- 150,167 ----
len = getstringsize(v);
if (len > 0 && namebuf[len-1] != SEP)
namebuf[len++] = SEP;
strcpy(namebuf+len, name);
! strcat(namebuf, ext);
! if (stat(namebuf,&s) == -1)
! continue;
! if (access(namebuf,R_OK) == -1)
! continue;
! *mtime = s.st_mtime;
! return TRUE;
}
}
! namebuf[0] = '\0';
! return FALSE;
}

static object *
***************
*** 175,183 ****
long mtime;
extern long getmtime();

! fp = open_module(name, namebuf);
! if (fp == NULL) {
#ifdef USE_DL
if (namebuf[0] != '\0') {
char funcname[258];
dl_funcptr p;
--- 180,188 ----
long mtime;
extern long getmtime();

!
#ifdef USE_DL
+ if (find_module(name,O_SUFFIX,namebuf,&mtime)) {
if (namebuf[0] != '\0') {
char funcname[258];
dl_funcptr p;
***************
*** 186,193 ****
p = dl_loadmod(argv0, namebuf, funcname);
if (p == NULL) {
D(fprintf(stderr, "dl_loadmod failed\n"));
! }
! else {
if (verbose)
fprintf(stderr,
"import %s # dynamically loaded from \"%s\"\n",
--- 191,197 ----
p = dl_loadmod(argv0, namebuf, funcname);
if (p == NULL) {
D(fprintf(stderr, "dl_loadmod failed\n"));
! } else {
if (verbose)
fprintf(stderr,
"import %s # dynamically loaded from \"%s\"\n",
***************
*** 198,205 ****
err_setstr(SystemError,
"dynamic module missing");
return NULL;
! }
! else {
D(fprintf(stderr,
"module %s loaded!\n", name));
INCREF(None);
--- 202,208 ----
err_setstr(SystemError,
"dynamic module missing");
return NULL;
! } else {
D(fprintf(stderr,
"module %s loaded!\n", name));
INCREF(None);
***************
*** 207,213 ****
--- 210,272 ----
}
}
}
+ } else
#endif
+ if (find_module(name,PYC_SUFFIX,namebuf,&mtime)) {
+ read_pyc:
+ fpc = fopen(namebuf, "rb");
+ namebuf[(strlen(namebuf)-1)] = '\0';
+ mtime = getmtime(namebuf);
+ if (fpc != NULL) {
+ long pyc_mtime;
+ long magic;
+ magic = rd_long(fpc);
+ pyc_mtime = rd_long(fpc);
+ if (mtime != -1 && mtime > pyc_mtime) {
+ fclose(fpc);
+ goto read_py;
+ }
+ if (magic == MAGIC) {
+ v = rd_object(fpc);
+ if (v == NULL || err_occurred() || !is_codeobject(v)) {
+ err_clear();
+ XDECREF(v);
+ }
+ else
+ co = (codeobject *)v;
+ }
+ fclose(fpc);
+ if (verbose) {
+ if (co != NULL)
+ fprintf(stderr,
+ "import %s # precompiled from \"%s\"\n",
+ name, namebuf);
+ else
+ fprintf(stderr,
+ "# invalid precompiled file \"%s\"\n",
+ namebuf);
+ }
+ }
+ } else if (find_module(name,PY_SUFFIX,namebuf,&mtime)) {
+ read_py:
+ fp = fopen(namebuf,"rb");
+ namelen = strlen(namebuf);
+ if (co == NULL) {
+ if (verbose)
+ fprintf(stderr,
+ "import %s # from \"%s\"\n",
+ name, namebuf);
+ err = parse_file(fp, namebuf, file_input, &n);
+ } else
+ err = E_DONE;
+ fclose(fp);
+ if (err != E_DONE) {
+ err_input(err);
+ return NULL;
+ }
+ } else {
if (m == NULL) {
sprintf(namebuf, "no module named %.200s", name);
err_setstr(ImportError, namebuf);
***************
*** 216,269 ****
sprintf(namebuf, "no source for module %.200s", name);
err_setstr(ImportError, namebuf);
}
- return NULL;
- }
- /* Get mtime -- always useful */
- mtime = getmtime(namebuf);
- /* Check ".pyc" file first */
- namelen = strlen(namebuf);
- namebuf[namelen] = 'c';
- namebuf[namelen+1] = '\0';
- fpc = fopen(namebuf, "rb");
- if (fpc != NULL) {
- long pyc_mtime;
- long magic;
- magic = rd_long(fpc);
- pyc_mtime = rd_long(fpc);
- if (magic == MAGIC && pyc_mtime == mtime && mtime != 0 && mtime != -1) {
- v = rd_object(fpc);
- if (v == NULL || err_occurred() || !is_codeobject(v)) {
- err_clear();
- XDECREF(v);
- }
- else
- co = (codeobject *)v;
- }
- fclose(fpc);
- if (verbose) {
- if (co != NULL)
- fprintf(stderr,
- "import %s # precompiled from \"%s\"\n",
- name, namebuf);
- else
- fprintf(stderr,
- "# invalid precompiled file \"%s\"\n",
- namebuf);
- }
- }
- namebuf[namelen] = '\0';
- if (co == NULL) {
- if (verbose)
- fprintf(stderr,
- "import %s # from \"%s\"\n",
- name, namebuf);
- err = parse_file(fp, namebuf, file_input, &n);
- }
- else
- err = E_DONE;
- fclose(fp);
- if (err != E_DONE) {
- err_input(err);
return NULL;
}
if (m == NULL) {
--- 275,280 ----

--

Lance Ellinghouse lance@markv.com

1231 bit key fingerprint = 56 DA 31 0C 17 51 36 6A 4E D4 E0 11 D9 B8 06 0A 1024 bit key fingerprint = 66 2C 75 F2 E9 1C 32 84 3A E3 B0 5E 48 01 4C 37 You can recieve my Public Key by `finger lance@mark.com`