0
0
mirror of https://github.com/vim/vim.git synced 2025-09-24 03:44:06 -04:00

updated for version 7.3.1174

Problem:    Python 2 and 3 use different ways to load modules.
Solution:   Use the same method. (ZyX)
This commit is contained in:
Bram Moolenaar
2013-06-12 14:41:04 +02:00
parent 27610ed76c
commit 81c40c507c
5 changed files with 213 additions and 325 deletions

View File

@@ -752,12 +752,6 @@ static PyObject *DictionaryGetattr(PyObject *, char*);
static PyObject *ListGetattr(PyObject *, char *);
static PyObject *FunctionGetattr(PyObject *, char *);
static PyObject *FinderFindModule(PyObject *, PyObject *);
static PyObject *VimPathHook(PyObject *, PyObject *);
static PyObject *py_find_module;
static PyObject *py_load_module;
#ifndef Py_VISIT
# define Py_VISIT(obj) visit(obj, arg)
#endif
@@ -1382,204 +1376,11 @@ python_tabpage_free(tabpage_T *tab)
}
#endif
static void
LoaderDestructor(LoaderObject *self)
{
Py_DECREF(self->module);
DESTRUCTOR_FINISH(self);
}
static PyObject *
LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
{
PyObject *r = self->module;
Py_INCREF(r);
return r;
}
static struct PyMethodDef LoaderMethods[] = {
/* name, function, calling, doc */
{"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
{ NULL, NULL, 0, NULL}
};
static PyObject *
call_load_module(char *name, int len, PyObject *find_module_result)
{
PyObject *fd, *pathname, *description;
if (!PyTuple_Check(find_module_result)
|| PyTuple_GET_SIZE(find_module_result) != 3)
{
PyErr_SetString(PyExc_TypeError,
_("expected 3-tuple as imp.find_module() result"));
return NULL;
}
if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
|| !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
|| !(description = PyTuple_GET_ITEM(find_module_result, 2)))
{
PyErr_SetString(PyExc_RuntimeError,
_("internal error: imp.find_module returned tuple with NULL"));
return NULL;
}
return PyObject_CallFunction(py_load_module,
"s#OOO", name, len, fd, pathname, description);
}
static PyObject *
find_module(char *fullname, char *tail, PyObject *new_path)
{
PyObject *find_module_result;
PyObject *module;
char *dot;
if ((dot = (char *) vim_strchr((char_u *) tail, '.')))
{
/*
* There is a dot in the name: call find_module recursively without the
* first component
*/
PyObject *newest_path;
int partlen = (int) (dot - 1 - tail);
if (!(find_module_result = PyObject_CallFunction(py_find_module,
"s#O", tail, partlen, new_path)))
return NULL;
if (!(module = call_load_module(
fullname,
((int) (tail - fullname)) + partlen,
find_module_result)))
{
Py_DECREF(find_module_result);
return NULL;
}
Py_DECREF(find_module_result);
if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
{
Py_DECREF(module);
return NULL;
}
Py_DECREF(module);
module = find_module(fullname, dot + 1, newest_path);
Py_DECREF(newest_path);
return module;
}
else
{
if (!(find_module_result = PyObject_CallFunction(py_find_module,
"sO", tail, new_path)))
return NULL;
if (!(module = call_load_module(
fullname,
STRLEN(fullname),
find_module_result)))
{
Py_DECREF(find_module_result);
return NULL;
}
Py_DECREF(find_module_result);
return module;
}
}
static PyObject *
FinderFindModule(PyObject *self, PyObject *args)
{
char *fullname;
PyObject *module;
PyObject *new_path;
LoaderObject *loader;
if (!PyArg_ParseTuple(args, "s", &fullname))
return NULL;
if (!(new_path = Vim_GetPaths(self)))
return NULL;
module = find_module(fullname, fullname, new_path);
Py_DECREF(new_path);
if (!module)
{
Py_INCREF(Py_None);
return Py_None;
}
if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
{
Py_DECREF(module);
return NULL;
}
loader->module = module;
return (PyObject *) loader;
}
static PyObject *
VimPathHook(PyObject *self UNUSED, PyObject *args)
{
char *path;
if (PyArg_ParseTuple(args, "s", &path)
&& STRCMP(path, vim_special_path) == 0)
{
Py_INCREF(vim_module);
return vim_module;
}
PyErr_Clear();
PyErr_SetNone(PyExc_ImportError);
return NULL;
}
static int
PythonMod_Init(void)
{
/* The special value is removed from sys.path in Python_Init(). */
static char *(argv[2]) = {"/must>not&exist/foo", NULL};
PyObject *imp;
if (!(imp = PyImport_ImportModule("imp")))
return -1;
if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
{
Py_DECREF(imp);
return -1;
}
if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
{
Py_DECREF(py_find_module);
Py_DECREF(imp);
return -1;
}
Py_DECREF(imp);
vim_memset(&LoaderType, 0, sizeof(LoaderType));
LoaderType.tp_name = "vim.Loader";
LoaderType.tp_basicsize = sizeof(LoaderObject);
LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
LoaderType.tp_doc = "vim message object";
LoaderType.tp_methods = LoaderMethods;
LoaderType.tp_dealloc = (destructor)LoaderDestructor;
if (init_types())
return -1;