@@ -463,21 +463,24 @@ \section{Embedding Python}
463463\code {PySys_SetArgv(\var {argc}, \var {argv})} subsequent to the call
464464to \code {Py_Initialize()}.
465465
466- On Unix, \code {Py_Initialize()} calculates the module search path
467- based upon its best guess for the location of the standard Python
468- interpreter executable, assuming that the Python library is found in a
469- fixed location relative to the Python interpreter executable. In
470- particular, it looks for a directory named \code {lib/python1.5}
471- (replacing \code {1.5} with the current interpreter version) relative
472- to the parent directory where the executable named \code {python} is
473- found on the shell command search path (the environment variable
474- \code {\$ PATH}). For instance, if the Python executable is found in
475- \code {/usr/local/bin/python}, it will assume that the libraries are in
476- \code {/usr/local/lib/python1.5}. In fact, this also the `` fallback''
477- location, used when no executable file named \code {python} is found
478- along \code {\$ PATH}. The user can change this behavior by setting the
479- environment variable \code {\$ PYTHONHOME}, and can insert additional
480- directories in front of the standard path by setting
466+ On most systems (in particular, on Unix and Windows, although the
467+ details are slightly different), \code {Py_Initialize()} calculates the
468+ module search path based upon its best guess for the location of the
469+ standard Python interpreter executable, assuming that the Python
470+ library is found in a fixed location relative to the Python
471+ interpreter executable. In particular, it looks for a directory named
472+ \code {lib/python1.5} (replacing \code {1.5} with the current
473+ interpreter version) relative to the parent directory where the
474+ executable named \code {python} is found on the shell command search
475+ path (the environment variable \code {\$ PATH}).
476+
477+ For instance, if the Python executable is found in
478+ \code {/usr/local/bin/python}, it will assume that the libraries are in
479+ \code {/usr/local/lib/python1.5}. In fact, this also the `` fallback''
480+ location, used when no executable file named \code {python} is found
481+ along \code {\$ PATH}. The user can change this behavior by setting the
482+ environment variable \code {\$ PYTHONHOME}, and can insert additional
483+ directories in front of the standard path by setting
481484\code {\$ PYTHONPATH}.
482485
483486The embedding application can steer the search by calling
@@ -646,14 +649,6 @@ \chapter{Initialization and Shutdown of an Embedded Python Interpreter}
646649e.g., when the object administration appears to be corrupted.
647650\end {cfuncdesc }
648651
649- \begin {cfuncdesc }{void}{PyImport_Init}{}
650- Initialize the module table. For internal use only.
651- \end {cfuncdesc }
652-
653- \begin {cfuncdesc }{void}{PyImport_Cleanup}{}
654- Empty the module table. For internal use only.
655- \end {cfuncdesc }
656-
657652\begin {cfuncdesc }{void}{PyBuiltin_Init}{}
658653Initialize the \code {__builtin__} module. For internal use only.
659654\end {cfuncdesc }
@@ -742,7 +737,35 @@ \chapter{Exception Handling}
742737\code {type} (the first argument to the last call to one of the
743738\code {PyErr_Set*()} functions or to \code {PyErr_Restore()}). If not
744739set, return \NULL {}. You do not own a reference to the return value,
745- so you do not need to \code {Py_DECREF()} it.
740+ so you do not need to \code {Py_DECREF()} it. Note: do not compare the
741+ return value to a specific exception; use
742+ \code {PyErr_ExceptionMatches} instead, shown below.
743+ \end {cfuncdesc }
744+
745+ \begin {cfuncdesc }{int}{PyErr_ExceptionMatches}{PyObject *exc}
746+ \strong {NEW in 1.5a4!}
747+ Equivalent to
748+ \code {PyErr_GivenExceptionMatches(PyErr_Occurred(), \var {exc})}.
749+ This should only be called when an exception is actually set.
750+ \end {cfuncdesc }
751+
752+ \begin {cfuncdesc }{int}{PyErr_GivenExceptionMatches}{PyObject *given, PyObject *exc}
753+ \strong {NEW in 1.5a4!}
754+ Return true if the \var {given} exception matches the exception in
755+ \var {exc}. If \var {exc} is a class object, this also returns true
756+ when \var {given} is a subclass. If \var {exc} is a tuple, all
757+ exceptions in the tuple (and recursively in subtuples) are searched
758+ for a match. This should only be called when an exception is actually
759+ set.
760+ \end {cfuncdesc }
761+
762+ \begin {cfuncdesc }{void}{PyErr_NormalizeException}{PyObject**exc, PyObject**val, PyObject**tb}
763+ \strong {NEW in 1.5a4!}
764+ Under certain circumstances, the values returned by
765+ \code {PyErr_Fetch()} below can be `` unnormalized'' , meaning that
766+ \var {*exc} is a class object but \var {*val} is not an instance of the
767+ same class. This function can be used to instantiate the class in
768+ that case. If the values are already normalized, nothing happens.
746769\end {cfuncdesc }
747770
748771\begin {cfuncdesc }{void}{PyErr_Clear}{}
@@ -846,13 +869,39 @@ \chapter{Exception Handling}
846869raised.
847870\end {cfuncdesc }
848871
872+ \begin {cfuncdesc }{PyObject *}{PyErr_NewException}{char *name,
873+ PyObject *base, PyObject *dict}
874+ \strong {NEW in 1.5a4!}
875+ This utility function creates and returns a new exception object. The
876+ \var {name} argument must be the name of the new exception, a C string
877+ of the form \code {module.class}. The \var {base} and \var {dict}
878+ arguments are normally \code {NULL}. Normally, this creates a class
879+ object derived from the root for all exceptions, the built-in name
880+ \code {Exception} (accessible in C as \code {PyExc_Exception}). In this
881+ case the \code {__module__} attribute of the new class is set to the
882+ first part (up to the last dot) of the \var {name} argument, and the
883+ class name is set to the last part (after the last dot). When the
884+ user has specified the \code {-X} command line option to use string
885+ exceptions, for backward compatibility, or when the \var {base}
886+ argument is not a class object (and not \code {NULL}), a string object
887+ created from the entire \var {name} argument is returned. The
888+ \var {base} argument can be used to specify an alternate base class.
889+ The \var {dict} argument can be used to specify a dictionary of class
890+ variables and methods.
891+ \end {cfuncdesc }
892+
893+
849894\section {Standard Exceptions }
850895
851896All standard Python exceptions are available as global variables whose
852897names are \code {PyExc_} followed by the Python exception name.
853898These have the type \code {PyObject *}; they are all string objects.
854- For completion, here are all the variables:
855- \code {PyExc_AccessError},
899+ For completeness, here are all the variables (the first four are new
900+ in Python 1.5a4):
901+ \code {PyExc_Exception},
902+ \code {PyExc_StandardError},
903+ \code {PyExc_ArithmeticError},
904+ \code {PyExc_LookupError},
856905\code {PyExc_AssertionError},
857906\code {PyExc_AttributeError},
858907\code {PyExc_EOFError},
@@ -880,6 +929,8 @@ \chapter{Utilities}
880929parsing function arguments and constructing Python values from C
881930values.
882931
932+ \section {OS Utilities }
933+
883934\begin {cfuncdesc }{int}{Py_FdIsInteractive}{FILE *fp, char *filename}
884935Return true (nonzero) if the standard I/O file \code {fp} with name
885936\code {filename} is deemed interactive. This is the case for files for
@@ -896,6 +947,153 @@ \chapter{Utilities}
896947\end {cfuncdesc }
897948
898949
950+ \section {Importing modules }
951+
952+ \begin {cfuncdesc }{PyObject *}{PyImport_ImportModule}{char *name}
953+ This is a simplified interface to \code {PyImport_ImportModuleEx}
954+ below, leaving the \var {globals} and \var {locals} arguments set to
955+ \code {NULL}. When the \var {name} argument contains a dot (i.e., when
956+ it specifies a submodule of a package), the \var {fromlist} argument is
957+ set to the list \code {['*']} so that the return value is the named
958+ module rather than the top-level package containing it as would
959+ otherwise be the case. (Unfortunately, this has an additional side
960+ effect when \var {name} in fact specifies a subpackage instead of a
961+ submodule: the submodules specified in the package's \code {__all__}
962+ variable are loaded.) Return a new reference to the imported module,
963+ or \code {NULL} with an exception set on failure (the module may still
964+ be created in this case).
965+ \end {cfuncdesc }
966+
967+ \begin {cfuncdesc }{PyObject *}{PyImport_ImportModuleEx}{char *name, PyObject *globals, PyObject *locals, PyObject *fromlist}
968+ \strong {NEW in 1.5a4!}
969+ Import a module. This is best described by referring to the built-in
970+ Python function \code {__import()__}, as the standard
971+ \code {__import__()} function calls this function directly.
972+
973+ % Should move this para to libfuncs.tex:
974+ For example, the statement \code {import spam} results in the following
975+ call:
976+ \code {__import__('spam', globals(), locals(), [])};
977+ the statement \code {from spam.ham import eggs} results in
978+ \code {__import__('spam.ham', globals(), locals(), ['eggs'])}.
979+ Note that even though \code {locals()} and \code {['eggs']} are passed
980+ in as arguments, the \code {__import__()} function does not set the
981+ local variable named \code {eggs}; this is done by subsequent code that
982+ is generated for the import statement.
983+
984+ The return value is a new reference to the imported module or
985+ top-level package, or \code {NULL} with an exception set on failure
986+ (the module may still be created in this case). When the \var {name}
987+ variable is of the form \code {package.module}, normally, the top-level
988+ package (the name up till the first dot) is returned, \emph {not } the
989+ module named by \var {name}. However, when a non-empty \var {fromlist}
990+ argument is given, the module named by \var {name} is returned. This
991+ is done for compatibility with the bytecode generated for the
992+ different kinds of import statement; when using \code {import
993+ spam.ham.eggs}, the top-level package \code {spam} must be placed in
994+ the importing namespace, but when using \code {from spam.ham import
995+ eggs}, the \code {spam.ham} subpackage must be used to find the
996+ \code {eggs} variable.
997+ \end {cfuncdesc }
998+
999+ \begin {cfuncdesc }{PyObject *}{PyImport_Import}{PyObject *name}
1000+ This is a higher-level interface that calls the current `` import hook
1001+ function'' . It invokes the \code {__import__()} function from the
1002+ \code {__builtins__} of the current globals. This means that the
1003+ import is done using whatever import hooks are installed in the
1004+ current environment, e.g. by \code {rexec} or \code {ihooks}.
1005+ \end {cfuncdesc }
1006+
1007+ \begin {cfuncdesc }{PyObject *}{PyImport_ReloadModule}{PyObject *m}
1008+ Reload a module. This is best described by referring to the built-in
1009+ Python function \code {reload()}, as the standard \code {reload()}
1010+ function calls this function directly. Return a new reference to the
1011+ reloaded module, or \code {NULL} with an exception set on failure (the
1012+ module still exists in this case).
1013+ \end {cfuncdesc }
1014+
1015+ \begin {cfuncdesc }{PyObject *}{PyImport_AddModule}{char *name}
1016+ Return the module object corresponding to a module name. The
1017+ \var {name} argument may be of the form \code {package.module}). First
1018+ check the modules dictionary if there's one there, and if not, create
1019+ a new one and insert in in the modules dictionary. Because the former
1020+ action is most common, this does not return a new reference, and you
1021+ do not own the returned reference. Return \code {NULL} with an
1022+ exception set on failure.
1023+ \end {cfuncdesc }
1024+
1025+ \begin {cfuncdesc }{PyObject *}{PyImport_ExecCodeModule}{char *name, PyObject *co}
1026+ Given a module name (possibly of the form \code {package.module}) and a
1027+ code object read from a Python bytecode file or obtained from the
1028+ built-in function \code {compile()}, load the module. Return a new
1029+ reference to the module object, or \code {NULL} with an exception set
1030+ if an error occurred (the module may still be created in this case).
1031+ (This function would reload the module if it was already imported.)
1032+ \end {cfuncdesc }
1033+
1034+ \begin {cfuncdesc }{long}{PyImport_GetMagicNumber}{}
1035+ Return the magic number for Python bytecode files (a.k.a. \code {.pyc}
1036+ and \code {.pyo} files). The magic number should be present in the
1037+ first four bytes of the bytecode file, in little-endian byte order.
1038+ \end {cfuncdesc }
1039+
1040+ \begin {cfuncdesc }{PyObject *}{PyImport_GetModuleDict}{}
1041+ Return the dictionary used for the module administration
1042+ (a.k.a. \code {sys.modules}). Note that this is a per-interpreter
1043+ variable.
1044+ \end {cfuncdesc }
1045+
1046+ \begin {cfuncdesc }{void}{_PyImport_Init}{}
1047+ Initialize the import mechanism. For internal use only.
1048+ \end {cfuncdesc }
1049+
1050+ \begin {cfuncdesc }{void}{PyImport_Cleanup}{}
1051+ Empty the module table. For internal use only.
1052+ \end {cfuncdesc }
1053+
1054+ \begin {cfuncdesc }{void}{_PyImport_Fini}{}
1055+ Finalize the import mechanism. For internal use only.
1056+ \end {cfuncdesc }
1057+
1058+ \begin {cvardesc }{extern PyObject *}{_PyImport_FindExtension}{char *, char *}
1059+ For internal use only.
1060+ \end {cvardesc }
1061+
1062+ \begin {cvardesc }{extern PyObject *}{_PyImport_FixupExtension}{char *, char *}
1063+ For internal use only.
1064+ \end {cvardesc }
1065+
1066+ \begin {cfuncdesc }{int}{PyImport_ImportFrozenModule}{char *}
1067+ Load a frozen module. Return \code {1} for success, \code {0} if the
1068+ module is not found, and \code {-1} with an exception set if the
1069+ initialization failed. To access the imported module on a successful
1070+ load, use \code {PyImport_ImportModule()).
1071+ (Note the misnomer -- this function would reload the module if it was
1072+ already imported.)
1073+ \end {cfuncdesc}
1074+
1075+ \begin {ctypedesc }{struct _frozen}
1076+ This is the structure type definition for frozen module descriptors,
1077+ as generated by the \code {freeze} utility (see \file {Tools/freeze/} in
1078+ the Python source distribution). Its definition is:
1079+ \bcode \begin {verbatim }
1080+ struct _frozen {
1081+ char *name;
1082+ unsigned char *code;
1083+ int size;
1084+ };
1085+ \end {verbatim }\ecode
1086+ \end {ctypedesc }
1087+
1088+ \begin {cvardesc }{struct _frozen *}{PyImport_FrozenModules}
1089+ This pointer is initialized to point to an array of \code {struct
1090+ _freeze} records, terminated by one whose members are all \code {NULL}
1091+ or zero. When a frozen module is imported, it is searched in this
1092+ table. Third party code could play tricks with this to provide a
1093+ dynamically created collection of frozen modules.
1094+ \end {cvardesc }
1095+
1096+
8991097\chapter {Debugging }
9001098
9011099XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG.
@@ -1557,20 +1755,29 @@ \chapter{Initialization, Finalization, and Threads}
15571755modules (\code {sys.modules}), and creates the fundamental modules
15581756\code {__builtin__}, \code {__main__} and \code {sys}. It also
15591757initializes the module search path (\code {sys.path}). It does not set
1560- \code {sys.argv}; use \code {PySys_SetArgv()} for that. It is a fatal
1561- error to call it for a second time without calling
1562- \code {Py_Finalize()} first. There is no return value; it is a fatal
1563- error if the initialization fails.
1758+ \code {sys.argv}; use \code {PySys_SetArgv()} for that. This is a no-op
1759+ when called for a second time (without calling \code {Py_Finalize()}
1760+ first). There is no return value; it is a fatal error if the
1761+ initialization fails.
1762+ \end {cfuncdesc }
1763+
1764+ \begin {cfuncdesc }{int}{Py_IsInitialized}{}
1765+ \strong {NEW in 1.5a4!}
1766+ Return true (nonzero) when the Python interpreter has been
1767+ initialized, false (zero) if not. After \code {Py_Finalize()} is
1768+ called, this returns false until \code {Py_Initialize()} is called
1769+ again.
15641770\end {cfuncdesc }
15651771
15661772\begin {cfuncdesc }{void}{Py_Finalize}{}
1773+ \strong {NEW in 1.5a3!}
15671774Undo all initializations made by \code {Py_Initialize()} and subsequent
15681775use of Python/C API functions, and destroy all sub-interpreters (see
15691776\code {Py_NewInterpreter()} below) that were created and not yet
1570- destroyed since the last call to \code {Py_Initialize()}. Ideally,
1571- this frees all memory allocated by the Python interpreter. It is a
1572- fatal error to call it for a second time without calling
1573- \code {Py_Initialize()} again first. There is no return value; errors
1777+ destroyed since the last call to \code {Py_Initialize()}. Ideally,
1778+ this frees all memory allocated by the Python interpreter. This is a
1779+ no-op when called for a second time ( without calling
1780+ \code {Py_Initialize()} again first) . There is no return value; errors
15741781during finalization are ignored.
15751782
15761783This function is provided for a number of reasons. An embedding
@@ -1595,6 +1802,7 @@ \chapter{Initialization, Finalization, and Threads}
15951802\end {cfuncdesc }
15961803
15971804\begin {cfuncdesc }{PyThreadState *}{Py_NewInterpreter}{}
1805+ \strong {NEW in 1.5a3!}
15981806Create a new sub-interpreter. This is an (almost) totally separate
15991807environment for the execution of Python code. In particular, the new
16001808interpreter has separate, independent versions of all imported
@@ -1647,6 +1855,7 @@ \chapter{Initialization, Finalization, and Threads}
16471855\end {cfuncdesc }
16481856
16491857\begin {cfuncdesc }{void}{Py_EndInterpreter}{PyThreadState *tstate}
1858+ \strong {NEW in 1.5a3!}
16501859Destroy the (sub-)interpreter represented by the given thread state.
16511860The given thread state must be the current thread state. See the
16521861discussion of thread states below. When the call returns, the current
@@ -1658,6 +1867,7 @@ \chapter{Initialization, Finalization, and Threads}
16581867\end {cfuncdesc }
16591868
16601869\begin {cfuncdesc }{void}{Py_SetProgramName}{char *name}
1870+ \strong {NEW in 1.5a3!}
16611871This function should be called before \code {Py_Initialize()} is called
16621872for the first time, if it is called at all. It tells the interpreter
16631873the value of the \code {argv[0]} argument to the \code {main()} function
@@ -1729,12 +1939,13 @@ \chapter{Initialization, Finalization, and Threads}
17291939\end {cfuncdesc }
17301940
17311941\begin {cfuncdesc }{char *}{Py_GetProgramFullPath}{}
1942+ \strong {NEW in 1.5a3!}
17321943Return the full program name of the Python executable; this is
17331944computed as a side-effect of deriving the default module search path
17341945from the program name (set by \code {Py_SetProgramName()} above). The
17351946returned string points into static storage; the caller should not
17361947modify its value. The value is available to Python code as
1737- \code {sys.executable}. % XXX is that the right sys.name?
1948+ \code {sys.executable}.
17381949\end {cfuncdesc }
17391950
17401951\begin {cfuncdesc }{char *}{Py_GetPath}{}
@@ -1822,15 +2033,22 @@ \chapter{Initialization, Finalization, and Threads}
18222033\section {Thread State and the Global Interpreter Lock }
18232034
18242035\begin {cfuncdesc }{void}{PyEval_AcquireLock}{}
2036+ \strong {NEW in 1.5a3!}
2037+ HIRO
2038+
2039+
18252040\end {cfuncdesc }
18262041
18272042\begin {cfuncdesc }{void}{PyEval_ReleaseLock}{}
2043+ \strong {NEW in 1.5a3!}
18282044\end {cfuncdesc }
18292045
18302046\begin {cfuncdesc }{void}{PyEval_AcquireThread}{PyThreadState *tstate}
2047+ \strong {NEW in 1.5a3!}
18312048\end {cfuncdesc }
18322049
18332050\begin {cfuncdesc }{void}{PyEval_ReleaseThread}{PyThreadState *tstate}
2051+ \strong {NEW in 1.5a3!}
18342052\end {cfuncdesc }
18352053
18362054\begin {cfuncdesc }{void}{PyEval_RestoreThread}{PyThreadState *tstate}
0 commit comments