~ubuntu-branches/ubuntu/natty/python3.1/natty-security

« back to all changes in this revision

Viewing changes to Python/importdl.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-07-06 16:52:42 UTC
  • mfrom: (1.2.1 upstream) (2.1.11 sid)
  • Revision ID: james.westby@ubuntu.com-20100706165242-2xv4i019r3et6c0j
Tags: 3.1.2+20100706-1ubuntu1
* Merge with Debian; remaining changes:
  - Regenerate the control file.
  - Add debian/patches/overwrite-semaphore-check for Lucid buildds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
#include "importdl.h"
14
14
 
15
15
extern dl_funcptr _PyImport_GetDynLoadFunc(const char *name,
16
 
                                           const char *shortname,
17
 
                                           const char *pathname, FILE *fp);
 
16
                                           const char *shortname,
 
17
                                           const char *pathname, FILE *fp);
18
18
 
19
19
 
20
20
 
21
21
PyObject *
22
22
_PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp)
23
23
{
24
 
        PyObject *m;
25
 
        PyObject *path;
26
 
        char *lastdot, *shortname, *packagecontext, *oldcontext;
27
 
        dl_funcptr p0;
28
 
        PyObject* (*p)(void);
29
 
        struct PyModuleDef *def;
30
 
 
31
 
        if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
32
 
                Py_INCREF(m);
33
 
                return m;
34
 
        }
35
 
        lastdot = strrchr(name, '.');
36
 
        if (lastdot == NULL) {
37
 
                packagecontext = NULL;
38
 
                shortname = name;
39
 
        }
40
 
        else {
41
 
                packagecontext = name;
42
 
                shortname = lastdot+1;
43
 
        }
44
 
 
45
 
        p0 = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp);
46
 
        p = (PyObject*(*)(void))p0;
47
 
        if (PyErr_Occurred())
48
 
                return NULL;
49
 
        if (p == NULL) {
50
 
                PyErr_Format(PyExc_ImportError,
51
 
                   "dynamic module does not define init function (PyInit_%.200s)",
52
 
                             shortname);
53
 
                return NULL;
54
 
        }
55
 
        oldcontext = _Py_PackageContext;
56
 
        _Py_PackageContext = packagecontext;
57
 
        m = (*p)();
58
 
        _Py_PackageContext = oldcontext;
59
 
        if (m == NULL)
60
 
                return NULL;
61
 
 
62
 
        if (PyErr_Occurred()) {
63
 
                Py_DECREF(m);
64
 
                PyErr_Format(PyExc_SystemError,
65
 
                             "initialization of %s raised unreported exception",
66
 
                             shortname);
67
 
                return NULL;
68
 
        }
69
 
 
70
 
        /* Remember pointer to module init function. */
71
 
        def = PyModule_GetDef(m);
72
 
        def->m_base.m_init = p;
73
 
 
74
 
        /* Remember the filename as the __file__ attribute */
75
 
        path = PyUnicode_DecodeFSDefault(pathname);
76
 
        if (PyModule_AddObject(m, "__file__", path) < 0)
77
 
                PyErr_Clear(); /* Not important enough to report */
78
 
 
79
 
        if (_PyImport_FixupExtension(m, name, pathname) < 0)
80
 
                return NULL;
81
 
        if (Py_VerboseFlag)
82
 
                PySys_WriteStderr(
83
 
                        "import %s # dynamically loaded from %s\n",
84
 
                        name, pathname);
85
 
        return m;
 
24
    PyObject *m;
 
25
    PyObject *path;
 
26
    char *lastdot, *shortname, *packagecontext, *oldcontext;
 
27
    dl_funcptr p0;
 
28
    PyObject* (*p)(void);
 
29
    struct PyModuleDef *def;
 
30
 
 
31
    if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
 
32
        Py_INCREF(m);
 
33
        return m;
 
34
    }
 
35
    lastdot = strrchr(name, '.');
 
36
    if (lastdot == NULL) {
 
37
        packagecontext = NULL;
 
38
        shortname = name;
 
39
    }
 
40
    else {
 
41
        packagecontext = name;
 
42
        shortname = lastdot+1;
 
43
    }
 
44
 
 
45
    p0 = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp);
 
46
    p = (PyObject*(*)(void))p0;
 
47
    if (PyErr_Occurred())
 
48
        return NULL;
 
49
    if (p == NULL) {
 
50
        PyErr_Format(PyExc_ImportError,
 
51
           "dynamic module does not define init function (PyInit_%.200s)",
 
52
                     shortname);
 
53
        return NULL;
 
54
    }
 
55
    oldcontext = _Py_PackageContext;
 
56
    _Py_PackageContext = packagecontext;
 
57
    m = (*p)();
 
58
    _Py_PackageContext = oldcontext;
 
59
    if (m == NULL)
 
60
        return NULL;
 
61
 
 
62
    if (PyErr_Occurred()) {
 
63
        Py_DECREF(m);
 
64
        PyErr_Format(PyExc_SystemError,
 
65
                     "initialization of %s raised unreported exception",
 
66
                     shortname);
 
67
        return NULL;
 
68
    }
 
69
 
 
70
    /* Remember pointer to module init function. */
 
71
    def = PyModule_GetDef(m);
 
72
    def->m_base.m_init = p;
 
73
 
 
74
    /* Remember the filename as the __file__ attribute */
 
75
    path = PyUnicode_DecodeFSDefault(pathname);
 
76
    if (PyModule_AddObject(m, "__file__", path) < 0)
 
77
        PyErr_Clear(); /* Not important enough to report */
 
78
 
 
79
    if (_PyImport_FixupExtension(m, name, pathname) < 0)
 
80
        return NULL;
 
81
    if (Py_VerboseFlag)
 
82
        PySys_WriteStderr(
 
83
            "import %s # dynamically loaded from %s\n",
 
84
            name, pathname);
 
85
    return m;
86
86
}
87
87
 
88
88
#endif /* HAVE_DYNAMIC_LOADING */