summaryrefslogtreecommitdiffstats
path: root/venv/Include/site/python3.9/pygame/include/pgimport.h
blob: 6649865b3faa627d1f7f352d5704b9ecb643f0db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef PGIMPORT_H
#define PGIMPORT_H

/* Prefix when initializing module */
#define MODPREFIX ""
/* Prefix when importing module */
#define IMPPREFIX "pygame."

#ifdef __SYMBIAN32__

/* On Symbian there is no pygame package. The extensions are built-in or in
 * sys\bin. */
#undef MODPREFIX
#undef IMPPREFIX
#define MODPREFIX "pygame_"
#define IMPPREFIX "pygame_"

#endif /* __SYMBIAN32__ */

#include "pgcompat.h"

#define PYGAMEAPI_LOCAL_ENTRY "_PYGAME_C_API"
#define PG_CAPSULE_NAME(m) (IMPPREFIX m "." PYGAMEAPI_LOCAL_ENTRY)

/*
 * fill API slots defined by PYGAMEAPI_DEFINE_SLOTS/PYGAMEAPI_EXTERN_SLOTS
 */
#define _IMPORT_PYGAME_MODULE(module)                                        \
    {                                                                        \
        PyObject *_mod_##module = PyImport_ImportModule(IMPPREFIX #module);  \
                                                                             \
        if (_mod_##module != NULL) {                                         \
            PyObject *_c_api =                                               \
                PyObject_GetAttrString(_mod_##module, PYGAMEAPI_LOCAL_ENTRY);\
                                                                             \
            Py_DECREF(_mod_##module);                                        \
            if (_c_api != NULL && PyCapsule_CheckExact(_c_api)) {            \
                void **localptr = (void **)PyCapsule_GetPointer(             \
                    _c_api, PG_CAPSULE_NAME(#module));                       \
                _PGSLOTS_ ## module = localptr;                              \
            }                                                                \
            Py_XDECREF(_c_api);                                              \
        }                                                                    \
    }

#define PYGAMEAPI_IS_IMPORTED(module) (_PGSLOTS_ ## module != NULL)

/*
 * source file must include one of these in order to use _IMPORT_PYGAME_MODULE.
 * this is set by import_pygame_*() functions.
 * disable with NO_PYGAME_C_API
 */
#define PYGAMEAPI_DEFINE_SLOTS(module) \
    void ** _PGSLOTS_ ## module = NULL
#define PYGAMEAPI_EXTERN_SLOTS(module) \
    extern void **_PGSLOTS_ ## module
#define PYGAMEAPI_GET_SLOT(module, index) \
    _PGSLOTS_ ## module [(index)]

/*
 * disabled API with NO_PYGAME_C_API; do nothing instead
 */
#ifdef NO_PYGAME_C_API

#undef PYGAMEAPI_DEFINE_SLOTS
#undef PYGAMEAPI_EXTERN_SLOTS

#define PYGAMEAPI_DEFINE_SLOTS(module)
#define PYGAMEAPI_EXTERN_SLOTS(module)

/* intentionally leave this defined to cause a compiler error *
#define PYGAMEAPI_GET_SLOT(api_root, index)
#undef PYGAMEAPI_GET_SLOT*/

#undef _IMPORT_PYGAME_MODULE
#define _IMPORT_PYGAME_MODULE(module)

#endif /* NO_PYGAME_C_API */

#endif /* ~PGIMPORT_H */