clean cprint

This commit is contained in:
shenjack 2022-11-15 06:30:19 +08:00
parent 77b873c0b0
commit 0c8177c965
12 changed files with 0 additions and 370 deletions

View File

@ -1,19 +0,0 @@
cmake_minimum_required(VERSION 3.22)
project(cprint VERSION 0.0.1 LANGUAGES C)
#
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_C_STANDARD 99)
#
add_compile_options(-O3)
add_compile_options(-Wall)
add_compile_options(-Werror)
#add_compile_options(-shared)
#add_compile_options(-o ../../cprint.dll)
# py3.8 path
include_directories(.)
include_directories(C:\\Users\\shenjack.SHENJACK-5600X\\AppData\\Local\\Programs\\Python\\Python38\\include\\.)
#
#add_executable(cprint cprint.c)
#
#add_executable(cprint cprint.c)
add_executable(py_cprint py_cprint.c)

View File

@ -1,12 +0,0 @@
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

View File

@ -1,79 +0,0 @@
import os
os.system('')
reset = '\033[0m'
print('A 000')
do = False
def rgb(r, g, b):
return 2, r, g, b
i_list = list(range(30, 39, 1))
# i_list = [39]
j_list = list(range(40, 49, 1))
# j_list = [49]
for i in i_list:
for j in j_list:
print(f'\033[{i};{j}m{i}|{j} {reset}', end='|')
print()
print()
def color_print(*args):
args = [str(i) for i in args]
out = '|'.join(args)
line = ';'.join(args)
print(f'\033[{line}m{out}\033[0m')
reset_color = '\033[0m'
# exit(0)
color_print(94)
color_print(41, 93)
color_print(36, 40)
print()
color_print(48, *rgb(200, 100, 20), 34)
color_print(48, *rgb(255, 161, 72))
color_print(48, *rgb(255, 161, 72), 38, *rgb(1, 50, 255))
color_print(48, *rgb(178, 112, 50), 38, *rgb(98, 96, 167))
print()
color_print(48, *rgb(120, 10, 10), 38, *rgb(50, 100, 110))
color_print(34, 40)
color_print(32, 40)
print('trace: ', end='')
color_print(48, *rgb(40, 40, 40))
print('some trace', reset_color)
print('fine: ', end='')
color_print(35)
print('some fine', reset_color)
print('debug: ', end='')
color_print(38, *rgb(133, 138, 149))
print('some debug', reset_color)
print('info: ', end='')
color_print(0)
print('some info', reset_color)
print('warn: ', end='')
# color_print(31, 48, *rgb(56, 28, 0))
color_print(33)
print('some warn', reset_color)
print('error: ', end='')
color_print(31)
print('some error', reset_color)
print('fatal: ', end='')
color_print(33, 41)
print('some fatal', reset_color)

View File

@ -1,12 +0,0 @@
# -------------------------------
# Difficult Rocket
# Copyright © 2021-2022 by shenjackyuanjie 3695888@qq.com
# All rights reserved
# -------------------------------
from colorama import Fore, Back, Style
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

View File

@ -1,39 +0,0 @@
import os
# include 目录
include_paths = ["C:\\Users\\shenjack.SHENJACK-5600X\\AppData\\Local\\Programs\\Python\\Python38\\include\\."]
# 编译选项
compile_options = {
"-shared": None,
"-O3": None,
"-Wall": None,
"-Werror": None,
"-o": "./py_cprint.dll"
}
include_command = "-I "
# 处理 include 目录命令
for path in include_paths:
include_command = "{}, {}".format(include_command, path)
else:
if len(include_paths) != 0:
include_command = include_command[:3] + include_command[4:]
compile_option_command = ""
# 处理编译选项
for option, key in compile_options.items():
if key is None:
compile_option_command = "{} {} ".format(compile_option_command, option)
else:
compile_option_command = "{} {} {}".format(compile_option_command, option, key)
print(include_command, compile_option_command)
compile_command = "gcc.exe ./py_cprint.c {}{}".format(include_command, compile_option_command)
print(compile_command)
os.system(compile_command)

View File

@ -1,30 +0,0 @@
//
// Created by shenjack on 2022/6/24.
//
//#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
int CcPrint(PyObject *self, PyObject *args, PyObject *kwargs){
char *print_str;
char *line_end = NULL; // 准备参数
static char *kwlist[] = {"some_stuf", "end", NULL}; // 准备参数列表
if(!PyArg_ParseTupleAndKeywords(args, kwargs, "ss", kwlist, &print_str, &line_end)){
return 0;
};
return 1;
};
// 如果你非得调用我·····
int main(){
printf("aaaa");
if(false){
CcPrint(PyLong_FromLong(1), PyLong_FromLong(1), PyLong_FromLong(1));
};
return 0;
};

View File

@ -1,7 +0,0 @@
from objprint import objprint
from ctypes import cdll
cprint = cdll.LoadLibrary("./cprint.dll")
# objprint(cprint)

View File

@ -1,101 +0,0 @@
//
// Created by shenjack on 2022/7/6.
//
#define PY_SSIZE_T_CLEAN
#define Py_size long long int
#include <Python.h>
#include <stdint.h>
int *print_PyUcs4(PyObject *pyObject){
if(PyUnicode_READY(pyObject) == -1){
PyErr_SetString(PyExc_UnicodeDecodeError, "failed");
return NULL;
}
#if defined(__linux__)
const char *out_char = PyUnicode_AsUTF8(pyObject);
printf("%s", out_char);
#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(pyObject);
printf("%ws", ucs4); // win
#endif
return (int *) 1;
};
static PyObject *pycprint_print(PyObject *self, PyObject *args){
if(!print_PyUcs4(PyTuple_GetItem(args, 0))){
return NULL;
};
Py_RETURN_NONE;
};
const char *get_char_from_PyUnicode(PyObject *unicodeObject){
if(!PyUnicode_READY(unicodeObject)){
return NULL;
};
const char *char_obj = PyUnicode_AsUTF8(unicodeObject);
if(char_obj == NULL){
return NULL;
} else {
return char_obj;
};
};
static PyObject *pycpint_printf(PyObject *self, PyObject *args, PyObject *kwargs){
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) {
print_PyUcs4(cache_obj);
} else if (PyList_Check(cache_obj) == 1) {
};
};
printf("text_len = %lld\n", (Py_size)text_len);
};
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 = PyDict_GetItemString(kwargs, "end"); // 先获取出来 Pyobj
end = PyUnicode_AsUTF8(end_unicode);
};
};
printf("%s", end);
Py_RETURN_NONE;
};
static PyMethodDef PyCprintMethods[] = {
{"print", pycprint_print, METH_VARARGS, "直接使用c的printf输出"},
{"printf", (PyCFunction)(void(*))pycpint_printf, METH_VARARGS | METH_KEYWORDS,
"传入类似py print然后进行format的print"},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef pycprintmodule = {
PyModuleDef_HEAD_INIT,
"pycprint",
"直接调用c的printf",
-1,
PyCprintMethods
};
PyMODINIT_FUNC PyInit_pycprint(void){
return PyModule_Create(&pycprintmodule);
};

View File

@ -1,16 +0,0 @@
# -------------------------------
# Difficult Rocket
# Copyright © 2021-2022 by shenjackyuanjie 3695888@qq.com
# All rights reserved
# -------------------------------
from build import pycprint
pycprint.print("来个测试\n")
pycprint.print("再来个测试\n")
pycprint.print("abcdefg\n")
pycprint.printf('aaaa')
pycprint.printf("啊啊啊abc啊怎么了")

View File

@ -1,10 +0,0 @@
# -------------------------------
# Difficult Rocket
# Copyright © 2021-2022 by shenjackyuanjie 3695888@qq.com
# All rights reserved
# -------------------------------
def print(text: str) -> None: ... # 直接调用fprint
def printf(text: str) -> None: ... # 测试中

View File

@ -1,30 +0,0 @@
from setuptools import setup, Extension
import shutil
import os
pycprint = Extension('pycprint',
sources=['py_cprint.c'])
setup(name='PyCprint',
version='0.0.1',
author='shenjack',
author_email='3695888@qq.com',
description='这是一个用于直接调用c的printf的库',
ext_modules=[pycprint])
def dir_not_temp(path: str):
return path.find('temp') and os.path.isdir(os.path.join('build', path))
build_dir = os.listdir('./build')
build_dir = list(filter(dir_not_temp, build_dir))
# 用列表推导式把 是文件夹 且 名字里没有 temp 的抽出来
print(build_dir)
os.chdir('build')
# 把运行路径迁移过去方便copy
for build in build_dir:
copy = os.listdir(build)
shutil.copy(os.path.join(build, copy[0]), './')

View File

@ -1,15 +0,0 @@
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 ---------------------结束测试---------------------