Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96774c1

Browse files
committed
Marc-Andre Lemburg:
Changed all references to the MAGIC constant to use a global pyc_magic instead. This global is initially set to MAGIC, but can be changed by the _PyImport_Init() function to provide for special features implemented in the compiler which are settable using command line switches and affect the way PYC files are generated. Currently this change is only done for the -U flag.
1 parent d67ddbb commit 96774c1

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

Python/import.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ extern long PyOS_GetLastModificationTime(); /* In getmtime.c */
8686
/* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */
8787
#define MAGIC (50428 | ((long)'\r'<<16) | ((long)'\n'<<24))
8888

89+
/* Magic word as global; note that _PyImport_Init() can change the
90+
value of this global to accomodate for alterations of how the
91+
compiler works which are enabled by command line switches. */
92+
static long pyc_magic = MAGIC;
93+
8994
/* See _PyImport_FixupExtension() below */
9095
static PyObject *extensions = NULL;
9196

@@ -135,6 +140,13 @@ _PyImport_Init()
135140
filetab->suffix = ".pyo";
136141
}
137142
}
143+
144+
if (Py_UnicodeFlag) {
145+
/* Fix the pyc_magic so that byte compiled code created
146+
using the all-Unicode method doesn't interfere with
147+
code created in normal operation mode. */
148+
pyc_magic = MAGIC + 1;
149+
}
138150
}
139151

140152
void
@@ -366,7 +378,7 @@ PyImport_Cleanup()
366378
long
367379
PyImport_GetMagicNumber()
368380
{
369-
return MAGIC;
381+
return pyc_magic;
370382
}
371383

372384

@@ -572,7 +584,7 @@ check_compiled_module(pathname, mtime, cpathname)
572584
if (fp == NULL)
573585
return NULL;
574586
magic = PyMarshal_ReadLongFromFile(fp);
575-
if (magic != MAGIC) {
587+
if (magic != pyc_magic) {
576588
if (Py_VerboseFlag)
577589
PySys_WriteStderr("# %s has bad magic\n", cpathname);
578590
fclose(fp);
@@ -627,7 +639,7 @@ load_compiled_module(name, cpathname, fp)
627639
PyObject *m;
628640

629641
magic = PyMarshal_ReadLongFromFile(fp);
630-
if (magic != MAGIC) {
642+
if (magic != pyc_magic) {
631643
PyErr_Format(PyExc_ImportError,
632644
"Bad magic number in %.200s", cpathname);
633645
return NULL;
@@ -685,7 +697,7 @@ write_compiled_module(co, cpathname, mtime)
685697
"# can't create %s\n", cpathname);
686698
return;
687699
}
688-
PyMarshal_WriteLongToFile(MAGIC, fp);
700+
PyMarshal_WriteLongToFile(pyc_magic, fp);
689701
/* First write a 0 for mtime */
690702
PyMarshal_WriteLongToFile(0L, fp);
691703
PyMarshal_WriteObjectToFile((PyObject *)co, fp);
@@ -1953,10 +1965,10 @@ imp_get_magic(self, args)
19531965

19541966
if (!PyArg_ParseTuple(args, ":get_magic"))
19551967
return NULL;
1956-
buf[0] = (char) ((MAGIC >> 0) & 0xff);
1957-
buf[1] = (char) ((MAGIC >> 8) & 0xff);
1958-
buf[2] = (char) ((MAGIC >> 16) & 0xff);
1959-
buf[3] = (char) ((MAGIC >> 24) & 0xff);
1968+
buf[0] = (char) ((pyc_magic >> 0) & 0xff);
1969+
buf[1] = (char) ((pyc_magic >> 8) & 0xff);
1970+
buf[2] = (char) ((pyc_magic >> 16) & 0xff);
1971+
buf[3] = (char) ((pyc_magic >> 24) & 0xff);
19601972

19611973
return PyString_FromStringAndSize(buf, 4);
19621974
}

0 commit comments

Comments
 (0)