From aa9c80b1f45540382b4b8278fa10d9ffec7faab0 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 9 Jul 2022 11:57:00 +0800 Subject: [PATCH] dev C --- Difficult_Rocket/utils/cprint/build.ps1 | 12 ++++++ Difficult_Rocket/utils/cprint/py_cprint.c | 46 ++++++++++++++++++++-- Difficult_Rocket/utils/cprint/py_cprint.py | 10 ++++- Difficult_Rocket/utils/cprint/pycprint.pyi | 2 + Difficult_Rocket/utils/cprint/test.ps1 | 15 +++++++ 5 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 Difficult_Rocket/utils/cprint/build.ps1 create mode 100644 Difficult_Rocket/utils/cprint/test.ps1 diff --git a/Difficult_Rocket/utils/cprint/build.ps1 b/Difficult_Rocket/utils/cprint/build.ps1 new file mode 100644 index 0000000..e5a76bc --- /dev/null +++ b/Difficult_Rocket/utils/cprint/build.ps1 @@ -0,0 +1,12 @@ + +Write-Output ==========_win-py3.8_========== +python3.8.exe ./setup.py build + +Write-Output ==========_win-py3.10_========== +python3.10.exe ./setup.py build + +Write-Output ==========_wsl-py3.8_========== +wsl.exe python3 setup.py build + +Write-Output ==========_wsl-py3.10_========== +wsl.exe python3.10 setup.py build diff --git a/Difficult_Rocket/utils/cprint/py_cprint.c b/Difficult_Rocket/utils/cprint/py_cprint.c index 00bef71..6786de2 100644 --- a/Difficult_Rocket/utils/cprint/py_cprint.c +++ b/Difficult_Rocket/utils/cprint/py_cprint.c @@ -3,6 +3,7 @@ // #define PY_SSIZE_T_CLEAN +#define Py_size long long int #include #include @@ -15,12 +16,49 @@ static PyObject *pycprint_print(PyObject *self, PyObject *args){ Py_RETURN_NONE; }; +Py_UCS4 *get_ucs4from_unicode(PyObject *unicode){ + +}; + static PyObject *pycpint_printf(PyObject *self, PyObject *args, PyObject *kwargs){ - PyObject *a; - if(!PyArg_ParseTuple(args, "O", &a)){ - return NULL; + printf("args == NULL: %d\n", args == NULL); + printf("kwargs == NULL: %d\n", kwargs == NULL); + + const char *end = "\n"; + const char *sep = " "; + + if (args != NULL){ // 传入了字符串 + Py_ssize_t text_len = PyTuple_Size(args); // 获取字符串的长度 + PyObject *cache_obj; // 创建一个缓存 + + for (Py_ssize_t i = 0; i < text_len; i++){ // for 遍历 + cache_obj = PyTuple_GetItem(args, i); // 获取一个字符串 + if (cache_obj == NULL){ return NULL; }; // 出毛病了就报错 + if (PyUnicode_Check(cache_obj) == 1) { // 他不是个字符串(实际上如果有pyi文件就不需要这个检查) + + } else if (PyList_Check(cache_obj) == 1) { + + }; + }; + printf("text_len = %lld\n", (Py_size)text_len); + }; - Py_ssize_t text_len = PyTuple_Size(args); + if(kwargs != NULL){ // 传入了 end 或者 sep + Py_ssize_t kwargs_len = PyDict_Size(kwargs); + printf("kwargs_len = %lld\n", (Py_size) kwargs_len);; + if(PyDict_Contains(kwargs, PyUnicode_FromString("end"))){ // 如果包含 end 的参数 + PyObject *end_unicode; // 整个缓存 + end_unicode = PyDict_GetItemString(kwargs, "end"); // 先获取出来 Pyobj + if(!PyUnicode_READY(end_unicode)){ return NULL; }; // 确认这个字符串对象可以用宏 + + Py_ssize_t end_unicode_len = PyUnicode_GetLength(end_unicode); // 缓存一手长度 + Py_UCS4 *new_end = malloc(sizeof(char) * end_unicode_len); // 提前分配好不定长度的字符串内存 + for (Py_ssize_t i = 0; i < end_unicode_len; i++){ + new_end[i] = PyUnicode_ReadChar(end_unicode, i); // 每一位对应读取 + }; + }; + }; + printf("%s", end); Py_RETURN_NONE; }; diff --git a/Difficult_Rocket/utils/cprint/py_cprint.py b/Difficult_Rocket/utils/cprint/py_cprint.py index 010a7d4..1642aa1 100644 --- a/Difficult_Rocket/utils/cprint/py_cprint.py +++ b/Difficult_Rocket/utils/cprint/py_cprint.py @@ -6,4 +6,12 @@ from build import pycprint -pycprint.print("aaa\n") +pycprint.print("a啊a\n") + +pycprint.printf("a啊a\n", "aaa", "aaa", end='') + +print() + +pycprint.printf() + +pycprint.printf("aaa\n", "aaa", "aaa") diff --git a/Difficult_Rocket/utils/cprint/pycprint.pyi b/Difficult_Rocket/utils/cprint/pycprint.pyi index 2203386..fcd3c83 100644 --- a/Difficult_Rocket/utils/cprint/pycprint.pyi +++ b/Difficult_Rocket/utils/cprint/pycprint.pyi @@ -5,3 +5,5 @@ # ------------------------------- def print(text: str) -> None: ... # 直接调用fprint + +def printf(text: str) -> None: ... # 测试中 diff --git a/Difficult_Rocket/utils/cprint/test.ps1 b/Difficult_Rocket/utils/cprint/test.ps1 new file mode 100644 index 0000000..122ab65 --- /dev/null +++ b/Difficult_Rocket/utils/cprint/test.ps1 @@ -0,0 +1,15 @@ +Write-Output ---------------------开始测试--------------------- + +Write-Output ==========_win-py3.8_========== +python3.8.exe ./py_cprint.py + +Write-Output ==========_win-py3.10_========== +python3.10.exe ./py_cprint.py + +Write-Output ==========_wsl-py3.8_========== +wsl.exe python3 py_cprint.py + +Write-Output ==========_wsl-py3.10_========== +wsl.exe python3.10 ./py_cprint.py + +Write-Output ---------------------结束测试---------------------