PageRenderTime 24ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/module/_sre/test/support_test_app_sre.py

https://bitbucket.org/pypy/pypy/
Python | 32 lines | 24 code | 7 blank | 1 comment | 4 complexity | 22257afb4eeb7aca98fe34b60f5ad6c8 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. """Support functions for app-level _sre tests."""
  2. import locale, _sre
  3. from sre_constants import OPCODES, ATCODES, CHCODES, MAXREPEAT
  4. def encode_literal(string):
  5. opcodes = []
  6. for character in string:
  7. opcodes.extend([OPCODES["literal"], ord(character)])
  8. return opcodes
  9. def assert_match(opcodes, strings):
  10. assert_something_about_match(lambda x: x, opcodes, strings)
  11. def assert_no_match(opcodes, strings):
  12. assert_something_about_match(lambda x: not x, opcodes, strings)
  13. def assert_something_about_match(assert_modifier, opcodes, strings):
  14. if isinstance(strings, str):
  15. strings = [strings]
  16. for string in strings:
  17. assert assert_modifier(search(opcodes, string))
  18. def search(opcodes, string):
  19. pattern = _sre.compile("ignore", 0, opcodes, 0, {}, None)
  20. return pattern.search(string)
  21. def void_locale():
  22. locale.setlocale(locale.LC_ALL, (None, None))
  23. def assert_lower_equal(tests, flags):
  24. for arg, expected in tests:
  25. assert ord(expected) == _sre.getlower(ord(arg), flags)