diff --git a/pythonforandroid/build.py b/pythonforandroid/build.py index f9da24d3d..22b99b62e 100644 --- a/pythonforandroid/build.py +++ b/pythonforandroid/build.py @@ -66,6 +66,9 @@ class Context: # Whether to strip debug symbols in `.so` files with_debug_symbols = False + # Whether to byte-compile the python stdlib and site-packages with -OO + optimize_python = True + env = environ.copy() # the filepath of toolchain.py root_dir = None diff --git a/pythonforandroid/recipes/python3/__init__.py b/pythonforandroid/recipes/python3/__init__.py index 3f1b91ef7..46048364a 100644 --- a/pythonforandroid/recipes/python3/__init__.py +++ b/pythonforandroid/recipes/python3/__init__.py @@ -412,7 +412,10 @@ def compile_python_files(self, dir): longer used...uses .pyc (https://www.python.org/dev/peps/pep-0488) ''' args = [self.ctx.hostpython] - args += ['-OO', '-m', 'compileall', '-b', '-f', '-q', dir] + if self.ctx.optimize_python: + # -OO = strip docstrings and asserts + args.append('-OO') + args += ['-m', 'compileall', '-b', '-f', '-q', dir] subprocess.call(args) def create_python_bundle(self, dirn, arch): diff --git a/pythonforandroid/toolchain.py b/pythonforandroid/toolchain.py index 0d06797a3..0fa9bd9f5 100644 --- a/pythonforandroid/toolchain.py +++ b/pythonforandroid/toolchain.py @@ -396,6 +396,13 @@ def __init__(self): help=('The java build tool to use when packaging the APK, defaults ' 'to automatically selecting an appropriate tool.')) + # Also an argument of the bootstrap build.py + generic_parser.add_argument( + '--no-optimize-python', dest='optimize_python', + action='store_false', default=True, + help=('Whether to compile to optimised .pyc files, using -OO ' + '(strips docstrings and asserts)')) + add_boolean_option( generic_parser, ['copy-libs'], default=False, @@ -592,6 +599,8 @@ def __init__(self): args.unknown_args += ["--release"] if getattr(args, "with_debug_symbols", False): args.unknown_args += ["--with-debug-symbols"] + if not getattr(args, "optimize_python", True): + args.unknown_args += ["--no-optimize-python"] if getattr(args, "ignore_setup_py", False): args.use_setup_py = False if getattr(args, "activity_class_name", "org.kivy.android.PythonActivity") != 'org.kivy.android.PythonActivity': @@ -618,6 +627,7 @@ def __init__(self): self.ctx.with_debug_symbols = getattr( args, "with_debug_symbols", False ) + self.ctx.optimize_python = getattr(args, "optimize_python", True) # Process requirements and put version in environ if hasattr(args, 'requirements'): diff --git a/tests/recipes/test_python3.py b/tests/recipes/test_python3.py index b5e239cbc..158985c08 100644 --- a/tests/recipes/test_python3.py +++ b/tests/recipes/test_python3.py @@ -43,6 +43,16 @@ def test_compile_python_files(self, mock_subprocess): [hostpy, '-OO', '-m', 'compileall', '-b', '-f', '-q', fake_compile_dir], ) + @mock.patch("pythonforandroid.recipes.python3.subprocess.call") + def test_compile_python_files_without_optimization(self, mock_subprocess): + fake_compile_dir = '/fake/compile/dir' + hostpy = self.recipe.ctx.hostpython = '/fake/hostpython3' + with mock.patch.object(self.recipe.ctx, 'optimize_python', False): + self.recipe.compile_python_files(fake_compile_dir) + mock_subprocess.assert_called_once_with( + [hostpy, '-m', 'compileall', '-b', '-f', '-q', fake_compile_dir], + ) + @mock.patch("pythonforandroid.recipe.Recipe.check_recipe_choices") @mock.patch("shutil.which") def test_get_recipe_env(