Skip to content

Upgrade syntax to Python 3 #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions bench/py_functioncalls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ def empty_call():
pass

#- group: empty-inst ---------------------------------------------------------
class EmptyCall(object):
class EmptyCall:
def empty_call(self):
pass


#- group: builtin-args-free --------------------------------------------------
class Value(object):
class Value:
def __init__(self):
self.m_int = 42

Expand All @@ -26,7 +26,7 @@ def take_a_struct(val):
pass

#- group: builtin-args-inst --------------------------------------------------
class TakeAValue(object):
class TakeAValue:
def take_an_int(self, val):
pass

Expand All @@ -45,12 +45,12 @@ def do_work(val):
return math.atan(val)

#- group: do_work-inst -------------------------------------------------------
class DoWork(object):
class DoWork:
def do_work(self, val):
return math.atan(val)


#- group: overload-inst ------------------------------------------------------
class OverloadedCall(object):
class OverloadedCall:
def add_it(self, *args):
return 3.1415 + sum(args)
3 changes: 1 addition & 2 deletions bench/support.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import py, sys, subprocess

currpath = py.path.local(__file__).dirpath()
Expand All @@ -11,4 +10,4 @@ def setup_make(targetname):
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, _ = popen.communicate()
if popen.returncode:
raise OSError("'make' failed:\n%s" % (stdout,))
raise OSError("'make' failed:\n{}".format(stdout))
14 changes: 7 additions & 7 deletions python/cppyy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def __ne__(self, other):

# std::make_shared/unique create needless templates: rely on Python's introspection
# instead. This also allows Python derived classes to be handled correctly.
class py_make_smartptr(object):
class py_make_smartptr:
__slots__ = ['cls', 'ptrcls']
def __init__(self, cls, ptrcls):
self.cls = cls
Expand All @@ -164,7 +164,7 @@ def __call__(self, *args):
obj = self.cls(*args)
return self.ptrcls[self.cls](obj) # C++ takes ownership

class make_smartptr(object):
class make_smartptr:
__slots__ = ['ptrcls', 'maker']
def __init__(self, ptrcls, maker):
self.ptrcls = ptrcls
Expand All @@ -187,7 +187,7 @@ def __getitem__(self, cls):


#--- interface to Cling ------------------------------------------------------
class _stderr_capture(object):
class _stderr_capture:
def __init__(self):
self._capture = not gbl.Cpp.IsDebugOutputEnabled()
self.err = ""
Expand Down Expand Up @@ -240,7 +240,7 @@ def load_library(name):
CppInterOp = gbl.Cpp
result = CppInterOp.LoadLibrary(name)
if result == False:
raise RuntimeError('Could not load library "%s": %s' % (name, err.err))
raise RuntimeError('Could not load library "{}": {}'.format(name, err.err))

return True

Expand All @@ -249,7 +249,7 @@ def include(header):
with _stderr_capture() as err:
errcode = gbl.Cpp.Declare('#include "%s"' % header)
if not errcode == 0:
raise ImportError('Failed to load header file "%s"%s' % (header, err.err))
raise ImportError('Failed to load header file "{}"{}'.format(header, err.err))
return True

def c_include(header):
Expand All @@ -259,7 +259,7 @@ def c_include(header):
#include "%s"
}""" % header)
if not errcode == 0:
raise ImportError('Failed to load header file "%s"%s' % (header, err.err))
raise ImportError('Failed to load header file "{}"{}'.format(header, err.err))
return True

def add_include_path(path):
Expand Down Expand Up @@ -389,7 +389,7 @@ def sizeof(tt):
try:
sz = ctypes.sizeof(tt)
except TypeError:
sz = gbl.Cpp.Evaluate("sizeof(%s)" % (_get_name(tt),))
sz = gbl.Cpp.Evaluate("sizeof({})".format(_get_name(tt)))
_sizes[tt] = sz
return sz

Expand Down
6 changes: 3 additions & 3 deletions python/cppyy/_cpython_cppyy.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def ismethod(object):


### template support ---------------------------------------------------------
class Template(object): # expected/used by ProxyWrappers.cxx in CPyCppyy
class Template: # expected/used by ProxyWrappers.cxx in CPyCppyy
stl_sequence_types = ['std::vector', 'std::list', 'std::set', 'std::deque']
stl_unrolled_types = ['std::pair']
stl_fixed_size_types = ['std::array']
Expand All @@ -67,7 +67,7 @@ def __init__(self, name, scope):
self.__scope__ = scope

def __repr__(self):
return "<cppyy.Template '%s' object at %s>" % (self.__name__, hex(id(self)))
return "<cppyy.Template '{}' object at {}>".format(self.__name__, hex(id(self)))

def __getitem__(self, *args):
# multi-argument to [] becomes a single tuple argument
Expand Down Expand Up @@ -177,7 +177,7 @@ def add_default_paths():
f = line.strip()
if (os.path.exists(f)):
libCppInterOp.AddSearchPath(f)
except IOError:
except OSError:
pass
add_default_paths()
del add_default_paths
Expand Down
20 changes: 10 additions & 10 deletions python/cppyy/_pythonization.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@ def set_ownership_policy(match_class, match_method, python_owns_result):
# NB: Ideally, we'd use the version commented out below, but for now, we
# make do with the hackier version here.
def rename_attribute(match_class, orig_attribute, new_attribute, keep_orig=False):
class attribute_pythonizor(object):
class getter(object):
class attribute_pythonizor:
class getter:
def __init__(self, attr):
self.attr = attr
def __call__(self, obj):
return getattr(obj, self.attr)

class setter(object):
class setter:
def __init__(self, attr):
self.attr = attr
def __call__(self, obj, value):
return setattr(obj, self.attr, value)

class deleter(object):
class deleter:
def __init__(self, attr):
self.attr = attr
def __call__(self, obj):
Expand Down Expand Up @@ -123,7 +123,7 @@ def __call__(self, obj, name):
# Shared with PyPy:

def add_overload(match_class, match_method, overload):
class method_pythonizor(object):
class method_pythonizor:
def __init__(self, match_class, match_method, overload):
import re
self.match_class = re.compile(match_class)
Expand All @@ -146,7 +146,7 @@ def __call__(self, obj, name):


def compose_method(match_class, match_method, g):
class composition_pythonizor(object):
class composition_pythonizor:
def __init__(self, match_class, match_method, g):
import re
self.match_class = re.compile(match_class)
Expand Down Expand Up @@ -174,7 +174,7 @@ def h(self, *args, **kwargs):


def set_method_property(match_class, match_method, prop, value):
class method_pythonizor(object):
class method_pythonizor:
def __init__(self, match_class, match_method, prop, value):
import re
self.match_class = re.compile(match_class)
Expand All @@ -196,7 +196,7 @@ def __call__(self, obj, name):


def make_property(match_class, match_get, match_set=None, match_del=None, prop_name=None):
class property_pythonizor(object):
class property_pythonizor:
def __init__(self, match_class, match_get, match_set, match_del, prop_name):
import re
self.match_class = re.compile(match_class)
Expand Down Expand Up @@ -230,7 +230,7 @@ def __init__(self, match_class, match_get, match_set, match_del, prop_name):
self.prop_name = prop_name

def make_get_del_proxy(self, getter):
class proxy(object):
class proxy:
def __init__(self, getter):
self.getter = getter

Expand All @@ -239,7 +239,7 @@ def __call__(self, obj):
return proxy(getter)

def make_set_proxy(self, setter):
class proxy(object):
class proxy:
def __init__(self, setter):
self.setter = setter

Expand Down
2 changes: 1 addition & 1 deletion python/cppyy/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


#- fake namespace for interactive lazy lookups -------------------------------
class InteractiveLazy(object):
class InteractiveLazy:
def __init__(self, hook_okay):
self._hook_okay = hook_okay

Expand Down
4 changes: 2 additions & 2 deletions python/cppyy/ll.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def argc():
# import low-level python converters
for _name in ['addressof', 'as_cobject', 'as_capsule', 'as_ctypes']:
try:
exec('%s = cppyy._backend.%s' % (_name, _name))
exec('{} = cppyy._backend.{}'.format(_name, _name))
__all__.append(_name)
except AttributeError:
pass
Expand Down Expand Up @@ -81,7 +81,7 @@ def argc():


# helper for sizing arrays
class ArraySizer(object):
class ArraySizer:
def __init__(self, func):
self.func = func
def __getitem__(self, t):
Expand Down
12 changes: 6 additions & 6 deletions python/cppyy/numba_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class CppFunctionNumbaType(nb_types.Callable):
requires_gil = False

def __init__(self, func, is_method=False):
super(CppFunctionNumbaType, self).__init__('CppFunction(%s)' % str(func))
super().__init__('CppFunction(%s)' % str(func))

self.sig = None
self._func = func
Expand Down Expand Up @@ -195,7 +195,7 @@ def __init__(self, dmm, fe_type):
# the function pointer of this overload can not be exactly typed, but
# only the storage size is relevant, so simply use a void*
be_type = ir.PointerType(dmm.lookup(nb_types.void).get_value_type())
super(CppFunctionModel, self).__init__(dmm, fe_type, be_type)
super().__init__(dmm, fe_type, be_type)

@nb_iutils.lower_constant(CppFunctionNumbaType)
def constant_function_pointer(context, builder, ty, pyval):
Expand All @@ -207,7 +207,7 @@ def constant_function_pointer(context, builder, ty, pyval):
#
# C++ method / data member -> Numba
#
class CppDataMemberInfo(object):
class CppDataMemberInfo:
__slots__ = ['f_name', 'f_offset', 'f_nbtype', 'f_irtype']

def __init__(self, name, offset, cpptype):
Expand All @@ -222,7 +222,7 @@ def __init__(self, name, offset, cpptype):
#
class CppClassNumbaType(CppFunctionNumbaType):
def __init__(self, scope, qualifier):
super(CppClassNumbaType, self).__init__(scope.__init__)
super().__init__(scope.__init__)
self.name = 'CppClass(%s)' % scope.__cpp_name__ # overrides value in Type
self._scope = scope
self._qualifier = qualifier
Expand All @@ -234,7 +234,7 @@ def get_qualifier(self):
return self._qualifier

def get_call_type(self, context, args, kwds):
sig = super(CppClassNumbaType, self).get_call_type(context, args, kwds)
sig = super().get_call_type(context, args, kwds)
self.sig = sig
return sig

Expand Down Expand Up @@ -428,7 +428,7 @@ def get_value_type(self):

# TODO: this doesn't work for real PODs, b/c those are unpacked into their elements and
# passed through registers
return ir.PointerType(super(ImplClassModel, self).get_value_type())
return ir.PointerType(super().get_value_type())

# argument: representation used for function argument. Needs to be builtin type,
# but unlike other Numba composites, C++ proxies are not flattened.
Expand Down
4 changes: 2 additions & 2 deletions python/cppyy_compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def pypy58_57_compat():
os.chdir(os.path.dirname(c._name))
imp.init_builtin('cppyy')
except ImportError:
raise EnvironmentError('"%s" missing in LD_LIBRARY_PATH' %\
raise OSError('"%s" missing in LD_LIBRARY_PATH' %\
os.path.dirname(c._name))
finally:
os.chdir(olddir)
Expand Down Expand Up @@ -52,7 +52,7 @@ def py59_compat():
actual_name = __name__; __name__ = ''
import _cppyy as _backend
except ImportError:
raise EnvironmentError('"%s" missing in LD_LIBRARY_PATH' % os.path.dirname(c._name))
raise OSError('"%s" missing in LD_LIBRARY_PATH' % os.path.dirname(c._name))
finally:
__name__ = actual_name
os.chdir(olddir)
Expand Down
2 changes: 0 additions & 2 deletions test/bindexplib.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

import os, sys, subprocess

target = sys.argv[1]
Expand Down
4 changes: 2 additions & 2 deletions test/make_dict_win32.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import glob, os, sys, subprocess

USES_PYTHON_CAPI = set(('pythonizables',))
USES_PYTHON_CAPI = {'pythonizables'}

fn = sys.argv[1]

Expand All @@ -15,7 +15,7 @@
if fn[-4:] == '.cxx': fn = fn[:-4]
elif fn[-2:] == '.h': fn = fn[:-2]
if not os.path.exists(fn+'.h'):
print("file %s.h does not exist" % (fn,))
print("file {}.h does not exist".format(fn))
sys.exit(1)

uses_python_capi = False
Expand Down
3 changes: 1 addition & 2 deletions test/support.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import os, py, sys, subprocess

currpath = py.path.local(__file__).dirpath()
Expand All @@ -13,7 +12,7 @@ def setup_make(targetname):
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, _ = popen.communicate()
if popen.returncode:
raise OSError("'make' failed:\n%s" % (stdout,))
raise OSError("'make' failed:\n{}".format(stdout))

if sys.hexversion >= 0x3000000:
pylong = int
Expand Down
4 changes: 2 additions & 2 deletions test/test_advancedcpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,9 @@ def test16_template_global_functions(self):
f = cppyy.gbl.my_templated_function

assert f('c') == 'c'
assert type(f('c')) == type('c')
assert type(f('c')) == str
assert f(3.) == 3.
assert type(f(4.)) == type(4.)
assert type(f(4.)) == float

def test17_assign_to_return_byref(self):
"""Test assignment to an instance returned by reference"""
Expand Down
2 changes: 1 addition & 1 deletion test/test_concurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class Simulation2 {

cppyy.gbl.CPPOverloadReuse.Simulation2.do_something.__release_gil__ = True

class State(object):
class State:
lock = threading.Lock()
c1, c2, c3 = 0, 0, 0

Expand Down
Loading