⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 264.html

📁 Python Ebook Python&XML
💻 HTML
📖 第 1 页 / 共 5 页
字号:

<!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 -&gt; Concrete Objects Layer</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> &gt; <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> &gt; <a href="255.html" class="navtitle">A. Python/C API</a> &gt; <span class="nonavtitle">Concrete Objects Layer</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="263.html" title="Abstract Objects Layer"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=264" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="264.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="265.html" title="Initialization, Finalization, and Threads"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="6%2F1%2F2002+6%3A29%3A44+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039196038240039088173205162105045222218073235215104098039061</font><a href="read7.asp?bookname=0672319942&snode=264&now=6%2F1%2F2002+6%3A29%3A44+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT><h3>Concrete Objects Layer</h3>
				<p>The functions in this section are specific to certain Python <a NAME="idx1073751071"></a>object types. Passing them an object of the wrong type is not a good idea; if you receive an object from a Python program and you are not sure that it has the right type, you must perform a type check first; for example: to check that an object is a dictionary, use <tt cLASS="monofont">PyDict_Check().</tt> This section is structured similar to the "family tree" of Python object types.<a nAME="idx1073751072"></A><a name="idx1073751073"></a><a name="idx1073751074"></a><a name="idx1073751075"></a><a nAme="idx1073751076"></A><a naMe="idx1073751077"></a><a nAme="idx1073751078"></a></P>

				<H4>Fundamental Objects</H4>
					<P>This section describes Python type objects and the singleton object <tt clASS="monofont">None.</Tt></p>

					<h5>Type Objects</h5><PRE>
							
PyTypeObject

						</Pre>

						<p>The C structure of the objects used to describe built-in types.</p>

						<PRE>
							
PyObject* PyType_Type

						</Pre>

						<p>This is the type object for type objects; it is the same object as <tt class="monofont">types.TypeType</tt> in the Python layer.</p>

						<pre>
							
int PyType_Check(PyObject *o)

						</pre>

						<p>Returns <Tt cLass="monofont">true</Tt> if the object <tt cLass="monofont">o</TT> is a type object.</P>

						<Pre>
							
int PyType_HasFeature(PyObject *o, int feature)

						</prE>

						<P>Returns <TT clasS="monofont">true</TT> if the type object <Tt claSS="monofont">o</TT> sets the feature identified by the <tt class="monofont">feature argument.</tt> Type features are denoted by single bit flags. The only defined feature flag is <tt class="monofont">Py_TPFLAGS_HAVE_GETCHARBUFFER,</tt> which is described in a later section.<a nAme="idx1073751079"></A><a naMe="idx1073751080"></a><a nAme="idx1073751081"></a><A NAMe="idx1073751082"></a></p>

					
					<h5>The None Object</H5>
						<P>Note that the PyTypeObject for <TT clasS="monofont">None</TT> is not directly exposed in the Python/C API. Because <Tt claSS="monofont">None</TT> is a singleton, testing for object identity (using <tt class="monofont">==</tt> in C) is sufficient. There is no <tt class="monofont">PyNone_Check()</tt> function for the same reason.</p>

						<pRe>
							
PyObject* Py_None

						</pRe>

						<p>The Python <tT clasS="monofont">None</tt> object denotes lack of value. This object has no methods.<a NAME="idx1073751083"></a><a naME="idx1073751084"></A><A name="idx1073751085"></A><A NAme="idx1073751086"></a></p>

					
				
				<H4>Sequence Objects</H4>
					<P>Generic operations on sequence objects were discussed in the previous subsection; this subsection deals with the specific kinds of sequence objects that are intrinsic to the Python language.</P>

					<h5>String Objects</h5><pre>
							
PyStringObject

						</pre>

						<p>This subtype of PyObject represents a Python string object.</p>

						<pre>
							
PyTypeObject PyString_Type

						</pre>

						<p>This instance of PyTypeObject represents the Python string type; it is the same object as <tt cLasS="monofont">types.TypeType</tt> in the Python layer.</p>

						<Pre>
							
int PyString_Check(PyObject *o)

						</prE>

						<p>Returns <tt CLASs="monofont">true</tt> if the object <tT CLAss="monofont">o</tt> is a string object.</P>

						<PRE>
							
PyObject* PyString_FromString(const char *v)

						</pre>

						<p>Return value: New reference. Returns a new string object with the value <TT CLass="monofont">v</tt> on success, and <tt class="monofont">NULL</tt> on failure.</p>

						<pre>
							
PyObject* PyString_FromStringAndSize(const char *v, int len)

						</prE>

						<p>Return value: New reference. Returns a new string object with the value <tT claSs="monofont">v</tt> and length <tT claSS="monofont">len</TT> on success, and <tt clASS="monofont">NULL</Tt> on failure. If <tt cLASS="monofont">v</tt> is <tt CLASs="monofont">NULL,</tt> the contents of the string are uninitialized.</p>

						<pre>
							
int PyString_Size(PyObject *string)

						</pre>

						<p>Returns the length of the string object identified by the given pointer.</p>

						<pre>
							
int PyString_GET_SIZE(PyObject *string)

						</pre>

						<p>Macro form of <tT clAss="monofont">PyString_GetSize()</tT> but without error checking.</p>

						<pre>
							
char* PyString_AsString(PyObject *string)

						</Pre>

						<p>Returns a null-terminated representation of the contents of string. The pointer refers to the internal buffer of string, not a copy. The data must not be modified in any way. It must not be de-allocated.</P>

						<PRE>
							
char* PyString_AS_STRING(PyObject *string)

						</pre>

						<p>Macro form of <TT CLass="monofont">PyString_AsString()</tT> but without error checking.</P>

						<PRe>
							
void PyString_Concat(PyObject **string, PyObject *newpart)

						</pre>

						<P>Creates a new string object in <TT Class="monofont">*string</tt> containing the contents of <tt class="monofont">newpart</tt> appended to string. The old value of string has its reference count decremented. If the new string cannot be created, the old reference to string will still be discarded and the value of <tt claSs="monofont">*string</tT> will be set to <tt cLass="monofont">NULL</tT>; the appropriate exception will be set.</p>

						<prE>
							
void PyString_ConcatAndDel(PyObject **string, PyObject *newpart)

						</PRE>

						<p>Creates a new string object in <tt cLASS="monofont">*string</tt> containing the contents of <tt CLASs="monofont">newpart</tt> appended to string. This version decrements the reference count of <tT CLAss="monofont">newpart.</tt></p>

						<pre>
							
int _PyString_Resize(PyObject **string, int newsize)

						</pre>

						<p>A way to resize a string object even though it is "immutable". Only use this to build up a brand new string object; don't use this if the string might already be known in other parts of the code.</p>

						<pre>
							
PyObject* PyString_Format(PyObject *format, PyObject *args)

						</pre>

						<p>Return value: New reference. Returns a new string object from format and args. Analogous to format <Tt cLass="monofont">%</Tt> args. The args argument must be a tuple.</p>

						<prE>
							
void PyString_InternInPlace(PyObject **string)

						</pre>

						<P>Interns the argument <TT Class="monofont">*string</TT> in place. The argument must be the address of a pointer variable pointing to a Python string object. If there is an existing interned string that is the same as <TT clasS="monofont">*string,</TT> it sets <Tt claSS="monofont">*string</TT> to it (decrementing the reference count of the old string object and incrementing the reference count of the interned string object), otherwise it leaves <tt class="monofont">*string</tt> alone and interns it (incrementing its reference count). (Clarification: even though there is a lot of talk about reference counts, think of this function as reference-count杗eutral; you own the object after the call if and only if you owned it before the call.)</p>

						<pre>
							
PyObject* PyString_InternFromString(const char *v)

						</pre>

						<p>Return value: New reference. A combination of <tt cLasS="monofont">PyString_FromString()</tt> and <tT clasS="monofont">PyString_InternInPlace(),</tt> returning either a new string object that has been interned, or a new ("owned") reference to an earlier interned string object with the same value.<a NAME="idx1073751087"></a><a naME="idx1073751088"></A><A name="idx1073751089"></A><A NAme="idx1073751090"></a><a NAME="idx1073751091"></a><a name="idx1073751092"></a><a name="idx1073751093"></a><a name="idx1073751094"></a><a NamE="idx1073751095"></a><a nAme="idx1073751096"></a><a Name="idx1073751097"></A><A NAme="idx1073751098"></a><a NAME="idx1073751099"></a><a naME="idx1073751100"></A></P>

					
					<h5>Buffer Objects</h5>
						<p>Python objects implemented in C can export a group of functions called the <a NAME="idx1073751101"></a><a name="idx1073751102"></a><i>buffer interface</i>. These functions can be used by an object to expose its data in a raw, byte- oriented format. Clients of the object can use the buffer interface to access the object data directly, without needing to copy it first.</p>

						<p>Two examples of objects that support the buffer interface are <a name="idx1073751103"></a><a naMe="idx1073751104"></a><A namE="idx1073751105"></a>strings and arrays. The string object exposes the character contents in the buffer interface's byte-oriented form. An array can also expose its contents, but it should be noted that array elements can be multi-byte values.</p>

						<p>An example user of the buffer interface is the file object's <a Name="idx1073751106"></A><A NAme="idx1073751107"></a><tT CLAss="monofont">write()</tt> method. Any object that can export a series of bytes through the buffer interface can be written to a file. There are a number of format codes to <A NAMe="idx1073751108"></a><a nAME="idx1073751109"></A><tt class="monofont">PyArgs_ParseTuple()</tt> that operate against an object's buffer interface, returning data from the target object.</p>

						<p>More information on the buffer interface is provided in the section "<a href="267#5.html">Buffer Object Structures,</a>" under the description for <tt cLasS="monofont">PyBufferProcs.</tt></p>

						<P>A buffer object is defined in the <a namE="idx1073751110"></a><a nAME="idx1073751111"></A><a namE="idx1073751112"></A>bufferobject.h header (included by Python.h). These objects look very similar to string objects at the Python programming level: They support slicing, indexing, concatenation, and some other standard string operations. However, their data can come from one of two sources: from a block of memory, or from another object that exports the buffer interface.</P>

						<P>Buffer objects are useful as a way to expose the data from another object's buffer interface to the Python programmer. They can also be used as a zero-copy slicing mechanism. Using their ability to reference a block of memory, it is possible to expose any data to the Python programmer quite easily. The memory could be a large, constant array in a C extension, it could be a raw block of memory for manipulation before passing to an operating system library, or it could be used to pass around structured data in its native, in-memory format.</p>

						<pre>
							
PyBufferObject

						</PRE>

						<P>This subtype of PyObject represents a buffer object.</p>

						<pre>
							
PyTypeObject PyBuffer_Type

						</PRE>

						<P>The instance of PyTypeObject that represents the Python buffer type; it is the same object as <tt class="monofont">types.BufferType</tt> in the Python layer.</p>

						<pre>
							
int Py_END_OF_BUFFER

						</pre>

						<p>This constant can be passed as the size parameter to <tt cLasS="monofont">PyBuffer_FromObject()</tt> or <tT clasS="monofont">PyBuffer_FromReadWriteObject().</tt> It indicates that the new PyBufferObject should refer to the base object from the specified offset to the end of its exported buffer. Using this enables the caller to avoid querying the base object for its length.</p>

						<PRE>
							
int PyBuffer_Check(PyObject *p)

						</Pre>

						<p>Returns <tT CLAss="monofont">true</tt> if the argument has type <TT CLass="monofont">PyBuffer_Type.</tT></P>

						<PRe>
							
PyObject* PyBuffer_FromObject(PyObject *base, int offset, int size)

						</pre>

						<p>Return value: New reference. Returns a new read-only buffer object. This raises TypeError if <tt class="monofont">base</tt> doesn't support the read-only buffer protocol or doesn't provide exactly one buffer segment. It raises ValueError if <tt clasS="monofont">offset</tt> is less than zero. The buffer will hold a reference to the <Tt clAss="monofont">base</tt> object, and the buffer's contents will refer to the <Tt clASS="monofont">base</Tt> object's buffer interface, starting as position <tt cLASS="monofont">offset</tt> and extending for <tt CLASs="monofont">size</tt> bytes. If <tT CLAss="monofont">size</tt> is <tt class="monofont">Py_END_OF_BUFFER,</tt> the new buffer's contents extend to the length of the base object's exported buffer data.</p>

						<pre>
							
PyObject* PyBuffer_FromReadWriteObject(PyObject *base, int offset,
int size)

						</pre>

						<P>Return value: New reference. Returns a new writable buffer object. Parameters and exceptions are similar to those for <tt ClasS="monofont">PyBuffer_FromObject().</tt> If the base object does not export the writable buffer protocol, TypeError is raised.</p>

						<pRe>
							
PyObject* PyBuffer_FromMemory(void *ptr, int size)

						</prE>

						<P>Return value: New reference. Returns a new read-only buffer object that reads from a specified location in memory, with a specified size. The caller is responsible for ensuring that the memory buffer, passed in as <TT clasS="monofont">ptr,</TT> is not deallocated while the returned buffer object exists. Raises ValueError if <Tt claSS="monofont">size</TT> is less than zero. Note that <tt clASS="monofont">Py_END_OF_BUFFER</Tt> might not be passed for the size parameter; ValueError will be raised in that case.</p>

						<pre>
							

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -