From 2c0a79a8177ff4c3271b3d305ef696b2178725b5 Mon Sep 17 00:00:00 2001 From: Andre Miras Date: Wed, 6 May 2020 18:50:51 +0200 Subject: [PATCH] Uses Python 3 syntax This is a follow up for #482, uses Python 3 syntax: - Simplifies `super()` calls - Removes some unused `super()` (no parent class) - Removes `object` inheritance - Drops `IS_PY2` logic - Drops Python 2 imports --- kivy_ios/recipes/hostpython3/__init__.py | 2 +- kivy_ios/recipes/kivy/__init__.py | 4 +- kivy_ios/recipes/numpy/__init__.py | 4 +- kivy_ios/recipes/python3/__init__.py | 2 +- kivy_ios/toolchain.py | 63 +++++------------------- 5 files changed, 19 insertions(+), 56 deletions(-) diff --git a/kivy_ios/recipes/hostpython3/__init__.py b/kivy_ios/recipes/hostpython3/__init__.py index 665a459..d5d1784 100644 --- a/kivy_ios/recipes/hostpython3/__init__.py +++ b/kivy_ios/recipes/hostpython3/__init__.py @@ -17,7 +17,7 @@ class Hostpython3Recipe(Recipe): build_subdir = 'native-build' def init_with_ctx(self, ctx): - super(Hostpython3Recipe, self).init_with_ctx(ctx) + super().init_with_ctx(ctx) self.set_hostpython(self, "3.8") self.ctx.so_suffix = ".cpython-38m-darwin.so" self.ctx.hostpython = join(self.ctx.dist_dir, "hostpython3", "bin", "python") diff --git a/kivy_ios/recipes/kivy/__init__.py b/kivy_ios/recipes/kivy/__init__.py index 0f1000a..940a67e 100644 --- a/kivy_ios/recipes/kivy/__init__.py +++ b/kivy_ios/recipes/kivy/__init__.py @@ -22,7 +22,7 @@ class KivyRecipe(CythonRecipe): pre_build_ext = True def get_recipe_env(self, arch): - env = super(KivyRecipe, self).get_recipe_env(arch) + env = super().get_recipe_env(arch) env["KIVY_SDL2_PATH"] = ":".join([ join(self.ctx.dist_dir, "include", "common", "sdl2"), join(self.ctx.dist_dir, "include", "common", "sdl2_image"), @@ -32,7 +32,7 @@ class KivyRecipe(CythonRecipe): def build_arch(self, arch): self._patch_setup() - super(KivyRecipe, self).build_arch(arch) + super().build_arch(arch) def _patch_setup(self): # patch setup to remove some functionnalities diff --git a/kivy_ios/recipes/numpy/__init__.py b/kivy_ios/recipes/numpy/__init__.py index 3f75487..9ece825 100644 --- a/kivy_ios/recipes/numpy/__init__.py +++ b/kivy_ios/recipes/numpy/__init__.py @@ -21,7 +21,7 @@ class NumpyRecipe(CythonRecipe): self.set_marker("patched") def get_recipe_env(self, arch): - env = super(NumpyRecipe, self).get_recipe_env(arch) + env = super().get_recipe_env(arch) # CC must have the CFLAGS with arm arch, because numpy tries first to # compile and execute an empty C to see if the compiler works. This is # obviously not working when crosscompiling @@ -32,7 +32,7 @@ class NumpyRecipe(CythonRecipe): return env def build_arch(self, arch): - super(NumpyRecipe, self).build_arch(arch) + super().build_arch(arch) sh.cp(sh.glob(join(self.build_dir, "build", "temp.*", "libnpy*.a")), self.build_dir) diff --git a/kivy_ios/recipes/python3/__init__.py b/kivy_ios/recipes/python3/__init__.py index 7ccfac4..1a8e423 100644 --- a/kivy_ios/recipes/python3/__init__.py +++ b/kivy_ios/recipes/python3/__init__.py @@ -16,7 +16,7 @@ class Python3Recipe(Recipe): pbx_libraries = ["libz", "libbz2", "libsqlite3"] def init_with_ctx(self, ctx): - super(Python3Recipe, self).init_with_ctx(ctx) + super().init_with_ctx(ctx) self.set_python(self, "3.8") ctx.python_ver_dir = "python3.8" ctx.python_prefix = join(ctx.dist_dir, "root", "python3") diff --git a/kivy_ios/toolchain.py b/kivy_ios/toolchain.py index 4b18158..5e28877 100755 --- a/kivy_ios/toolchain.py +++ b/kivy_ios/toolchain.py @@ -25,11 +25,7 @@ from contextlib import contextmanager from datetime import datetime from pprint import pformat import logging - -try: - from urllib.request import FancyURLopener, urlcleanup -except ImportError: - from urllib import FancyURLopener, urlcleanup +from urllib.request import FancyURLopener, urlcleanup try: from pbxproj import XcodeProject @@ -54,10 +50,6 @@ sh_logging.setLevel(logging.WARNING) logger = logging.getLogger(__name__) -IS_PY3 = sys.version_info[0] >= 3 -IS_PY2 = sys.version_info[0] == 2 - - @contextmanager def cd(newdir): prevdir = getcwd() @@ -108,12 +100,11 @@ class ChromeDownloader(FancyURLopener): urlretrieve = ChromeDownloader().retrieve -class JsonStore(object): +class JsonStore: """Replacement of shelve using json, needed for support python 2 and 3. """ def __init__(self, filename): - super(JsonStore, self).__init__() self.filename = filename self.data = {} if exists(filename): @@ -151,17 +142,11 @@ class JsonStore(object): self.sync() def sync(self): - # https://stackoverflow.com/a/14870531/185510 - if IS_PY3: - with open(self.filename, 'w') as fd: - json.dump(self.data, fd, ensure_ascii=False) - else: - with io.open(self.filename, 'w', encoding='utf-8') as fd: - fd.write(unicode( # noqa: F821 - json.dumps(self.data, ensure_ascii=False))) + with open(self.filename, 'w') as fd: + json.dump(self.data, fd, ensure_ascii=False) -class Arch(object): +class Arch: def __init__(self, ctx): self.ctx = ctx self._ccsh = None @@ -277,7 +262,7 @@ class Arch64IOS(Arch): sysroot = sh.xcrun("--sdk", "iphoneos", "--show-sdk-path").strip() -class Graph(object): +class Graph: # Taken from python-for-android/depsort def __init__(self): # `graph`: dict that maps each package to a set of its dependencies. @@ -321,7 +306,7 @@ class Graph(object): bset.discard(result) -class Context(object): +class Context: env = environ.copy() root_dir = None cache_dir = None @@ -335,7 +320,6 @@ class Context(object): so_suffix = None # set by one of the hostpython def __init__(self): - super(Context, self).__init__() self.include_dirs = [] ok = True @@ -439,7 +423,7 @@ class Context(object): return "IDEBuildOperationMaxNumberOfConcurrentCompileTasks={}".format(self.num_cores) -class Recipe(object): +class Recipe: props = { "is_alias": False, "version": None, @@ -462,7 +446,7 @@ class Recipe(object): for prop, value in cls.props.items(): if not hasattr(cls, prop): setattr(cls, prop, value) - return super(Recipe, cls).__new__(cls) + return super().__new__(cls) # API available for recipes def download_file(self, url, filename, cwd=None): @@ -494,32 +478,11 @@ class Recipe(object): while True: try: urlretrieve(url, filename, report_hook) - except AttributeError: - if IS_PY2: - # This is caused by bug in python-future, causing occasional - # AttributeError: '_fileobject' object has no attribute 'readinto' - # It can be removed if the upstream fix is accepted. See also: - # * https://github.com/kivy/kivy-ios/issues/322 - # * https://github.com/PythonCharmers/python-future/pull/423 - import requests - - logger.warning("urlretrieve failed. Falling back to request") - - headers = {'User-agent': 'Mozilla/5.0 (X11; Linux x86_64) ' - 'AppleWebKit/537.36 (KHTML, like Gecko) ' - 'Chrome/28.0.1500.71 Safari/537.36'} - r = requests.get(url, headers=headers) - - with open(filename, "wb") as fw: - fw.write(r.content) - break - else: - raise - except OSError as e: + except OSError: attempts += 1 if attempts >= 5: logger.error('Max download attempts reached: {}'.format(attempts)) - raise e + raise logger.warning('Download failed. Retrying in 1 second...') time.sleep(1) continue @@ -1092,7 +1055,7 @@ class CythonRecipe(PythonRecipe): shprint(cmd, join(self.build_dir, "lib{}.a".format(self.name)), *dirs) def get_recipe_env(self, arch): - env = super(CythonRecipe, self).get_recipe_env(arch) + env = super().get_recipe_env(arch) env["KIVYIOSROOT"] = self.ctx.root_dir env["IOSSDKROOT"] = arch.sysroot env["CUSTOMIZED_OSX_COMPILER"] = 'True' @@ -1248,7 +1211,7 @@ def update_pbxproj(filename, pbx_frameworks=None): project.save() -class ToolchainCL(object): +class ToolchainCL: def __init__(self): parser = argparse.ArgumentParser( description="Tool for managing the iOS / Python toolchain",