annotate Python/importdl.c @ 69110:110bb604bc2f

#11282: merge with 3.2.
author Ezio Melotti
date Sun, 03 Apr 2011 17:39:19 +0300
parents ef2b6305d395
children 1b7f484bab6e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Support for dynamic loading of extension modules */
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5314
diff changeset
4 #include "Python.h"
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11600
diff changeset
5
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11600
diff changeset
6 /* ./configure sets HAVE_DYNAMIC_LOADING if dynamic loading of modules is
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11600
diff changeset
7 supported on this platform. configure will then compile and link in one
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11600
diff changeset
8 of the dynload_*.c files, as appropriate. We will call a function in
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11600
diff changeset
9 those modules to get a function pointer to the module's init function.
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11600
diff changeset
10 */
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11600
diff changeset
11 #ifdef HAVE_DYNAMIC_LOADING
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11600
diff changeset
12
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 #include "importdl.h"
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
68108
0874698b1201 Issue #3080: Remove unused argument of _PyImport_GetDynLoadFunc()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65418
diff changeset
15 extern dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
16 const char *pathname, FILE *fp);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
68716
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
18 /* name should be ASCII only because the C language doesn't accept non-ASCII
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
19 identifiers, and dynamic modules are written in C. */
11600
14561e8395bb Moved most of the platform-specific code to dynload_<platform>.c files.
Guido van Rossum <guido@python.org>
parents: 11469
diff changeset
20
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5314
diff changeset
21 PyObject *
68716
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
22 _PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
24 PyObject *m;
68716
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
25 PyObject *pathbytes;
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
26 char *namestr, *lastdot, *shortname, *packagecontext, *oldcontext;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
27 dl_funcptr p0;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
28 PyObject* (*p)(void);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
29 struct PyModuleDef *def;
11600
14561e8395bb Moved most of the platform-specific code to dynload_<platform>.c files.
Guido van Rossum <guido@python.org>
parents: 11469
diff changeset
30
68716
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
31 namestr = _PyUnicode_AsString(name);
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
32 if (namestr == NULL)
65418
fd5135cbd928 _PyImport_FixupExtension() and _PyImport_FindExtension() uses FS encoding
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61466
diff changeset
33 return NULL;
fd5135cbd928 _PyImport_FixupExtension() and _PyImport_FindExtension() uses FS encoding
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61466
diff changeset
34
68716
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
35 m = _PyImport_FindExtensionObject(name, path);
68712
2425717c6430 Issue #3080: Import builtins using Unicode strings
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68108
diff changeset
36 if (m != NULL) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
37 Py_INCREF(m);
68716
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
38 return m;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
39 }
68716
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
40
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
41 lastdot = strrchr(namestr, '.');
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
42 if (lastdot == NULL) {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
43 packagecontext = NULL;
68716
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
44 shortname = namestr;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
45 }
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
46 else {
68716
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
47 packagecontext = namestr;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
48 shortname = lastdot+1;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
49 }
3907
ac1d5abfd78e Changes for AIX sharedlibs.
Guido van Rossum <guido@python.org>
parents: 3898
diff changeset
50
68716
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
51 pathbytes = PyUnicode_EncodeFSDefault(path);
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
52 if (pathbytes == NULL)
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
53 return NULL;
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
54 p0 = _PyImport_GetDynLoadFunc(shortname,
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
55 PyBytes_AS_STRING(pathbytes), fp);
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
56 Py_DECREF(pathbytes);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
57 p = (PyObject*(*)(void))p0;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
58 if (PyErr_Occurred())
68716
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
59 return NULL;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
60 if (p == NULL) {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
61 PyErr_Format(PyExc_ImportError,
68716
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
62 "dynamic module does not define init function"
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
63 " (PyInit_%s)",
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
64 shortname);
68716
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
65 return NULL;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
66 }
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
67 oldcontext = _Py_PackageContext;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
68 _Py_PackageContext = packagecontext;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
69 m = (*p)();
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
70 _Py_PackageContext = oldcontext;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
71 if (m == NULL)
68716
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
72 return NULL;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
74 if (PyErr_Occurred()) {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
75 Py_DECREF(m);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
76 PyErr_Format(PyExc_SystemError,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
77 "initialization of %s raised unreported exception",
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
78 shortname);
68716
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
79 return NULL;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
80 }
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
81
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
82 /* Remember pointer to module init function. */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
83 def = PyModule_GetDef(m);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
84 def->m_base.m_init = p;
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
85
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
86 /* Remember the filename as the __file__ attribute */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
87 if (PyModule_AddObject(m, "__file__", path) < 0)
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
88 PyErr_Clear(); /* Not important enough to report */
65418
fd5135cbd928 _PyImport_FixupExtension() and _PyImport_FindExtension() uses FS encoding
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61466
diff changeset
89 else
fd5135cbd928 _PyImport_FixupExtension() and _PyImport_FindExtension() uses FS encoding
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61466
diff changeset
90 Py_INCREF(path);
30316
19611d101d23 Patch #794826: Add __file__ in dynamically loaded modules for multiple
Martin v. Löwis <martin@v.loewis.de>
parents: 25040
diff changeset
91
68716
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
92 if (_PyImport_FixupExtensionObject(m, name, path) < 0)
68712
2425717c6430 Issue #3080: Import builtins using Unicode strings
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68108
diff changeset
93 return NULL;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47580
diff changeset
94 if (Py_VerboseFlag)
68712
2425717c6430 Issue #3080: Import builtins using Unicode strings
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68108
diff changeset
95 PySys_FormatStderr(
68716
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
96 "import %U # dynamically loaded from %R\n",
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
97 name, path);
ef2b6305d395 Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68712
diff changeset
98 return m;
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11600
diff changeset
99 }
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11600
diff changeset
100
11600
14561e8395bb Moved most of the platform-specific code to dynload_<platform>.c files.
Guido van Rossum <guido@python.org>
parents: 11469
diff changeset
101 #endif /* HAVE_DYNAMIC_LOADING */