[Python-checkins] cpython: Issue #24932: Use proper command line parsing in _testembed

steve.dower python-checkins at python.org
Sun Jan 1 23:25:34 EST 2017


https://hg.python.org/cpython/rev/69b2a6284f3d
changeset:   105950:69b2a6284f3d
user:        Steve Dower <steve.dower at microsoft.com>
date:        Sun Jan 01 20:25:03 2017 -0800
summary:
  Issue #24932: Use proper command line parsing in _testembed

files:
  Lib/test/test_capi.py |   2 +-
  Misc/NEWS             |   2 +
  Programs/_testembed.c |  55 ++++++++++++++++++++++++------
  3 files changed, 47 insertions(+), 12 deletions(-)


diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py
--- a/Lib/test/test_capi.py
+++ b/Lib/test/test_capi.py
@@ -385,7 +385,7 @@
 
     def test_subinterps(self):
         # This is just a "don't crash" test
-        out, err = self.run_embedded_interpreter()
+        out, err = self.run_embedded_interpreter("repeated_init_and_subinterpreters")
         if support.verbose:
             print()
             print(out)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -680,6 +680,8 @@
 Tests
 -----
 
+- Issue #24932: Use proper command line parsing in _testembed
+
 - Issue #28950: Disallow -j0 to be combined with -T/-l in regrtest
   command line arguments.
 
diff --git a/Programs/_testembed.c b/Programs/_testembed.c
--- a/Programs/_testembed.c
+++ b/Programs/_testembed.c
@@ -33,7 +33,7 @@
     );
 }
 
-static void test_repeated_init_and_subinterpreters(void)
+static int test_repeated_init_and_subinterpreters(void)
 {
     PyThreadState *mainstate, *substate;
 #ifdef WITH_THREAD
@@ -70,6 +70,7 @@
         PyEval_RestoreThread(mainstate);
         Py_Finalize();
     }
+    return 0;
 }
 
 /*****************************************************
@@ -103,7 +104,7 @@
     Py_Finalize();
 }
 
-static void test_forced_io_encoding(void)
+static int test_forced_io_encoding(void)
 {
     /* Check various combinations */
     printf("--- Use defaults ---\n");
@@ -122,19 +123,51 @@
         printf("Unexpected success calling Py_SetStandardStreamEncoding");
     }
     Py_Finalize();
+    return 0;
 }
 
-/* Different embedding tests */
+/* *********************************************************
+ * List of test cases and the function that implements it.
+ * 
+ * Names are compared case-sensitively with the first
+ * argument. If no match is found, or no first argument was
+ * provided, the names of all test cases are printed and
+ * the exit code will be -1.
+ *
+ * The int returned from test functions is used as the exit
+ * code, and test_capi treats all non-zero exit codes as a
+ * failed test. 
+ *********************************************************/
+struct TestCase
+{
+    const char *name;
+    int (*func)(void);
+};
+
+static struct TestCase TestCases[] = {
+    { "forced_io_encoding", test_forced_io_encoding },
+    { "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters },
+    { NULL, NULL }
+};
+
 int main(int argc, char *argv[])
 {
+    if (argc > 1) {
+        for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
+            if (strcmp(argv[1], tc->name) == 0)
+                return (*tc->func)();
+        }
+    }
 
-    /* TODO: Check the argument string to allow for more test cases */
-    if (argc > 1) {
-        /* For now: assume "forced_io_encoding */
-        test_forced_io_encoding();
-    } else {
-        /* Run the original embedding test case by default */
-        test_repeated_init_and_subinterpreters();
+    /* No match found, or no test name provided, so display usage */
+    printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n"
+           "Normally executed via 'EmbeddingTests' in Lib/test/test_capi.py\n\n"
+           "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]);
+    for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
+        printf("  %s\n", tc->name);
     }
-    return 0;
+
+    /* Non-zero exit code will cause test_capi.py tests to fail.
+       This is intentional. */
+    return -1;
 }

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list