Issue #12626: In regrtest, allow to filter tests using a glob filter
with the `-m` (or `--match`) option.  This works with all test cases
using the unittest module.  This is useful with long test suites
such as test_io or test_subprocess.
diff --git a/Lib/test/support.py b/Lib/test/support.py
index 09846a8..e82a4f9 100644
--- a/Lib/test/support.py
+++ b/Lib/test/support.py
@@ -21,6 +21,7 @@
 import imp
 import time
 import sysconfig
+import fnmatch
 import logging.handlers
 
 try:
@@ -177,6 +178,7 @@
                          # small sizes, to make sure they work.)
 real_max_memuse = 0
 failfast = False
+match_tests = None
 
 # _original_stdout is meant to hold stdout at the time regrtest began.
 # This may be "the real" stdout, or IDLE's emulation of stdout, or whatever.
@@ -1183,6 +1185,18 @@
     return guards.get(platform.python_implementation().lower(), default)
 
 
+def _filter_suite(suite, pred):
+    """Recursively filter test cases in a suite based on a predicate."""
+    newtests = []
+    for test in suite._tests:
+        if isinstance(test, unittest.TestSuite):
+            _filter_suite(test, pred)
+            newtests.append(test)
+        else:
+            if pred(test):
+                newtests.append(test)
+    suite._tests = newtests
+
 
 def _run_suite(suite):
     """Run tests from a unittest.TestSuite-derived class."""
@@ -1218,6 +1232,14 @@
             suite.addTest(cls)
         else:
             suite.addTest(unittest.makeSuite(cls))
+    def case_pred(test):
+        if match_tests is None:
+            return True
+        for name in test.id().split("."):
+            if fnmatch.fnmatchcase(name, match_tests):
+                return True
+        return False
+    _filter_suite(suite, case_pred)
     _run_suite(suite)