Exemplo n.º 1
0
def compile(p, flags=0):
    # internal: convert pattern list to internal format

    if type(p) in STRING_TYPES:
        import sre_parse
        pattern = p
        p = sre_parse.parse(p, flags)
    else:
        pattern = None

    code = _code(p, flags)

    # print code

    # FIXME: <fl> get rid of this limitation!
    assert p.pattern.groups <= 100,\
           "sorry, but this version only supports 100 named groups"

    # map in either direction
    groupindex = p.pattern.groupdict
    indexgroup = [None] * p.pattern.groups
    for k, i in groupindex.items():
        indexgroup[i] = k

    return _sre.compile(
        pattern, flags, code,
        p.pattern.groups-1,
        groupindex, indexgroup
        )
Exemplo n.º 2
0
def compile(p, flags=0):
    # internal: convert pattern list to internal format

    if isstring(p):
        pattern = p
        p = sre_parse.parse(p, flags)
    else:
        pattern = None

    code = _code(p, flags)

    # print code

    # XXX: <fl> get rid of this limitation!
    if p.pattern.groups > 100:
        raise AssertionError(
            "sorry, but this version only supports 100 named groups")

    # map in either direction
    groupindex = p.pattern.groupdict
    indexgroup = [None] * p.pattern.groups
    for k, i in groupindex.items():
        indexgroup[i] = k

    return _sre.compile(pattern, flags | p.pattern.flags, code,
                        p.pattern.groups - 1, groupindex, indexgroup)
Exemplo n.º 3
0
def compile(p, flags=0):
    # internal: convert pattern list to internal format

    if type(p) in STRING_TYPES:
        import sre_parse
        pattern = p
        p = sre_parse.parse(p, flags)
    else:
        pattern = None

    code = _code(p, flags)

    # print code

    # XXX: <fl> get rid of this limitation!
    assert p.pattern.groups <= 100,\
           "sorry, but this version only supports 100 named groups"

    # map in either direction
    groupindex = p.pattern.groupdict
    indexgroup = [None] * p.pattern.groups
    for k, i in groupindex.items():
        indexgroup[i] = k

    return _sre.compile(pattern, flags, code, p.pattern.groups - 1, groupindex,
                        indexgroup)
Exemplo n.º 4
0
def compile(p, flags=0):
    # internal: convert pattern list to internal format

    #if isstring(p):
    #    pattern = p
    #    p = sre_parse.parse(p, flags)
    #else:
    #    pattern = None

    #code = _code(p, flags)

    ## print(code)

    ## map in either direction
    #groupindex = p.pattern.groupdict
    #indexgroup = [None] * p.pattern.groups
    #for k, i in groupindex.items():
    #    indexgroup[i] = k

    #return _sre.compile(
    #    pattern, flags | p.pattern.flags, code,
    #    p.pattern.groups-1,
    #    groupindex, indexgroup
    #    )

    return _sre.compile(p, flags)
Exemplo n.º 5
0
def compile(p, flags=0):
    # internal: convert pattern list to internal format

    if isstring(p):
        pattern = p
        p = sre_parse.parse(p, flags)
    else:
        pattern = None

    code = _code(p, flags)

    if flags & SRE_FLAG_DEBUG:
        print()
        dis(code)

    # map in either direction
    groupindex = p.pattern.groupdict
    indexgroup = [None] * p.pattern.groups
    for k, i in groupindex.items():
        indexgroup[i] = k

    return _sre.compile(
        pattern, flags | p.pattern.flags, code,
        p.pattern.groups-1,
        groupindex, tuple(indexgroup)
        )
Exemplo n.º 6
0
def compile(p, flags=0):
    # internal: convert pattern list to internal format

    if isstring(p):
        pattern = p
        p = sre_parse.parse(p, flags)
    else:
        pattern = None

    code = _code(p, flags)

    # print code

    # XXX: <fl> get rid of this limitation!
    if p.pattern.groups > 100:
        raise AssertionError(
            "sorry, but this version only supports 100 named groups"
        )

    # map in either direction
    groupindex = p.pattern.groupdict
    indexgroup = [None] * p.pattern.groups
    for k, i in groupindex.items():
        indexgroup[i] = k

    return _sre.compile(
        pattern, flags | p.pattern.flags, code,
        p.pattern.groups - 1,
        groupindex, indexgroup
    )
Exemplo n.º 7
0
def compile(p, flags=0):
    # internal: convert pattern list to internal format

    if isstring(p):
        pattern = p
        p = sre_parse.parse(p, flags)
    else:
        pattern = None

    code = _code(p, flags)

    if flags & SRE_FLAG_DEBUG:
        print()
        dis(code)

    # map in either direction
    groupindex = p.state.groupdict
    indexgroup = [None] * p.state.groups
    for k, i in groupindex.items():
        indexgroup[i] = k

    return _sre.compile(
        pattern, flags | p.state.flags, code,
        p.state.groups-1,
        groupindex, tuple(indexgroup)
        )
Exemplo n.º 8
0
def main():
    if 0:
        from Pattern import make_match_function

        joy_match = make_match_function('[Aa](m)i(?P<what>t)\Z')
    else:
        import _sre

        joy_match = _sre.compile(
            None,  #'[Aa](m)i(?P<what>t)\\Z',
            0,
            [
                17,
                9,
                4,
                4,
                4,
                19,
                65,
                19,
                97,
                0,
                15,
                6,
                19,
                65,
                19,
                97,
                0,
                21,
                0,
                19,
                109,
                21,
                1,
                19,
                105,
                21,
                2,
                19,
                116,
                21,
                3,
                6,
                7,
                1,
            ],
            2,
            {
                'what': 2
            },
            ((None, None, 'what')),
        ).match

    m = joy_match('Joy')

    print m.group(0, 1, 2)
Exemplo n.º 9
0
def build_compiled(pattern, p, flags, code):
    if p.pattern.groups > 100:
        raise AssertionError(
            "sorry, but this version only supports 100 named groups")
    # map in either direction
    groupindex = p.pattern.groupdict
    indexgroup = [None] * p.pattern.groups
    for k, i in groupindex.items():
        indexgroup[i] = k

    return _sre.compile(pattern, flags | p.pattern.flags, code,
                        p.pattern.groups - 1, groupindex, indexgroup)
Exemplo n.º 10
0
def compile(p, flags=0):
    if isstring(p):
        pattern = p
        p = sre_parse.parse(p, flags)
    else:
        pattern = None
    code = _code(p, flags)
    groupindex = p.pattern.groupdict
    indexgroup = [None] * p.pattern.groups
    for k, i in groupindex.items():
        indexgroup[i] = k
    return _sre.compile(pattern, flags | p.pattern.flags, code,
                        p.pattern.groups - 1, groupindex, indexgroup)
Exemplo n.º 11
0
def compile(p, flags = 0):
    if isstring(p):
        pattern = p
        p = sre_parse.parse(p, flags)
    else:
        pattern = None
    code = _code(p, flags)
    if p.pattern.groups > 100:
        raise AssertionError('sorry, but this version only supports 100 named groups')
    groupindex = p.pattern.groupdict
    indexgroup = [None] * p.pattern.groups
    for k, i in groupindex.items():
        indexgroup[i] = k

    return _sre.compile(pattern, flags | p.pattern.flags, code, p.pattern.groups - 1, groupindex, indexgroup)
Exemplo n.º 12
0
def compile(p, flags = 0):
    if (type(p) in STRING_TYPES):
        import sre_parse
        pattern = p
        p = sre_parse.parse(p, flags)
    else:
        pattern = None
    code = _code(p, flags)
    assert (p.pattern.groups <= 100), 'sorry, but this version only supports 100 named groups'
    groupindex = p.pattern.groupdict
    indexgroup = ([None] * p.pattern.groups)
    for (k, i,) in groupindex.items():
        indexgroup[i] = k

    return _sre.compile(pattern, flags, code, (p.pattern.groups - 1), groupindex, indexgroup)
Exemplo n.º 13
0
def compile(p, flags=0):
    if isstring(p):
        pattern = p
        p = sre_parse.parse(p, flags)
    else:
        pattern = None
    code = _code(p, flags)
    if p.pattern.groups > 100:
        raise AssertionError('sorry, but this version only supports 100 named groups')
    groupindex = p.pattern.groupdict
    indexgroup = [None] * p.pattern.groups
    for k, i in groupindex.items():
        indexgroup[i] = k

    return _sre.compile(pattern, flags | p.pattern.flags, code, p.pattern.groups - 1, groupindex, indexgroup)
Exemplo n.º 14
0
def compile(p, flags=0):
    if type(p) in STRING_TYPES:
        import sre_parse
        pattern = p
        p = sre_parse.parse(p, flags)
    else:
        pattern = None
    code = _code(p, flags)
    groupindex = p.pattern.groupdict
    indexgroup = [None] * p.pattern.groups
    for (k, i) in groupindex.items():
        indexgroup[i] = k

    return _sre.compile(pattern, flags, code, p.pattern.groups - 1, groupindex,
                        indexgroup)
Exemplo n.º 15
0
def compile(p, flags=0):
    if (type(p) in STRING_TYPES):
        import sre_parse
        pattern = p
        p = sre_parse.parse(p, flags)
    else:
        pattern = None
    code = _code(p, flags)
    assert (p.pattern.groups <=
            100), 'sorry, but this version only supports 100 named groups'
    groupindex = p.pattern.groupdict
    indexgroup = ([None] * p.pattern.groups)
    for (
            k,
            i,
    ) in groupindex.items():
        indexgroup[i] = k

    return _sre.compile(pattern, flags, code, (p.pattern.groups - 1),
                        groupindex, indexgroup)
Exemplo n.º 16
0
def compile(p, flags=0):
    global _code_cache  # , _code_cache_dirty
    # internal: convert pattern list to internal format
    
    cachekey = (p, flags)   # type(p),
    pp, code = _code_cache.get(cachekey, (None, None))

    if code is None:

        if isstring(p):
            import sre_parse
            pattern = p
            pp = sre_parse.parse(p, flags)
        else:
            # TODO Error
            pattern = None
            pp = p
 
        code = _code(pp, flags)
    
        assert pp.pattern.groups <= 100,\
               "sorry, but this version only supports 100 named groups"

        _code_cache[cachekey] = (pp, code)
        # _code_cache_dirty = True

    # map in either direction
    groupindex = pp.pattern.groupdict
    indexgroup = [None] * pp.pattern.groups
    for k, i in groupindex.items():
        indexgroup[i] = k

    return _sre.compile(
        p, flags, code,
        pp.pattern.groups-1,
        groupindex, indexgroup
        )
Exemplo n.º 17
0
def compile(p, flags=0):
    # internal: convert pattern list to internal format

    if isstring(p):
        pattern = p
        p = sre_parse.parse(p, flags)
    else:
        pattern = None

    code = _code(p, flags)

    # print code

    # map in either direction
    groupindex = p.pattern.groupdict
    indexgroup = [None] * p.pattern.groups
    for k, i in groupindex.items():
        indexgroup[i] = k

    return _sre.compile(
        pattern, flags | p.pattern.flags, code,
        p.pattern.groups-1,
        groupindex, indexgroup
        )
Exemplo n.º 18
0
    [21, 3967, 26, 10, 23, 54113]
    [29, 23, 0, 2, 5]
    [31, 64351, 0, 28, 3, 22281, 20, 4463, 9, 25, 59154, 15245, 2,
                  16343, 3, 11600, 24380, 10, 37556, 10, 31, 15, 31]

Here is also a 'code' that triggers an infinite uninterruptible loop:

    [29, 1, 8, 21, 1, 43083, 6]

"""

import _sre, random

def pick():
    n = random.randrange(-65536, 65536)
    if n < 0:
        n &= 31
    return n

ss = ["", "world", "x" * 500]

while 1:
    code = [pick() for i in range(random.randrange(5, 25))]
    print code
    pat = _sre.compile(None, 0, code)
    for s in ss:
        try:
            pat.match(s)
        except RuntimeError:
            pass
Exemplo n.º 19
0
def f(n):
    if n:
        return f(n-1)
    p = _sre.compile('', 0, [1])
    print type(p)
    return weakref.ref(p)
Exemplo n.º 20
0
@undefined_decorator
def func():
    return 1

# -----------------
# operators
# -----------------

string = '%s %s' % (1, 2)

# Shouldn't raise an error, because `string` is really just a string, not an
# array or something.
string.upper

# -----------------
# imports
# -----------------

# Star imports and the like in modules should not cause attribute errors in
# this module.
import import_tree

import_tree.a
import_tree.b

# This is something that raised an error, because it was using a complex
# mixture of Jedi fakes and compiled objects.
import _sre
#! 15 attribute-error
_sre.compile().not_existing
Exemplo n.º 21
0
# See the License for the specific language governing permissions and
# limitations under the License.

import regfuzz, _sre, re, sys, urllib

lenbias = 8
progress = 2048

regfuzz.set_seed(int(sys.argv[1]))

match = 0
bad = 0

for i in range(int(sys.argv[2])):
    regex = regfuzz.getregex(lenbias, (1 << 3))
    # print urllib.quote(regex)
    if i % progress == 0:
        # print "sample: " + urllib.quote(regex)
        print "count: " + str(i) + " match: " + str(match) + " bad: " + str(
            bad)
    try:
        # try sre first
        _sre.compile(regex, ~0, [])
        # these may throw
        r = re.compile(regex)
        if re.search(r, regex):
            match += 1
    except:
        bad += 1
        continue
Exemplo n.º 22
0
def f(n):
    if n:
        return f(n-1)
    p = _sre.compile('', 0, [1])
    print type(p)
    return weakref.ref(p)
Exemplo n.º 23
0
    [31, 64351, 0, 28, 3, 22281, 20, 4463, 9, 25, 59154, 15245, 2,
                  16343, 3, 11600, 24380, 10, 37556, 10, 31, 15, 31]

Here is also a 'code' that triggers an infinite uninterruptible loop:

    [29, 1, 8, 21, 1, 43083, 6]

"""

import _sre, random


def pick():
    n = random.randrange(-65536, 65536)
    if n < 0:
        n &= 31
    return n


ss = ["", "world", "x" * 500]

while 1:
    code = [pick() for i in range(random.randrange(5, 25))]
    print(code)
    pat = _sre.compile(None, 0, code)
    for s in ss:
        try:
            pat.match(s)
        except RuntimeError:
            pass
Exemplo n.º 24
0
#
Exemplo n.º 25
0
#
Exemplo n.º 26
0
# See the License for the specific language governing permissions and
# limitations under the License.


import regfuzz,_sre,re,sys,urllib

lenbias = 8
progress = 2048

regfuzz.set_seed(int(sys.argv[1]))

match = 0
bad = 0

for i in range(int(sys.argv[2])):
    regex = regfuzz.getregex(lenbias, (1<<3))
    # print urllib.quote(regex)
    if i % progress == 0:
        # print "sample: " + urllib.quote(regex)
        print "count: " + str(i) + " match: " + str(match) + " bad: " + str(bad)
    try:
        # try sre first
        _sre.compile(regex, ~0, [])
        # these may throw
        r = re.compile(regex)
        if re.search(r,regex):
            match += 1;
    except:
        bad += 1
        continue
Exemplo n.º 27
0
def search(opcodes, string):
    pattern = _sre.compile("ignore", 0, opcodes, 0, {}, None)
    return pattern.search(string)
Exemplo n.º 28
0
 def search(self, opcodes, string):
     pattern = _sre.compile("ignore", 0, opcodes, 0, {}, None)
     return pattern.search(string)
Exemplo n.º 29
0
def test():
    rx = []

    while len(rx) < 100:
        try:
            args = [fstr(), 4, flst(), fint(), fdict(), tuple(flst())]
            r = _sre.compile(*args)
            rx.append((r, args))
        except (RuntimeError):
            pass

    for r, args in rx:
        for _ in range(9999):
            try:
                r.findall(fstr())
                r.findall(fstr(), fint())
                r.findall(fstr(), fint(), fint())

                list(r.finditer(fstr()))
                list(r.finditer(fstr(), fint()))
                list(r.finditer(fstr(), fint(), fint()))

                r.split(fstr())

                try:
                    r.sub(fstr(), fstr())
                    r.sub(fstr(), fstr(), fint())
                    r.subn(fstr(), fstr())
                    r.subn(fstr(), fstr(), fint())
                except (re.error, KeyError, IndexError):
                    pass

                s = r.scanner(fstr())
                s.match()
                s.search()
                s.pattern
            except (RuntimeError, SystemError):
                pass

    fullmatch_matches = []
    while len(fullmatch_matches) < 100:
        for r, rargs in rx:
            args = [fstr(), fint(), fint()]
            try:
                m = r.fullmatch(*args)
                if m: fullmatch_matches.append((m, (args, rargs)))
            except RuntimeError:
                pass

    match_matches = []
    while len(match_matches) < 100:
        for r, rargs in rx:
            args = [fstr(), fint(), fint()]
            try:
                m = r.match(*args)
                if m: match_matches.append((m, (args, rargs)))
            except RuntimeError:
                pass

    search_matches = []
    while len(search_matches) < 100:
        for r, rargs in rx:
            args = [fstr(), fint(), fint()]
            try:
                m = r.search(*args)
                if m: search_matches.append((m, (args, rargs)))
            except RuntimeError:
                pass

    for m, args in (fullmatch_matches + match_matches + search_matches):
        for _ in range(9999):
            try:
                m.start(randint(255) - 128)
                m.end(randint(255) - 128)
                m.span(randint(255) - 128)
                m.group(*flst())
                m.groupdict()
                m.lastgroup
                m.lastindex
            except IndexError:
                pass