Skip to content

Commit 26273cc

Browse files
committed
0.0.1 用 C/C++ 实现一个 + 1 的功能,作为拓展的核心功能
1 parent 79d9db5 commit 26273cc

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist*
2+
extend_cpython.egg-info*

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
11
# extend-cpython
2+
3+
本项目是一个模板项目,主要完成如下内容
4+
5+
1、使用 C/C++ 来实现部分功能
6+
7+
2、让软件支持 pip install 来安装
8+
9+
3、用 C/C++ 实现的功能支持像原生 Python 模块一样的导入
10+
11+
---

setup.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
import re
3+
from setuptools import setup,Extension
4+
5+
setup(name='extend-cpython',
6+
version="0.0.1",
7+
description='extend-cpython',
8+
author="Neeky",
9+
author_email="[email protected]",
10+
maintainer='Neeky',
11+
maintainer_email='[email protected]',
12+
python_requires='>=3.6.*',
13+
ext_modules = [Extension('plugins/maths',["src/maths.cpp"])],
14+
classifiers=[
15+
'Development Status :: 4 - Beta',
16+
'Intended Audience :: Developers',
17+
'Operating System :: POSIX',
18+
'Operating System :: MacOS :: MacOS X',
19+
'Programming Language :: Python :: 3.6',
20+
'Programming Language :: Python :: 3.7',
21+
'Programming Language :: Python :: 3.8']
22+
)

src/maths.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <Python.h>
2+
3+
// 用 C++ 实现一个加一的函数
4+
double plus_one(double x)
5+
{
6+
return x + 1;
7+
}
8+
9+
// 把 C/C++ 的一个函数包装成一个 Python 函数
10+
static PyObject *py_plus_one(PyObject *self,PyObject *args)
11+
{
12+
double x = 0,result = 0;
13+
PyArg_ParseTuple(args,"d",&x);
14+
result = plus_one(x);
15+
return Py_BuildValue("d",result);
16+
}
17+
18+
// 定义模块要包含的函数列表
19+
static PyMethodDef methods[] = {
20+
{"plus_one",py_plus_one,METH_VARARGS,"plus one"},
21+
{0,0,0,0}
22+
};
23+
24+
// 定义模块
25+
static struct PyModuleDef module = {
26+
PyModuleDef_HEAD_INIT,
27+
"plugins",
28+
"extend cpython",
29+
-1,
30+
methods
31+
};
32+
33+
// 定义模块的初始化方法
34+
PyMODINIT_FUNC PyInit_maths(void)
35+
{
36+
return PyModule_Create(&module);
37+
}

0 commit comments

Comments
 (0)