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

Skip to content

Commit 3adc7b7

Browse files
committed
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.
1 parent 903c27c commit 3adc7b7

3 files changed

Lines changed: 24 additions & 29 deletions

File tree

Doc/c-api/import.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ Importing Modules
178178
.. c:function:: const char * PyImport_GetMagicTag()
179179
180180
Return the magic tag string for :pep:`3147` format Python bytecode file
181-
names.
181+
names. Keep in mind that the value at ``sys.implementation.cache_tag`` is
182+
authoritative and should be used instead of this function.
182183
183184
.. versionadded:: 3.2
184185

Python/import.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ typedef unsigned short mode_t;
116116
*/
117117
#define MAGIC (3230 | ((long)'\r'<<16) | ((long)'\n'<<24))
118118
#define CACHEDIR "__pycache__"
119-
/* Current magic word and string tag as globals. */
119+
/* Current magic word as global. */
120120
static long pyc_magic = MAGIC;
121121

122122
/* See _PyImport_FixupExtensionObject() below */
@@ -520,22 +520,12 @@ PyImport_GetMagicNumber(void)
520520
}
521521

522522

523+
extern const char * _PySys_ImplCacheTag;
524+
523525
const char *
524526
PyImport_GetMagicTag(void)
525527
{
526-
PyObject *impl, *tag;
527-
const char *raw_tag;
528-
529-
/* We could also pull it from imp or importlib. */
530-
impl = PySys_GetObject("implementation");
531-
if (impl == NULL)
532-
return NULL;
533-
tag = PyObject_GetAttrString(impl, "cache_tag");
534-
if (tag == NULL)
535-
return NULL;
536-
raw_tag = PyUnicode_DATA(tag);
537-
Py_DECREF(tag);
538-
return raw_tag;
528+
return _PySys_ImplCacheTag;
539529
}
540530

541531

Python/sysmodule.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,22 @@ make_version_info(void)
14781478
return version_info;
14791479
}
14801480

1481+
/* sys.implementation values */
1482+
#define NAME "cpython"
1483+
const char *_PySys_ImplName = NAME;
1484+
#define QUOTE(arg) #arg
1485+
#define STRIFY(name) QUOTE(name)
1486+
#define MAJOR STRIFY(PY_MAJOR_VERSION)
1487+
#define MINOR STRIFY(PY_MINOR_VERSION)
1488+
#define TAG NAME "-" MAJOR MINOR;
1489+
const char *_PySys_ImplCacheTag = TAG;
1490+
#undef NAME
1491+
#undef QUOTE
1492+
#undef STRIFY
1493+
#undef MAJOR
1494+
#undef MINOR
1495+
#undef TAG
1496+
14811497
static PyObject *
14821498
make_impl_info(PyObject *version_info)
14831499
{
@@ -1490,33 +1506,21 @@ make_impl_info(PyObject *version_info)
14901506

14911507
/* populate the dict */
14921508

1493-
#define NAME "cpython"
1494-
#define QUOTE(arg) #arg
1495-
#define STRIFY(name) QUOTE(name)
1496-
#define MAJOR STRIFY(PY_MAJOR_VERSION)
1497-
#define MINOR STRIFY(PY_MINOR_VERSION)
1498-
#define TAG NAME "-" MAJOR MINOR
1499-
value = PyUnicode_FromString(NAME);
1509+
value = PyUnicode_FromString(_PySys_ImplName);
15001510
if (value == NULL)
15011511
goto error;
15021512
res = PyDict_SetItemString(impl_info, "name", value);
15031513
Py_DECREF(value);
15041514
if (res < 0)
15051515
goto error;
15061516

1507-
value = PyUnicode_FromString(TAG);
1517+
value = PyUnicode_FromString(_PySys_ImplCacheTag);
15081518
if (value == NULL)
15091519
goto error;
15101520
res = PyDict_SetItemString(impl_info, "cache_tag", value);
15111521
Py_DECREF(value);
15121522
if (res < 0)
15131523
goto error;
1514-
#undef NAME
1515-
#undef QUOTE
1516-
#undef STRIFY
1517-
#undef MAJOR
1518-
#undef MINOR
1519-
#undef TAG
15201524

15211525
res = PyDict_SetItemString(impl_info, "version", version_info);
15221526
if (res < 0)

0 commit comments

Comments
 (0)