Commit b7ac87e4 authored by Brett Cannon's avatar Brett Cannon

Issue #15166: Re-implement imp.get_tag() using sys.implementation.

Also eliminates some C code in Python/import.c as well.

Patch by Eric Snow with verification by comparing against another
patch from Jeff Knupp.
parent 44f48052
......@@ -11,7 +11,7 @@ from _imp import (lock_held, acquire_lock, release_lock,
init_builtin, init_frozen, is_builtin, is_frozen,
_fix_co_filename, extension_suffixes)
# Could move out of _imp, but not worth the code
from _imp import get_magic, get_tag
from _imp import get_magic
from importlib._bootstrap import new_module
from importlib._bootstrap import cache_from_source
......@@ -37,6 +37,11 @@ PY_CODERESOURCE = 8
IMP_HOOK = 9
def get_tag():
"""Return the magic tag for .pyc or .pyo files."""
return sys.implementation.cache_tag
def get_suffixes():
warnings.warn('imp.get_suffixes() is deprecated; use the constants '
'defined on importlib.machinery instead',
......
......@@ -1452,7 +1452,7 @@ def _setup(sys_module, _imp_module):
# Constants
setattr(self_module, '_relax_case', _make_relax_case())
setattr(self_module, '_MAGIC_NUMBER', _imp_module.get_magic())
setattr(self_module, '_TAG', _imp.get_tag())
setattr(self_module, '_TAG', sys.implementation.cache_tag)
if builtin_os == 'nt':
SOURCE_SUFFIXES.append('.pyw')
......
......@@ -17,6 +17,8 @@ Core and Builtins
Library
-------
- Issue #15166: Implement imp.get_tag() using sys.implementation.cache_tag.
- Issue #15210: Catch KeyError when imprortlib.__init__ can't find
_frozen_importlib in sys.modules, not ImportError.
......
......@@ -112,23 +112,11 @@ typedef unsigned short mode_t;
/* MAGIC must change whenever the bytecode emitted by the compiler may no
longer be understood by older implementations of the eval loop (usually
due to the addition of new opcodes)
TAG must change for each major Python release. The magic number will take
care of any bytecode changes that occur during development.
*/
#define QUOTE(arg) #arg
#define STRIFY(name) QUOTE(name)
#define MAJOR STRIFY(PY_MAJOR_VERSION)
#define MINOR STRIFY(PY_MINOR_VERSION)
#define MAGIC (3230 | ((long)'\r'<<16) | ((long)'\n'<<24))
#define TAG "cpython-" MAJOR MINOR;
#define CACHEDIR "__pycache__"
/* Current magic word and string tag as globals. */
static long pyc_magic = MAGIC;
static const char *pyc_tag = TAG;
#undef QUOTE
#undef STRIFY
#undef MAJOR
#undef MINOR
/* See _PyImport_FixupExtensionObject() below */
static PyObject *extensions = NULL;
......@@ -534,9 +522,22 @@ PyImport_GetMagicNumber(void)
const char *
PyImport_GetMagicTag(void)
{
return pyc_tag;
PyObject *impl, *tag;
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;
}
/* Magic for extension modules (built-in as well as dynamically
loaded). To prevent initializing an extension module more than
once, we keep a static dictionary 'extensions' keyed by module name
......@@ -1846,12 +1847,6 @@ imp_get_magic(PyObject *self, PyObject *noargs)
return imp_make_magic(pyc_magic);
}
static PyObject *
imp_get_tag(PyObject *self, PyObject *noargs)
{
return PyUnicode_FromString(pyc_tag);
}
static PyObject *
imp_extension_suffixes(PyObject *self, PyObject *noargs)
{
......@@ -2002,10 +1997,6 @@ PyDoc_STRVAR(doc_get_magic,
"get_magic() -> string\n\
Return the magic number for .pyc or .pyo files.");
PyDoc_STRVAR(doc_get_tag,
"get_tag() -> string\n\
Return the magic tag for .pyc or .pyo files.");
PyDoc_STRVAR(doc_extension_suffixes,
"extension_suffixes() -> list of strings\n\
Returns the list of file suffixes used to identify extension modules.");
......@@ -2029,7 +2020,6 @@ On platforms without threads, this function does nothing.");
static PyMethodDef imp_methods[] = {
{"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
{"get_tag", imp_get_tag, METH_NOARGS, doc_get_tag},
{"extension_suffixes", imp_extension_suffixes, METH_NOARGS,
doc_extension_suffixes},
{"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
......
This diff is collapsed.
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