Skip to content

Commit 06af658

Browse files
author
georg.brandl
committed
Missed one big file to split up.
git-svn-id: http://svn.python.org/projects/python/trunk@60110 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 95da70e commit 06af658

7 files changed

Lines changed: 1175 additions & 1166 deletions

File tree

Doc/c-api/arg.rst

Lines changed: 509 additions & 0 deletions
Large diffs are not rendered by default.

Doc/c-api/conversion.rst

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
.. highlightlang:: c
2+
3+
.. _string-conversion:
4+
5+
String conversion and formatting
6+
================================
7+
8+
Functions for number conversion and formatted string output.
9+
10+
11+
.. cfunction:: int PyOS_snprintf(char *str, size_t size, const char *format, ...)
12+
13+
Output not more than *size* bytes to *str* according to the format string
14+
*format* and the extra arguments. See the Unix man page :manpage:`snprintf(2)`.
15+
16+
17+
.. cfunction:: int PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
18+
19+
Output not more than *size* bytes to *str* according to the format string
20+
*format* and the variable argument list *va*. Unix man page
21+
:manpage:`vsnprintf(2)`.
22+
23+
:cfunc:`PyOS_snprintf` and :cfunc:`PyOS_vsnprintf` wrap the Standard C library
24+
functions :cfunc:`snprintf` and :cfunc:`vsnprintf`. Their purpose is to
25+
guarantee consistent behavior in corner cases, which the Standard C functions do
26+
not.
27+
28+
The wrappers ensure that *str*[*size*-1] is always ``'\0'`` upon return. They
29+
never write more than *size* bytes (including the trailing ``'\0'`` into str.
30+
Both functions require that ``str != NULL``, ``size > 0`` and ``format !=
31+
NULL``.
32+
33+
If the platform doesn't have :cfunc:`vsnprintf` and the buffer size needed to
34+
avoid truncation exceeds *size* by more than 512 bytes, Python aborts with a
35+
*Py_FatalError*.
36+
37+
The return value (*rv*) for these functions should be interpreted as follows:
38+
39+
* When ``0 <= rv < size``, the output conversion was successful and *rv*
40+
characters were written to *str* (excluding the trailing ``'\0'`` byte at
41+
*str*[*rv*]).
42+
43+
* When ``rv >= size``, the output conversion was truncated and a buffer with
44+
``rv + 1`` bytes would have been needed to succeed. *str*[*size*-1] is ``'\0'``
45+
in this case.
46+
47+
* When ``rv < 0``, "something bad happened." *str*[*size*-1] is ``'\0'`` in
48+
this case too, but the rest of *str* is undefined. The exact cause of the error
49+
depends on the underlying platform.
50+
51+
The following functions provide locale-independent string to number conversions.
52+
53+
54+
.. cfunction:: double PyOS_ascii_strtod(const char *nptr, char **endptr)
55+
56+
Convert a string to a :ctype:`double`. This function behaves like the Standard C
57+
function :cfunc:`strtod` does in the C locale. It does this without changing the
58+
current locale, since that would not be thread-safe.
59+
60+
:cfunc:`PyOS_ascii_strtod` should typically be used for reading configuration
61+
files or other non-user input that should be locale independent.
62+
63+
.. versionadded:: 2.4
64+
65+
See the Unix man page :manpage:`strtod(2)` for details.
66+
67+
68+
.. cfunction:: char * PyOS_ascii_formatd(char *buffer, size_t buf_len, const char *format, double d)
69+
70+
Convert a :ctype:`double` to a string using the ``'.'`` as the decimal
71+
separator. *format* is a :cfunc:`printf`\ -style format string specifying the
72+
number format. Allowed conversion characters are ``'e'``, ``'E'``, ``'f'``,
73+
``'F'``, ``'g'`` and ``'G'``.
74+
75+
The return value is a pointer to *buffer* with the converted string or NULL if
76+
the conversion failed.
77+
78+
.. versionadded:: 2.4
79+
80+
81+
.. cfunction:: double PyOS_ascii_atof(const char *nptr)
82+
83+
Convert a string to a :ctype:`double` in a locale-independent way.
84+
85+
.. versionadded:: 2.4
86+
87+
See the Unix man page :manpage:`atof(2)` for details.
88+
89+
90+
.. cfunction:: char * PyOS_stricmp(char *s1, char *s2)
91+
92+
Case insensitive comparsion of strings. The functions works almost
93+
identical to :cfunc:`strcmp` except that it ignores the case.
94+
95+
.. versionadded:: 2.6
96+
97+
98+
.. cfunction:: char * PyOS_strnicmp(char *s1, char *s2, Py_ssize_t size)
99+
100+
Case insensitive comparsion of strings. The functions works almost
101+
identical to :cfunc:`strncmp` except that it ignores the case.
102+
103+
.. versionadded:: 2.6

Doc/c-api/import.rst

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
.. highlightlang:: c
2+
3+
.. _importing:
4+
5+
Importing Modules
6+
=================
7+
8+
9+
.. cfunction:: PyObject* PyImport_ImportModule(const char *name)
10+
11+
.. index::
12+
single: package variable; __all__
13+
single: __all__ (package variable)
14+
single: modules (in module sys)
15+
16+
This is a simplified interface to :cfunc:`PyImport_ImportModuleEx` below,
17+
leaving the *globals* and *locals* arguments set to *NULL* and *level* set
18+
to 0. When the *name*
19+
argument contains a dot (when it specifies a submodule of a package), the
20+
*fromlist* argument is set to the list ``['*']`` so that the return value is the
21+
named module rather than the top-level package containing it as would otherwise
22+
be the case. (Unfortunately, this has an additional side effect when *name* in
23+
fact specifies a subpackage instead of a submodule: the submodules specified in
24+
the package's ``__all__`` variable are loaded.) Return a new reference to the
25+
imported module, or *NULL* with an exception set on failure. Before Python 2.4,
26+
the module may still be created in the failure case --- examine ``sys.modules``
27+
to find out. Starting with Python 2.4, a failing import of a module no longer
28+
leaves the module in ``sys.modules``.
29+
30+
.. versionchanged:: 2.4
31+
failing imports remove incomplete module objects.
32+
33+
.. versionchanged:: 2.6
34+
always use absolute imports
35+
36+
37+
.. cfunction:: PyObject* PyImport_ImportModuleNoBlock(const char *name)
38+
39+
This version of :cfunc:`PyImport_ImportModule` does not block. It's intended
40+
to be used in C functions that import other modules to execute a function.
41+
The import may block if another thread holds the import lock. The function
42+
:cfunc:`PyImport_ImportModuleNoBlock` never blocks. It first tries to fetch
43+
the module from sys.modules and falls back to :cfunc:`PyImport_ImportModule`
44+
unless the lock is held, in which case the function will raise an
45+
:exc:`ImportError`.
46+
47+
.. versionadded:: 2.6
48+
49+
50+
.. cfunction:: PyObject* PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist)
51+
52+
.. index:: builtin: __import__
53+
54+
Import a module. This is best described by referring to the built-in Python
55+
function :func:`__import__`, as the standard :func:`__import__` function calls
56+
this function directly.
57+
58+
The return value is a new reference to the imported module or top-level package,
59+
or *NULL* with an exception set on failure (before Python 2.4, the module may
60+
still be created in this case). Like for :func:`__import__`, the return value
61+
when a submodule of a package was requested is normally the top-level package,
62+
unless a non-empty *fromlist* was given.
63+
64+
.. versionchanged:: 2.4
65+
failing imports remove incomplete module objects.
66+
67+
.. versionchanged:: 2.6
68+
The function is an alias for :cfunc:`PyImport_ImportModuleLevel` with
69+
-1 as level, meaning relative import.
70+
71+
72+
.. cfunction:: PyObject* PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)
73+
74+
Import a module. This is best described by referring to the built-in Python
75+
function :func:`__import__`, as the standard :func:`__import__` function calls
76+
this function directly.
77+
78+
The return value is a new reference to the imported module or top-level package,
79+
or *NULL* with an exception set on failure. Like for :func:`__import__`,
80+
the return value when a submodule of a package was requested is normally the
81+
top-level package, unless a non-empty *fromlist* was given.
82+
83+
.. versionadded:: 2.5
84+
85+
86+
.. cfunction:: PyObject* PyImport_Import(PyObject *name)
87+
88+
.. index::
89+
module: rexec
90+
module: ihooks
91+
92+
This is a higher-level interface that calls the current "import hook function".
93+
It invokes the :func:`__import__` function from the ``__builtins__`` of the
94+
current globals. This means that the import is done using whatever import hooks
95+
are installed in the current environment, e.g. by :mod:`rexec` or :mod:`ihooks`.
96+
97+
.. versionchanged:: 2.6
98+
always use absolute imports
99+
100+
101+
.. cfunction:: PyObject* PyImport_ReloadModule(PyObject *m)
102+
103+
.. index:: builtin: reload
104+
105+
Reload a module. This is best described by referring to the built-in Python
106+
function :func:`reload`, as the standard :func:`reload` function calls this
107+
function directly. Return a new reference to the reloaded module, or *NULL*
108+
with an exception set on failure (the module still exists in this case).
109+
110+
111+
.. cfunction:: PyObject* PyImport_AddModule(const char *name)
112+
113+
Return the module object corresponding to a module name. The *name* argument
114+
may be of the form ``package.module``. First check the modules dictionary if
115+
there's one there, and if not, create a new one and insert it in the modules
116+
dictionary. Return *NULL* with an exception set on failure.
117+
118+
.. note::
119+
120+
This function does not load or import the module; if the module wasn't already
121+
loaded, you will get an empty module object. Use :cfunc:`PyImport_ImportModule`
122+
or one of its variants to import a module. Package structures implied by a
123+
dotted name for *name* are not created if not already present.
124+
125+
126+
.. cfunction:: PyObject* PyImport_ExecCodeModule(char *name, PyObject *co)
127+
128+
.. index:: builtin: compile
129+
130+
Given a module name (possibly of the form ``package.module``) and a code object
131+
read from a Python bytecode file or obtained from the built-in function
132+
:func:`compile`, load the module. Return a new reference to the module object,
133+
or *NULL* with an exception set if an error occurred. Before Python 2.4, the
134+
module could still be created in error cases. Starting with Python 2.4, *name*
135+
is removed from :attr:`sys.modules` in error cases, and even if *name* was already
136+
in :attr:`sys.modules` on entry to :cfunc:`PyImport_ExecCodeModule`. Leaving
137+
incompletely initialized modules in :attr:`sys.modules` is dangerous, as imports of
138+
such modules have no way to know that the module object is an unknown (and
139+
probably damaged with respect to the module author's intents) state.
140+
141+
This function will reload the module if it was already imported. See
142+
:cfunc:`PyImport_ReloadModule` for the intended way to reload a module.
143+
144+
If *name* points to a dotted name of the form ``package.module``, any package
145+
structures not already created will still not be created.
146+
147+
.. versionchanged:: 2.4
148+
*name* is removed from :attr:`sys.modules` in error cases.
149+
150+
151+
.. cfunction:: long PyImport_GetMagicNumber()
152+
153+
Return the magic number for Python bytecode files (a.k.a. :file:`.pyc` and
154+
:file:`.pyo` files). The magic number should be present in the first four bytes
155+
of the bytecode file, in little-endian byte order.
156+
157+
158+
.. cfunction:: PyObject* PyImport_GetModuleDict()
159+
160+
Return the dictionary used for the module administration (a.k.a.
161+
``sys.modules``). Note that this is a per-interpreter variable.
162+
163+
164+
.. cfunction:: void _PyImport_Init()
165+
166+
Initialize the import mechanism. For internal use only.
167+
168+
169+
.. cfunction:: void PyImport_Cleanup()
170+
171+
Empty the module table. For internal use only.
172+
173+
174+
.. cfunction:: void _PyImport_Fini()
175+
176+
Finalize the import mechanism. For internal use only.
177+
178+
179+
.. cfunction:: PyObject* _PyImport_FindExtension(char *, char *)
180+
181+
For internal use only.
182+
183+
184+
.. cfunction:: PyObject* _PyImport_FixupExtension(char *, char *)
185+
186+
For internal use only.
187+
188+
189+
.. cfunction:: int PyImport_ImportFrozenModule(char *name)
190+
191+
Load a frozen module named *name*. Return ``1`` for success, ``0`` if the
192+
module is not found, and ``-1`` with an exception set if the initialization
193+
failed. To access the imported module on a successful load, use
194+
:cfunc:`PyImport_ImportModule`. (Note the misnomer --- this function would
195+
reload the module if it was already imported.)
196+
197+
198+
.. ctype:: struct _frozen
199+
200+
.. index:: single: freeze utility
201+
202+
This is the structure type definition for frozen module descriptors, as
203+
generated by the :program:`freeze` utility (see :file:`Tools/freeze/` in the
204+
Python source distribution). Its definition, found in :file:`Include/import.h`,
205+
is::
206+
207+
struct _frozen {
208+
char *name;
209+
unsigned char *code;
210+
int size;
211+
};
212+
213+
214+
.. cvar:: struct _frozen* PyImport_FrozenModules
215+
216+
This pointer is initialized to point to an array of :ctype:`struct _frozen`
217+
records, terminated by one whose members are all *NULL* or zero. When a frozen
218+
module is imported, it is searched in this table. Third-party code could play
219+
tricks with this to provide a dynamically created collection of frozen modules.
220+
221+
222+
.. cfunction:: int PyImport_AppendInittab(char *name, void (*initfunc)(void))
223+
224+
Add a single module to the existing table of built-in modules. This is a
225+
convenience wrapper around :cfunc:`PyImport_ExtendInittab`, returning ``-1`` if
226+
the table could not be extended. The new module can be imported by the name
227+
*name*, and uses the function *initfunc* as the initialization function called
228+
on the first attempted import. This should be called before
229+
:cfunc:`Py_Initialize`.
230+
231+
232+
.. ctype:: struct _inittab
233+
234+
Structure describing a single entry in the list of built-in modules. Each of
235+
these structures gives the name and initialization function for a module built
236+
into the interpreter. Programs which embed Python may use an array of these
237+
structures in conjunction with :cfunc:`PyImport_ExtendInittab` to provide
238+
additional built-in modules. The structure is defined in
239+
:file:`Include/import.h` as::
240+
241+
struct _inittab {
242+
char *name;
243+
void (*initfunc)(void);
244+
};
245+
246+
247+
.. cfunction:: int PyImport_ExtendInittab(struct _inittab *newtab)
248+
249+
Add a collection of modules to the table of built-in modules. The *newtab*
250+
array must end with a sentinel entry which contains *NULL* for the :attr:`name`
251+
field; failure to provide the sentinel value can result in a memory fault.
252+
Returns ``0`` on success or ``-1`` if insufficient memory could be allocated to
253+
extend the internal table. In the event of failure, no modules are added to the
254+
internal table. This should be called before :cfunc:`Py_Initialize`.

0 commit comments

Comments
 (0)