Python re.error() Examples

The following are 30 code examples for showing how to use re.error(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module re , or try the search function .

Example 1
Project: SublimeKSP   Author: nojanath   File: lex.py    License: GNU General Public License v3.0 6 votes vote down vote up
def get_tokens(self):
        tokens = self.ldict.get("tokens",None)
        if not tokens:
            self.log.error("No token list is defined")
            self.error = 1
            return

        if not isinstance(tokens,(list, tuple)):
            self.log.error("tokens must be a list or tuple")
            self.error = 1
            return
        
        if not tokens:
            self.log.error("tokens is empty")
            self.error = 1
            return

        self.tokens = tokens

    # Validate the tokens 
Example 2
Project: jawfish   Author: war-and-code   File: test_re.py    License: MIT License 6 votes vote down vote up
def test_sre_character_class_literals(self):
        for i in [0, 8, 16, 32, 64, 127, 128, 255, 256, 0xFFFF, 0x10000, 0x10FFFF]:
            if i < 256:
                self.assertIsNotNone(re.match(r"[\%o]" % i, chr(i)))
                self.assertIsNotNone(re.match(r"[\%o8]" % i, chr(i)))
                self.assertIsNotNone(re.match(r"[\%03o]" % i, chr(i)))
                self.assertIsNotNone(re.match(r"[\%03o0]" % i, chr(i)))
                self.assertIsNotNone(re.match(r"[\%03o8]" % i, chr(i)))
                self.assertIsNotNone(re.match(r"[\x%02x]" % i, chr(i)))
                self.assertIsNotNone(re.match(r"[\x%02x0]" % i, chr(i)))
                self.assertIsNotNone(re.match(r"[\x%02xz]" % i, chr(i)))
            if i < 0x10000:
                self.assertIsNotNone(re.match(r"[\u%04x]" % i, chr(i)))
                self.assertIsNotNone(re.match(r"[\u%04x0]" % i, chr(i)))
                self.assertIsNotNone(re.match(r"[\u%04xz]" % i, chr(i)))
            self.assertIsNotNone(re.match(r"[\U%08x]" % i, chr(i)))
            self.assertIsNotNone(re.match(r"[\U%08x0]" % i, chr(i)+"0"))
            self.assertIsNotNone(re.match(r"[\U%08xz]" % i, chr(i)+"z"))
        self.assertIsNotNone(re.match(r"[\U0001d49c-\U0001d4b5]", "\U0001d49e"))
        self.assertRaises(re.error, re.match, r"[\911]", "")
        self.assertRaises(re.error, re.match, r"[\x1z]", "")
        self.assertRaises(re.error, re.match, r"[\u123z]", "")
        self.assertRaises(re.error, re.match, r"[\U0001234z]", "")
        self.assertRaises(re.error, re.match, r"[\U00110000]", "") 
Example 3
Project: razzy-spinner   Author: rafasashi   File: concordance_app.py    License: GNU General Public License v3.0 6 votes vote down vote up
def run(self):
            q = self.processed_query()
            sent_pos, i, sent_count = [], 0, 0
            for sent in self.model.tagged_sents[self.model.last_sent_searched:]:
                try:
                    m = re.search(q, sent)
                except re.error:
                    self.model.reset_results()
                    self.model.queue.put(SEARCH_ERROR_EVENT)
                    return
                if m:
                    sent_pos.append((sent, m.start(), m.end()))
                    i += 1
                    if i > self.count:
                        self.model.last_sent_searched += sent_count - 1
                        break
                sent_count += 1
            if (self.count >= len(sent_pos)):
                self.model.last_sent_searched += sent_count - 1
                self.model.last_page = self.page
                self.model.set_results(self.page, sent_pos)
            else:
                self.model.set_results(self.page, sent_pos[:-1])
            self.model.queue.put(SEARCH_TERMINATED_EVENT) 
Example 4
Project: misp42splunk   Author: remg427   File: validators.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def __call__(self, value):

        if value is None:
            return value

        path = six.text_type(value)

        if not os.path.isabs(path):
            path = os.path.join(self.directory, path)

        try:
            value = open(path, self.mode) if self.buffering is None else open(path, self.mode, self.buffering)
        except IOError as error:
            raise ValueError('Cannot open {0} with mode={1} and buffering={2}: {3}'.format(
                value, self.mode, self.buffering, error))

        return value 
Example 5
Project: misp42splunk   Author: remg427   File: lex.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def get_tokens(self):
        tokens = self.ldict.get('tokens', None)
        if not tokens:
            self.log.error('No token list is defined')
            self.error = True
            return

        if not isinstance(tokens, (list, tuple)):
            self.log.error('tokens must be a list or tuple')
            self.error = True
            return

        if not tokens:
            self.log.error('tokens is empty')
            self.error = True
            return

        self.tokens = tokens

    # Validate the tokens 
Example 6
Project: misp42splunk   Author: remg427   File: validators.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def __call__(self, value):

        if value is None:
            return value

        path = six.text_type(value)

        if not os.path.isabs(path):
            path = os.path.join(self.directory, path)

        try:
            value = open(path, self.mode) if self.buffering is None else open(path, self.mode, self.buffering)
        except IOError as error:
            raise ValueError('Cannot open {0} with mode={1} and buffering={2}: {3}'.format(
                value, self.mode, self.buffering, error))

        return value 
Example 7
Project: misp42splunk   Author: remg427   File: validators.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def __call__(self, value):

        if value is None or isinstance(value, list):
            return value

        try:
            value = next(csv.reader([value], self.Dialect))
        except csv.Error as error:
            raise ValueError(error)

        if self._validator is None:
            return value

        try:
            for index, item in enumerate(value):
                value[index] = self._validator(item)
        except ValueError as error:
            raise ValueError('Could not convert item {}: {}'.format(index, error))

        return value 
Example 8
Project: misp42splunk   Author: remg427   File: _format.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def checks(self, format, raises=()):
        """
        Register a decorated function as validating a new format.

        Arguments:

            format (str):

                The format that the decorated function will check.

            raises (Exception):

                The exception(s) raised by the decorated function when
                an invalid instance is found.

                The exception object will be accessible as the
                :attr:`ValidationError.cause` attribute of the resulting
                validation error.

        """

        def _checks(func):
            self.checkers[format] = (func, raises)
            return func
        return _checks 
Example 9
Project: misp42splunk   Author: remg427   File: validators.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def __call__(self, value):

        if value is None or isinstance(value, list):
            return value

        try:
            value = next(csv.reader([value], self.Dialect))
        except csv.Error as error:
            raise ValueError(error)

        if self._validator is None:
            return value

        try:
            for index, item in enumerate(value):
                value[index] = self._validator(item)
        except ValueError as error:
            raise ValueError('Could not convert item {}: {}'.format(index, error))

        return value 
Example 10
Project: misp42splunk   Author: remg427   File: lex.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def get_tokens(self):
        tokens = self.ldict.get('tokens', None)
        if not tokens:
            self.log.error('No token list is defined')
            self.error = True
            return

        if not isinstance(tokens, (list, tuple)):
            self.log.error('tokens must be a list or tuple')
            self.error = True
            return

        if not tokens:
            self.log.error('tokens is empty')
            self.error = True
            return

        self.tokens = tokens

    # Validate the tokens 
Example 11
Project: misp42splunk   Author: remg427   File: validators.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def __call__(self, value):

        if value is None:
            return value

        path = six.text_type(value)

        if not os.path.isabs(path):
            path = os.path.join(self.directory, path)

        try:
            value = open(path, self.mode) if self.buffering is None else open(path, self.mode, self.buffering)
        except IOError as error:
            raise ValueError('Cannot open {0} with mode={1} and buffering={2}: {3}'.format(
                value, self.mode, self.buffering, error))

        return value 
Example 12
Project: misp42splunk   Author: remg427   File: validators.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def __call__(self, value):

        if value is None or isinstance(value, list):
            return value

        try:
            value = next(csv.reader([value], self.Dialect))
        except csv.Error as error:
            raise ValueError(error)

        if self._validator is None:
            return value

        try:
            for index, item in enumerate(value):
                value[index] = self._validator(item)
        except ValueError as error:
            raise ValueError('Could not convert item {}: {}'.format(index, error))

        return value 
Example 13
Project: misp42splunk   Author: remg427   File: _format.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def checks(self, format, raises=()):
        """
        Register a decorated function as validating a new format.

        Arguments:

            format (str):

                The format that the decorated function will check.

            raises (Exception):

                The exception(s) raised by the decorated function when
                an invalid instance is found.

                The exception object will be accessible as the
                :attr:`ValidationError.cause` attribute of the resulting
                validation error.

        """

        def _checks(func):
            self.checkers[format] = (func, raises)
            return func
        return _checks 
Example 14
Project: plugin.video.kmediatorrent   Author: jmarth   File: urls.py    License: GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, url_rule, view_func, name, options):
        self._name = name
        self._url_rule = url_rule
        self._view_func = view_func
        self._options = options or {}
        self._keywords = re.findall(r'\<(.+?)\>', url_rule)

        #change <> to {} for use with str.format()
        self._url_format = self._url_rule.replace('<', '{').replace('>', '}')

        # Make a regex pattern for matching incoming URLs
        rule = self._url_rule
        if rule != '/':
            # Except for a path of '/', the trailing slash is optional.
            rule = self._url_rule.rstrip('/') + '/?'
        p = rule.replace('<', '(?P<').replace('>', '>[^/]+?)')

        try:
            self._regex = re.compile('^' + p + '$')
        except re.error, e:
            raise ValueError, ('There was a problem creating this URL rule. '
                               'Ensure you do not have any unpaired angle '
                               'brackets: "<" or ">"') 
Example 15
Project: qutebrowser   Author: qutebrowser   File: mhtml.py    License: GNU General Public License v3.0 6 votes vote down vote up
def _error(self, url, item, *_args):
        """Callback when a download error occurred.

        Args:
            url: The original url of the asset as QUrl.
            item: The DownloadItem given by the DownloadManager.
        """
        assert self.writer is not None
        try:
            self.pending_downloads.remove((url, item))
        except KeyError:
            # This might happen if .collect_zombies() calls .finished() and the
            # error handler will be called after .collect_zombies
            log.downloads.debug("Oops! Download already gone: {}".format(item))
            return
        item.fileobj.actual_close()
        # Add a stub file, see comment in .fetch_url() for more information
        self.writer.add_file(urlutils.encoded_url(url), b'')
        if self.pending_downloads:
            return
        self._finish_file() 
Example 16
Project: core   Author: getavalon   File: _format.py    License: MIT License 6 votes vote down vote up
def checks(self, format, raises=()):
        """
        Register a decorated function as validating a new format.

        :argument str format: the format that the decorated function will check
        :argument Exception raises: the exception(s) raised by the decorated
            function when an invalid instance is found. The exception object
            will be accessible as the :attr:`ValidationError.cause` attribute
            of the resulting validation error.

        """

        def _checks(func):
            self.checkers[format] = (func, raises)
            return func
        return _checks 
Example 17
Project: Forager   Author: opensourcesec   File: tools.py    License: MIT License 6 votes vote down vote up
def regex(ioc_type):
    ioc_patts = {
        "ip":b"((?:(?:[12]\d?\d?|[1-9]\d|[1-9])(?:\[\.\]|\.)){3}(?:[12]\d?\d?|[\d+]{1,2}))",
        "domain":b"([A-Za-z0-9]+(?:[\-|\.][A-Za-z0-9]+)*(?:\[\.\]|\.)(?:com|net|edu|ru|org|de|uk|jp|br|pl|info|fr|it|cn|in|su|pw|biz|co|eu|nl|kr|me))",
        "md5":b"\W([A-Fa-f0-9]{32})(?:\W|$)",
        "sha1":b"\W([A-Fa-f0-9]{40})(?:\W|$)",
        "sha256":b"\W([A-Fa-f0-9]{64})(?:\W|$)",
        "email":b"[a-zA-Z0-9_]+(?:\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?!([a-zA-Z0-9]*\.[a-zA-Z0-9]*\.[a-zA-Z0-9]*\.))(?:[A-Za-z0-9](?:[a-zA-Z0-9-]*[A-Za-z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?",
        "URL":b"((?:http|ftp|https)\:\/\/(?:[\w+?\.\w+])+[a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;]+)",
        "yara":b"(rule\s[\w\W]{,30}\{[\w\W\s]*\})"
    }

    try:
        pattern = re.compile(ioc_patts[ioc_type])
    except re.error:
        print('[!] Invalid type specified.')
        sys.exit(0)

    return pattern 
Example 18
Project: Forager   Author: opensourcesec   File: tools.py    License: MIT License 6 votes vote down vote up
def update_progress(progress):
    barLength = 20  # Modify this value to change the length of the progress bar
    status = ""
    if isinstance(progress, int):
        progress = float(progress)
    if not isinstance(progress, float):
        progress = 0
        status = "error: progress var must be float\r\n"
    if progress < 0:
        progress = 0
        status = Fore.RED + "Halt!\r\n"
    if progress >= .999:
        progress = 1
        status = Fore.GREEN + " Complete!\r\n"
    block = int(round(barLength*progress))
    text = "\r[*] Progress: [{0}] {1}% {2}".format("#"*block + "-"*(barLength-block), round(progress*100), status)
    sys.stdout.write(text)
    sys.stdout.flush() 
Example 19
Project: bender   Author: ahelal   File: base.py    License: MIT License 5 votes vote down vote up
def _msg_grammar(self, msg):
        ''' Return only message if grammar is not defined. Return regex if grammar is defined. And None if no match'''
        if self.mention:
            msg = self._remove_bot_id(msg, self.bot_id)
            if not self.grammar or not msg:
                # No grammar rule or or no mention of the bot, Just return msg
                return msg
        try:
            regex = re.compile(r"{}".format(self.grammar))
        except re.error as regex_error:
            fail_unless(False, "The grammar expression '{}' has an error : {}".format(self.grammar, regex_error))

        return regex.match(msg) 
Example 20
Project: ANGRYsearch   Author: DoTheEvo   File: angrysearch.py    License: GNU General Public License v2.0 5 votes vote down vote up
def new_query_new_thread(self, input_text):
        if not self.setting_params['fts'] or self.setting_params['regex_mode']:
            self.status_bar.showMessage(' ...')

        if input_text == '' and self.regex_query_ready:
            self.show_first_500()
            return

        if self.setting_params['regex_mode']:
            try:
                re.compile(input_text)
                is_valid = True
            except re.error:
                is_valid = False

            if not is_valid:
                self.status_bar.showMessage('regex not valid')
                return

            self.queries_threads.append(
                {'input': input_text,
                 'thread': ThreadDBQuery(input_text, self.setting_params)})

            self.queries_threads[-1]['thread'].db_query_signal.connect(
                self.database_query_done, Qc.Qt.QueuedConnection)
            if self.regex_query_ready:
                self.queries_threads[-1]['thread'].start()
                self.regex_query_ready = False
        else:
            self.queries_threads.append(
                {'input': input_text,
                 'thread': ThreadDBQuery(input_text, self.setting_params)})

            self.queries_threads[-1]['thread'].db_query_signal.connect(
                self.database_query_done, Qc.Qt.QueuedConnection)
            self.queries_threads[-1]['thread'].start()

        if len(self.queries_threads) > 100:
            del self.queries_threads[0:80]

    # CHECK IF THE RESULTS COME FROM THE LAST ONE OR THERE ARE SOME STILL GOING 
Example 21
Project: SublimeKSP   Author: nojanath   File: lex.py    License: GNU General Public License v3.0 5 votes vote down vote up
def error(self,msg,*args,**kwargs):
        self.f.write("ERROR: " + (msg % args) + "\n") 
Example 22
Project: SublimeKSP   Author: nojanath   File: lex.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self.lexre = None             # Master regular expression. This is a list of
                                      # tuples (re,findex) where re is a compiled
                                      # regular expression and findex is a list
                                      # mapping regex group numbers to rules
        self.lexretext = None         # Current regular expression strings
        self.lexstatere = {}          # Dictionary mapping lexer states to master regexs
        self.lexstateretext = {}      # Dictionary mapping lexer states to regex strings
        self.lexstaterenames = {}     # Dictionary mapping lexer states to symbol names
        self.lexstate = "INITIAL"     # Current lexer state
        self.lexstatestack = []       # Stack of lexer states
        self.lexstateinfo = None      # State information
        self.lexstateignore = {}      # Dictionary of ignored characters for each state
        self.lexstateerrorf = {}      # Dictionary of error functions for each state
        self.lexreflags = 0           # Optional re compile flags
        self.lexdata = None           # Actual input data (as a string)
        self.lexpos = 0               # Current position in input text
        self.lexlen = 0               # Length of the input text
        self.lexerrorf = None         # Error rule (if any)
        self.lextokens = None         # List of valid tokens
        self.lexignore = ""           # Ignored characters
        self.lexliterals = ""         # Literal characters that can be passed through
        self.lexmodule = None         # Module
        self.lineno = 1               # Current line number
        self.lexoptimize = 0          # Optimized mode 
Example 23
Project: SublimeKSP   Author: nojanath   File: lex.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __init__(self,ldict,log=None,reflags=0):
        self.ldict      = ldict
        self.error_func = None
        self.tokens     = []
        self.reflags    = reflags
        self.stateinfo  = { 'INITIAL' : 'inclusive'}
        self.modules    = {}
        self.error      = 0

        if log is None:
            self.log = PlyLogger(sys.stderr)
        else:
            self.log = log

    # Get all of the basic information 
Example 24
Project: SublimeKSP   Author: nojanath   File: lex.py    License: GNU General Public License v3.0 5 votes vote down vote up
def validate_all(self):
        self.validate_tokens()
        self.validate_literals()
        self.validate_rules()
        return self.error

    # Get the tokens map 
Example 25
Project: SublimeKSP   Author: nojanath   File: lex.py    License: GNU General Public License v3.0 5 votes vote down vote up
def validate_literals(self):
        try:
            for c in self.literals:
                if not isinstance(c,StringTypes) or len(c) > 1:
                    self.log.error("Invalid literal %s. Must be a single character", repr(c))
                    self.error = 1

        except TypeError:
            self.log.error("Invalid literals specification. literals must be a sequence of characters")
            self.error = 1 
Example 26
Project: SublimeKSP   Author: nojanath   File: lex.py    License: GNU General Public License v3.0 5 votes vote down vote up
def get_states(self):
        self.states = self.ldict.get("states",None)
        # Build statemap
        if self.states:
             if not isinstance(self.states,(tuple,list)):
                  self.log.error("states must be defined as a tuple or list")
                  self.error = 1
             else:
                  for s in self.states:
                        if not isinstance(s,tuple) or len(s) != 2:
                               self.log.error("Invalid state specifier %s. Must be a tuple (statename,'exclusive|inclusive')",repr(s))
                               self.error = 1
                               continue
                        name, statetype = s
                        if not isinstance(name,StringTypes):
                               self.log.error("State name %s must be a string", repr(name))
                               self.error = 1
                               continue
                        if not (statetype == 'inclusive' or statetype == 'exclusive'):
                               self.log.error("State type for state %s must be 'inclusive' or 'exclusive'",name)
                               self.error = 1
                               continue
                        if name in self.stateinfo:
                               self.log.error("State '%s' already defined",name)
                               self.error = 1
                               continue
                        self.stateinfo[name] = statetype

    # Get all of the symbols with a t_ prefix and sort them into various
    # categories (functions, strings, error functions, and ignore characters) 
Example 27
Project: SublimeKSP   Author: nojanath   File: lex.py    License: GNU General Public License v3.0 5 votes vote down vote up
def validate_module(self, module):
        lines, linen = inspect.getsourcelines(module)

        fre = re.compile(r'\s*def\s+(t_[a-zA-Z_0-9]*)\(')
        sre = re.compile(r'\s*(t_[a-zA-Z_0-9]*)\s*=')

        counthash = { }
        linen += 1
        for l in lines:
            m = fre.match(l)
            if not m:
                m = sre.match(l)
            if m:
                name = m.group(1)
                prev = counthash.get(name)
                if not prev:
                    counthash[name] = linen
                else:
                    filename = inspect.getsourcefile(module)
                    self.log.error("%s:%d: Rule %s redefined. Previously defined on line %d",filename,linen,name,prev)
                    self.error = 1
            linen += 1
            
# -----------------------------------------------------------------------------
# lex(module)
#
# Build all of the regular expression rules from definitions in the supplied module
# ----------------------------------------------------------------------------- 
Example 28
Project: aws-ops-automator   Author: awslabs   File: tag_filter_set.py    License: Apache License 2.0 5 votes vote down vote up
def match_string(self, filter_string, tested_string):
        """
        Matches a single string against a single filter
        :param filter_string: string containing the filer to match against. The string can start or end with a wildcard
        character, contain
        just the wildcard character of a regular expression starting with a \ character.
        :param tested_string: The string to test
        :return: True id the string matches, False if not
        """

        # empty or none matches empty or none
        if filter_string == "" or filter_string is None:
            return tested_string == "" or tested_string is None

        # filter is regex
        try:
            if filter_string.startswith(self._regex_char) and len(filter_string) > 1:
                return re.match(filter_string[1:], tested_string) is not None
        except re.error as ex:
            raise ValueError("\"{}\" is not a valid regular expression ({})".format(filter_string[1:], ex))

        # just "*" matches any value
        if filter_string == self._wildcard_char:
            return True

        if filter_string.startswith(self._wildcard_char):
            if filter_string.endswith(self._wildcard_char):
                # *contained*
                return filter_string[1:-1] in tested_string
            else:
                # *endswith
                return tested_string.endswith(filter_string[1:])

        if filter_string.endswith(self._wildcard_char):
            # startswith*
            return tested_string.startswith(filter_string[:-1])
        else:
            # exact match
            return filter_string == tested_string 
Example 29
Project: jawfish   Author: war-and-code   File: test_re.py    License: MIT License 5 votes vote down vote up
def test_sub_template_numeric_escape(self):
        # bug 776311 and friends
        self.assertEqual(re.sub('x', r'\0', 'x'), '\0')
        self.assertEqual(re.sub('x', r'\000', 'x'), '\000')
        self.assertEqual(re.sub('x', r'\001', 'x'), '\001')
        self.assertEqual(re.sub('x', r'\008', 'x'), '\0' + '8')
        self.assertEqual(re.sub('x', r'\009', 'x'), '\0' + '9')
        self.assertEqual(re.sub('x', r'\111', 'x'), '\111')
        self.assertEqual(re.sub('x', r'\117', 'x'), '\117')

        self.assertEqual(re.sub('x', r'\1111', 'x'), '\1111')
        self.assertEqual(re.sub('x', r'\1111', 'x'), '\111' + '1')

        self.assertEqual(re.sub('x', r'\00', 'x'), '\x00')
        self.assertEqual(re.sub('x', r'\07', 'x'), '\x07')
        self.assertEqual(re.sub('x', r'\08', 'x'), '\0' + '8')
        self.assertEqual(re.sub('x', r'\09', 'x'), '\0' + '9')
        self.assertEqual(re.sub('x', r'\0a', 'x'), '\0' + 'a')

        self.assertEqual(re.sub('x', r'\400', 'x'), '\0')
        self.assertEqual(re.sub('x', r'\777', 'x'), '\377')

        self.assertRaises(re.error, re.sub, 'x', r'\1', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\8', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\9', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\11', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\18', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\1a', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\90', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\99', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\118', 'x') # r'\11' + '8'
        self.assertRaises(re.error, re.sub, 'x', r'\11a', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\181', 'x') # r'\18' + '1'
        self.assertRaises(re.error, re.sub, 'x', r'\800', 'x') # r'\80' + '0'

        # in python2.3 (etc), these loop endlessly in sre_parser.py
        self.assertEqual(re.sub('(((((((((((x)))))))))))', r'\11', 'x'), 'x')
        self.assertEqual(re.sub('((((((((((y))))))))))(.)', r'\118', 'xyz'),
                         'xz8')
        self.assertEqual(re.sub('((((((((((y))))))))))(.)', r'\11a', 'xyz'),
                         'xza') 
Example 30
Project: jawfish   Author: war-and-code   File: test_re.py    License: MIT License 5 votes vote down vote up
def test_sre_character_literals(self):
        for i in [0, 8, 16, 32, 64, 127, 128, 255, 256, 0xFFFF, 0x10000, 0x10FFFF]:
            if i < 256:
                self.assertIsNotNone(re.match(r"\%03o" % i, chr(i)))
                self.assertIsNotNone(re.match(r"\%03o0" % i, chr(i)+"0"))
                self.assertIsNotNone(re.match(r"\%03o8" % i, chr(i)+"8"))
                self.assertIsNotNone(re.match(r"\x%02x" % i, chr(i)))
                self.assertIsNotNone(re.match(r"\x%02x0" % i, chr(i)+"0"))
                self.assertIsNotNone(re.match(r"\x%02xz" % i, chr(i)+"z"))
            if i < 0x10000:
                self.assertIsNotNone(re.match(r"\u%04x" % i, chr(i)))
                self.assertIsNotNone(re.match(r"\u%04x0" % i, chr(i)+"0"))
                self.assertIsNotNone(re.match(r"\u%04xz" % i, chr(i)+"z"))
            self.assertIsNotNone(re.match(r"\U%08x" % i, chr(i)))
            self.assertIsNotNone(re.match(r"\U%08x0" % i, chr(i)+"0"))
            self.assertIsNotNone(re.match(r"\U%08xz" % i, chr(i)+"z"))
        self.assertIsNotNone(re.match(r"\0", "\000"))
        self.assertIsNotNone(re.match(r"\08", "\0008"))
        self.assertIsNotNone(re.match(r"\01", "\001"))
        self.assertIsNotNone(re.match(r"\018", "\0018"))
        self.assertIsNotNone(re.match(r"\567", chr(0o167)))
        self.assertRaises(re.error, re.match, r"\911", "")
        self.assertRaises(re.error, re.match, r"\x1", "")
        self.assertRaises(re.error, re.match, r"\x1z", "")
        self.assertRaises(re.error, re.match, r"\u123", "")
        self.assertRaises(re.error, re.match, r"\u123z", "")
        self.assertRaises(re.error, re.match, r"\U0001234", "")
        self.assertRaises(re.error, re.match, r"\U0001234z", "")
        self.assertRaises(re.error, re.match, r"\U00110000", "")