youtube:python-mixing-003x1
差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン | |||
youtube:python-mixing-003x1 [2024/02/28 18:26] – 削除 - 外部編集 (不明な日付) 127.0.0.1 | youtube:python-mixing-003x1 [2024/02/28 18:26] (現在) – ↷ toybox:python-mixing-003x1 から youtube:python-mixing-003x1 へページを移動しました。 freemikan | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ====== PythonのクラスオブジェトCの関数に渡す ====== | ||
+ | |||
+ | 作成日: 2023-08-04 (金) | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | ===== ソースコード ===== | ||
+ | |||
+ | ==== setup.py ==== | ||
+ | |||
+ | <file python> | ||
+ | from setuptools import setup, Extension | ||
+ | |||
+ | setup( | ||
+ | name=" | ||
+ | ext_modules=[ | ||
+ | Extension(" | ||
+ | ] | ||
+ | ) | ||
+ | </ | ||
+ | |||
+ | ==== point.py ==== | ||
+ | |||
+ | <file python> | ||
+ | class Point: | ||
+ | def __init__(self, | ||
+ | self.x = x | ||
+ | self.y = y | ||
+ | |||
+ | def to_s(self): | ||
+ | return "({}, {})" | ||
+ | </ | ||
+ | |||
+ | ==== pointlibmodule.c ==== | ||
+ | |||
+ | <file c> | ||
+ | #define PY_SSIZE_T_CLEAN | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | static PyObject *pointlib_distance(PyObject *self, PyObject *args) { | ||
+ | PyObject *point_obj = NULL; | ||
+ | if (!PyArg_ParseTuple(args, | ||
+ | return NULL; | ||
+ | } | ||
+ | |||
+ | PyObject *x_obj = PyObject_GetAttrString(point_obj, | ||
+ | if (x_obj == NULL) { | ||
+ | return NULL; | ||
+ | } | ||
+ | |||
+ | PyObject *y_obj = PyObject_GetAttrString(point_obj, | ||
+ | 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[] = { | ||
+ | {" | ||
+ | " | ||
+ | {NULL, NULL, 0, NULL} | ||
+ | }; | ||
+ | |||
+ | static PyModuleDef pointlib_module = { | ||
+ | PyModuleDef_HEAD_INIT, | ||
+ | " | ||
+ | " | ||
+ | -1, | ||
+ | pointlib_methods | ||
+ | }; | ||
+ | // clang-format on | ||
+ | |||
+ | PyMODINIT_FUNC PyInit_pointlib(void) { | ||
+ | return PyModule_Create(& | ||
+ | } | ||
+ | </ | ||