Python C APIの使用詳細(一)
8667 ワード
概要
Python仮想マシンの初期化と終了,Python基本データ型のオブジェクト作成,CとPython間のデータ型相互変換について説明する.
Python仮想マシンの初期化と終了
Python仮想マシンの初期化には
C/C++でPythonを呼び出す前に、仮想マシンを初期化する必要があります.
仮想マシンを終了するときに
プロセス終了時にPython仮想マシンを終了します.
例:
コンパイル方法とパラメータ:
PyObject
Pythonのすべてのオブジェクトタイプはこのタイプの拡張です.これはPythonがオブジェクトのポインタをオブジェクトと見なすために必要な情報を含むタイプです.通常のパブリケーションバージョンでは、オブジェクトの参照数と対応するタイプのオブジェクトへのポインタのみが含まれます.実際にPyObjectとして宣言されるものは何もありませんが、Pythonオブジェクトを指すポインタごとにPyObject*に変換できます.マクロPy_を使用する必要がありますREFCNTとPy_TYPEがメンバーを訪問します.
マクロの説明、すべてを含まない
Py_BuildValue
Cのすべての基本データ型をPythonがアクセスできるデータ型に変換するには、Cを使用します.
識別子の説明:
完全なPythonオブジェクトの作成
ロングフォームのPythonオブジェクトの作成
浮動小数点タイプのPythonオブジェクトの作成
ブール型オブジェクトの作成
Python stringオブジェクトの作成
unicode文字列オブジェクトの作成
Py_を使うNone
main関数
転載先:https://blog.51cto.com/11293981/2167897
Python仮想マシンの初期化と終了,Python基本データ型のオブジェクト作成,CとPython間のデータ型相互変換について説明する.
Python仮想マシンの初期化と終了
Python仮想マシンの初期化には
Py_Initialize()
を呼び出す必要があります.Py_IsInitialized()
Python仮想マシンの初期化が成功したかどうかを判断するために使用され、Trueは成功し、Falseは失敗した.C/C++でPythonを呼び出す前に、仮想マシンを初期化する必要があります.
仮想マシンを終了するときに
Py_Finalize()
を呼び出します.プロセス終了時にPython仮想マシンを終了します.
例:
#include
#include
using namespace std;
int main() {
// Python
Py_Initialize();
// Python
if (Py_IsInitialized() == 0){
printf("fal to initialize Python
");
return -1;
}
printf("server start
");
// Python
Py_Finalize();
return 0;
}
コンパイル方法とパラメータ:
Python2 ,Python3 , Python Python3
g++ -I/usr/include/python2.7 -c main.cpp
g++ -o main main.o -L/usr/local/lib -lpython2.7 -lrt -lpthread -lutil -ldl
PyObject
Pythonのすべてのオブジェクトタイプはこのタイプの拡張です.これはPythonがオブジェクトのポインタをオブジェクトと見なすために必要な情報を含むタイプです.通常のパブリケーションバージョンでは、オブジェクトの参照数と対応するタイプのオブジェクトへのポインタのみが含まれます.実際にPyObjectとして宣言されるものは何もありませんが、Pythonオブジェクトを指すポインタごとにPyObject*に変換できます.マクロPy_を使用する必要がありますREFCNTとPy_TYPEがメンバーを訪問します.
マクロの説明、すべてを含まない
Py_TYPE: Python
Py_REFCNT: Python
Py_SIZE: Python
...
Py_BuildValue
Cのすべての基本データ型をPythonがアクセスできるデータ型に変換するには、Cを使用します.
識別子の説明:
s(str None)[char *]
'utf-8' null C Python str 。 C NULL, None。
s#(str None)[char *,int]
'utf-8' C Python str 。 C NULL, None。
y( )[char *]
C Python 。 C NULL, None。
y#( )[char *,int]
C Python 。 C NULL, None。
z(str None)[char *]
s 。
z#(str None)[char *,int]
s# 。
u(str)[Py_UNICODE *]
Unicode(UCS-2 UCS-4) null Python Unicode 。 Unicode NULL, None。
u#(str)[Py_UNICODE *,int]
Unicode(UCS-2 UCS-4) Python Unicode 。 Unicode NULL, None。
U(str None)[char *]
s 。
U#(str None)[char *,int]
s# 。
i(int)[int]
C int Python 。
b(int)[char]
C char Python 。
h(int)[short int]
C short int Python 。
l(int)[long int]
C long int Python 。
B(int)[unsigned char]
C unsigned char Python 。
H(int)[unsigned short int]
C unsigned short int Python 。
I(int)[unsigned int]
C unsigned int Python 。
k(int)[unsigned long]
C unsigned long Python 。
L(int)[long long]
C long long Python 。
K(int)[unsigned long long]
C unsigned long long Python 。
n(int)[Py_ssize_t]
C Py_ssize_t Python 。
c( 1 )[char]
C int 1 Python 。
C( 1 str)[int]
C int 1 Python str 。
d(float) [double]
C double Python 。
f(float) [float]
C float Python 。
D(complex) [Py_complex *]
C Py_complex Python 。
O(object) [PyObject *]
Python ( , 1)。 NULL , 。 ,Py_BuildValue() NULL 。 , SystemError。
S(object) [PyObject *]
O
N((object) [PyObject *]
O , 。 。
O&(object) [converter, anything]
Python 。 ( void * ) , “ ”Python , NULL。
(items) (tuple) [matching-items]
C Python 。
[items](list) [matching-items]
C Python 。
{items}(dict) [matching-items]
C Python 。 C , 。
, SystemError NULL。
完全なPythonオブジェクトの作成
Py_BuildValue
を使用して、整数オブジェクトを作成します.void int_object(){
//
PyObject *py_ival = Py_BuildValue("i", -5987); // Python
PyObject *py_ival2 = PyLong_FromLong(-8979);
int ival = PyLong_AsLong(py_ival); // Python C
int ival2 = PyLong_AsLong(py_ival2); // Python C
printf("ival = %d, ival2 = %d
", ival, ival2);
//
PyObject *py_uval = Py_BuildValue("I", 465486); // Python
PyObject *py_uval2 = PyLong_FromUnsignedLong(1654864);
unsigned int uval = PyLong_AsUnsignedLong(py_uval); // Python C
unsigned int uval2 = PyLong_AsUnsignedLong(py_uval2); // Python C
printf("uval = %u, uval2 = %u
", uval, uval2);
}
ロングフォームのPythonオブジェクトの作成
void long_object(){
//
PyObject *py_lval = Py_BuildValue("L", 45648946484984); // Python
long long c_lval = PyLong_AsLongLong(py_lval); // C
printf("clval = %lld
", c_lval);
//
PyObject *py_lval2 = PyLong_FromLongLong(234234623454525); // PyLong_FromLongLong Python
long long c_lval2 = PyLong_AsLongLong(py_lval2); // C
printf("clval2 = %lld
", c_lval2);
}
浮動小数点タイプのPythonオブジェクトの作成
void double_object(){
//
float fval = 632.045;
PyObject *py_fval = Py_BuildValue("d", fval); // Python
float c_fval = PyFloat_AsDouble(py_fval); // C
printf("fval = %f
", c_fval);
//
double dval = 48941546.578;
PyObject *py_dval = PyFloat_FromDouble(dval); // Python
double c_dval = PyFloat_AsDouble(py_dval); // C
printf("c_dval = %lf
", c_dval);
}
ブール型オブジェクトの作成
void boolean_object(){
//
bool bval = true; // false
PyObject *py_bval = Py_BuildValue("b", bval); // Python
int c_bval = PyInt_AsLong(py_bval);
printf("c_bval = %d
", c_bval);
//
bool bval2 = false;
PyObject *py_bval2 = PyBool_FromLong(bval2); // Python
int c_bval2 = PyInt_AsLong(py_bval2);
printf("c_bval2 = %d
", c_bval2);
}
Python stringオブジェクトの作成
void string_object(){
//
const char *pstr = "this is a test";
PyObject *py_str = Py_BuildValue("s", pstr); // Python
char *c_pstr = PyString_AsString(py_str); // C
printf("c_pstr = %s
", c_pstr);
//
const char *pstr2 = "this is a test1";
PyObject *py_str2 = PyString_FromString(pstr2); // Python
char *c_pstr2 = PyString_AsString(py_str2); // C
printf("c_pstr2 = %s
", c_pstr2);
//
//
const int mem_len = 1024;
char *mem = new char[mem_len];
PyObject *py_mem = Py_BuildValue("s#", mem, mem_len); // 1. 2. 3.
int c_data_len = PyString_Size(py_mem);
printf("c_data_len = %d
", c_data_len);
//
PyObject *py_mem2 = PyString_FromStringAndSize(mem, mem_len);
int c_data_len2 = PyString_Size(py_mem2);
printf("c_data_len2 = %d
", c_data_len2);
}
unicode文字列オブジェクトの作成
void unicode_object(){
const char *p_ustr = " ";
PyObject *py_unicode = PyUnicode_FromString(p_ustr); // C Python unicode
// unicode C
PyObject *py_utf8 = PyUnicode_AsUTF8String(py_unicode); // unicode utf-8
const char *c_string = PyString_AsString(py_utf8); // utf-8 c
printf("c_utf8 = %s
", c_string);
// unicode
// unicode
PyObject *py_unicode_fmt = PyUnicode_FromFormat("%s%d%s", " ", 18, " ");
// unicode C
PyObject *py_utf8_fmt = PyUnicode_AsUTF8String(py_unicode_fmt);
const char *utf8_fmt = PyString_AsString(py_utf8_fmt);
printf("utf8_fmt = %s
", utf8_fmt);
}
Py_を使うNone
Py_None
はグローバル変数ですPyObject* none_object(){
Py_RETURN_NONE; // return
}
main関数
#include
#include
#include
#include
int main() {
// Python
Py_Initialize();
// Python
if (Py_IsInitialized() == 0){
printf("fal to initialize Python
");
return -1;
}
printf("server start
");
int_object();
long_object();
double_object();
boolean_object();
string_object();
unicode_object();
PyObject *py_ret = none_object();
if (py_ret == Py_None){
printf("is none object
");
}else{
printf("is not none object
");
}
// Python
Py_Finalize();
return 0;
}
転載先:https://blog.51cto.com/11293981/2167897