History

python: remove automatic compiled module loading (.pyc files)

cinap_lenrek committed 10 years ago

Commit 6103afd1 parent f222a0c1 file · +6 −135Browse files

Showing the changes to sys/src/cmd/python/Python (1 of 1 file). Show all

Unified · Split

sys/src/cmd/python/Python/import.c+6−135
@@ -88,16 +88,11 @@ struct filedescr * _PyImport_Filetab = NULL; · 87 unmodified lines
88#ifdef RISCOS88#ifdef RISCOS
89static const struct filedescr _PyImport_StandardFiletab[] = {89static const struct filedescr _PyImport_StandardFiletab[] = {
90 {"/py", "U", PY_SOURCE},90 {"/py", "U", PY_SOURCE},
91 {"/pyc", "rb", PY_COMPILED},
92 {0, 0}91 {0, 0}
93};92};
94#else93#else
95static const struct filedescr _PyImport_StandardFiletab[] = {94static const struct filedescr _PyImport_StandardFiletab[] = {
96 {".py", "U", PY_SOURCE},95 {".py", "U", PY_SOURCE},
97#ifdef MS_WINDOWS
98 {".pyw", "U", PY_SOURCE},
99#endif
100 {".pyc", "rb", PY_COMPILED},
101 {0, 0}96 {0, 0}
102};97};
103#endif98#endif
@@ -688,69 +683,6 @@ PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname) · 584 unmodified lines
688}683}
689 684
690 685
691/* Given a pathname for a Python source file, fill a buffer with the
692 pathname for the corresponding compiled file. Return the pathname
693 for the compiled file, or NULL if there's no space in the buffer.
694 Doesn't set an exception. */
695
696static char *
697make_compiled_pathname(char *pathname, char *buf, size_t buflen)
698{
699 size_t len = strlen(pathname);
700 if (len+2 > buflen)
701 return NULL;
702
703#ifdef MS_WINDOWS
704 /* Treat .pyw as if it were .py. The case of ".pyw" must match
705 that used in _PyImport_StandardFiletab. */
706 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
707 --len; /* pretend 'w' isn't there */
708#endif
709 memcpy(buf, pathname, len);
710 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
711 buf[len+1] = '\0';
712
713 return buf;
714}
715
716
717/* Given a pathname for a Python source file, its time of last
718 modification, and a pathname for a compiled file, check whether the
719 compiled file represents the same version of the source. If so,
720 return a FILE pointer for the compiled file, positioned just after
721 the header; if not, return NULL.
722 Doesn't set an exception. */
723
724static FILE *
725check_compiled_module(char *pathname, time_t mtime, char *cpathname)
726{
727 FILE *fp;
728 long magic;
729 long pyc_mtime;
730
731 fp = fopen(cpathname, "rb");
732 if (fp == NULL)
733 return NULL;
734 magic = PyMarshal_ReadLongFromFile(fp);
735 if (magic != pyc_magic) {
736 if (Py_VerboseFlag)
737 PySys_WriteStderr("# %s has bad magic\n", cpathname);
738 fclose(fp);
739 return NULL;
740 }
741 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
742 if (pyc_mtime != mtime) {
743 if (Py_VerboseFlag)
744 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
745 fclose(fp);
746 return NULL;
747 }
748 if (Py_VerboseFlag)
749 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
750 return fp;
751}
752
753
754/* Read a code object from a file and check it for validity */686/* Read a code object from a file and check it for validity */
755 687
756static PyCodeObject *688static PyCodeObject *
@@ -854,47 +786,6 @@ open_exclusive(char *filename) · 97 unmodified lines
854#endif786#endif
855}787}
856 788
857
858/* Write a compiled module to a file, placing the time of last
859 modification of its source into the header.
860 Errors are ignored, if a write error occurs an attempt is made to
861 remove the file. */
862
863static void
864write_compiled_module(PyCodeObject *co, char *cpathname, time_t mtime)
865{
866 FILE *fp;
867
868 fp = open_exclusive(cpathname);
869 if (fp == NULL) {
870 if (Py_VerboseFlag)
871 PySys_WriteStderr(
872 "# can't create %s\n", cpathname);
873 return;
874 }
875 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
876 /* First write a 0 for mtime */
877 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
878 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
879 if (fflush(fp) != 0 || ferror(fp)) {
880 if (Py_VerboseFlag)
881 PySys_WriteStderr("# can't write %s\n", cpathname);
882 /* Don't keep partial file */
883 fclose(fp);
884 (void) unlink(cpathname);
885 return;
886 }
887 /* Now write the true mtime */
888 fseek(fp, 4L, 0);
889 assert(mtime < LONG_MAX);
890 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
891 fflush(fp);
892 fclose(fp);
893 if (Py_VerboseFlag)
894 PySys_WriteStderr("# wrote %s\n", cpathname);
895}
896
897
898/* Load a source module from a given file and return its module789/* Load a source module from a given file and return its module
899 object WITH INCREMENTED REFERENCE COUNT. If there's a matching790 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
900 byte-compiled file, use that instead. */791 byte-compiled file, use that instead. */
@@ -905,7 +796,6 @@ load_source_module(char *name, char *pathname, FILE *fp) · 4 unmodified lines
905 time_t mtime;796 time_t mtime;
906 FILE *fpc;797 FILE *fpc;
907 char buf[MAXPATHLEN+1];798 char buf[MAXPATHLEN+1];
908 char *cpathname;
909 PyCodeObject *co;799 PyCodeObject *co;
910 PyObject *m;800 PyObject *m;
911 801
@@ -927,31 +817,12 @@ load_source_module(char *name, char *pathname, FILE *fp) · 15 unmodified lines
927 return NULL;817 return NULL;
928 }818 }
929#endif819#endif
930 cpathname = make_compiled_pathname(pathname, buf,820 co = parse_source_module(pathname, fp);
931 (size_t)MAXPATHLEN + 1);821 if (co == NULL)
932 if (cpathname != NULL &&822 return NULL;
933 (fpc = check_compiled_module(pathname, mtime, cpathname))) {823 if (Py_VerboseFlag)
934 co = read_compiled_module(cpathname, fpc);824 PySys_WriteStderr("import %s # from %s\n",
935 fclose(fpc);825 name, pathname);
936 if (co == NULL)
937 return NULL;
938 if (Py_VerboseFlag)
939 PySys_WriteStderr("import %s # precompiled from %s\n",
940 name, cpathname);
941 pathname = cpathname;
942 }
943 else {
944 co = parse_source_module(pathname, fp);
945 if (co == NULL)
946 return NULL;
947 if (Py_VerboseFlag)
948 PySys_WriteStderr("import %s # from %s\n",
949 name, pathname);
950
951 if(0) /* disabled this for now -- cinap */
952 if (cpathname)
953 write_compiled_module(co, cpathname, mtime);
954 }
955 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);826 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
956 Py_DECREF(co);827 Py_DECREF(co);
957 828