Python文件

__pycache__的作用

项目第一次运行之后根目录会生成__pycache__文件夹,cpython代表使用C语言实现的python解释器,-38表示python3.8

img

pyc的作用

让python解释器不需要重复编译py文件

解释器工作流程:

\1. 加载和链接模块

\2. 将源码编译成PyCodeObject对象(即字节码),写入内存

\3. 逐行从内存中读取并运行,结束后将PyCodeObject写入磁盘(即pyc或pyo)

\4. 再次运行时先检查本地是否有pyc或pyo(未被修改),有就直接执行

参考:https://www.cnblogs.com/hls-code/p/14846193.html

pyc和pyo的区别

pyo相比pyc做了优化,少了断言和debug信息,体积上更小,python3.5之后不再是pyo文件,而是.opt-1.pyc

参考:Pyc和pyo是怎样一种存在? - 波罗学的回答 - 知乎 https://www.zhihu.com/question/321177296/answer/659527131

用C语言写Python库

python对C语言的扩展支持

python3

colin.h

#ifndef Colin_h

#define Colin_h

typedef struct {

int *a;

int len;

} x_t;

typedef struct {

x_t *ax;

int len;

} y_t;

int fun1(int a);

int fun2(y_t *p);

void free_y_t(y_t *p);

#endif

colin.c

#include “colin.h”

#include <stdlib.h>

int fun1(int a) {

return a*a;

}

int fun2(y_t *p) {

int result;

int sum;

int i,j;

result = 1;

for(i=0; ilen; i++) {

​ sum = 0;

​ for(j=0; jax[i].len; j++) {

​ sum += p->ax[i].a[j];

​ result *= sum;

​ }

}

return result;

}

void free_y_t(y_t *p) {

int i;

for(i=0; ilen; i++) {

​ free(p->ax[i].a);

}

free(p->ax);

}

wrap.c

#include <Python.h>

#include <stdlib.h>

#include “colin.h”

PyObject* wrap_fun1(PyObject* self, PyObject* args) {

int n, result;

if (!PyArg_ParseTuple(args, “i”, &n))

​ return NULL;

result = fun1(n);

return Py_BuildValue(“i”, result);

}

PyObject* wrap_fun2(PyObject* self, PyObject* args) {

int n, result;

int i, j;

int size1, size2;

PyObject *p, *q;

y_t *y;

y = malloc(sizeof(y_t));

size1 = PyTuple_Size(args);

y->len = size1;

y->ax = malloc(sizeof(x_t)*size1);

for(i=0; i<size1; i++) {

​ p = PyTuple_GetItem(args, i);

​ size2 = PyList_Size(p);

​ y->ax[i].len = size2;

​ y->ax[i].a = malloc(sizeof(int)*size2);

​ for(j=0; j<size2; j++) {

​ q = PyList_GetItem(p, j);

​ PyArg_Parse(q, “i”, &y->ax[i].a[j]);

​ }

}

result = fun2(y);

free_y_t(y);

free(y);

return Py_BuildValue(“i”, result);

}

static PyMethodDef colinMethods[] = {

{“fun1”, wrap_fun1, METH_VARARGS, “fun1 test”},

{“fun2”, wrap_fun2, METH_VARARGS, “fun2 test”},

{NULL, NULL, METH_NOARGS, NULL}

};

static struct PyModuleDef ColinDef = {

PyModuleDef_HEAD_INIT,

“colin”,

“colin module”,

sizeof(colinMethods),

colinMethods

};

// python2.7

//void initcolin() {

// PyObject *m;

// m = Py_InitModule(“colin”, colinMethods);

//}

PyMODINIT_FUNC PyInit_colin(void) {

return PyModule_Create(&ColinDef);

}

python3使用PyModule_Create代替Py_InitModule

生成so文件

gcc -I /home/user/anaconda3/envs/pythonLearn/include/python3.10/ -fPIC -shared colin.c wrap.c -o colin.so

-I指定头文件位置

导入colin模块

import colin

r1 = colin.fun1(99)

r2 = colin.fun2([1, 2], [3, 4])

print(r1)

print(r2)

参考:

https://www.cnblogs.com/Colin-Cai/p/7594551.html

https://www.cnblogs.com/gelare/articles/16496534.html

https://www.osgeo.cn/cpython/c-api/module.html#initializing-modules

解决python没有Py_InitModule的问题:

http://cn.voidcc.com/question/p-bbjymykj-un.html

https://blog.csdn.net/weixin_39916360/article/details/113518226

https://blog.csdn.net/u012719832/article/details/101647664