python - Cython 的 language_level 3 和 3str 之间有什么区别?

标签 python cython

在即将推出的 Cython 3.0 版本中,3str language_level(与 Cython 0.29 一起引入)成为新的默认值而不是当前默认值 2,即如果 language_level 是未设置 ( how to set ),我们收到以下警告:

FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: /home/ed/mygithub/cython/foo.pyx tree = Parsing.p_module(s, pxd, full_module_name)

但是 3str3 语言级别之间有什么区别,对于哪些代码,使用 3str 编译的模块的行为会有所不同> 和 3 语言水平?

最佳答案

TLDR: 3str不假定字符串文字在 Python2.x 下是 unicode,这使得从 Python2.x 到 Python3 的迁移更容易。

不是一个完整的答案,因为我不知道突出差异的代码,这仍然为问题留下空间,但这可能有用,Whats new in cython 0.29 :

A new language-level'

Cython 0.29 supports a new setting for the language_level directive, language_level=3str, which will become the new default language level in Cython 3.0. We already added it now, so that users can opt in and benefit from it right away, and already prepare their code for the coming change. It's an "in between" kind of setting, which enables all the nice Python 3 goodies that are not syntax compatible with Python 2.x, but without requiring all unprefixed string literals to become Unicode strings when the compiled code runs in Python 2.x. This was one of the biggest problems in the general Py3 migration. And in the context of Cython's integration with C code, it got in the way of our users even a bit more than it would in Python code. Our goals are to make it easy for new users who come from Python 3 to compile their code with Cython and to allow existing (Cython/Python 2) code bases to make use of the benefits before they can make a 100% switch.

同时被 Debian's manpage for cython 注意到:

--embed[=<method_name>] Generate a main() function that embeds the Python interpreter.
-2 Compile based on Python-2 syntax and code semantics.
-3 Compile based on Python-3 syntax and code semantics.
--3str Compile based on Python-3 syntax and code semantics without assuming unicode by default for string literals under Python 2.

最后由 cython docs 记录:

The 3str option enables Python 3 semantics but does not change the str type and unprefixed string literals to unicode when the compiled code runs in Python 2.x.

关于python - Cython 的 language_level 3 和 3str 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57415446/

相关文章:

python - 从 X-Y 元组列表的字典中获取最大 X 和 Y 的最有效方法

python - 基于路由的应用程序的架构

python - 向 MySQL 添加信息不起作用

python - Tensorflow 1.8.0 : Wide and Deep Model results are not stable. 随机种子不起作用

c++ - 使用 cython 包装 c++ 结构模板

python - 将指针传递给cython中的结构

python - 在 C++/Cython 中,是否可以仅声明相关属性在 Python 中可见?

python - Python的unittest中向TestCase添加元数据

python - CPython:动态模块未定义模块导出函数错误

python - 为什么我的代码没有删除内部矩形