HYPER MIKAN BOX
検索
最近の変更
メディアマネージャー
サイトマップ
文書の表示
以前のリビジョン
バックリンク
ログイン
トレース:
この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。
====== PythonのクラスオブジェトCの関数に渡す ====== 作成日: 2023-08-04 (金) [[https://youtu.be/kYp_yQfunhU|第3回 Python/C APIに触れてみる (2)]] ===== ソースコード ===== ==== setup.py ==== <file python> from setuptools import setup, Extension setup( name="pointlib", ext_modules=[ Extension("pointlib", ["pointlibmodule.c"]) ] ) </file> ==== point.py ==== <file python> class Point: def __init__(self, x, y): self.x = x self.y = y def to_s(self): return "({}, {})".format(self.x, self.y) </file> ==== pointlibmodule.c ==== <file c> #define PY_SSIZE_T_CLEAN #include <Python.h> #include <math.h> static PyObject *pointlib_distance(PyObject *self, PyObject *args) { PyObject *point_obj = NULL; if (!PyArg_ParseTuple(args, "O", &point_obj)) { return NULL; } PyObject *x_obj = PyObject_GetAttrString(point_obj, "x"); if (x_obj == NULL) { return NULL; } PyObject *y_obj = PyObject_GetAttrString(point_obj, "y"); if (y_obj == NULL) { Py_XDECREF(x_obj); return NULL; } long x = PyLong_AsLong(x_obj); long y = PyLong_AsLong(y_obj); Py_XDECREF(y_obj); Py_XDECREF(x_obj); return PyFloat_FromDouble(sqrt(x * x + y * y)); } // clang-format off static PyMethodDef pointlib_methods[] = { {"distance", pointlib_distance, METH_VARARGS, "Distance from origin to the point."}, {NULL, NULL, 0, NULL} }; static PyModuleDef pointlib_module = { PyModuleDef_HEAD_INIT, "pointlib", "Example for passing a Python object to C function.", -1, pointlib_methods }; // clang-format on PyMODINIT_FUNC PyInit_pointlib(void) { return PyModule_Create(&pointlib_module); } </file>
文書の先頭へ