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