comparing with /home/esnow/projects/.external/.cpython searching for changes changeset: 76512:be32afe39701 tag: issue13959_tag.diff tag: qbase tag: qtip tag: tip user: Eric Snow date: Tue Apr 24 05:33:01 2012 +0000 summary: [issue 13959] port TAG to importlib diff --git a/Lib/imp.py b/Lib/imp.py --- a/Lib/imp.py +++ b/Lib/imp.py @@ -11,7 +11,7 @@ init_builtin, init_frozen, is_builtin, is_frozen, _fix_co_filename) # Could move out of _imp, but not worth the code -from _imp import get_magic, get_tag +from _imp import get_magic # Can (probably) move to importlib from _imp import get_suffixes # Should be re-implemented here (and mostly deprecated) @@ -194,3 +194,8 @@ encoding = tokenize.detect_encoding(file.readline)[0] file = open(file_path, mode, encoding=encoding) return file, file_path, (suffix, mode, type_) + + +def get_tag(): + """Return the magic tag for .pyc or .pyo files.""" + return _bootstrap._TAG diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -175,6 +175,32 @@ code_type = type(_wrap.__code__) +# legacy imp interface support ################################################ + +# _TAG must change for each major Python release. The magic number +# will take care of any bytecode changes that occur during development. +# (See PEP 3147.) +def _get_tag(): + # first find the Python implementation + # XXX is there a better way? (sys.implementation.name?) + # see http://mail.python.org/pipermail/python-ideas/2012-March/014555.html + if sys.version.startswith("IronPython"): + impl = 'ironpython' + elif sys.version.startswith("java"): + impl = 'jython' + elif "PyPy" in sys.version: + impl = 'pypy' + else: + impl = 'cpython' + + #return "{}-{}{}".format(sys.implementation.name, *sys.version_info) + return "{}-{}{}".format(impl, *sys.version_info) + +# moved to _setup() due to reliance on sys +#_TAG = _get_tag() +_TAG = None + + def _new_module(name): """Create a new module. @@ -1140,7 +1166,6 @@ _MAGIC_NUMBER = None # Set in _setup() -_TAG = None # Set in _setup() def _setup(sys_module, _imp_module): @@ -1192,7 +1217,7 @@ # 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', _get_tag()) if builtin_os == 'nt': SOURCE_SUFFIXES.append('.pyw') diff --git a/Python/import.c b/Python/import.c --- a/Python/import.c +++ b/Python/import.c @@ -111,23 +111,11 @@ /* 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 (3220 | ((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; @@ -595,7 +583,11 @@ const char * PyImport_GetMagicTag(void) { - return pyc_tag; + PyInterpreterState *interp = PyThreadState_Get()->interp; + PyObject *pyc_tag = PyObject_GetAttrString(interp->importlib, "_TAG"); + if (pyc_tag == NULL) + return NULL; + return _PyUnicode_AsString(pyc_tag); } /* Magic for extension modules (built-in as well as dynamically @@ -1989,12 +1981,6 @@ } static PyObject * -imp_get_tag(PyObject *self, PyObject *noargs) -{ - return PyUnicode_FromString(pyc_tag); -} - -static PyObject * imp_get_suffixes(PyObject *self, PyObject *noargs) { PyObject *list; @@ -2192,10 +2178,6 @@ "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_get_suffixes, "get_suffixes() -> [(suffix, mode, type), ...]\n\ Return a list of (suffix, mode, type) tuples describing the files\n\ @@ -2220,7 +2202,6 @@ static PyMethodDef imp_methods[] = { {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic}, - {"get_tag", imp_get_tag, METH_NOARGS, doc_get_tag}, {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes}, {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held}, {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},