0
0
mirror of https://github.com/vim/vim.git synced 2025-09-26 04:04:07 -04:00

patch 8.1.0247: Python: error message for failing import is incorrect

Problem:    Python: error message for failing import is incorrect.
Solution:   Adjust how modules are loaded. (Ozaki Kiichi, closes #3162)
This commit is contained in:
Bram Moolenaar
2018-08-07 19:45:27 +02:00
parent ee380ae376
commit 447bd5a346
4 changed files with 64 additions and 31 deletions

View File

@@ -544,27 +544,57 @@ PythonIO_Init_io(void)
} }
#if PY_VERSION_HEX < 0x030700f0 #if PY_VERSION_HEX < 0x030700f0
static PyObject *call_load_module(char *name, int len, PyObject *find_module_result);
typedef struct typedef struct
{ {
PyObject_HEAD PyObject_HEAD
PyObject *module; char *fullname;
PyObject *result;
} LoaderObject; } LoaderObject;
static PyTypeObject LoaderType; static PyTypeObject LoaderType;
static void static void
LoaderDestructor(LoaderObject *self) LoaderDestructor(LoaderObject *self)
{ {
Py_DECREF(self->module); vim_free(self->fullname);
Py_XDECREF(self->result);
DESTRUCTOR_FINISH(self); DESTRUCTOR_FINISH(self);
} }
static PyObject * static PyObject *
LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED) LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
{ {
PyObject *ret = self->module; char *fullname = self->fullname;
PyObject *result = self->result;
PyObject *module;
Py_INCREF(ret); if (!fullname)
return ret; {
module = result ? result : Py_None;
Py_INCREF(module);
return module;
}
module = call_load_module(fullname, (int)STRLEN(fullname), result);
self->fullname = NULL;
self->result = module;
vim_free(fullname);
Py_DECREF(result);
if (!module)
{
if (PyErr_Occurred())
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
Py_INCREF(module);
return module;
} }
static struct PyMethodDef LoaderMethods[] = { static struct PyMethodDef LoaderMethods[] = {
@@ -1252,7 +1282,11 @@ find_module(char *fullname, char *tail, PyObject *new_path)
if (!(find_module_result = PyObject_CallFunction(py_find_module, if (!(find_module_result = PyObject_CallFunction(py_find_module,
"s#O", tail, partlen, new_path))) "s#O", tail, partlen, new_path)))
{
if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
PyErr_Clear();
return NULL; return NULL;
}
if (!(module = call_load_module( if (!(module = call_load_module(
fullname, fullname,
@@ -1273,30 +1307,23 @@ find_module(char *fullname, char *tail, PyObject *new_path)
Py_DECREF(module); Py_DECREF(module);
module = find_module(fullname, dot + 1, newest_path); find_module_result = find_module(fullname, dot + 1, newest_path);
Py_DECREF(newest_path); Py_DECREF(newest_path);
return module; return find_module_result;
} }
else else
{ {
if (!(find_module_result = PyObject_CallFunction(py_find_module, if (!(find_module_result = PyObject_CallFunction(py_find_module,
"sO", tail, new_path))) "sO", tail, new_path)))
return NULL;
if (!(module = call_load_module(
fullname,
(int)STRLEN(fullname),
find_module_result)))
{ {
Py_DECREF(find_module_result); if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
PyErr_Clear();
return NULL; return NULL;
} }
Py_DECREF(find_module_result); return find_module_result;
return module;
} }
} }
@@ -1304,7 +1331,7 @@ find_module(char *fullname, char *tail, PyObject *new_path)
FinderFindModule(PyObject *self, PyObject *args) FinderFindModule(PyObject *self, PyObject *args)
{ {
char *fullname; char *fullname;
PyObject *module; PyObject *result;
PyObject *new_path; PyObject *new_path;
LoaderObject *loader; LoaderObject *loader;
@@ -1314,31 +1341,35 @@ FinderFindModule(PyObject *self, PyObject *args)
if (!(new_path = Vim_GetPaths(self))) if (!(new_path = Vim_GetPaths(self)))
return NULL; return NULL;
module = find_module(fullname, fullname, new_path); result = find_module(fullname, fullname, new_path);
Py_DECREF(new_path); Py_DECREF(new_path);
if (!module) if (!result)
{ {
if (PyErr_Occurred()) if (PyErr_Occurred())
{
if (PyErr_ExceptionMatches(PyExc_ImportError))
PyErr_Clear();
else
return NULL; return NULL;
}
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
if (!(loader = PyObject_NEW(LoaderObject, &LoaderType))) if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
{ {
Py_DECREF(module); Py_DECREF(result);
PyErr_NoMemory();
return NULL; return NULL;
} }
loader->module = module; if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
{
vim_free(fullname);
Py_DECREF(result);
return NULL;
}
loader->fullname = fullname;
loader->result = result;
return (PyObject *) loader; return (PyObject *) loader;
} }

View File

@@ -701,7 +701,7 @@ vim.foreach_rtp(FailingCall()):NotImplementedError:('call',)
vim.foreach_rtp(int, 2):TypeError:('foreach_rtp() takes exactly one argument (2 given)',) vim.foreach_rtp(int, 2):TypeError:('foreach_rtp() takes exactly one argument (2 given)',)
> import > import
import xxx_no_such_module_xxx:ImportError:('No module named xxx_no_such_module_xxx',) import xxx_no_such_module_xxx:ImportError:('No module named xxx_no_such_module_xxx',)
import failing_import:ImportError:('No module named failing_import',) import failing_import:ImportError:()
import failing:NotImplementedError:() import failing:NotImplementedError:()
> Options > Options
>> OptionsItem >> OptionsItem

View File

@@ -701,7 +701,7 @@ vim.foreach_rtp(FailingCall()):(<class 'NotImplementedError'>, NotImplementedErr
vim.foreach_rtp(int, 2):(<class 'TypeError'>, TypeError('foreach_rtp() takes exactly one argument (2 given)',)) vim.foreach_rtp(int, 2):(<class 'TypeError'>, TypeError('foreach_rtp() takes exactly one argument (2 given)',))
> import > import
import xxx_no_such_module_xxx:(<class 'ImportError'>, ImportError('No module named xxx_no_such_module_xxx',)) import xxx_no_such_module_xxx:(<class 'ImportError'>, ImportError('No module named xxx_no_such_module_xxx',))
import failing_import:(<class 'ImportError'>, ImportError('No module named failing_import',)) import failing_import:(<class 'ImportError'>, ImportError())
import failing:(<class 'NotImplementedError'>, NotImplementedError()) import failing:(<class 'NotImplementedError'>, NotImplementedError())
> Options > Options
>> OptionsItem >> OptionsItem

View File

@@ -794,6 +794,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
247,
/**/ /**/
246, 246,
/**/ /**/