Linux/Unix > Python Essential Reference, Second Edition > B. Extending and Embedding Python > Abstract Object Layer | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238111161105095080117111038137200

### Abstract Object Layer

The functions in Tables B.4 through B.8 are used to manipulate objects from C, in much the same manner as from the interpreter. All the functions in this section that return an `int` return `-1` if an error occurs. Likewise, functions that return a `PyObject *` return `NULL` on failure. Note that an “error” in this context is not the same as the false result of a test. For instance, the `PyNumber_Check(PyObject *``obj` `)` function returns `0` if `obj` is not a number, but this isn’t the same as an error. Finally, unless otherwise noted, all functions in this section that return a `PyObject *` return ownership with the object. It’s up to the caller to decrement the reference count of the returned object if necessary.

##### Table B.4. Objects

Type | Function
--- | ---
`int` | `PyCallable_Check(PyObject *``o` `)`
`PyObject *` | `PyObject_CallFunction(PyObject *``callable_object` `, char *``format` `,...)`
`PyObject *` | `PyObject_CallMethod(PyObject *``o` `, char *``methodname` `, char *``format` `, ...)`
`PyObject *` | `PyObject_CallObject(PyObject *``callable_object` `, PyObject *``args` `)`
`void` | `PyObject_ClearWeakRefs(PyObject *``obj` `)`
`int` | `PyObject_Cmp(PyObject *``o1` `, PyObject *``o2` `, int *``result` `)`
`int` | `PyObject_Compare(PyObject *``o1` `, PyObject *``o2` `)`
`int` | `PyObject_DelAttr(PyObject *``o` `, PyObject *``attr_name` `)`
`int` | `PyObject_DelAttrString(PyObject *``o` `, char *``attr_name` `)`
`int` | `PyObject_DelItem(PyObject *``o` `, PyObject *``key` `)`
`PyObject *` | `PyObject_GetAttr(PyObject *``o` `, PyObject *``attr_name` `)`
`PyObject *` | `PyObject_GetAttrString(PyObject *``o` `, char *``attr_name` `)`
`PyObject *` | `PyObject_GetItem(PyObject *``o` `, PyObject *``key` `)`
`int` | `PyObject_HasAttr(PyObject *``o` `, PyObject *``attr_name` `)`
`int` | `PyObject_HasAttrString(PyObject *``o` `, char *``attr_name` `)`
`int` | `PyObject_Hash(PyObject *``o` `)`
`int` | `PyObject_IsTrue(PyObject *``o` `)`
`int` | `PyObject_Length(PyObject *``o` `)`
`int` | `PyObject_Print(PyObject *``o` `, FILE *``fp` `, int` `flags` `)`
`PyObject *` | `PyObject_Repr(PyObject *``o` `)`
`PyObject *` | `PyObject_RichCompare(PyObject *``o1` `, PyObject *``o2` `, int` `op` `)`
`int` | `PyObject_RichCompareBool(PyObject *``o1` `, PyObject *``o2` `, int` `op` `)`
`int` | `PyObject_SetAttr(PyObject *``o` `, PyObject *``attr_name` `, PyObject *``v` `)`
`int` | `PyObject_SetAttrString(PyObject *``o` `, char *``attr_name` `, PyObject *``v` `)`
`int` | `PyObject_SetItem(PyObject *``o` `, PyObject *``key` `, PyObject *``v` `)`
`PyObject *` | `PyObject_Str(PyObject *``o` `)`
`PyObject *` | `PyObject_Type(PyObject *``o` `)`

The `flags` argument of `PyObject_Print()` is used to select printing options. Currently, the only option is `Py_PRINT_RAW`, which forces `PyObject_Print()` to produce output using `PyObject_Str()` as opposed to `PyObject_Repr()` (the default).

`PyObject_Hash()` and `PyObject_Length()` return a positive integer result on success and `-1` on error.

The `op` argument to `PyObject_RichCompare()` and `PyObject_RichCompareBool()` is one of `Py_EQ`, `Py_NE`, `Py_LT`, `Py_GT`, `Py_GE`, or `Py_LE`.

##### Table B.5. Numbers

Type | Function
--- | ---
`PyObject *` | `PyNumber_Absolute(PyObject *``o` `)`
`PyObject *` | `PyNumber_Add(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_And(PyObject *``o1` `, PyObject *``o2` `)`
`int` | `PyNumber_Check(PyObject *``o` `)`
`PyObject *` | `PyNumber_Coerce(PyObject **``p1` `, PyObject **``p2` `)`
`PyObject *` | `PyNumber_Divide(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_Divmod(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_Float(PyObject *``o` `)`
`PyObject *` | `PyNumber_Int(PyObject *``o` `)`
`PyObject *` | `PyNumber_Invert(PyObject *``o` `)`
`PyObject *` | `PyNumber_Long(PyObject *``o` `)`
`PyObject *` | `PyNumber_Lshift(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_Multiply(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_Negative(PyObject *``o` `)`
`PyObject *` | `PyNumber_Or(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_Positive(PyObject *``o` `)`
`PyObject *` | `PyNumber_Power(PyObject *``o1` `, PyObject *``o2` `, PyObject *``o3` `)`
`PyObject *` | `PyNumber_Remainder(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_Rshift(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_Subtract(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_Xor(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_InPlaceAdd(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_InPlaceSubtract(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_InPlaceMultiply(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_InPlaceDivide(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_InPlaceRemainder(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_InPlacePower(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_InPlaceLshift(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_InPlaceRshift(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_InPlaceAnd(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_InPlaceXor(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PyNumber_InPlaceOr(PyObject *``o1` `, PyObject *``o2` `)`

##### Table B.6. Sequences

Type | Function
--- | ---
`int` | `PySequence_Check(PyObject *``o` `)`
`PyObject *` | `PySequence_Concat(PyObject *``o1` `, PyObject *``o2` `)`
`int` | `PySequence_Contains(PyObject *``o` `, PyObject *``value` `)`
`int` | `PySequence_Count(PyObject *``o` `, PyObject *``value` `)`
`int` | `PySequence_DelItem(PyObject *``o` `, int )`
`int` | `PySequence_DelSlice(PyObject *``o` `, int` `i1` `, int` `i2` `)`
`PyObject *` | `PySequence_GetItem(PyObject *``o` `, int )`
`PyObject *` | `PySequence_GetSlice(PyObject *``o` `, int` `i1` `, int` `i2` `)`
`int` | `PySequence_In(PyObject *``o` `, PyObject *``value` `)`
`int` | `PySequence_Index(PyObject *``o` `, PyObject *``value` `)`
`PyObject *` | `PySequence_List(PyObject *``o` `)`
`PyObject *` | `PySequence_Repeat(PyObject *``o` `, int` `count` `)`
`int` | `PySequence_SetItem(PyObject *``o` `, int , PyObject *``v` `)`
`int` | `PySequence_SetSlice(PyObject *``o` `, int` `i1` `, int` `i2` `, PyObject *``v` `)`
`PyObject *` | `PySequence_Tuple(PyObject *``o` `)`
`PyObject *` | `PySequence_InPlaceConcat(PyObject *``o1` `, PyObject *``o2` `)`
`PyObject *` | `PySequence_InPlaceRepeat(PyObject *``o1` `, int` `count` `)`
`PyObject *` | `PySequence_Fast(PyObject *``o` `, const char *``errmsg` `)`
`PyObject *` | `PySequence_Fast_GET_ITEM(PyObject *``o` `, int )`

The `PySequence_Fast()` function returns `o` unmodified if it’s already a tuple or a list and increases its reference count. Otherwise, `o` must be a sequence type. If it’s not a sequence, a `TypeError` is raised and `errmsg` is used as the error message. The `PySequence_Fast_GET_ITEM()` function should only be used with objects returned by `PySequence_Fast()`.

##### Table B.7. Mappings

Type | Function
--- | ---
`int` | `PyMapping_Check(PyObject *``o` `)`
`int` | `PyMapping_Clear(PyObject *``o` `)`
`int` | `PyMapping_DelItem(PyObject *``o` `, PyObject *``key` `)`
`int` | `PyMapping_DelItemString(PyObject *``o` `, char *``key` `)`
`PyObject *` | `PyMapping_GetItemString(PyObject *``o` `, char *``key` `)`
`int` | `PyMapping_HasKey(PyObject *``o` `, PyObject *``key` `)`
`int` | `PyMapping_HasKeyString(PyObject *``o` `, char *``key` `)`
`PyObject *` | `PyMapping_Items(PyObject *``o` `)`
`PyObject *` | `PyMapping_Keys(PyObject *``o` `)`
`int` | `PyMapping_Length(PyObject *``o` `)`
`int` | `PyMapping_SetItemString(PyObject *``o` `, char *``key` `, PyObject *``v` `)`
`PyObject *` | `PyMapping_Values(PyObject *``o` `)`

##### Table B.8. Buffer Interface

Type | Function
--- | ---
`int` | `PyObject_AsCharBuffer(PyObject *, const char **``buffer` `, int *``len` `)`
`int` | `PyObject_AsReadBuffer(PyObject *, const void **``buffer` `, int *``len` `)`
`int` | `PyObject_AsWriteBuffer(PyObject *, void **``buffer` `, int *``len` `)`

The buffer interface is used by objects that want to expose the raw bytes used to store data to the caller without having to make a copy. Typically this is only used by strings, Unicode strings, and arrays as created in the `array` module. The size and interpretation of the data depends on the underlying object.

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.