blob: c9554148cc5f5caad7e79fdfa68481f4cef3a8cf [file] [log] [blame]
Guido van Rossum22a1d361999-12-20 21:18:49 +00001
2/* Support for dynamic loading of extension modules */
3
4#include "dl.h"
5#include <errno.h>
6
7#include "Python.h"
8#include "importdl.h"
9
Guido van Rossum96a8fb71999-12-22 14:09:35 +000010#if defined(__hp9000s300)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011#define FUNCNAME_PATTERN "_PyInit_%.200s"
Guido van Rossum96a8fb71999-12-22 14:09:35 +000012#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000013#define FUNCNAME_PATTERN "PyInit_%.200s"
Guido van Rossum96a8fb71999-12-22 14:09:35 +000014#endif
Guido van Rossum22a1d361999-12-20 21:18:49 +000015
Brett Cannon2657df42012-05-04 15:20:40 -040016const char *_PyImport_DynLoadFiletab[] = {SHLIB_EXT, NULL};
Guido van Rossum22a1d361999-12-20 21:18:49 +000017
Victor Stinner42040fb2011-02-22 23:16:19 +000018dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019 const char *pathname, FILE *fp)
Guido van Rossum22a1d361999-12-20 21:18:49 +000020{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000021 dl_funcptr p;
22 shl_t lib;
23 int flags;
24 char funcname[258];
Guido van Rossum22a1d361999-12-20 21:18:49 +000025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 flags = BIND_FIRST | BIND_DEFERRED;
27 if (Py_VerboseFlag) {
28 flags = BIND_FIRST | BIND_IMMEDIATE |
29 BIND_NONFATAL | BIND_VERBOSE;
30 printf("shl_load %s\n",pathname);
31 }
32 lib = shl_load(pathname, flags, 0);
33 /* XXX Chuck Blake once wrote that 0 should be BIND_NOSTART? */
34 if (lib == NULL) {
35 char buf[256];
Brett Cannon3dfc22c2012-04-20 15:31:11 -040036 PyObject *pathname_ob = NULL;
37 PyObject *buf_ob = NULL;
38 PyObject *shortname_ob = NULL;
39
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 if (Py_VerboseFlag)
41 perror(pathname);
42 PyOS_snprintf(buf, sizeof(buf), "Failed to load %.200s",
43 pathname);
Brett Cannon3dfc22c2012-04-20 15:31:11 -040044 buf_ob = PyUnicode_FromString(buf);
45 shortname_ob = PyUnicode_FromString(shortname);
46 pathname_ob = PyUnicode_FromString(pathname);
47 PyErr_SetImportError(buf_ob, shortname_ob, pathname_ob);
48 Py_DECREF(buf_ob);
49 Py_DECREF(shortname_ob);
50 Py_DECREF(pathname_ob);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 return NULL;
52 }
53 PyOS_snprintf(funcname, sizeof(funcname), FUNCNAME_PATTERN, shortname);
54 if (Py_VerboseFlag)
55 printf("shl_findsym %s\n", funcname);
56 if (shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p) == -1) {
57 shl_unload(lib);
58 p = NULL;
59 }
60 if (p == NULL && Py_VerboseFlag)
61 perror(funcname);
Guido van Rossum22a1d361999-12-20 21:18:49 +000062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 return p;
Guido van Rossum22a1d361999-12-20 21:18:49 +000064}