Skip to content

Commit d07dbb8

Browse files
committed
Allow Android testbed to take all Python command-line options
1 parent a92aec1 commit d07dbb8

File tree

6 files changed

+126
-124
lines changed

6 files changed

+126
-124
lines changed

Android/android.py

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import asyncio
44
import argparse
5+
import json
56
import os
67
import platform
78
import re
@@ -546,27 +547,22 @@ async def gradle_task(context):
546547
task_prefix = "connected"
547548
env["ANDROID_SERIAL"] = context.connected
548549

549-
if context.command:
550-
mode = "-c"
551-
module = context.command
552-
else:
553-
mode = "-m"
554-
module = context.module or "test"
550+
if not any(arg in context.args for arg in ["-c", "-m"]):
551+
context.args[0:0] = ["-m", "test"]
555552

556553
args = [
557554
gradlew, "--console", "plain", f"{task_prefix}DebugAndroidTest",
558555
] + [
559-
# Build-time properties
560-
f"-Ppython.{name}={value}"
561-
for name, value in [
562-
("sitePackages", context.site_packages), ("cwd", context.cwd)
563-
] if value
564-
] + [
565-
# Runtime properties
566-
f"-Pandroid.testInstrumentationRunnerArguments.python{name}={value}"
556+
f"-P{name}={value}"
567557
for name, value in [
568-
("Mode", mode), ("Module", module), ("Args", join_command(context.args))
569-
] if value
558+
("python.sitePackages", context.site_packages),
559+
("python.cwd", context.cwd),
560+
(
561+
"android.testInstrumentationRunnerArguments.pythonArgs",
562+
json.dumps(context.args),
563+
),
564+
]
565+
if value
570566
]
571567
if context.verbose >= 2:
572568
args.append("--info")
@@ -734,13 +730,19 @@ def ci(context):
734730
else:
735731
with TemporaryDirectory(prefix=SCRIPT_NAME) as temp_dir:
736732
print("::group::Tests")
733+
737734
# Prove the package is self-contained by using it to run the tests.
738735
shutil.unpack_archive(package_path, temp_dir)
739736

740-
# Randomization is disabled because order-dependent failures are
741-
# much less likely to pass on a rerun in single-process mode.
742737
launcher_args = ["--managed", "maxVersion", "-v"]
743-
test_args = ["--fast-ci", "--single-process", "--no-randomize"]
738+
test_args = [
739+
# See _add_ci_python_opts in libregrtest/main.py.
740+
"-W", "error", "-bb", "-E",
741+
742+
# Randomization is disabled because order-dependent failures are
743+
# much less likely to pass on a rerun in single-process mode.
744+
"-m", "test", "--fast-ci", "--single-process", "--no-randomize"
745+
]
744746
run(
745747
["./android.py", "test", *launcher_args, "--", *test_args],
746748
cwd=temp_dir
@@ -825,18 +827,11 @@ def add_parser(*args, **kwargs):
825827
test.add_argument(
826828
"--cwd", metavar="DIR", type=abspath,
827829
help="Directory to copy as the app's working directory.")
828-
829-
mode_group = test.add_mutually_exclusive_group()
830-
mode_group.add_argument(
831-
"-c", dest="command", help="Execute the given Python code.")
832-
mode_group.add_argument(
833-
"-m", dest="module", help="Execute the module with the given name.")
834-
test.epilog = (
835-
"If neither -c nor -m are passed, the default is '-m test', which will "
836-
"run Python's own test suite.")
837830
test.add_argument(
838-
"args", nargs="*", help=f"Arguments to add to sys.argv. "
839-
f"Separate them from {SCRIPT_NAME}'s own arguments with `--`.")
831+
"args", nargs="*", help=f"Python command-line arguments. "
832+
f"Separate them from {SCRIPT_NAME}'s own arguments with `--`. "
833+
f"If neither -c nor -m are included, `-m test` will be prepended, "
834+
f"which will run Python's own test suite.")
840835

841836
# Package arguments.
842837
for subcommand in [package, ci]:

Android/testbed/app/src/androidTest/java/org/python/testbed/PythonSuite.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class PythonSuite {
2020
val status = PythonTestRunner(
2121
InstrumentationRegistry.getInstrumentation().targetContext
2222
).run(
23-
InstrumentationRegistry.getArguments()
23+
InstrumentationRegistry.getArguments().getString("pythonArgs")!!,
2424
)
2525
assertEquals(0, status)
2626
} finally {

Android/testbed/app/src/main/c/main_activity.c

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <jni.h>
44
#include <pthread.h>
55
#include <Python.h>
6+
#include <signal.h>
67
#include <stdio.h>
78
#include <string.h>
89
#include <unistd.h>
@@ -15,6 +16,13 @@ static void throw_runtime_exception(JNIEnv *env, const char *message) {
1516
message);
1617
}
1718

19+
static void throw_errno(JNIEnv *env, const char *error_prefix) {
20+
char error_message[1024];
21+
snprintf(error_message, sizeof(error_message),
22+
"%s: %s", error_prefix, strerror(errno));
23+
throw_runtime_exception(env, error_message);
24+
}
25+
1826

1927
// --- Stdio redirection ------------------------------------------------------
2028

@@ -95,10 +103,7 @@ JNIEXPORT void JNICALL Java_org_python_testbed_PythonTestRunner_redirectStdioToL
95103
for (StreamInfo *si = STREAMS; si->file; si++) {
96104
char *error_prefix;
97105
if ((error_prefix = redirect_stream(si))) {
98-
char error_message[1024];
99-
snprintf(error_message, sizeof(error_message),
100-
"%s: %s", error_prefix, strerror(errno));
101-
throw_runtime_exception(env, error_message);
106+
throw_errno(env, error_prefix);
102107
return;
103108
}
104109
}
@@ -107,41 +112,86 @@ JNIEXPORT void JNICALL Java_org_python_testbed_PythonTestRunner_redirectStdioToL
107112

108113
// --- Python initialization ---------------------------------------------------
109114

110-
static PyStatus set_config_string(
111-
JNIEnv *env, PyConfig *config, wchar_t **config_str, jstring value
112-
) {
113-
const char *value_utf8 = (*env)->GetStringUTFChars(env, value, NULL);
114-
PyStatus status = PyConfig_SetBytesString(config, config_str, value_utf8);
115-
(*env)->ReleaseStringUTFChars(env, value, value_utf8);
116-
return status;
115+
static char *init_signals() {
116+
// Some tests use SIGUSR1, but that's blocked by default in an Android app in
117+
// order to make it available to `sigwait` in the Signal Catcher thread.
118+
// (https://cs.android.com/android/platform/superproject/+/android14-qpr3-release:art/runtime/signal_catcher.cc).
119+
// That thread's functionality is only useful for debugging the JVM, so disabling
120+
// it should not weaken the tests.
121+
//
122+
// There's no safe way of stopping the thread completely (#123982), but simply
123+
// unblocking SIGUSR1 is enough to fix most tests.
124+
//
125+
// However, in tests that generate multiple different signals in quick
126+
// succession, it's possible for SIGUSR1 to arrive while the main thread is busy
127+
// running the C-level handler for a different signal. In that case, the SIGUSR1
128+
// may be sent to the Signal Catcher thread instead, which will generate a log
129+
// message containing the text "reacting to signal".
130+
//
131+
// Such tests may need to be changed in one of the following ways:
132+
// * Use a signal other than SIGUSR1 (e.g. test_stress_delivery_simultaneous in
133+
// test_signal.py).
134+
// * Send the signal to a specific thread rather than the whole process (e.g.
135+
// test_signals in test_threadsignals.py.
136+
sigset_t set;
137+
if (sigemptyset(&set)) {
138+
return "sigemptyset";
139+
}
140+
if (sigaddset(&set, SIGUSR1)) {
141+
return "sigaddset";
142+
}
143+
if ((errno = pthread_sigmask(SIG_UNBLOCK, &set, NULL))) {
144+
return "pthread_sigmask";
145+
}
146+
return NULL;
117147
}
118148

119149
static void throw_status(JNIEnv *env, PyStatus status) {
120150
throw_runtime_exception(env, status.err_msg ? status.err_msg : "");
121151
}
122152

123153
JNIEXPORT int JNICALL Java_org_python_testbed_PythonTestRunner_runPython(
124-
JNIEnv *env, jobject obj, jstring home, jstring runModule
154+
JNIEnv *env, jobject obj, jstring home, jarray args
125155
) {
156+
const char *home_utf8 = (*env)->GetStringUTFChars(env, home, NULL);
157+
char cwd[PATH_MAX];
158+
snprintf(cwd, sizeof(cwd), "%s/%s", home_utf8, "cwd");
159+
if (chdir(cwd)) {
160+
throw_errno(env, "chdir");
161+
return 1;
162+
}
163+
164+
char *error_prefix;
165+
if ((error_prefix = init_signals())) {
166+
throw_errno(env, error_prefix);
167+
return 1;
168+
}
169+
126170
PyConfig config;
127171
PyStatus status;
128-
PyConfig_InitIsolatedConfig(&config);
172+
PyConfig_InitPythonConfig(&config);
129173

130-
status = set_config_string(env, &config, &config.home, home);
131-
if (PyStatus_Exception(status)) {
174+
jsize argc = (*env)->GetArrayLength(env, args);
175+
const char *argv[argc + 1];
176+
for (int i = 0; i < argc; i++) {
177+
jobject arg = (*env)->GetObjectArrayElement(env, args, i);
178+
argv[i] = (*env)->GetStringUTFChars(env, arg, NULL);
179+
}
180+
argv[argc] = NULL;
181+
182+
// PyConfig_SetBytesArgv "must be called before other methods, since the
183+
// preinitialization configuration depends on command line arguments"
184+
if (PyStatus_Exception(status = PyConfig_SetBytesArgv(&config, argc, (char**)argv))) {
132185
throw_status(env, status);
133186
return 1;
134187
}
135188

136-
status = set_config_string(env, &config, &config.run_module, runModule);
189+
status = PyConfig_SetBytesString(&config, &config.home, home_utf8);
137190
if (PyStatus_Exception(status)) {
138191
throw_status(env, status);
139192
return 1;
140193
}
141194

142-
// Some tests generate SIGPIPE and SIGXFSZ, which should be ignored.
143-
config.install_signal_handlers = 1;
144-
145195
status = Py_InitializeFromConfig(&config);
146196
if (PyStatus_Exception(status)) {
147197
throw_status(env, status);

Android/testbed/app/src/main/java/org/python/testbed/MainActivity.kt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.os.*
55
import android.system.Os
66
import android.widget.TextView
77
import androidx.appcompat.app.*
8+
import org.json.JSONArray
89
import java.io.*
910

1011

@@ -15,30 +16,25 @@ class MainActivity : AppCompatActivity() {
1516
override fun onCreate(savedInstanceState: Bundle?) {
1617
super.onCreate(savedInstanceState)
1718
setContentView(R.layout.activity_main)
18-
val status = PythonTestRunner(this).run("-m", "test", "-W -uall")
19+
val status = PythonTestRunner(this).run("""["-m", "test", "-W", "-uall"]""")
1920
findViewById<TextView>(R.id.tvHello).text = "Exit status $status"
2021
}
2122
}
2223

2324

2425
class PythonTestRunner(val context: Context) {
25-
fun run(instrumentationArgs: Bundle) = run(
26-
instrumentationArgs.getString("pythonMode")!!,
27-
instrumentationArgs.getString("pythonModule")!!,
28-
instrumentationArgs.getString("pythonArgs") ?: "",
29-
)
30-
3126
/** Run Python.
3227
*
33-
* @param mode Either "-c" or "-m".
34-
* @param module Python statements for "-c" mode, or a module name for
35-
* "-m" mode.
36-
* @param args Arguments to add to sys.argv. Will be parsed by `shlex.split`.
28+
* @param args Python command-line, encoded as JSON.
3729
* @return The Python exit status: zero on success, nonzero on failure. */
38-
fun run(mode: String, module: String, args: String) : Int {
39-
Os.setenv("PYTHON_MODE", mode, true)
40-
Os.setenv("PYTHON_MODULE", module, true)
41-
Os.setenv("PYTHON_ARGS", args, true)
30+
fun run(args: String) : Int {
31+
// We leave argument 0 as an empty string, which is a placeholder for the
32+
// executable name in embedded mode.
33+
val argsJsonArray = JSONArray(args)
34+
val argsStringArray = Array<String>(argsJsonArray.length() + 1) { it -> ""}
35+
for (i in 0..<argsJsonArray.length()) {
36+
argsStringArray[i + 1] = argsJsonArray.getString(i)
37+
}
4238

4339
// Python needs this variable to help it find the temporary directory,
4440
// but Android only sets it on API level 33 and later.
@@ -47,10 +43,7 @@ class PythonTestRunner(val context: Context) {
4743
val pythonHome = extractAssets()
4844
System.loadLibrary("main_activity")
4945
redirectStdioToLogcat()
50-
51-
// The main module is in src/main/python. We don't simply call it
52-
// "main", as that could clash with third-party test code.
53-
return runPython(pythonHome.toString(), "android_testbed_main")
46+
return runPython(pythonHome.toString(), argsStringArray)
5447
}
5548

5649
private fun extractAssets() : File {
@@ -59,6 +52,13 @@ class PythonTestRunner(val context: Context) {
5952
throw RuntimeException("Failed to delete $pythonHome")
6053
}
6154
extractAssetDir("python", context.filesDir)
55+
56+
// Empty directories are lost in the asset packing/unpacking process.
57+
val cwd = File(pythonHome, "cwd")
58+
if (!cwd.exists()) {
59+
cwd.mkdir()
60+
}
61+
6262
return pythonHome
6363
}
6464

@@ -88,5 +88,5 @@ class PythonTestRunner(val context: Context) {
8888
}
8989

9090
private external fun redirectStdioToLogcat()
91-
private external fun runPython(home: String, runModule: String) : Int
91+
private external fun runPython(home: String, args: Array<String>) : Int
9292
}

Android/testbed/app/src/main/python/android_testbed_main.py

Lines changed: 0 additions & 48 deletions
This file was deleted.

Lib/test/libregrtest/main.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -646,15 +646,20 @@ def _add_cross_compile_opts(self, regrtest_opts):
646646
return (environ, keep_environ)
647647

648648
def _add_ci_python_opts(self, python_opts, keep_environ):
649-
# --fast-ci and --slow-ci add options to Python:
650-
# "-u -W default -bb -E"
651-
652-
# Unbuffered stdout and stderr
653-
if not sys.stdout.write_through:
649+
# --fast-ci and --slow-ci add options to Python.
650+
#
651+
# Some platforms cannot change options after startup, so if these
652+
# options are changed, also update the copies in:
653+
# * cpython/Android/android.py
654+
# * buildmaster-config/master/custom/factories.py
655+
656+
# Unbuffered stdout and stderr. This isn't helpful on Android, because
657+
# it would cause lines to be split into multiple log messages.
658+
if not sys.stdout.write_through and sys.platform != "android":
654659
python_opts.append('-u')
655660

656661
# Add warnings filter 'error'
657-
if 'default' not in sys.warnoptions:
662+
if 'error' not in sys.warnoptions:
658663
python_opts.extend(('-W', 'error'))
659664

660665
# Error on bytes/str comparison

0 commit comments

Comments
 (0)