开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 0

source-code-analysis/python3.7.4

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
python3.7.4
/
Doc
/
howto
/
cporting.rst
python3.7.4
/
Doc
/
howto
/
cporting.rst
cporting.rst 7.75 KB
一键复制 编辑 原始数据 按行查看 历史
zhangweibo 提交于 2021年11月17日 13:49 +08:00 . git init
.. highlightlang:: c

Porting Extension Modules to Python 3

author: Benjamin Peterson

Abstract

Although changing the C-API was not one of Python 3's objectives, the many Python-level changes made leaving Python 2's API intact impossible. In fact, some changes such as :func:`int` and :func:`long` unification are more obvious on the C level. This document endeavors to document incompatibilities and how they can be worked around.

Conditional compilation

The easiest way to compile only some code for Python 3 is to check if :c:macro:`PY_MAJOR_VERSION` is greater than or equal to 3.

#if PY_MAJOR_VERSION >= 3
#define IS_PY3K
#endif

API functions that are not present can be aliased to their equivalents within conditional blocks.

Changes to Object APIs

Python 3 merged together some types with similar functions while cleanly separating others.

str/unicode Unification

Python 3's :func:`str` type is equivalent to Python 2's :func:`unicode`; the C functions are called PyUnicode_* for both. The old 8-bit string type has become :func:`bytes`, with C functions called PyBytes_*. Python 2.6 and later provide a compatibility header, :file:`bytesobject.h`, mapping PyBytes names to PyString ones. For best compatibility with Python 3, :c:type:`PyUnicode` should be used for textual data and :c:type:`PyBytes` for binary data. It's also important to remember that :c:type:`PyBytes` and :c:type:`PyUnicode` in Python 3 are not interchangeable like :c:type:`PyString` and :c:type:`PyUnicode` are in Python 2. The following example shows best practices with regards to :c:type:`PyUnicode`, :c:type:`PyString`, and :c:type:`PyBytes`.

#include "stdlib.h"
#include "Python.h"
#include "bytesobject.h"

/* text example */
static PyObject *
say_hello(PyObject *self, PyObject *args) {
 PyObject *name, *result;

 if (!PyArg_ParseTuple(args, "U:say_hello", &name))
 return NULL;

 result = PyUnicode_FromFormat("Hello, %S!", name);
 return result;
}

/* just a forward */
static char * do_encode(PyObject *);

/* bytes example */
static PyObject *
encode_object(PyObject *self, PyObject *args) {
 char *encoded;
 PyObject *result, *myobj;

 if (!PyArg_ParseTuple(args, "O:encode_object", &myobj))
 return NULL;

 encoded = do_encode(myobj);
 if (encoded == NULL)
 return NULL;
 result = PyBytes_FromString(encoded);
 free(encoded);
 return result;
}

long/int Unification

Python 3 has only one integer type, :func:`int`. But it actually corresponds to Python 2's :func:`long` type—the :func:`int` type used in Python 2 was removed. In the C-API, PyInt_* functions are replaced by their PyLong_* equivalents.

Module initialization and state

Python 3 has a revamped extension module initialization system. (See

CObject replaced with Capsule

The :c:type:`Capsule` object was introduced in Python 3.1 and 2.7 to replace :c:type:`CObject`. CObjects were useful, but the :c:type:`CObject` API was problematic: it didn't permit distinguishing between valid CObjects, which allowed mismatched CObjects to crash the interpreter, and some of its APIs relied on undefined behavior in C. (For further reading on the rationale behind Capsules, please see :issue:`5630`.)

If you're currently using CObjects, and you want to migrate to 3.1 or newer, you'll need to switch to Capsules. :c:type:`CObject` was deprecated in 3.1 and 2.7 and completely removed in Python 3.2. If you only support 2.7, or 3.1 and above, you can simply switch to :c:type:`Capsule`. If you need to support Python 3.0, or versions of Python earlier than 2.7, you'll have to support both CObjects and Capsules. (Note that Python 3.0 is no longer supported, and it is not recommended for production use.)

The following example header file :file:`capsulethunk.h` may solve the problem for you. Simply write your code against the :c:type:`Capsule` API and include this header file after :file:`Python.h`. Your code will automatically use Capsules in versions of Python with Capsules, and switch to CObjects when Capsules are unavailable.

:file:`capsulethunk.h` simulates Capsules using CObjects. However, :c:type:`CObject` provides no place to store the capsule's "name". As a result the simulated :c:type:`Capsule` objects created by :file:`capsulethunk.h` behave slightly differently from real Capsules. Specifically:

You can find :file:`capsulethunk.h` in the Python source distribution as :source:`Doc/includes/capsulethunk.h`. We also include it here for your convenience:

.. literalinclude:: ../includes/capsulethunk.h



Other options

If you are writing a new extension module, you might consider /python_sourcecode/python3.7.4/blob/master/Doc/howto/cporting.rst

未知许可证
查看未知开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/python_sourcecode/python3.7.4.git
git@gitee.com:python_sourcecode/python3.7.4.git
python_sourcecode
python3.7.4
python3.7.4
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /