Linux/Unix > Python Essential Reference, Second Edition > B. Extending and Embedding Python > Low-Level Functions on Built-in Types | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238111161104058145059051143020002

### Low-Level Functions on Built-in Types

The functions in Tables B.9 through B.21 can be used to manipulate various built-in types. Functions of the form `Py<``type` `>_Check()` are used to check the type of an object and return `1` if an object is the correct type, `0` otherwise. Functions of the form `Py<``type` `>_From<``type` `>` are used to create a Python object from a C datatype. Functions of the form `Py<``type` `>_As<``type` `>` are used to convert from Python to C. These functions are presented without further description.

##### Table B.9. Integers

Type | Function
--- | ---
`long` | `PyInt_AsLong(PyObject *``iobj` `)`
`int` | `PyInt_Check(PyObject *``obj` `)`
`PyObject *` | `PyInt_FromLong(long)`
`long` | `PyInt_GetMax(void)`

##### Table B.10. Long Integers

Type | Function
--- | ---
`double` | `PyLong_AsDouble(PyObject *``lobj` `)`
`long` | `PyLong_AsLong(PyObject *``lobj` `)`
`long long` | `PyLong_AsLongLong(PyObject *``lobj` `)`
`unsigned long` | `PyLong_AsUnsignedLong(PyObject *``lobj` `)`
`unsigned long long` | `PyLong_AsUnsignedLongLong(PyObject *``lobj` `)`
`void *` | `PyLong_AsVoidPtr(PyObject *``lobj` `)`
`int` | `PyLong_Check(PyObject *``obj` `)`
`PyObject *` | `PyLong_FromDouble(double)`
`PyObject *` | `PyLong_FromLong(long)`
`PyObject *` | `PyLong_FromLongLong(long long)`
`PyObject *` | `PyLong_FromUnsignedLong(unsigned long)`
`PyObject *` | `PyLong_FromUnsignedLongLong(unsigned long long)`
`PyObject *` | `PyLong_FromVoidPtr(void *)`

##### Table B.11. Floats

Type | Function
--- | ---
`int` | `PyFloat_Check(PyObject *``obj` `)`
`double` | `PyFloat_AsDouble(PyObject *``fobj` `)`
`PyObject *` | `PyFloat_FromDouble(double)`

##### Table B.12. Complex

Type | Function
--- | ---
`Py_complex` | `PyComplex_AsCComplex(PyObject *``cobj` `)`
`int` | `PyComplex_Check(PyObject *``obj` `)`
`PyObject *` | `PyComplex_FromCComplex(Py_complex *``cobj` `)`
`PyObject *` | `PyComplex_FromDoubles(double` `real` `, double` `imag` `)`
`double` | `PyComplex_ImagAsDouble(PyObject *``cobj` `)`
`double` | `PyComplex_RealAsDouble(PyObject *``cobj` `)`

##### Table B.13. Strings

Type | Function
--- | ---
`char *` | `PyString_AsString(PyObject *``str` `)`
`PyObject *` | `PyString_AsEncodedString(PyObject *``unicode` `, const char *``encoding` `, const char *``errors` `)`
`int` | `PyString_Check(PyObject *``obj` `)`
`void` | `PyString_Concat(PyObject **``str` `, PyObject *``newpart` `)`
`void` | `PyString_ConcatAndDel(PyObject **``str` `, PyObject *``newpart` `)`
`PyObject *` | `PyString_Decode(const char *``s` `, int` `size` `, const char *``encoding` `, const char *``errors` `)`
`PyObject *` | `PyString_Encode(const Py_UNICODE *``s` `, int` `size` `, const char *``encoding` `, const char *``errors` `)`
`PyObject *` | `PyString_Format(PyObject *``format` `, PyObject *``args` `)`
`PyObject *` | `PyString_FromString(char *``str` `)`
`PyObject *` | `PyString_FromStringAndSize(char *``str` `, int` `len` `)`
`int` | `PyString_Resize(PyObject **``str` `, int` `newsize` `)`
`int` | `PyString_Size(PyObject *``str` `)`

Note: `Encode` and `Decode` functions expect encoding and error parameters that are the same as the built-in `unicode()` function.

##### Table B.14. Unicode

Type | Function
--- | ---
`int` | `PyUnicode_Check(PyObject *``o` `)`
`PyObject *` | `PyUnicode_FromUnicode(Py_UNICODE *, int` `size` `)`
`Py_UNICODE *` | `PyUnicode_AsUnicode(PyObject *``o` `)`
`int` | `PyUnicode_GetSize(PyObject *``o` `)`
`PyObject *` | `PyUnicode_FromEncodedObject(PyObject *``obj` `, const char *``encoding` `, const char *``errors` `)`
`PyObject *` | `PyUnicode_FromObject(PyObject *``o` `)`
`PyObject *` | `PyUnicode_FromWideChar(const wchar_t *, int` `size` `)`
`int` | `PyUnicode_AsWideChar(PyObject *``o` `, wchar_t *``buf` `, int` `maxlen` `)`

##### Table B.15. Lists

Type | Function
--- | ---
`int` | `PyList_Append(PyObject *``list` `, PyObject *``obj` `)`
`PyObject *` | `PyList_AsTuple(PyObject *``list` `)`
`int` | `PyList_Check(PyObject *``obj` `)`
`PyObject *` | `PyList_GetItem(PyObject *``list` `, int` `index` `)`
`PyObject *` | `PyList_GetSlice(PyObject *``list` `, int` `i` `, int` `j` `)`
`int` | `PyList_Insert(PyObject *``list` `, int` `index` `, PyObject *``obj` `)`
`PyObject *` | `PyList_New(int` `size` `)`
`int` | `PyList_Reverse(PyObject *``list` `)`
`int` | `PyList_SetItem(PyObject *``list` `, int` `index` `, PyObject *``obj` `)`
`int` | `PyList_SetSlice(PyObject *``list` `, int` `i` `, int` `j` `, PyObject *``slc` `)`
`int` | `PyList_Size(PyObject *``list` `)`
`int` | `PyList_Sort(PyObject *``list` `)`

Note: `PyList_GetItem()` returns a borrowed reference.

##### Table B.16. Tuples

Type | Function
--- | ---
`int` | `PyTuple_Check(PyObject *``obj` `)`
`PyObject *` | `PyTuple_GetItem(PyObject *``tup` `, int` `index` `)`
`PyObject *` | `PyTuple_GetSlice(PyObject *``tup` `, int` `i` `, int` `j` `)`
`PyObject *` | `PyTuple_New(int` `size` `)`
`int` | `PyTuple_SetItem(PyObject *``tup` `, int` `index` `, PyObject *``obj` `)`
`int` | `PyTuple_Size(PyObject *``tup` `)`

Note: `PyTuple_SetItem()` increments the reference count of `obj` even if it fails, and `PyTuple_GetItem()` returns a borrowed reference.

##### Table B.17. Dictionaries

Type | Function
--- | ---
`int` | `PyDict_Check(PyObject *``obj` `)`
`void` | `PyDict_Clear(PyObject *``dict` `)`
`int` | `PyDict_DelItem(PyObject *``dict` `, PyObject *``key` `)`
`int` | `PyDict_DelItemString(PyObject *``dict` `, char *``key` `)`
`PyObject *` | `PyDict_GetItem(PyObject *``dict` `, PyObject *``key` `)`
`PyObject *` | `PyDict_GetItemString(PyObject *``dict` `, char *``key` `)`
`PyObject *` | `PyDict_Items(PyObject *``dict` `)`
`PyObject *` | `PyDict_Keys(PyObject *``dict` `)`
`PyObject *` | `PyDict_New(void)`
`int` | `PyDict_SetItem(PyObject *``dict` `, PyObject *``key` `, PyObject *``value` `)`
`int` | `PyDict_SetItemString(PyObject *``dict` `, char *``key` `, PyObject *``value` `)`
`int` | `PyDict_Size(PyObject *``dict` `)`
`PyObject *` | `PyDict_Values(PyObject *``dict` `)`

Note: `PyDict_GetItem()` and `PyDict_GetItemString()` return borrowed references.

##### Table B.18. Buffer Objects

Type | Function
--- | ---
`int` | `PyBuffer_check(PyObject *``o` `)`
`PyObject *` | `PyBuffer_FromObject(PyObject *``base` `, int` `offset` `, int` `size` `)`
`PyObject *` | `PyBuffer_FromReadWriteObject(PyObject *``base` `, int` `offset` `, int` `size` `)`
`PyObject *` | `PyBuffer_FromMemory(void *``ptr` `, int` `size` `)`
`PyObject *` | `PyBuffer_FromReadWriteMemory(void *``ptr` `, int` `size` `)`
`PyObject *` | `PyBuffer_New(int` `size` `)`

##### Table B.19. Files

Type | Function
--- | ---
`FILE *` | `PyFile_AsFile(PyObject *``file` `)`
`int` | `PyFile_Check(PyObject *``obj` `)`
`PyObject *` | `PyFile_FromFile(FILE *, char *, char *, int (*)(FILE *))`
`PyObject *` | `PyFile_FromString(char *``name` `, char *``mode` `)`
`PyObject *` | `PyFile_GetLine(PyObject *``file` `, int)`
`PyObject *` | `PyFile_Name(PyObject *``file` `)`
`void` | `PyFile_SetBufSize(PyObject *``file` `, int` `size` `)`
`int` | `PyFile_SoftSpace(PyObject *``file` `, int)`
`int` | `PyFile_WriteObject(PyObject *``file` `, PyObject *``obj` `, int)`
`int` | `PyFile_WriteString(char *``str` `, PyObject *``file` `)`

##### Table B.20. Modules

Type | Function
--- | ---
`int` | `PyModule_Check(PyObject *``obj` `)`
`PyObject *` | `PyModule_GetDict(PyObject *``mod` `)`
`char *` | `PyModule_GetFilename(PyObject *``mod` `)`
`char *` | `PyModule_GetName(PyObject *``mod` `)`
`PyObject *` | `PyModule_New(char *``name` `)`

##### Table B.21. CObjects

Type | Function
--- | ---
`int` | `PyCObject_Check(PyObject *``o` `)`
`PyObject *` | `PyCObject_FromVoidPtr(void *``cobj` `, void (*destr)(void *))`
`PyObject *` | `PyCObject_FromVoidPtrAndDesc(void *``cobj` `, void *``desc` `, void (*destr)(void *, void *))`
`void *` | `PyCObject_AsVoidPtr(PyObject *``self` `)`
`void *` | `PyCObject_GetDesc(PyObject *``self` `)`

Note: The CObject interface is typically used to provide a wrapper around an arbitrary C pointer. These objects are mostly used by extension-building tools such as SWIG.

| 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.