comparison src/if_py_both.h @ 14373:380217380738 v8.1.0201

patch 8.1.0201: newer Python uses "importlib" instead of "imp" commit https://github.com/vim/vim/commit/79a494d5e2f97c10e74f92ea529552623c314422 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Jul 22 04:30:21 2018 +0200 patch 8.1.0201: newer Python uses "importlib" instead of "imp" Problem: Newer Python uses "importlib" instead of "imp". Solution: Use "importlib" for newer Python versions. (closes https://github.com/vim/vim/issues/3163)
author Christian Brabandt <cb@256bit.org>
date Sun, 22 Jul 2018 04:45:04 +0200
parents f761a55a8aed
children c15bef307de6
comparison
equal deleted inserted replaced
14372:2a4a2dc35c55 14373:380217380738
86 static PyObject *py_fchdir; 86 static PyObject *py_fchdir;
87 static PyObject *py_getcwd; 87 static PyObject *py_getcwd;
88 static PyObject *vim_module; 88 static PyObject *vim_module;
89 static PyObject *vim_special_path_object; 89 static PyObject *vim_special_path_object;
90 90
91 #if PY_VERSION_HEX >= 0x030700f0
92 static PyObject *py_find_spec;
93 #else
91 static PyObject *py_find_module; 94 static PyObject *py_find_module;
92 static PyObject *py_load_module; 95 static PyObject *py_load_module;
96 #endif
93 97
94 static PyObject *VimError; 98 static PyObject *VimError;
95 99
96 /* 100 /*
97 * obtain a lock on the Vim data structures 101 * obtain a lock on the Vim data structures
537 } 541 }
538 542
539 return 0; 543 return 0;
540 } 544 }
541 545
546 #if PY_VERSION_HEX < 0x030700f0
542 typedef struct 547 typedef struct
543 { 548 {
544 PyObject_HEAD 549 PyObject_HEAD
545 PyObject *module; 550 PyObject *module;
546 } LoaderObject; 551 } LoaderObject;
565 static struct PyMethodDef LoaderMethods[] = { 570 static struct PyMethodDef LoaderMethods[] = {
566 /* name, function, calling, doc */ 571 /* name, function, calling, doc */
567 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""}, 572 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
568 { NULL, NULL, 0, NULL} 573 { NULL, NULL, 0, NULL}
569 }; 574 };
575 #endif
570 576
571 /* Check to see whether a Vim error has been reported, or a keyboard 577 /* Check to see whether a Vim error has been reported, or a keyboard
572 * interrupt has been detected. 578 * interrupt has been detected.
573 */ 579 */
574 580
1161 } 1167 }
1162 1168
1163 return ret; 1169 return ret;
1164 } 1170 }
1165 1171
1172 #if PY_VERSION_HEX >= 0x030700f0
1173 static PyObject *
1174 FinderFindSpec(PyObject *self, PyObject *args)
1175 {
1176 char *fullname;
1177 PyObject *paths;
1178 PyObject *target = Py_None;
1179 PyObject *spec;
1180
1181 if (!PyArg_ParseTuple(args, "s|O", &fullname, &target))
1182 return NULL;
1183
1184 if (!(paths = Vim_GetPaths(self)))
1185 return NULL;
1186
1187 spec = PyObject_CallFunction(py_find_spec, "sNN", fullname, paths, target);
1188
1189 Py_DECREF(paths);
1190
1191 if (!spec)
1192 {
1193 if (PyErr_Occurred())
1194 return NULL;
1195
1196 Py_INCREF(Py_None);
1197 return Py_None;
1198 }
1199
1200 return spec;
1201 }
1202 #else
1166 static PyObject * 1203 static PyObject *
1167 call_load_module(char *name, int len, PyObject *find_module_result) 1204 call_load_module(char *name, int len, PyObject *find_module_result)
1168 { 1205 {
1169 PyObject *fd, *pathname, *description; 1206 PyObject *fd, *pathname, *description;
1170 1207
1303 1340
1304 loader->module = module; 1341 loader->module = module;
1305 1342
1306 return (PyObject *) loader; 1343 return (PyObject *) loader;
1307 } 1344 }
1345 #endif
1308 1346
1309 static PyObject * 1347 static PyObject *
1310 VimPathHook(PyObject *self UNUSED, PyObject *args) 1348 VimPathHook(PyObject *self UNUSED, PyObject *args)
1311 { 1349 {
1312 char *path; 1350 char *path;
1334 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"}, 1372 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1335 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"}, 1373 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
1336 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"}, 1374 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1337 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"}, 1375 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1338 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"}, 1376 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
1377 #if PY_VERSION_HEX >= 0x030700f0
1378 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
1379 #else
1339 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"}, 1380 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
1381 #endif
1340 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"}, 1382 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1341 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"}, 1383 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1342 { NULL, NULL, 0, NULL} 1384 { NULL, NULL, 0, NULL}
1343 }; 1385 };
1344 1386
6543 OptionsType.tp_as_mapping = &OptionsAsMapping; 6585 OptionsType.tp_as_mapping = &OptionsAsMapping;
6544 OptionsType.tp_dealloc = (destructor)OptionsDestructor; 6586 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6545 OptionsType.tp_traverse = (traverseproc)OptionsTraverse; 6587 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6546 OptionsType.tp_clear = (inquiry)OptionsClear; 6588 OptionsType.tp_clear = (inquiry)OptionsClear;
6547 6589
6590 #if PY_VERSION_HEX < 0x030700f0
6548 vim_memset(&LoaderType, 0, sizeof(LoaderType)); 6591 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6549 LoaderType.tp_name = "vim.Loader"; 6592 LoaderType.tp_name = "vim.Loader";
6550 LoaderType.tp_basicsize = sizeof(LoaderObject); 6593 LoaderType.tp_basicsize = sizeof(LoaderObject);
6551 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT; 6594 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6552 LoaderType.tp_doc = "vim message object"; 6595 LoaderType.tp_doc = "vim message object";
6553 LoaderType.tp_methods = LoaderMethods; 6596 LoaderType.tp_methods = LoaderMethods;
6554 LoaderType.tp_dealloc = (destructor)LoaderDestructor; 6597 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6598 #endif
6555 6599
6556 #if PY_MAJOR_VERSION >= 3 6600 #if PY_MAJOR_VERSION >= 3
6557 vim_memset(&vimmodule, 0, sizeof(vimmodule)); 6601 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6558 vimmodule.m_name = "vim"; 6602 vimmodule.m_name = "vim";
6559 vimmodule.m_doc = "Vim Python interface\n"; 6603 vimmodule.m_doc = "Vim Python interface\n";
6581 PYTYPE_READY(DictionaryType); 6625 PYTYPE_READY(DictionaryType);
6582 PYTYPE_READY(ListType); 6626 PYTYPE_READY(ListType);
6583 PYTYPE_READY(FunctionType); 6627 PYTYPE_READY(FunctionType);
6584 PYTYPE_READY(OptionsType); 6628 PYTYPE_READY(OptionsType);
6585 PYTYPE_READY(OutputType); 6629 PYTYPE_READY(OutputType);
6630 #if PY_VERSION_HEX < 0x030700f0
6586 PYTYPE_READY(LoaderType); 6631 PYTYPE_READY(LoaderType);
6632 #endif
6587 return 0; 6633 return 0;
6588 } 6634 }
6589 6635
6590 static int 6636 static int
6591 init_sys_path(void) 6637 init_sys_path(void)
6705 {"TabPage", (PyObject *)&TabPageType}, 6751 {"TabPage", (PyObject *)&TabPageType},
6706 {"Dictionary", (PyObject *)&DictionaryType}, 6752 {"Dictionary", (PyObject *)&DictionaryType},
6707 {"List", (PyObject *)&ListType}, 6753 {"List", (PyObject *)&ListType},
6708 {"Function", (PyObject *)&FunctionType}, 6754 {"Function", (PyObject *)&FunctionType},
6709 {"Options", (PyObject *)&OptionsType}, 6755 {"Options", (PyObject *)&OptionsType},
6756 #if PY_VERSION_HEX < 0x030700f0
6710 {"_Loader", (PyObject *)&LoaderType}, 6757 {"_Loader", (PyObject *)&LoaderType},
6758 #endif
6711 }; 6759 };
6712 6760
6713 #define ADD_OBJECT(m, name, obj) \ 6761 #define ADD_OBJECT(m, name, obj) \
6714 if (PyModule_AddObject(m, name, obj)) \ 6762 if (PyModule_AddObject(m, name, obj)) \
6715 return -1; 6763 return -1;
6727 { 6775 {
6728 int i; 6776 int i;
6729 PyObject *other_module; 6777 PyObject *other_module;
6730 PyObject *attr; 6778 PyObject *attr;
6731 PyObject *imp; 6779 PyObject *imp;
6780 #if PY_VERSION_HEX >= 0x030700f0
6781 PyObject *dict;
6782 PyObject *cls;
6783 #endif
6732 6784
6733 for (i = 0; i < (int)(sizeof(numeric_constants) 6785 for (i = 0; i < (int)(sizeof(numeric_constants)
6734 / sizeof(struct numeric_constant)); 6786 / sizeof(struct numeric_constant));
6735 ++i) 6787 ++i)
6736 ADD_CHECKED_OBJECT(m, numeric_constants[i].name, 6788 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
6799 if (!(vim_special_path_object = PyString_FromString(vim_special_path))) 6851 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6800 return -1; 6852 return -1;
6801 6853
6802 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object); 6854 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6803 6855
6856 #if PY_VERSION_HEX >= 0x030700f0
6857 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6858 return -1;
6859
6860 dict = PyModule_GetDict(imp);
6861
6862 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6863 {
6864 Py_DECREF(imp);
6865 return -1;
6866 }
6867
6868 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6869 {
6870 Py_DECREF(imp);
6871 return -1;
6872 }
6873
6874 Py_DECREF(imp);
6875
6876 ADD_OBJECT(m, "_find_spec", py_find_spec);
6877 #else
6804 if (!(imp = PyImport_ImportModule("imp"))) 6878 if (!(imp = PyImport_ImportModule("imp")))
6805 return -1; 6879 return -1;
6806 6880
6807 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module"))) 6881 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6808 { 6882 {
6819 6893
6820 Py_DECREF(imp); 6894 Py_DECREF(imp);
6821 6895
6822 ADD_OBJECT(m, "_find_module", py_find_module); 6896 ADD_OBJECT(m, "_find_module", py_find_module);
6823 ADD_OBJECT(m, "_load_module", py_load_module); 6897 ADD_OBJECT(m, "_load_module", py_load_module);
6898 #endif
6824 6899
6825 return 0; 6900 return 0;
6826 } 6901 }