| 
									
										
										
										
											1995-01-02 19:04:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Support for dynamic loading of extension modules */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-29 20:08:16 +00:00
										 |  |  | #include "Python.h"
 | 
					
						
							| 
									
										
										
										
											1995-01-02 19:04:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-12-22 14:09:35 +00:00
										 |  |  | /* ./configure sets HAVE_DYNAMIC_LOADING if dynamic loading of modules is
 | 
					
						
							|  |  |  |    supported on this platform. configure will then compile and link in one | 
					
						
							|  |  |  |    of the dynload_*.c files, as appropriate. We will call a function in | 
					
						
							|  |  |  |    those modules to get a function pointer to the module's init function. | 
					
						
							| 
									
										
										
										
											1995-01-02 19:04:15 +00:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											1999-12-20 21:20:42 +00:00
										 |  |  | #ifdef HAVE_DYNAMIC_LOADING
 | 
					
						
							| 
									
										
										
										
											1995-01-02 19:04:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-12-22 14:09:35 +00:00
										 |  |  | #include "importdl.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  | extern dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname, | 
					
						
							|  |  |  |                                               PyObject *pathname, FILE *fp); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2011-02-22 23:16:19 +00:00
										 |  |  | extern dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname, | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |                                            const char *pathname, FILE *fp); | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1997-11-22 21:53:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-29 20:08:16 +00:00
										 |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2011-03-14 15:54:07 -04:00
										 |  |  | _PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp) | 
					
						
							| 
									
										
										
										
											1995-01-02 19:04:15 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  |     PyObject *m = NULL; | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #ifndef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2011-03-14 15:54:07 -04:00
										 |  |  |     PyObject *pathbytes; | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  |     PyObject *nameascii; | 
					
						
							| 
									
										
										
										
											2011-03-14 15:54:07 -04:00
										 |  |  |     char *namestr, *lastdot, *shortname, *packagecontext, *oldcontext; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     dl_funcptr p0; | 
					
						
							|  |  |  |     PyObject* (*p)(void); | 
					
						
							|  |  |  |     struct PyModuleDef *def; | 
					
						
							| 
									
										
										
										
											1999-12-20 21:20:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 15:54:07 -04:00
										 |  |  |     m = _PyImport_FindExtensionObject(name, path); | 
					
						
							| 
									
										
										
										
											2011-03-07 18:20:56 +01:00
										 |  |  |     if (m != NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         Py_INCREF(m); | 
					
						
							| 
									
										
										
										
											2011-03-14 15:54:07 -04:00
										 |  |  |         return m; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-03-14 15:54:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  |     /* name must be encodable to ASCII because dynamic module must have a
 | 
					
						
							|  |  |  |        function called "PyInit_NAME", they are written in C, and the C language | 
					
						
							|  |  |  |        doesn't accept non-ASCII identifiers. */ | 
					
						
							|  |  |  |     nameascii = PyUnicode_AsEncodedString(name, "ascii", NULL); | 
					
						
							|  |  |  |     if (nameascii == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     namestr = PyBytes_AS_STRING(nameascii); | 
					
						
							|  |  |  |     if (namestr == NULL) | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 15:54:07 -04:00
										 |  |  |     lastdot = strrchr(namestr, '.'); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (lastdot == NULL) { | 
					
						
							|  |  |  |         packagecontext = NULL; | 
					
						
							| 
									
										
										
										
											2011-03-14 15:54:07 -04:00
										 |  |  |         shortname = namestr; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2011-03-14 15:54:07 -04:00
										 |  |  |         packagecontext = namestr; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         shortname = lastdot+1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											1995-07-18 14:40:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  |     p0 = _PyImport_GetDynLoadWindows(shortname, path, fp); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2011-03-14 15:54:07 -04:00
										 |  |  |     pathbytes = PyUnicode_EncodeFSDefault(path); | 
					
						
							|  |  |  |     if (pathbytes == NULL) | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  |         goto error; | 
					
						
							| 
									
										
										
										
											2011-03-14 15:54:07 -04:00
										 |  |  |     p0 = _PyImport_GetDynLoadFunc(shortname, | 
					
						
							|  |  |  |                                   PyBytes_AS_STRING(pathbytes), fp); | 
					
						
							|  |  |  |     Py_DECREF(pathbytes); | 
					
						
							| 
									
										
										
										
											2011-04-04 23:05:53 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     p = (PyObject*(*)(void))p0; | 
					
						
							|  |  |  |     if (PyErr_Occurred()) | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  |         goto error; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (p == NULL) { | 
					
						
							| 
									
										
										
										
											2012-04-20 15:22:50 -04:00
										 |  |  |         PyObject *msg = PyUnicode_FromFormat("dynamic module does not define " | 
					
						
							|  |  |  |                                              "init function (PyInit_%s)", | 
					
						
							|  |  |  |                                              shortname); | 
					
						
							|  |  |  |         PyErr_SetImportError(msg, name, path); | 
					
						
							|  |  |  |         Py_DECREF(msg); | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  |         goto error; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     oldcontext = _Py_PackageContext; | 
					
						
							|  |  |  |     _Py_PackageContext = packagecontext; | 
					
						
							|  |  |  |     m = (*p)(); | 
					
						
							|  |  |  |     _Py_PackageContext = oldcontext; | 
					
						
							|  |  |  |     if (m == NULL) | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  |         goto error; | 
					
						
							| 
									
										
										
										
											1995-01-02 19:04:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (PyErr_Occurred()) { | 
					
						
							|  |  |  |         PyErr_Format(PyExc_SystemError, | 
					
						
							|  |  |  |                      "initialization of %s raised unreported exception", | 
					
						
							|  |  |  |                      shortname); | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  |         goto error; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     /* Remember pointer to module init function. */ | 
					
						
							|  |  |  |     def = PyModule_GetDef(m); | 
					
						
							|  |  |  |     def->m_base.m_init = p; | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     /* Remember the filename as the __file__ attribute */ | 
					
						
							|  |  |  |     if (PyModule_AddObject(m, "__file__", path) < 0) | 
					
						
							|  |  |  |         PyErr_Clear(); /* Not important enough to report */ | 
					
						
							| 
									
										
										
										
											2010-10-17 01:24:53 +00:00
										 |  |  |     else | 
					
						
							|  |  |  |         Py_INCREF(path); | 
					
						
							| 
									
										
										
										
											2003-09-04 18:45:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 15:54:07 -04:00
										 |  |  |     if (_PyImport_FixupExtensionObject(m, name, path) < 0) | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  |         goto error; | 
					
						
							|  |  |  |     Py_DECREF(nameascii); | 
					
						
							| 
									
										
										
										
											2011-03-14 15:54:07 -04:00
										 |  |  |     return m; | 
					
						
							| 
									
										
										
										
											2011-05-07 12:46:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | error: | 
					
						
							|  |  |  |     Py_DECREF(nameascii); | 
					
						
							|  |  |  |     Py_XDECREF(m); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											1998-08-04 22:46:29 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1999-12-22 14:09:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #endif /* HAVE_DYNAMIC_LOADING */
 |