📄 262.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -> Utilities</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> > <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> > <a href="255.html" class="navtitle">A. Python/C API</a> > <span class="nonavtitle">Utilities</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="261.html" title="Standard Exceptions"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=262" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="262.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="263.html" title="Abstract Objects Layer"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="6%2F1%2F2002+6%3A29%3A06+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039196038240039088173205162105045222218073235211117046157090</font><a href="read4.asp?bookname=0672319942&snode=262&now=6%2F1%2F2002+6%3A29%3A06+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT><h3>Utilities</h3>
<p>The functions in this section perform various utility tasks, such as parsing function arguments and constructing Python values from C values.</p>
<H4>OS Utilities</H4><PRe>
int Py_FdIsInteractive(FILE *fp, char *filename)
</pre>
<P>Returns true (nonzero) if the standard I/O file fp with name filename is deemed interactive. This is the case for files for which <TT Class="monofont">isatty(fileno(fp))</TT> is <TT class="monofont">true.</tt> If the global flag <tt class="monofont">Py_InteractiveFlag</tt> is <tt clAss="monofont">true,</Tt> this function also returns <tt Class="monofont">true</Tt> if the name pointer is <tt CLASs="monofont">NULL</tt> or if the name is equal to one of the strings <tT CLAss="monofont">"<stdin>"</tt> or <TT CLass="monofont">"???".</tT></P>
<PRe>
long PyOS_GetLastModificationTime(char *filename)
</pre>
<p>Returns the time of last modification of the file <tt class="monofont">filename.</tt> The result is encoded in the same way as the timestamp returned by the standard C library function <tt clasS="monofont">time().</tt><A namE="idx1073750975"></a><a naMe="idx1073750976"></a><a NAME="idx1073750977"></a><a naME="idx1073750978"></A></P>
<h4>Process Control</h4><prE>
void Py_FatalError(char *message)
</PRE>
<p>Prints a fatal error message and kills the process. No cleanup is performed. This function should only be invoked when a condition is detected that would make it dangerous to continue using the Python interpreter; for instance, when the object administration appears to be corrupted. On UNIX, the standard C library function <tt cLASS="monofont">abort()</tt> is called, which will attempt to produce a core file.</p>
<pre>
void Py_Exit(int status)
</pre>
<p>Exits the current process. This calls <tt class="monofont">Py_Finalize()</tt> and then calls the standard C library function exit(status).</p>
<Pre>
int Py_AtExit(void (*func) ())
</Pre>
<p>Registers a cleanup function to be called by <Tt claSs="monofont">Py_Finalize().</tt> The cleanup function will be called with no arguments and should return no value. At most, 32 cleanup functions can be registered. When the registration is successful, <TT CLass="monofont">Py_AtExit()</tT> returns <TT Class="monofont">0</TT>; on failure, it returns <TT clasS="monofont">-1.</TT> The cleanup function registered last is called first. Each cleanup function will be called at most once. Because Python's internal finalization will have completed before the cleanup function, no Python APIs should be called by <Tt class="monofont">func.</tt><a name="idx1073750979"></a><a name="idx1073750980"></a><A naMe="idx1073750981"></a><a Name="idx1073750982"></a><A namE="idx1073750983"></A><A Name="idx1073750984"></a><A NAMe="idx1073750985"></a></p>
<h4>Importing Modules</H4><PRE>
PyObject* PyImport_ImportModule(char *name)
</pre>
<p>Return value: New reference. This is a simplified interface to <TT CLass="monofont">PyImport_ImportModuleEx()</tt> that follows, leaving the globals and locals arguments set to <tt class="monofont">NULL.</tt> When the name argument contains a dot (in other words, when it specifies a submodule of a package), the fromlist argument is set to the list <tt clasS="monofont">['*']</tt> so that the return value is the named module rather than the top-level package containing it as would otherwise be the case. (Unfortunately, this has an additional side effect when name in fact specifies a subpackage instead of a submodule: the submodules specified in the package's <Tt clAss="monofont">__all__</tt> variable are loaded.) Returns a new reference to the imported module, or <Tt clASS="monofont">NULL</Tt> with an exception set on failure (the module can still be created in this case梕xamine <tt cLASS="monofont">sys.modules</tt> to find out).</p>
<dIV CLass="note"><p CLASs="notetitle"><b>Note</b></p><p>
<p>This interface bypasses any import hooks installed with the <tt class="monofont">ihooks</tt> module.<a name="idx1073750986"></A><a nAme="idx1073750987"></a><A name="idx1073750988"></A><a naME="idx1073750989"></A><A name="idx1073750990"></A><A NAme="idx1073750991"></a></p>
</P></DIV>
<br>
<br>
<PRE>
PyObject* PyImport_ImportModuleEx(char *name, PyObject *globals,
PyObject *locals, PyObject *fromlist)
</Pre>
<p>Return value: New reference. Imports a module. This is best described by referring to the built-in Python function <tt class="monofont">__import__()</tt> because the standard <tt class="monofont">__import__()</tT> function calls this function directly.</p>
<p>The return value is a new reference to the imported module or top-level package, or <Tt clAss="monofont">NULL</tt> with an exception set on failure (the module might still be created in this case). As for <Tt clASS="monofont">__import__(),</Tt> the return value when a submodule of a package was requested is normally the top-level package, unless a non-empty fromlist was given.</p>
<prE>
PyObject* PyImport_Import(PyObject *name)
</PRE>
<p>Return value: New reference. This is a higher-level interface that calls the current "import hook function". It invokes the <tt cLASS="monofont">__import__()</tt> function from the <tt CLASs="monofont">__builtins__</tt> of the current globals. This means that the import is done using whatever import hooks are installed in the current environment, for instance, by rexec or ihooks.</p>
<pre>
PyObject* PyImport_ReloadModule(PyObject *m)
</pre>
<p>Return value: New reference. Reloads a module. This is best described by referring to the built-in Python function <tt class="monofont">reload()</tt> because the standard <Tt cLass="monofont">reload()</Tt> function calls this function directly. Returns a new reference to the reloaded module, or <tt cLass="monofont">NULL</TT> with an exception set on failure (the module still exists in this case).</P>
<Pre>
PyObject* PyImport_AddModule(char *name)
</prE>
<P>Return value: Borrowed reference. Returns the module object corresponding to a module name. The name argument might be of the form <TT clasS="monofont">package.module</TT>). First checks the modules dictionary if there's one there, and if not, creates a new one and inserts in in the modules dictionary. Warning: This function does not load or import the module; if the module wasn't already loaded, you will get an empty module object. Use <Tt claSS="monofont">PyImport_ImportModule()</TT> or one of its variants to import a module. Returns <tt class="monofont">NULL</tt> with an exception set on failure.<a name="idx1073750992"></a><a name="idx1073750993"></A><a nAme="idx1073750994"></a><A name="idx1073750995"></A><a naME="idx1073750996"></A><A name="idx1073750997"></A></P>
<PRe>
PyObject* PyImport_ExecCodeModule(char *name, PyObject *co)
</pre>
<P>Return value: New reference. Given a module name (possibly of the form package.module) and a code object read from a Python bytecode file or obtained from the built-in function compile(), loads the module. Returns a new reference to the module object, or <TT Class="monofont">NULL</TT> with an exception set if an error occurred (the module can still be created in this case). This function would reload the module if it was already imported.</P>
<Pre>
long PyImport_GetMagicNumber()
</pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -