1
from .object cimport PyObject
3
cdef extern from "Python.h":
4
ctypedef struct _inittab
6
#####################################################################
7
# 5.3 Importing Modules
8
#####################################################################
9
object PyImport_ImportModule(const char *name)
10
# Return value: New reference.
11
# This is a simplified interface to PyImport_ImportModuleEx()
12
# below, leaving the globals and locals arguments set to
13
# NULL. When the name argument contains a dot (when it specifies a
14
# submodule of a package), the fromlist argument is set to the
15
# list ['*'] so that the return value is the named module rather
16
# than the top-level package containing it as would otherwise be
17
# the case. (Unfortunately, this has an additional side effect
18
# when name in fact specifies a subpackage instead of a submodule:
19
# the submodules specified in the package's __all__ variable are
20
# loaded.) Return a new reference to the imported module, or NULL
21
# with an exception set on failure.
23
object PyImport_ImportModuleEx(const char *name, object globals, object locals, object fromlist)
24
# Return value: New reference.
26
# Import a module. This is best described by referring to the
27
# built-in Python function __import__(), as the standard
28
# __import__() function calls this function directly.
30
# The return value is a new reference to the imported module or
31
# top-level package, or NULL with an exception set on failure
32
# (before Python 2.4, the module may still be created in this
33
# case). Like for __import__(), the return value when a submodule
34
# of a package was requested is normally the top-level package,
35
# unless a non-empty fromlist was given. Changed in version 2.4:
36
# failing imports remove incomplete module objects.
38
object PyImport_ImportModuleLevel(char *name, object globals, object locals, object fromlist, int level)
39
# Return value: New reference.
41
# Import a module. This is best described by referring to the
42
# built-in Python function __import__(), as the standard
43
# __import__() function calls this function directly.
45
# The return value is a new reference to the imported module or
46
# top-level package, or NULL with an exception set on failure. Like
47
# for __import__(), the return value when a submodule of a package
48
# was requested is normally the top-level package, unless a
49
# non-empty fromlist was given.
51
object PyImport_Import(object name)
52
# Return value: New reference.
53
# This is a higher-level interface that calls the current ``import
54
# hook function''. It invokes the __import__() function from the
55
# __builtins__ of the current globals. This means that the import
56
# is done using whatever import hooks are installed in the current
57
# environment, e.g. by rexec or ihooks.
59
object PyImport_ReloadModule(object m)
60
# Return value: New reference.
61
# Reload a module. This is best described by referring to the
62
# built-in Python function reload(), as the standard reload()
63
# function calls this function directly. Return a new reference to
64
# the reloaded module, or NULL with an exception set on failure
65
# (the module still exists in this case).
67
PyObject* PyImport_AddModule(const char *name) except NULL
68
# Return value: Borrowed reference.
69
# Return the module object corresponding to a module name. The
70
# name argument may be of the form package.module. First check the
71
# modules dictionary if there's one there, and if not, create a
72
# new one and insert it in the modules dictionary. Return NULL
73
# with an exception set on failure. Note: This function does not
74
# load or import the module; if the module wasn't already loaded,
75
# you will get an empty module object. Use PyImport_ImportModule()
76
# or one of its variants to import a module. Package structures
77
# implied by a dotted name for name are not created if not already
80
object PyImport_ExecCodeModule(char *name, object co)
81
# Return value: New reference.
82
# Given a module name (possibly of the form package.module) and a
83
# code object read from a Python bytecode file or obtained from
84
# the built-in function compile(), load the module. Return a new
85
# reference to the module object, or NULL with an exception set if
86
# an error occurred. Name is removed from sys.modules in error
87
# cases, and even if name was already in sys.modules on entry to
88
# PyImport_ExecCodeModule(). Leaving incompletely initialized
89
# modules in sys.modules is dangerous, as imports of such modules
90
# have no way to know that the module object is an unknown (and
91
# probably damaged with respect to the module author's intents)
93
# This function will reload the module if it was already
94
# imported. See PyImport_ReloadModule() for the intended way to
96
# If name points to a dotted name of the form package.module, any
97
# package structures not already created will still not be
101
long PyImport_GetMagicNumber()
102
# Return the magic number for Python bytecode files (a.k.a. .pyc
103
# and .pyo files). The magic number should be present in the first
104
# four bytes of the bytecode file, in little-endian byte order.
106
PyObject* PyImport_GetModuleDict() except NULL
107
# Return value: Borrowed reference.
108
# Return the dictionary used for the module administration
109
# (a.k.a. sys.modules). Note that this is a per-interpreter
113
int PyImport_ImportFrozenModule(char *name) except -1
114
# Load a frozen module named name. Return 1 for success, 0 if the
115
# module is not found, and -1 with an exception set if the
116
# initialization failed. To access the imported module on a
117
# successful load, use PyImport_ImportModule(). (Note the misnomer
118
# -- this function would reload the module if it was already
122
int PyImport_ExtendInittab(_inittab *newtab) except -1
123
# Add a collection of modules to the table of built-in
124
# modules. The newtab array must end with a sentinel entry which
125
# contains NULL for the name field; failure to provide the
126
# sentinel value can result in a memory fault. Returns 0 on
127
# success or -1 if insufficient memory could be allocated to
128
# extend the internal table. In the event of failure, no modules
129
# are added to the internal table. This should be called before
132
#####################################################################
133
# 7.5.5 Module Objects
134
#####################################################################
136
# PyTypeObject PyModule_Type
138
# This instance of PyTypeObject represents the Python module
139
# type. This is exposed to Python programs as types.ModuleType.
141
bint PyModule_Check(object p)
142
# Return true if p is a module object, or a subtype of a module
145
bint PyModule_CheckExact(object p)
146
# Return true if p is a module object, but not a subtype of PyModule_Type.
148
object PyModule_NewObject(object name)
149
# Return a new module object with the __name__ attribute set to name.
150
# The module’s __name__, __doc__, __package__, and __loader__
151
# attributes are filled in (all but __name__ are set to None); the caller
152
# is responsible for providing a __file__ attribute.
154
object PyModule_New(const char *name)
155
# Return value: New reference.
156
# Return a new module object with the __name__ attribute set to
157
# name. Only the module's __doc__ and __name__ attributes are
158
# filled in; the caller is responsible for providing a __file__
161
PyObject* PyModule_GetDict(object module) except NULL
162
# Return value: Borrowed reference.
163
# Return the dictionary object that implements module's namespace;
164
# this object is the same as the __dict__ attribute of the module
165
# object. This function never fails. It is recommended extensions
166
# use other PyModule_*() and PyObject_*() functions rather than
167
# directly manipulate a module's __dict__.
169
object PyModule_GetNameObject(object module)
170
# Return module’s __name__ value. If the module does not provide one, or if
171
# it is not a string, SystemError is raised and NULL is returned.
173
char* PyModule_GetName(object module) except NULL
174
# Similar to PyModule_GetNameObject() but return the name encoded
177
void* PyModule_GetState(object module)
178
# Return the “state” of the module, that is, a pointer to the block of
179
# memory allocated at module creation time, or NULL.
180
# See PyModuleDef.m_size.
182
object PyModule_GetFilenameObject(object module)
183
# Return the name of the file from which module was loaded using module’s
184
# __file__ attribute. If this is not defined, or if it is not a unicode
185
# string, raise SystemError and return NULL; otherwise return a reference
186
# to a Unicode object.
188
char* PyModule_GetFilename(object module) except NULL
189
# Similar to PyModule_GetFilenameObject() but return the filename encoded
192
int PyModule_AddObject(object module, const char *name, object value) except -1
193
# Add an object to module as name. This is a convenience function
194
# which can be used from the module's initialization function.
195
# Return -1 on error, 0 on success.
197
# WARNING: This _steals_ a reference to value.
199
int PyModule_AddIntConstant(object module, const char *name, long value) except -1
200
# Add an integer constant to module as name. This convenience
201
# function can be used from the module's initialization
202
# function. Return -1 on error, 0 on success.
204
int PyModule_AddStringConstant(object module, const char *name, const char *value) except -1
205
# Add a string constant to module as name. This convenience
206
# function can be used from the module's initialization
207
# function. The string value must be null-terminated. Return -1 on
208
# error, 0 on success.