annotate Python/importdl.c @ 59857:ec6a995ecfbf

Merged revisions 79205,79219,79228,79230,79232-79233,79235,79237 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r79205 | benjamin.peterson | 2010-03-21 12:34:54 -0500 (Sun, 21 Mar 2010) | 1 line rewrite a bit ........ r79219 | benjamin.peterson | 2010-03-21 14:24:08 -0500 (Sun, 21 Mar 2010) | 1 line flatten condition ........ r79228 | benjamin.peterson | 2010-03-21 14:35:39 -0500 (Sun, 21 Mar 2010) | 1 line remove pointless condition ........ r79230 | benjamin.peterson | 2010-03-21 14:39:52 -0500 (Sun, 21 Mar 2010) | 1 line co_varnames is certainly a tuple, so let's not waste time finding out ........ r79232 | benjamin.peterson | 2010-03-21 14:54:56 -0500 (Sun, 21 Mar 2010) | 1 line fix import ........ r79233 | benjamin.peterson | 2010-03-21 14:56:37 -0500 (Sun, 21 Mar 2010) | 1 line don't write duplicate tests ........ r79235 | benjamin.peterson | 2010-03-21 15:21:00 -0500 (Sun, 21 Mar 2010) | 4 lines improve error message from passing inadequate number of keyword arguments #6474 Note this removes the "non-keyword" or "keyword" phrases from these messages. ........ r79237 | benjamin.peterson | 2010-03-21 15:30:30 -0500 (Sun, 21 Mar 2010) | 1 line take into account keyword arguments when passing too many args ........
author Benjamin Peterson <benjamin@python.org>
date Sun, 21 Mar 2010 21:00:50 +0000
parents 94f64f11fdd0
children cab14be0ada1
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
11600
14561e8395bb Moved most of the platform-specific code to dynload_<platform>.c files.
Guido van Rossum <guido@python.org>
parents: 11469
diff changeset
15 extern dl_funcptr _PyImport_GetDynLoadFunc(const char *name,
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11600
diff changeset
16 const char *shortname,
11600
14561e8395bb Moved most of the platform-specific code to dynload_<platform>.c files.
Guido van Rossum <guido@python.org>
parents: 11469
diff changeset
17 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
18
3898
94c6bf6fd7f4 If NO_DYNAMIC_LINK is defined, load_dynamic_module() will always fail.
Guido van Rossum <guido@python.org>
parents: 3732
diff changeset
19
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 *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
22 _PyImport_LoadDynamicModule(char *name, 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
23 {
25040
db49ee76b7d0 Simplify, and avoid PyModule_GetDict() while we're at it.
Fred Drake <fdrake@acm.org>
parents: 20462
diff changeset
24 PyObject *m;
43573
50599bbb0c67 Move decl so it compiles with C89.
Neal Norwitz <nnorwitz@gmail.com>
parents: 43569
diff changeset
25 PyObject *path;
20462
2f0c5af4d5ea SF patch #471839: Bug when extensions import extensions (Shane Hathaway)
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
26 char *lastdot, *shortname, *packagecontext, *oldcontext;
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
27 dl_funcptr p0;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
28 PyObject* (*p)(void);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
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
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
31 if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
32 Py_INCREF(m);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
33 return m;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
34 }
6602
4514a5ea3ec4 Fix importing of shared libraries from inside packages.
Guido van Rossum <guido@python.org>
parents: 6479
diff changeset
35 lastdot = strrchr(name, '.');
4514a5ea3ec4 Fix importing of shared libraries from inside packages.
Guido van Rossum <guido@python.org>
parents: 6479
diff changeset
36 if (lastdot == NULL) {
4514a5ea3ec4 Fix importing of shared libraries from inside packages.
Guido van Rossum <guido@python.org>
parents: 6479
diff changeset
37 packagecontext = NULL;
4514a5ea3ec4 Fix importing of shared libraries from inside packages.
Guido van Rossum <guido@python.org>
parents: 6479
diff changeset
38 shortname = name;
4514a5ea3ec4 Fix importing of shared libraries from inside packages.
Guido van Rossum <guido@python.org>
parents: 6479
diff changeset
39 }
4514a5ea3ec4 Fix importing of shared libraries from inside packages.
Guido van Rossum <guido@python.org>
parents: 6479
diff changeset
40 else {
4514a5ea3ec4 Fix importing of shared libraries from inside packages.
Guido van Rossum <guido@python.org>
parents: 6479
diff changeset
41 packagecontext = name;
4514a5ea3ec4 Fix importing of shared libraries from inside packages.
Guido van Rossum <guido@python.org>
parents: 6479
diff changeset
42 shortname = lastdot+1;
4514a5ea3ec4 Fix importing of shared libraries from inside packages.
Guido van Rossum <guido@python.org>
parents: 6479
diff changeset
43 }
3907
ac1d5abfd78e Changes for AIX sharedlibs.
Guido van Rossum <guido@python.org>
parents: 3898
diff changeset
44
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
45 p0 = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
46 p = (PyObject*(*)(void))p0;
11600
14561e8395bb Moved most of the platform-specific code to dynload_<platform>.c files.
Guido van Rossum <guido@python.org>
parents: 11469
diff changeset
47 if (PyErr_Occurred())
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 if (p == NULL) {
6602
4514a5ea3ec4 Fix importing of shared libraries from inside packages.
Guido van Rossum <guido@python.org>
parents: 6479
diff changeset
50 PyErr_Format(PyExc_ImportError,
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
51 "dynamic module does not define init function (PyInit_%.200s)",
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11600
diff changeset
52 shortname);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 }
20462
2f0c5af4d5ea SF patch #471839: Bug when extensions import extensions (Shane Hathaway)
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
55 oldcontext = _Py_PackageContext;
6602
4514a5ea3ec4 Fix importing of shared libraries from inside packages.
Guido van Rossum <guido@python.org>
parents: 6479
diff changeset
56 _Py_PackageContext = packagecontext;
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
57 m = (*p)();
20462
2f0c5af4d5ea SF patch #471839: Bug when extensions import extensions (Shane Hathaway)
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
58 _Py_PackageContext = oldcontext;
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
59 if (m == NULL)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
60 return NULL;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
62 if (PyErr_Occurred()) {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
63 Py_DECREF(m);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
64 PyErr_Format(PyExc_SystemError,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
65 "initialization of %s raised unreported exception",
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
66 shortname);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 }
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
69
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
70 /* Remember pointer to module init function. */
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
71 def = PyModule_GetDef(m);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
72 def->m_base.m_init = p;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
73
4042
6b4d0f1a0ae9 Many little fixes:
Guido van Rossum <guido@python.org>
parents: 3988
diff changeset
74 /* Remember the filename as the __file__ attribute */
43569
19319e70fc3e Patch #1272, by Christian Heimes and Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents: 30316
diff changeset
75 path = PyUnicode_DecodeFSDefault(pathname);
19319e70fc3e Patch #1272, by Christian Heimes and Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents: 30316
diff changeset
76 if (PyModule_AddObject(m, "__file__", path) < 0)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5314
diff changeset
77 PyErr_Clear(); /* Not important enough to report */
30316
19611d101d23 Patch #794826: Add __file__ in dynamically loaded modules for multiple
Martin v. Löwis <martin@v.loewis.de>
parents: 25040
diff changeset
78
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 43573
diff changeset
79 if (_PyImport_FixupExtension(m, name, pathname) < 0)
30316
19611d101d23 Patch #794826: Add __file__ in dynamically loaded modules for multiple
Martin v. Löwis <martin@v.loewis.de>
parents: 25040
diff changeset
80 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5314
diff changeset
81 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9299
diff changeset
82 PySys_WriteStderr(
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 "import %s # dynamically loaded from %s\n",
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 name, pathname);
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 return m;
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11600
diff changeset
86 }
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11600
diff changeset
87
11600
14561e8395bb Moved most of the platform-specific code to dynload_<platform>.c files.
Guido van Rossum <guido@python.org>
parents: 11469
diff changeset
88 #endif /* HAVE_DYNAMIC_LOADING */