Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / third_party / cython / src / Cython / Utility / ImportExport.c
blob60d7bf60e7ed4688912d93e7c9a2d2c84711771e
1 /////////////// PyIdentifierFromString.proto ///////////////
3 #if !defined(__Pyx_PyIdentifier_FromString)
4 #if PY_MAJOR_VERSION < 3
5 #define __Pyx_PyIdentifier_FromString(s) PyString_FromString(s)
6 #else
7 #define __Pyx_PyIdentifier_FromString(s) PyUnicode_FromString(s)
8 #endif
9 #endif
12 /////////////// Import.proto ///////////////
14 static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level); /*proto*/
16 /////////////// Import ///////////////
17 //@requires: ObjectHandling.c::PyObjectGetAttrStr
18 //@substitute: naming
20 static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level) {
21 PyObject *empty_list = 0;
22 PyObject *module = 0;
23 PyObject *global_dict = 0;
24 PyObject *empty_dict = 0;
25 PyObject *list;
26 #if PY_VERSION_HEX < 0x03030000
27 PyObject *py_import;
28 py_import = __Pyx_PyObject_GetAttrStr($builtins_cname, PYIDENT("__import__"));
29 if (!py_import)
30 goto bad;
31 #endif
32 if (from_list)
33 list = from_list;
34 else {
35 empty_list = PyList_New(0);
36 if (!empty_list)
37 goto bad;
38 list = empty_list;
40 global_dict = PyModule_GetDict($module_cname);
41 if (!global_dict)
42 goto bad;
43 empty_dict = PyDict_New();
44 if (!empty_dict)
45 goto bad;
46 #if PY_VERSION_HEX >= 0x02050000
48 #if PY_MAJOR_VERSION >= 3
49 if (level == -1) {
50 if (strchr(__Pyx_MODULE_NAME, '.')) {
51 /* try package relative import first */
52 #if PY_VERSION_HEX < 0x03030000
53 PyObject *py_level = PyInt_FromLong(1);
54 if (!py_level)
55 goto bad;
56 module = PyObject_CallFunctionObjArgs(py_import,
57 name, global_dict, empty_dict, list, py_level, NULL);
58 Py_DECREF(py_level);
59 #else
60 module = PyImport_ImportModuleLevelObject(
61 name, global_dict, empty_dict, list, 1);
62 #endif
63 if (!module) {
64 if (!PyErr_ExceptionMatches(PyExc_ImportError))
65 goto bad;
66 PyErr_Clear();
69 level = 0; /* try absolute import on failure */
71 #endif
72 if (!module) {
73 #if PY_VERSION_HEX < 0x03030000
74 PyObject *py_level = PyInt_FromLong(level);
75 if (!py_level)
76 goto bad;
77 module = PyObject_CallFunctionObjArgs(py_import,
78 name, global_dict, empty_dict, list, py_level, NULL);
79 Py_DECREF(py_level);
80 #else
81 module = PyImport_ImportModuleLevelObject(
82 name, global_dict, empty_dict, list, level);
83 #endif
86 #else
87 if (level>0) {
88 PyErr_SetString(PyExc_RuntimeError, "Relative import is not supported for Python <=2.4.");
89 goto bad;
91 module = PyObject_CallFunctionObjArgs(py_import,
92 name, global_dict, empty_dict, list, NULL);
93 #endif
94 bad:
95 #if PY_VERSION_HEX < 0x03030000
96 Py_XDECREF(py_import);
97 #endif
98 Py_XDECREF(empty_list);
99 Py_XDECREF(empty_dict);
100 return module;
104 /////////////// ImportFrom.proto ///////////////
106 static PyObject* __Pyx_ImportFrom(PyObject* module, PyObject* name); /*proto*/
108 /////////////// ImportFrom ///////////////
109 //@requires: ObjectHandling.c::PyObjectGetAttrStr
111 static PyObject* __Pyx_ImportFrom(PyObject* module, PyObject* name) {
112 PyObject* value = __Pyx_PyObject_GetAttrStr(module, name);
113 if (unlikely(!value) && PyErr_ExceptionMatches(PyExc_AttributeError)) {
114 PyErr_Format(PyExc_ImportError,
115 #if PY_MAJOR_VERSION < 3
116 "cannot import name %.230s", PyString_AS_STRING(name));
117 #else
118 "cannot import name %S", name);
119 #endif
121 return value;
125 /////////////// ModuleImport.proto ///////////////
127 static PyObject *__Pyx_ImportModule(const char *name); /*proto*/
129 /////////////// ModuleImport ///////////////
130 //@requires: PyIdentifierFromString
132 #ifndef __PYX_HAVE_RT_ImportModule
133 #define __PYX_HAVE_RT_ImportModule
134 static PyObject *__Pyx_ImportModule(const char *name) {
135 PyObject *py_name = 0;
136 PyObject *py_module = 0;
138 py_name = __Pyx_PyIdentifier_FromString(name);
139 if (!py_name)
140 goto bad;
141 py_module = PyImport_Import(py_name);
142 Py_DECREF(py_name);
143 return py_module;
144 bad:
145 Py_XDECREF(py_name);
146 return 0;
148 #endif
151 /////////////// SetPackagePathFromImportLib.proto ///////////////
153 #if PY_VERSION_HEX >= 0x03030000
154 static int __Pyx_SetPackagePathFromImportLib(const char* parent_package_name, PyObject *module_name);
155 #else
156 #define __Pyx_SetPackagePathFromImportLib(a, b) 0
157 #endif
159 /////////////// SetPackagePathFromImportLib ///////////////
160 //@requires: ObjectHandling.c::PyObjectGetAttrStr
161 //@substitute: naming
163 #if PY_VERSION_HEX >= 0x03030000
164 static int __Pyx_SetPackagePathFromImportLib(const char* parent_package_name, PyObject *module_name) {
165 PyObject *importlib, *loader, *osmod, *ossep, *parts, *package_path;
166 PyObject *path = NULL, *file_path = NULL;
167 int result;
168 if (parent_package_name) {
169 PyObject *package = PyImport_ImportModule(parent_package_name);
170 if (unlikely(!package))
171 goto bad;
172 path = PyObject_GetAttrString(package, "__path__");
173 Py_DECREF(package);
174 if (unlikely(!path) || unlikely(path == Py_None))
175 goto bad;
176 } else {
177 path = Py_None; Py_INCREF(Py_None);
179 // package_path = [importlib.find_loader(module_name, path).path.rsplit(os.sep, 1)[0]]
180 importlib = PyImport_ImportModule("importlib");
181 if (unlikely(!importlib))
182 goto bad;
183 loader = PyObject_CallMethod(importlib, "find_loader", "(OO)", module_name, path);
184 Py_DECREF(importlib);
185 Py_DECREF(path); path = NULL;
186 if (unlikely(!loader))
187 goto bad;
188 file_path = PyObject_GetAttrString(loader, "path");
189 Py_DECREF(loader);
190 if (unlikely(!file_path))
191 goto bad;
193 if (unlikely(__Pyx_SetAttrString($module_cname, "__file__", file_path) < 0))
194 goto bad;
196 osmod = PyImport_ImportModule("os");
197 if (unlikely(!osmod))
198 goto bad;
199 ossep = PyObject_GetAttrString(osmod, "sep");
200 Py_DECREF(osmod);
201 if (unlikely(!ossep))
202 goto bad;
203 parts = PyObject_CallMethod(file_path, "rsplit", "(Oi)", ossep, 1);
204 Py_DECREF(file_path); file_path = NULL;
205 Py_DECREF(ossep);
206 if (unlikely(!parts))
207 goto bad;
208 package_path = Py_BuildValue("[O]", PyList_GET_ITEM(parts, 0));
209 Py_DECREF(parts);
210 if (unlikely(!package_path))
211 goto bad;
212 goto set_path;
214 bad:
215 PyErr_WriteUnraisable(module_name);
216 Py_XDECREF(path);
217 Py_XDECREF(file_path);
219 // set an empty path list on failure
220 PyErr_Clear();
221 package_path = PyList_New(0);
222 if (unlikely(!package_path))
223 return -1;
225 set_path:
226 result = __Pyx_SetAttrString($module_cname, "__path__", package_path);
227 Py_DECREF(package_path);
228 return result;
230 #endif
233 /////////////// TypeImport.proto ///////////////
235 static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name, size_t size, int strict); /*proto*/
237 /////////////// TypeImport ///////////////
238 //@requires: PyIdentifierFromString
239 //@requires: ModuleImport
241 #ifndef __PYX_HAVE_RT_ImportType
242 #define __PYX_HAVE_RT_ImportType
243 static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name,
244 size_t size, int strict)
246 PyObject *py_module = 0;
247 PyObject *result = 0;
248 PyObject *py_name = 0;
249 char warning[200];
250 Py_ssize_t basicsize;
251 #ifdef Py_LIMITED_API
252 PyObject *py_basicsize;
253 #endif
255 py_module = __Pyx_ImportModule(module_name);
256 if (!py_module)
257 goto bad;
258 py_name = __Pyx_PyIdentifier_FromString(class_name);
259 if (!py_name)
260 goto bad;
261 result = PyObject_GetAttr(py_module, py_name);
262 Py_DECREF(py_name);
263 py_name = 0;
264 Py_DECREF(py_module);
265 py_module = 0;
266 if (!result)
267 goto bad;
268 if (!PyType_Check(result)) {
269 PyErr_Format(PyExc_TypeError,
270 "%.200s.%.200s is not a type object",
271 module_name, class_name);
272 goto bad;
274 #ifndef Py_LIMITED_API
275 basicsize = ((PyTypeObject *)result)->tp_basicsize;
276 #else
277 py_basicsize = PyObject_GetAttrString(result, "__basicsize__");
278 if (!py_basicsize)
279 goto bad;
280 basicsize = PyLong_AsSsize_t(py_basicsize);
281 Py_DECREF(py_basicsize);
282 py_basicsize = 0;
283 if (basicsize == (Py_ssize_t)-1 && PyErr_Occurred())
284 goto bad;
285 #endif
286 if (!strict && (size_t)basicsize > size) {
287 PyOS_snprintf(warning, sizeof(warning),
288 "%s.%s size changed, may indicate binary incompatibility",
289 module_name, class_name);
290 #if PY_VERSION_HEX < 0x02050000
291 if (PyErr_Warn(NULL, warning) < 0) goto bad;
292 #else
293 if (PyErr_WarnEx(NULL, warning, 0) < 0) goto bad;
294 #endif
296 else if ((size_t)basicsize != size) {
297 PyErr_Format(PyExc_ValueError,
298 "%.200s.%.200s has the wrong size, try recompiling",
299 module_name, class_name);
300 goto bad;
302 return (PyTypeObject *)result;
303 bad:
304 Py_XDECREF(py_module);
305 Py_XDECREF(result);
306 return NULL;
308 #endif
310 /////////////// FunctionImport.proto ///////////////
312 static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig); /*proto*/
314 /////////////// FunctionImport ///////////////
315 //@substitute: naming
317 #ifndef __PYX_HAVE_RT_ImportFunction
318 #define __PYX_HAVE_RT_ImportFunction
319 static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig) {
320 PyObject *d = 0;
321 PyObject *cobj = 0;
322 union {
323 void (*fp)(void);
324 void *p;
325 } tmp;
327 d = PyObject_GetAttrString(module, (char *)"$api_name");
328 if (!d)
329 goto bad;
330 cobj = PyDict_GetItemString(d, funcname);
331 if (!cobj) {
332 PyErr_Format(PyExc_ImportError,
333 "%.200s does not export expected C function %.200s",
334 PyModule_GetName(module), funcname);
335 goto bad;
337 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3 && PY_MINOR_VERSION==0)
338 if (!PyCapsule_IsValid(cobj, sig)) {
339 PyErr_Format(PyExc_TypeError,
340 "C function %.200s.%.200s has wrong signature (expected %.500s, got %.500s)",
341 PyModule_GetName(module), funcname, sig, PyCapsule_GetName(cobj));
342 goto bad;
344 tmp.p = PyCapsule_GetPointer(cobj, sig);
345 #else
346 {const char *desc, *s1, *s2;
347 desc = (const char *)PyCObject_GetDesc(cobj);
348 if (!desc)
349 goto bad;
350 s1 = desc; s2 = sig;
351 while (*s1 != '\0' && *s1 == *s2) { s1++; s2++; }
352 if (*s1 != *s2) {
353 PyErr_Format(PyExc_TypeError,
354 "C function %.200s.%.200s has wrong signature (expected %.500s, got %.500s)",
355 PyModule_GetName(module), funcname, sig, desc);
356 goto bad;
358 tmp.p = PyCObject_AsVoidPtr(cobj);}
359 #endif
360 *f = tmp.fp;
361 if (!(*f))
362 goto bad;
363 Py_DECREF(d);
364 return 0;
365 bad:
366 Py_XDECREF(d);
367 return -1;
369 #endif
371 /////////////// FunctionExport.proto ///////////////
373 static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig); /*proto*/
375 /////////////// FunctionExport ///////////////
376 //@substitute: naming
378 static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig) {
379 PyObject *d = 0;
380 PyObject *cobj = 0;
381 union {
382 void (*fp)(void);
383 void *p;
384 } tmp;
386 d = PyObject_GetAttrString($module_cname, (char *)"$api_name");
387 if (!d) {
388 PyErr_Clear();
389 d = PyDict_New();
390 if (!d)
391 goto bad;
392 Py_INCREF(d);
393 if (PyModule_AddObject($module_cname, (char *)"$api_name", d) < 0)
394 goto bad;
396 tmp.fp = f;
397 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
398 cobj = PyCapsule_New(tmp.p, sig, 0);
399 #else
400 cobj = PyCObject_FromVoidPtrAndDesc(tmp.p, (void *)sig, 0);
401 #endif
402 if (!cobj)
403 goto bad;
404 if (PyDict_SetItemString(d, name, cobj) < 0)
405 goto bad;
406 Py_DECREF(cobj);
407 Py_DECREF(d);
408 return 0;
409 bad:
410 Py_XDECREF(cobj);
411 Py_XDECREF(d);
412 return -1;
415 /////////////// VoidPtrImport.proto ///////////////
417 static int __Pyx_ImportVoidPtr(PyObject *module, const char *name, void **p, const char *sig); /*proto*/
419 /////////////// VoidPtrImport ///////////////
420 //@substitute: naming
422 #ifndef __PYX_HAVE_RT_ImportVoidPtr
423 #define __PYX_HAVE_RT_ImportVoidPtr
424 static int __Pyx_ImportVoidPtr(PyObject *module, const char *name, void **p, const char *sig) {
425 PyObject *d = 0;
426 PyObject *cobj = 0;
428 d = PyObject_GetAttrString(module, (char *)"$api_name");
429 if (!d)
430 goto bad;
431 cobj = PyDict_GetItemString(d, name);
432 if (!cobj) {
433 PyErr_Format(PyExc_ImportError,
434 "%.200s does not export expected C variable %.200s",
435 PyModule_GetName(module), name);
436 goto bad;
438 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3 && PY_MINOR_VERSION==0)
439 if (!PyCapsule_IsValid(cobj, sig)) {
440 PyErr_Format(PyExc_TypeError,
441 "C variable %.200s.%.200s has wrong signature (expected %.500s, got %.500s)",
442 PyModule_GetName(module), name, sig, PyCapsule_GetName(cobj));
443 goto bad;
445 *p = PyCapsule_GetPointer(cobj, sig);
446 #else
447 {const char *desc, *s1, *s2;
448 desc = (const char *)PyCObject_GetDesc(cobj);
449 if (!desc)
450 goto bad;
451 s1 = desc; s2 = sig;
452 while (*s1 != '\0' && *s1 == *s2) { s1++; s2++; }
453 if (*s1 != *s2) {
454 PyErr_Format(PyExc_TypeError,
455 "C variable %.200s.%.200s has wrong signature (expected %.500s, got %.500s)",
456 PyModule_GetName(module), name, sig, desc);
457 goto bad;
459 *p = PyCObject_AsVoidPtr(cobj);}
460 #endif
461 if (!(*p))
462 goto bad;
463 Py_DECREF(d);
464 return 0;
465 bad:
466 Py_XDECREF(d);
467 return -1;
469 #endif
471 /////////////// VoidPtrExport.proto ///////////////
473 static int __Pyx_ExportVoidPtr(PyObject *name, void *p, const char *sig); /*proto*/
475 /////////////// VoidPtrExport ///////////////
476 //@substitute: naming
477 //@requires: ObjectHandling.c::PyObjectSetAttrStr
479 static int __Pyx_ExportVoidPtr(PyObject *name, void *p, const char *sig) {
480 PyObject *d;
481 PyObject *cobj = 0;
483 d = PyDict_GetItem($moddict_cname, PYIDENT("$api_name"));
484 Py_XINCREF(d);
485 if (!d) {
486 d = PyDict_New();
487 if (!d)
488 goto bad;
489 if (__Pyx_PyObject_SetAttrStr($module_cname, PYIDENT("$api_name"), d) < 0)
490 goto bad;
492 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3 && PY_MINOR_VERSION==0)
493 cobj = PyCapsule_New(p, sig, 0);
494 #else
495 cobj = PyCObject_FromVoidPtrAndDesc(p, (void *)sig, 0);
496 #endif
497 if (!cobj)
498 goto bad;
499 if (PyDict_SetItem(d, name, cobj) < 0)
500 goto bad;
501 Py_DECREF(cobj);
502 Py_DECREF(d);
503 return 0;
504 bad:
505 Py_XDECREF(cobj);
506 Py_XDECREF(d);
507 return -1;
511 /////////////// SetVTable.proto ///////////////
513 static int __Pyx_SetVtable(PyObject *dict, void *vtable); /*proto*/
515 /////////////// SetVTable ///////////////
517 static int __Pyx_SetVtable(PyObject *dict, void *vtable) {
518 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
519 PyObject *ob = PyCapsule_New(vtable, 0, 0);
520 #else
521 PyObject *ob = PyCObject_FromVoidPtr(vtable, 0);
522 #endif
523 if (!ob)
524 goto bad;
525 if (PyDict_SetItem(dict, PYIDENT("__pyx_vtable__"), ob) < 0)
526 goto bad;
527 Py_DECREF(ob);
528 return 0;
529 bad:
530 Py_XDECREF(ob);
531 return -1;
535 /////////////// GetVTable.proto ///////////////
537 static void* __Pyx_GetVtable(PyObject *dict); /*proto*/
539 /////////////// GetVTable ///////////////
541 static void* __Pyx_GetVtable(PyObject *dict) {
542 void* ptr;
543 PyObject *ob = PyObject_GetItem(dict, PYIDENT("__pyx_vtable__"));
544 if (!ob)
545 goto bad;
546 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
547 ptr = PyCapsule_GetPointer(ob, 0);
548 #else
549 ptr = PyCObject_AsVoidPtr(ob);
550 #endif
551 if (!ptr && !PyErr_Occurred())
552 PyErr_SetString(PyExc_RuntimeError, "invalid vtable found for imported type");
553 Py_DECREF(ob);
554 return ptr;
555 bad:
556 Py_XDECREF(ob);
557 return NULL;