Kaydet (Commit) 3adc7b75 authored tarafından Brett Cannon's avatar Brett Cannon

Issue #15242: Have PyImport_GetMagicTag() return a const char *

defined in sysmodule.c instead of straight out of a Unicode object.

Thanks to Amaury Forgeot d'Arc for noticing the bug and Eric Snow for
writing the patch.
üst 903c27c1
...@@ -178,7 +178,8 @@ Importing Modules ...@@ -178,7 +178,8 @@ Importing Modules
.. c:function:: const char * PyImport_GetMagicTag() .. c:function:: const char * PyImport_GetMagicTag()
Return the magic tag string for :pep:`3147` format Python bytecode file Return the magic tag string for :pep:`3147` format Python bytecode file
names. names. Keep in mind that the value at ``sys.implementation.cache_tag`` is
authoritative and should be used instead of this function.
.. versionadded:: 3.2 .. versionadded:: 3.2
......
...@@ -116,7 +116,7 @@ typedef unsigned short mode_t; ...@@ -116,7 +116,7 @@ typedef unsigned short mode_t;
*/ */
#define MAGIC (3230 | ((long)'\r'<<16) | ((long)'\n'<<24)) #define MAGIC (3230 | ((long)'\r'<<16) | ((long)'\n'<<24))
#define CACHEDIR "__pycache__" #define CACHEDIR "__pycache__"
/* Current magic word and string tag as globals. */ /* Current magic word as global. */
static long pyc_magic = MAGIC; static long pyc_magic = MAGIC;
/* See _PyImport_FixupExtensionObject() below */ /* See _PyImport_FixupExtensionObject() below */
...@@ -520,22 +520,12 @@ PyImport_GetMagicNumber(void) ...@@ -520,22 +520,12 @@ PyImport_GetMagicNumber(void)
} }
extern const char * _PySys_ImplCacheTag;
const char * const char *
PyImport_GetMagicTag(void) PyImport_GetMagicTag(void)
{ {
PyObject *impl, *tag; return _PySys_ImplCacheTag;
const char *raw_tag;
/* We could also pull it from imp or importlib. */
impl = PySys_GetObject("implementation");
if (impl == NULL)
return NULL;
tag = PyObject_GetAttrString(impl, "cache_tag");
if (tag == NULL)
return NULL;
raw_tag = PyUnicode_DATA(tag);
Py_DECREF(tag);
return raw_tag;
} }
......
...@@ -1478,6 +1478,22 @@ make_version_info(void) ...@@ -1478,6 +1478,22 @@ make_version_info(void)
return version_info; return version_info;
} }
/* sys.implementation values */
#define NAME "cpython"
const char *_PySys_ImplName = NAME;
#define QUOTE(arg) #arg
#define STRIFY(name) QUOTE(name)
#define MAJOR STRIFY(PY_MAJOR_VERSION)
#define MINOR STRIFY(PY_MINOR_VERSION)
#define TAG NAME "-" MAJOR MINOR;
const char *_PySys_ImplCacheTag = TAG;
#undef NAME
#undef QUOTE
#undef STRIFY
#undef MAJOR
#undef MINOR
#undef TAG
static PyObject * static PyObject *
make_impl_info(PyObject *version_info) make_impl_info(PyObject *version_info)
{ {
...@@ -1490,13 +1506,7 @@ make_impl_info(PyObject *version_info) ...@@ -1490,13 +1506,7 @@ make_impl_info(PyObject *version_info)
/* populate the dict */ /* populate the dict */
#define NAME "cpython" value = PyUnicode_FromString(_PySys_ImplName);
#define QUOTE(arg) #arg
#define STRIFY(name) QUOTE(name)
#define MAJOR STRIFY(PY_MAJOR_VERSION)
#define MINOR STRIFY(PY_MINOR_VERSION)
#define TAG NAME "-" MAJOR MINOR
value = PyUnicode_FromString(NAME);
if (value == NULL) if (value == NULL)
goto error; goto error;
res = PyDict_SetItemString(impl_info, "name", value); res = PyDict_SetItemString(impl_info, "name", value);
...@@ -1504,19 +1514,13 @@ make_impl_info(PyObject *version_info) ...@@ -1504,19 +1514,13 @@ make_impl_info(PyObject *version_info)
if (res < 0) if (res < 0)
goto error; goto error;
value = PyUnicode_FromString(TAG); value = PyUnicode_FromString(_PySys_ImplCacheTag);
if (value == NULL) if (value == NULL)
goto error; goto error;
res = PyDict_SetItemString(impl_info, "cache_tag", value); res = PyDict_SetItemString(impl_info, "cache_tag", value);
Py_DECREF(value); Py_DECREF(value);
if (res < 0) if (res < 0)
goto error; goto error;
#undef NAME
#undef QUOTE
#undef STRIFY
#undef MAJOR
#undef MINOR
#undef TAG
res = PyDict_SetItemString(impl_info, "version", version_info); res = PyDict_SetItemString(impl_info, "version", version_info);
if (res < 0) if (res < 0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment