Example #1
0
    def test_compile_with_latin1_encoding(self):
        options = re2.Options()
        options.encoding = re2.Options.Encoding.LATIN1
        with self.assertRaisesRegex(
                re2.error, ('string type of pattern is Text .*, but '
                            'encoding specified in options is LATIN1')):
            re2.compile(u'.?', options=options)

        # ... whereas this is fine, of course.
        re2.compile(b'.?', options=options)
Example #2
0
def _str_to_regex(s: str, case_sensitive: bool) -> Pattern:
    """Compile to re2.Pattern, or raise UserVisibleError."""
    options = re2.Options()
    options.log_errors = False
    options.never_capture = True  # we don't capture anything: we filter
    options.case_sensitive = case_sensitive

    try:
        # Compile the actual regex, to generate the actual error message.
        return re2.compile(s, options)
    except re2.error as err:
        msg = str(err.args[0], encoding="utf-8", errors="replace")
        raise ConditionError([re.error(msg, pattern=s)]) from None
Example #3
0
 def test_option(self, name):
     options = re2.Options()
     value = getattr(options, name)
     if isinstance(value, re2.Options.Encoding):
         value = next(v for v in type(value).__members__.values()
                      if v != value)
     elif isinstance(value, bool):
         value = not value
     elif isinstance(value, int):
         value = value + 1
     else:
         raise TypeError('option {!r}: {!r} {!r}'.format(
             name, type(value), value))
     setattr(options, name, value)
     self.assertEqual(value, getattr(options, name))
Example #4
0
def delete_re2_invalid(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
    options = re2.Options()
    options.log_errors = False

    RealmFilter = apps.get_model("zerver", "RealmFilter")
    found_errors = False
    for linkifier in RealmFilter.objects.all():
        try:
            re2.compile(linkifier.pattern, options=options)
        except re2.error:
            if not found_errors:
                print()
            found_errors = True
            print(
                f"Deleting linkifier {linkifier.id} in realm {linkifier.realm.string_id} which is not compatible with new re2 engine:"
            )
            print(f"  {linkifier.pattern} -> {linkifier.url_format_string}")
            linkifier.delete()
Example #5
0
from wayback.cdxserver import BlockedException
from wayback.timestamp import datetime2timestamp, timestamp2datetime

from datetime import (
    datetime,
    timedelta,
)
import ipaddr
import logging
from pytz import utc
import re2 as re

from .exceptions import MalformedResponseException

# more python re2 code examples: https://github.com/google/re2/blob/abseil/python/re2_test.py
re2_options = re.Options()
re2_options.encoding = re.Options.Encoding.LATIN1


class Rule(object):
    """Rule represents a rule received from the rulesengine server."""
    def __init__(self,
                 surt,
                 policy,
                 neg_surt=None,
                 capture_date=None,
                 retrieve_date=None,
                 ip_range=None,
                 seconds_since_capture=None,
                 collection=None,
                 partner=None,
Example #6
0
 def test_compile_with_options(self):
     options = re2.Options()
     options.max_mem = 100
     with self.assertRaisesRegex(re2.error, 'pattern too large'):
         re2.compile('.{1000}', options=options)