栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python源码剖析, 第0部分 -- 编译python

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python源码剖析, 第0部分 -- 编译python

1. python版本选择

python2.7的代码太远古了, 在vs2019里重定向编译也有很多错误, 不想管了, 就直接用比较新的3.7的代码了
python3.7源码

2. python源码编译

参考文章, 把代码编译成功:
.Windows Visual Studio 2019 编译 Python 3.7.12 及 Python 3.10.0 源码
看Python 3.7.12 源码编译部分就行了, 大差不差

3. 代码更改

更改源码. 在print方法里面, 加上我们自定义的前缀:

// 搜索如下方法:
static PyObject *
builtin_print(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
	...
	...
	...


    // 输出我们自定义的内容
    PyFile_WriteString("this is our printn", file);
    
    for (i = 0; i < nargs; i++) {
        if (i > 0) {
            if (sep == NULL)
                err = PyFile_WriteString(" ", file);
            else
                err = PyFile_WriteObject(sep, file,
                                         Py_PRINT_RAW);
            if (err)
                return NULL;
        }
        // 具体的输出内容
        err = PyFile_WriteObject(args[i], file, Py_PRINT_RAW);
        if (err)
            return NULL;
    }

    // 分行输出
    if (end == NULL)
        err = PyFile_WriteString("n", file);
    else
        err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
    if (err)
        return NULL;  
}

更改代码后编译运行:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1004852.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号