by default don t filter tests function used by support rununittest and regrtest listcases if a pattern contains at least one dot it s considered as a full test identifier example test testos filetests testaccess ignore patterns which contain fnmatch patterns or for example ignore testaccess simple case all patterns are full test identifier the test bisectcmd utility only uses such full test identifiers the search is case sensitive on purpose don t use flagsre ignorecase the regex matches the whole identifier for example test testos filetests testaccess try to match parts of the test identifier for example split test testos filetests testaccess into test testos filetests and testaccess by default don t filter tests function used by support run_unittest and regrtest list cases if a pattern contains at least one dot it s considered as a full test identifier example test test_os filetests test_access ignore patterns which contain fnmatch patterns or for example ignore test_access simple case all patterns are full test identifier the test bisect_cmd utility only uses such full test identifiers the search is case sensitive on purpose don t use flags re ignorecase the regex matches the whole identifier for example test test_os filetests test_access try to match parts of the test identifier for example split test test_os filetests test_access into test test_os filetests and test_access
import itertools
import operator
import re



_test_matchers = ()
_test_patterns = ()


def match_test(test):
    
    result = False
    for matcher, result in reversed(_test_matchers):
        if matcher(test.id()):
            return result
    return not result


def _is_full_match_test(pattern):
    
    
    
    
    
    
    return ('.' in pattern) and (not re.search(r'[?*\[\]]', pattern))


def set_match_tests(patterns):
    global _test_matchers, _test_patterns

    if not patterns:
        _test_matchers = ()
        _test_patterns = ()
    else:
        itemgetter = operator.itemgetter
        patterns = tuple(patterns)
        if patterns != _test_patterns:
            _test_matchers = [
                (_compile_match_function(map(itemgetter(0), it)), result)
                for result, it in itertools.groupby(patterns, itemgetter(1))
            ]
            _test_patterns = patterns


def _compile_match_function(patterns):
    patterns = list(patterns)

    if all(map(_is_full_match_test, patterns)):
        
        
        return set(patterns).__contains__
    else:
        import fnmatch
        regex = '|'.join(map(fnmatch.translate, patterns))
        
        
        regex_match = re.compile(regex).match

        def match_test_regex(test_id, regex_match=regex_match):
            if regex_match(test_id):
                
                
                return True
            else:
                
                
                
                return any(map(regex_match, test_id.split(".")))

        return match_test_regex