Skip to content
Open
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
3 changes: 3 additions & 0 deletions pythonforandroid/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion pythonforandroid/recipes/python3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions pythonforandroid/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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':
Expand All @@ -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'):
Expand Down
10 changes: 10 additions & 0 deletions tests/recipes/test_python3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading