Commit 3a5a2e3b authored by Stefan Behnel's avatar Stefan Behnel Committed by GitHub

Merge pull request #2796 from cython/gh2565_language_level_3str

Change the default language level to "3str"
parents 96403849 c597e501
......@@ -419,12 +419,11 @@ class CythonMagics(Magics):
quiet=quiet,
annotate=args.annotate,
force=True,
language_level=min(3, sys.version_info[0]),
)
if args.language_level is not None:
assert args.language_level in (2, 3)
opts['language_level'] = args.language_level
elif sys.version_info[0] >= 3:
opts['language_level'] = 3
return cythonize([extension], **opts)
except CompileError:
return None
......
# cython: language_level = 2
# cython: language_level=3str
# cython: auto_pickle=False
#
# Code output module
......
# cython: language_level=3str
from __future__ import absolute_import
import cython
......
......@@ -1572,7 +1572,7 @@ cdef class NAME:
count = 0
INIT_ASSIGNMENTS
if IS_UNION and count > 1:
raise ValueError, "At most one union member should be specified."
raise ValueError("At most one union member should be specified.")
def __str__(self):
return STR_FORMAT % MEMBER_TUPLE
def __repr__(self):
......
......@@ -3688,12 +3688,12 @@ def p_module(s, pxd, full_module_name, ctx=Ctx):
s.parse_comments = False
if s.context.language_level is None:
s.context.set_language_level(2)
s.context.set_language_level('3str')
if pos[0].filename:
import warnings
warnings.warn(
"Cython directive 'language_level' not set, using 2 for now (Py2). "
"This will change in a later release! File: %s" % pos[0].filename,
"Cython directive 'language_level' not set, using '3str' for now (Py3). "
"This has changed from earlier releases! File: %s" % pos[0].filename,
FutureWarning,
stacklevel=1 if cython.compiled else 2,
)
......
# cython: infer_types=True, language_level=3, py2_import=True, auto_pickle=False
# cython: infer_types=True, language_level=3, auto_pickle=False
#
# Cython Scanner
#
......
......@@ -29,8 +29,7 @@ class StringParseContext(Main.Context):
include_directories = []
if compiler_directives is None:
compiler_directives = {}
# TODO: see if "language_level=3" also works for our internal code here.
Main.Context.__init__(self, include_directories, compiler_directives, cpp=cpp, language_level=2)
Main.Context.__init__(self, include_directories, compiler_directives, cpp=cpp, language_level='3str')
self.module_name = name
def find_module(self, module_name, relative_to=None, pos=None, need_pxd=1, absolute_fallback=True):
......
# cython: language_level=3str
# cython: auto_pickle=False
#=======================================================================
#
......@@ -31,7 +32,7 @@ class Return(Action):
return isinstance(other, Return) and self.value == other.value
def __repr__(self):
return "Return(%s)" % repr(self.value)
return "Return(%r)" % self.value
class Call(Action):
......
# cython: language_level=3str
# cython: auto_pickle=False
#=======================================================================
#
......
# cython: language_level=3str
"""
A small templating language
......
......@@ -19,9 +19,8 @@ class TestCodeWriter(CythonTest):
def test_print(self):
self.t(u"""
print x, y
print x + y ** 2
print x, y, z,
print(x + y ** 2)
print(x, y, z)
""")
def test_if(self):
......@@ -65,9 +64,9 @@ class TestCodeWriter(CythonTest):
def test_for_loop(self):
self.t(u"""
for x, y, z in f(g(h(34) * 2) + 23):
print x, y, z
print(x, y, z)
else:
print 43
print(43)
""")
def test_inplace_assignment(self):
......
......@@ -20,8 +20,7 @@ cdef class __Pyx_EnumMeta(type):
# @cython.internal
cdef object __Pyx_EnumBase
class __Pyx_EnumBase(int):
__metaclass__ = __Pyx_EnumMeta
class __Pyx_EnumBase(int, metaclass=__Pyx_EnumMeta):
def __new__(cls, value, name=None):
for v in cls:
if v == value:
......
......@@ -177,7 +177,7 @@ cdef class array:
if self.dtype_is_object:
p = <PyObject **> self.data
for i in range(self.len / itemsize):
for i in range(self.len // itemsize):
p[i] = Py_None
Py_INCREF(Py_None)
......@@ -910,7 +910,7 @@ cdef char *pybuffer_index(Py_buffer *view, char *bufp, Py_ssize_t index,
cdef char *resultp
if view.ndim == 0:
shape = view.len / itemsize
shape = view.len // itemsize
stride = itemsize
else:
shape = view.shape[dim]
......@@ -944,7 +944,7 @@ cdef int transpose_memslice({{memviewslice_name}} *memslice) nogil except 0:
# reverse strides and shape
cdef int i, j
for i in range(ndim / 2):
for i in range(ndim // 2):
j = ndim - 1 - i
strides[i], strides[j] = strides[j], strides[i]
shape[i], shape[j] = shape[j], shape[i]
......
########## TestClass ##########
# These utilities are for testing purposes
from __future__ import print_function
cdef extern from *:
cdef object __pyx_test_dep(object)
......@@ -15,29 +17,29 @@ cdef class TestClass(object):
return 'TestClass(%d)' % self.value
cdef cdef_method(self, int value):
print 'Hello from cdef_method', value
print('Hello from cdef_method', value)
cpdef cpdef_method(self, int value):
print 'Hello from cpdef_method', value
print('Hello from cpdef_method', value)
def def_method(self, int value):
print 'Hello from def_method', value
print('Hello from def_method', value)
@cname('cdef_cname')
cdef cdef_cname_method(self, int value):
print "Hello from cdef_cname_method", value
print("Hello from cdef_cname_method", value)
@cname('cpdef_cname')
cpdef cpdef_cname_method(self, int value):
print "Hello from cpdef_cname_method", value
print("Hello from cpdef_cname_method", value)
@cname('def_cname')
def def_cname_method(self, int value):
print "Hello from def_cname_method", value
print("Hello from def_cname_method", value)
@cname('__pyx_test_call_other_cy_util')
cdef test_call(obj):
print 'test_call'
print('test_call')
__pyx_test_dep(obj)
@cname('__pyx_TestClass_New')
......@@ -46,19 +48,20 @@ cdef _testclass_new(int value):
########### TestDep ##########
from __future__ import print_function
@cname('__pyx_test_dep')
cdef test_dep(obj):
print 'test_dep', obj
print('test_dep', obj)
########## TestScope ##########
@cname('__pyx_testscope')
cdef object _testscope(int value):
return "hello from cython scope, value=%d" % value
return f"hello from cython scope, value={value}"
########## View.TestScope ##########
@cname('__pyx_view_testscope')
cdef object _testscope(int value):
return "hello from cython.view scope, value=%d" % value
return f"hello from cython.view scope, value={value}"
......@@ -38,4 +38,4 @@ namespace A {
from my_lib cimport x
print x
print(x)
......@@ -32,6 +32,6 @@ from tt cimport Foo
cdef array a = array('i', [1,2,3])
cdef Foo x
print a.data.as_ints[0]
print(a.data.as_ints[0])
x = Foo(a)
print x.obj.data.as_ints[0]
print(x.obj.data.as_ints[0])
......@@ -45,12 +45,12 @@ from other cimport (
A,
foo,
)
print A, foo(10)
print(A, foo(10))
cimport other
print other.A, other.foo(10)
print(other.A, other.foo(10))
from pkg cimport sub
cdef sub.my_int a = 100
from pkg.subpkg cimport submod
\ No newline at end of file
from pkg.subpkg cimport submod
......@@ -32,7 +32,7 @@ static int foo(int a)
######## a.pyx ########
from b.other cimport foo
print foo(10)
print(foo(10))
cimport b.other
print b.other.foo(10)
print(b.other.foo(10))
......@@ -29,8 +29,8 @@ except ImportError as e:
traceback.print_exc()
def test():
print "FILE: ", initial_file
print "PATH: ", initial_path
print("FILE: ", initial_file)
print("PATH: ", initial_path)
assert initial_path[0].endswith('my_test_package'), initial_path
assert initial_file.endswith('__init__.py'), initial_file
assert import_error is None, import_error
......@@ -51,8 +51,8 @@ except ImportError as e:
traceback.print_exc()
def test():
print "FILE: ", initial_file
print "PATH: ", initial_path
print("FILE: ", initial_file)
print("PATH: ", initial_path)
assert initial_path[0].endswith('another'), initial_path
assert initial_file.endswith('__init__.py'), initial_file
assert import_error is None, import_error
......
......@@ -196,8 +196,8 @@ import a as a_mod
def ae(result, expected):
"assert equals"
if result != expected:
print 'result :', result
print 'expected:', expected
print('result :', result)
print('expected:', expected)
assert result == expected
......@@ -227,7 +227,7 @@ ae(myobj.cpdef_method[cy.int, cy.float](10, 10.0), (10, 10.0))
d = {'obj': obj, 'myobj': myobj, 'ae': ae}
exec s in d
exec(s, d)
# Test def methods
# ae(obj.def_method(12, 14.9), 26)
......
......@@ -3,6 +3,7 @@
PYTHON setup.py build_ext --inplace
PYTHON -c "from pkg.b import test; assert test() == (1, 2)"
PYTHON -c "from pkg.b_py2 import test; assert test() == (1, 2)"
PYTHON -c "from pkg.sub.c import test; assert test() == (1, 2)"
######## setup.py ########
......@@ -42,7 +43,23 @@ cdef class test_pxd:
from . cimport a
from .a cimport test_pxd
cimport a as implicitly_relative_a
assert a.test_pxd is test_pxd
def test():
cdef test_pxd obj = test_pxd()
obj.x = 1
obj.y = 2
return (obj.x, obj.y)
######## pkg/b_py2.pyx ########
# cython: language_level=2
from . cimport a
from .a cimport test_pxd
cimport a as implicitly_relative_a # <-- Py2 "feature"
assert a.test_pxd is test_pxd
assert implicitly_relative_a.test_pxd is test_pxd
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment