language_level
is used to indicate in which Python-version the pyx-file is written. Thus for language_level=3
the resulting behavior of the pyx-code is as if it were executed in Python3 even when the resulting extension is run with Python2 (see a more detailed explanation here).
Language level 3str
means "Python3 semantics, but with str literals (also in Python2.7)" - thus str
in the name. What are exactly the consequences?
Python3: When built in/for Python3 there are no differences between level 3
and level 3str
.
In Python3, str
is unicode
, so the type of
# foo.pyx
def test():
return type("aaa")
will stay the same (str
) for language_level=3
and language_level=3str
.
Python2: The situation is different when built with/for Python2. With language_level=3
the result of the above test
-function will be unicode
and with language_level=3str
the result will be str
(which is bytes in Python2). But also for Python2, in all other cases, 3
and 3str
have the same behavior.
It would be a mistake to think, that
cdef char *c_string = "some string"
would fail to build with language_level=3
(and build successfuly with 3str
for Python2, as "some string" were bytes
), because "some string"
is unicode and unicode literals can be only to coerced only to Py_UNICODE*
.
The literal on the righthand-side isn't a Python-object to begin with, but just a C-string in the generated C-code.