Exemplo n.º 1
0
 def test_empty_array(self):
     # SF buf 1647541
     import array
     for typecode in 'cbBuhHiIlLfd':
         a = array.array(typecode)
         assert re.compile("bla").match(a) == None
         assert re.compile("").match(a).groups() == ()
Exemplo n.º 2
0
 def test_empty_array(self):
     # SF buf 1647541
     import array
     for typecode in 'cbBuhHiIlLfd':
         a = array.array(typecode)
         assert re.compile("bla").match(a) == None
         assert re.compile("").match(a).groups() == ()
Exemplo n.º 3
0
 def test_bug_926075(self):
     try:
         unicode
     except NameError:
         return # no problem if we have no unicode
     assert (re.compile('bug_926075') is not
                  re.compile(eval("u'bug_926075'")))
Exemplo n.º 4
0
    def test_re_match(self):
        assert re.match('a', 'a').groups() == ()
        assert re.match('(a)', 'a').groups() == ('a',)
        assert re.match(r'(a)', 'a').group(0) == 'a'
        assert re.match(r'(a)', 'a').group(1) == 'a'
        assert re.match(r'(a)', 'a').group(1, 1) == ('a', 'a')

        pat = re.compile('((a)|(b))(c)?')
        assert pat.match('a').groups() == ('a', 'a', None, None)
        assert pat.match('b').groups() == ('b', None, 'b', None)
        assert pat.match('ac').groups() == ('a', 'a', None, 'c')
        assert pat.match('bc').groups() == ('b', None, 'b', 'c')
        assert pat.match('bc').groups("") == ('b', "", 'b', 'c')

        # A single group
        m = re.match('(a)', 'a')
        assert m.group(0) == 'a'
        assert m.group(0) == 'a'
        assert m.group(1) == 'a'
        assert m.group(1, 1) == ('a', 'a')

        pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
        assert pat.match('a').group(1, 2, 3) == ('a', None, None)
        assert pat.match('b').group('a1', 'b2', 'c3') == (
                         (None, 'b', None))
        assert pat.match('ac').group(1, 'b2', 3) == ('a', None, 'c')
Exemplo n.º 5
0
 def test_bug_926075(self):
     try:
         unicode
     except NameError:
         return  # no problem if we have no unicode
     assert (re.compile('bug_926075')
             is not re.compile(eval("u'bug_926075'")))
Exemplo n.º 6
0
    def test_re_match(self):
        assert re.match('a', 'a').groups() == ()
        assert re.match('(a)', 'a').groups() == ('a', )
        assert re.match(r'(a)', 'a').group(0) == 'a'
        assert re.match(r'(a)', 'a').group(1) == 'a'
        assert re.match(r'(a)', 'a').group(1, 1) == ('a', 'a')

        pat = re.compile('((a)|(b))(c)?')
        assert pat.match('a').groups() == ('a', 'a', None, None)
        assert pat.match('b').groups() == ('b', None, 'b', None)
        assert pat.match('ac').groups() == ('a', 'a', None, 'c')
        assert pat.match('bc').groups() == ('b', None, 'b', 'c')
        assert pat.match('bc').groups("") == ('b', "", 'b', 'c')

        # A single group
        m = re.match('(a)', 'a')
        assert m.group(0) == 'a'
        assert m.group(0) == 'a'
        assert m.group(1) == 'a'
        assert m.group(1, 1) == ('a', 'a')

        pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
        assert pat.match('a').group(1, 2, 3) == ('a', None, None)
        assert pat.match('b').group('a1', 'b2', 'c3') == ((None, 'b', None))
        assert pat.match('ac').group(1, 'b2', 3) == ('a', None, 'c')
Exemplo n.º 7
0
 def test_bug_931848(self):
     try:
         unicode
     except NameError:
         pass
     pattern = eval('u"[\u002E\u3002\uFF0E\uFF61]"')
     assert re.compile(pattern).split("a.b.c") == (['a', 'b', 'c'])
Exemplo n.º 8
0
 def test_bug_931848(self):
     try:
         unicode
     except NameError:
         pass
     pattern = eval('u"[\u002E\u3002\uFF0E\uFF61]"')
     assert re.compile(pattern).split("a.b.c") == (
                      ['a','b','c'])
Exemplo n.º 9
0
 def test_bug_764548(self):
     # bug 764548, re.compile() barfs on str/unicode subclasses
     try:
         unicode
     except NameError:
         return  # no problem if we have no unicode
     class my_unicode(unicode): pass
     pat = re.compile(my_unicode("abc"))
     assert pat.match("xyz") == None
Exemplo n.º 10
0
 def pickle_test(self, pickle):
     oldpat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
     s = pickle.dumps(oldpat)
     newpat = pickle.loads(s)
     # Not using object identity for _sre.py, since some Python builds do
     # not seem to preserve that in all cases (observed on an UCS-4 build
     # of 2.4.1).
     #self.assertEqual(oldpat, newpat)
     assert oldpat.__dict__ == newpat.__dict__
Exemplo n.º 11
0
    def test_bug_581080(self):
        iter = re.finditer(r"\s", "a b")
        assert iter.next().span() == (1,2)
        raises(StopIteration, iter.next)

        if 0:    # XXX
            scanner = re.compile(r"\s").scanner("a b")
            assert scanner.search().span() == (1, 2)
            assert scanner.search() == None
Exemplo n.º 12
0
    def test_bug_581080(self):
        iter = re.finditer(r"\s", "a b")
        assert iter.next().span() == (1, 2)
        raises(StopIteration, iter.next)

        if 0:  # XXX
            scanner = re.compile(r"\s").scanner("a b")
            assert scanner.search().span() == (1, 2)
            assert scanner.search() == None
Exemplo n.º 13
0
 def pickle_test(self, pickle):
     oldpat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
     s = pickle.dumps(oldpat)
     newpat = pickle.loads(s)
     # Not using object identity for _sre.py, since some Python builds do
     # not seem to preserve that in all cases (observed on an UCS-4 build
     # of 2.4.1).
     #self.assertEqual(oldpat, newpat)
     assert oldpat.__dict__ == newpat.__dict__
Exemplo n.º 14
0
    def test_re_escape(self):
        p=""
        for i in range(0, 256):
            p = p + chr(i)
            assert re.match(re.escape(chr(i)), chr(i)) is not None
            assert re.match(re.escape(chr(i)), chr(i)).span() == (0,1)

        pat=re.compile(re.escape(p))
        assert pat.match(p) is not None
        assert pat.match(p).span() == (0,256)
Exemplo n.º 15
0
    def test_re_escape(self):
        p = ""
        for i in range(0, 256):
            p = p + chr(i)
            assert re.match(re.escape(chr(i)), chr(i)) is not None
            assert re.match(re.escape(chr(i)), chr(i)).span() == (0, 1)

        pat = re.compile(re.escape(p))
        assert pat.match(p) is not None
        assert pat.match(p).span() == (0, 256)
Exemplo n.º 16
0
    def test_bug_764548(self):
        # bug 764548, re.compile() barfs on str/unicode subclasses
        try:
            unicode
        except NameError:
            return  # no problem if we have no unicode

        class my_unicode(unicode):
            pass

        pat = re.compile(my_unicode("abc"))
        assert pat.match("xyz") == None
Exemplo n.º 17
0
    def test_inline_flags(self):
        # Bug #1700
        upper_char = unichr(0x1ea0)  # Latin Capital Letter A with Dot Bellow
        lower_char = unichr(0x1ea1)  # Latin Small Letter A with Dot Bellow

        p = re.compile(upper_char, re.I | re.U)
        q = p.match(lower_char)
        assert q != None

        p = re.compile(lower_char, re.I | re.U)
        q = p.match(upper_char)
        assert q != None

        p = re.compile('(?i)' + upper_char, re.U)
        q = p.match(lower_char)
        assert q != None

        p = re.compile('(?i)' + lower_char, re.U)
        q = p.match(upper_char)
        assert q != None

        p = re.compile('(?iu)' + upper_char)
        q = p.match(lower_char)
        assert q != None

        p = re.compile('(?iu)' + lower_char)
        q = p.match(upper_char)
        assert q != None
Exemplo n.º 18
0
    def test_inline_flags(self):
        # Bug #1700
        upper_char = unichr(0x1ea0) # Latin Capital Letter A with Dot Bellow
        lower_char = unichr(0x1ea1) # Latin Small Letter A with Dot Bellow

        p = re.compile(upper_char, re.I | re.U)
        q = p.match(lower_char)
        assert q != None

        p = re.compile(lower_char, re.I | re.U)
        q = p.match(upper_char)
        assert q != None

        p = re.compile('(?i)' + upper_char, re.U)
        q = p.match(lower_char)
        assert q != None

        p = re.compile('(?i)' + lower_char, re.U)
        q = p.match(upper_char)
        assert q != None

        p = re.compile('(?iu)' + upper_char)
        q = p.match(lower_char)
        assert q != None

        p = re.compile('(?iu)' + lower_char)
        q = p.match(upper_char)
        assert q != None
Exemplo n.º 19
0
    def test_re_groupref_exists(self):
        assert re.match('^(\()?([^()]+)(?(1)\))$',
                        '(a)').groups() == (('(', 'a'))
        assert re.match('^(\()?([^()]+)(?(1)\))$',
                        'a').groups() == ((None, 'a'))
        assert re.match('^(\()?([^()]+)(?(1)\))$', 'a)') == None
        assert re.match('^(\()?([^()]+)(?(1)\))$', '(a') == None
        assert re.match('^(?:(a)|c)((?(1)b|d))$',
                        'ab').groups() == (('a', 'b'))
        assert re.match('^(?:(a)|c)((?(1)b|d))$',
                        'cd').groups() == ((None, 'd'))
        assert re.match('^(?:(a)|c)((?(1)|d))$',
                        'cd').groups() == ((None, 'd'))
        assert re.match('^(?:(a)|c)((?(1)|d))$', 'a').groups() == (('a', ''))

        # Tests for bug #1177831: exercise groups other than the first group
        p = re.compile('(?P<g1>a)(?P<g2>b)?((?(g2)c|d))')
        assert p.match('abc').groups() == (('a', 'b', 'c'))
        assert p.match('ad').groups() == (('a', None, 'd'))
        assert p.match('abd') == None
        assert p.match('ac') == None
Exemplo n.º 20
0
    def test_re_groupref_exists(self):
        assert re.match('^(\()?([^()]+)(?(1)\))$', '(a)').groups() == (
                         ('(', 'a'))
        assert re.match('^(\()?([^()]+)(?(1)\))$', 'a').groups() == (
                         (None, 'a'))
        assert re.match('^(\()?([^()]+)(?(1)\))$', 'a)') == None
        assert re.match('^(\()?([^()]+)(?(1)\))$', '(a') == None
        assert re.match('^(?:(a)|c)((?(1)b|d))$', 'ab').groups() == (
                         ('a', 'b'))
        assert re.match('^(?:(a)|c)((?(1)b|d))$', 'cd').groups() == (
                         (None, 'd'))
        assert re.match('^(?:(a)|c)((?(1)|d))$', 'cd').groups() == (
                         (None, 'd'))
        assert re.match('^(?:(a)|c)((?(1)|d))$', 'a').groups() == (
                         ('a', ''))

        # Tests for bug #1177831: exercise groups other than the first group
        p = re.compile('(?P<g1>a)(?P<g2>b)?((?(g2)c|d))')
        assert p.match('abc').groups() == (
                         ('a', 'b', 'c'))
        assert p.match('ad').groups() == (
                         ('a', None, 'd'))
        assert p.match('abd') == None
        assert p.match('ac') == None
Exemplo n.º 21
0
    string = chr(n) * n
    rsre_core.search(pattern, string)
    #
    unicodestr = unichr(n) * n
    ctx = rsre_core.UnicodeMatchContext(pattern, unicodestr, 0,
                                        len(unicodestr), 0)
    rsre_core.search_context(ctx)
    #
    return 0


def test_gengraph():
    t, typer, graph = gengraph(main, [int])


m = compile("(a|b)aaaaa")


def test_match():
    def f(i):
        if i:
            s = "aaaaaa"
        else:
            s = "caaaaa"
        g = m.match(s)
        if g is None:
            return 3
        return int("aaaaaa" == g.group(0))

    assert interpret(f, [3]) == 1
    assert interpret(f, [0]) == 3
Exemplo n.º 22
0
 def test_flags(self):
     for flag in [re.I, re.M, re.X, re.S, re.L]:
         assert re.compile('^pattern$', flag) != None
Exemplo n.º 23
0
 def test_bug_612074(self):
     pat = u"[" + re.escape(u"\u2039") + u"]"
     assert re.compile(pat) and 1 == 1
Exemplo n.º 24
0
    pattern = [n] * n
    string = chr(n) * n
    rsre_core.search(pattern, string)
    #
    unicodestr = unichr(n) * n
    ctx = rsre_core.UnicodeMatchContext(pattern, unicodestr, 0, len(unicodestr), 0)
    rsre_core.search_context(ctx)
    #
    return 0


def test_gengraph():
    t, typer, graph = gengraph(main, [int])


m = compile("(a|b)aaaaa")


def test_match():
    def f(i):
        if i:
            s = "aaaaaa"
        else:
            s = "caaaaa"
        g = m.match(s)
        if g is None:
            return 3
        return int("aaaaaa" == g.group(0))

    assert interpret(f, [3]) == 1
    assert interpret(f, [0]) == 3
Exemplo n.º 25
0
 def test_bug_612074(self):
     pat=u"["+re.escape(u"\u2039")+u"]"
     assert re.compile(pat) and 1 == 1
Exemplo n.º 26
0
 def test_flags(self):
     for flag in [re.I, re.M, re.X, re.S, re.L]:
         assert re.compile('^pattern$', flag) != None