def _compile(pattern, flags=0, kwargs={}):
 debugging = ((flags & DEBUG) != 0)
 locale_key = (type(pattern), pattern)
 if (_locale_sensitive.get(locale_key, True) or ((flags & LOCALE) != 0)):
  pattern_locale = _getlocale()[1]
 else:
  pattern_locale = None
 if (not debugging):
  try:
   args_key = (pattern, type(pattern), flags)
   args_needed = _named_args[args_key]
   args_supplied = set()
   if args_needed:
    for (k, v) in args_needed:
     try:
      args_supplied.add((k, frozenset(kwargs[k])))
     except KeyError:
      raise error('missing named list: {!r}'.format(k))
   args_supplied = frozenset(args_supplied)
   pattern_key = (pattern, type(pattern), flags, args_supplied, DEFAULT_VERSION, pattern_locale)
   return _cache[pattern_key]
  except KeyError:
   pass
 if isinstance(pattern, unicode):
  guess_encoding = UNICODE
 elif isinstance(pattern, str):
  guess_encoding = ASCII
 elif isinstance(pattern, _pattern_type):
  if flags:
   raise ValueError('cannot process flags argument with a compiled pattern')
  return pattern
 else:
  raise TypeError('first argument must be a string or compiled pattern')
 _regex_core.DEFAULT_VERSION = DEFAULT_VERSION
 global_flags = flags
 while True:
  caught_exception = None
  try:
   source = _Source(pattern)
   info = _Info(global_flags, source.char_type, kwargs)
   info.guess_encoding = guess_encoding
   source.ignore_space = bool((info.flags & VERBOSE))
   parsed = _parse_pattern(source, info)
   break
  except _UnscopedFlagSet:
   global_flags = info.global_flags
  except error as e:
   caught_exception = e
  if caught_exception:
   raise error(caught_exception.msg, caught_exception.pattern, caught_exception.pos)
 if (not source.at_end()):
  raise error('unbalanced parenthesis', pattern, source.pos)
 version = ((info.flags & _ALL_VERSIONS) or DEFAULT_VERSION)
 if (version not in (0, VERSION0, VERSION1)):
  raise ValueError('VERSION0 and VERSION1 flags are mutually incompatible')
 if ((info.flags & _ALL_ENCODINGS) not in (0, ASCII, LOCALE, UNICODE)):
  raise ValueError('ASCII, LOCALE and UNICODE flags are mutually incompatible')
 if (not (info.flags & _ALL_ENCODINGS)):
  if isinstance(pattern, unicode):
   info.flags |= UNICODE
  else:
   info.flags |= ASCII
 reverse = bool((info.flags & REVERSE))
 fuzzy = isinstance(parsed, _Fuzzy)
 _locale_sensitive[locale_key] = info.inline_locale
 caught_exception = None
 try:
  parsed.fix_groups(pattern, reverse, False)
 except error as e:
  caught_exception = e
 if caught_exception:
  raise error(caught_exception.msg, caught_exception.pattern, caught_exception.pos)
 if (flags & DEBUG):
  parsed.dump(indent=0, reverse=reverse)
 parsed = parsed.optimise(info, reverse)
 parsed = parsed.pack_characters(info)
 (req_offset, req_chars, req_flags) = _get_required_string(parsed, info.flags)
 named_lists = {}
 named_list_indexes = ([None] * len(info.named_lists_used))
 args_needed = set()
 for (key, index) in info.named_lists_used.items():
  (name, case_flags) = key
  values = frozenset(kwargs[name])
  if case_flags:
   items = frozenset((_fold_case(info, v) for v in values))
  else:
   items = values
  named_lists[name] = values
  named_list_indexes[index] = items
  args_needed.add((name, values))
 _check_group_features(info, parsed)
 code = parsed.compile(reverse)
 key = (0, reverse, fuzzy)
 ref = info.call_refs.get(key)
 if (ref is not None):
  code = (([(_OP.CALL_REF, ref)] + code) + [(_OP.END,)])
 code += [(_OP.SUCCESS,)]
 for (group, rev, fuz) in info.additional_groups:
  code += group.compile(rev, fuz)
 code = _flatten_code(code)
 if (not parsed.has_simple_start()):
  try:
   fs_code = _compile_firstset(info, parsed.get_firstset(reverse))
   fs_code = _flatten_code(fs_code)
   code = (fs_code + code)
  except _FirstSetError:
   pass
 index_group = dict(((v, n) for (n, v) in info.group_index.items()))
 compiled_pattern = _regex.compile(pattern, (info.flags | version), code, info.group_index, index_group, named_lists, named_list_indexes, req_offset, req_chars, req_flags, info.group_count)
 if (len(_cache) >= _MAXCACHE):
  _cache_lock.acquire()
  try:
   _shrink_cache(_cache, _named_args, _locale_sensitive, _MAXCACHE)
  finally:
   _cache_lock.release()
 if (not debugging):
  if ((info.flags & LOCALE) == 0):
   pattern_locale = None
  args_needed = frozenset(args_needed)
  pattern_key = (pattern, type(pattern), flags, args_needed, DEFAULT_VERSION, pattern_locale)
  _cache[pattern_key] = compiled_pattern
  _named_args[args_key] = args_needed
 return compiled_pattern

def _compile ( pattern , flags = 0 , kwargs = { } ) : debugging = ( ( flags & DEBUG ) != 0 ) locale_key = ( type ( pattern ) , pattern ) if ( _locale_sensitive . get ( locale_key , True ) or ( ( flags & LOCALE ) != 0 ) ) : pattern_locale = _getlocale ( ) [ 1 ] else : pattern_locale = None if ( not debugging ) : try : args_key = ( pattern , type ( pattern ) , flags ) args_needed = _named_args [ args_key ] args_supplied = set ( ) if args_needed : for ( k , v ) in args_needed : try : args_supplied . add ( ( k , frozenset ( kwargs [ k ] ) ) ) except KeyError : raise error ( 'missing named list: {!r}' . format ( k ) ) args_supplied = frozenset ( args_supplied ) pattern_key = ( pattern , type ( pattern ) , flags , args_supplied , DEFAULT_VERSION , pattern_locale ) return _cache [ pattern_key ] except KeyError : pass if isinstance ( pattern , unicode ) : guess_encoding = UNICODE elif isinstance ( pattern , str ) : guess_encoding = ASCII elif isinstance ( pattern , _pattern_type ) : if flags : raise ValueError ( 'cannot process flags argument with a compiled pattern' ) return pattern else : raise TypeError ( 'first argument must be a string or compiled pattern' ) _regex_core . DEFAULT_VERSION = DEFAULT_VERSION global_flags = flags while True : caught_exception = None try : source = _Source ( pattern ) info = _Info ( global_flags , source . char_type , kwargs ) info . guess_encoding = guess_encoding source . ignore_space = bool ( ( info . flags & VERBOSE ) ) parsed = _parse_pattern ( source , info ) break except _UnscopedFlagSet : global_flags = info . global_flags except error as e : caught_exception = e if caught_exception : raise error ( caught_exception . msg , caught_exception . pattern , caught_exception . pos ) if ( not source . at_end ( ) ) : raise error ( 'unbalanced parenthesis' , pattern , source . pos ) version = ( ( info . flags & _ALL_VERSIONS ) or DEFAULT_VERSION ) if ( version not in ( 0 , VERSION0 , VERSION1 ) ) : raise ValueError ( 'VERSION0 and VERSION1 flags are mutually incompatible' ) if ( ( info . flags & _ALL_ENCODINGS ) not in ( 0 , ASCII , LOCALE , UNICODE ) ) : raise ValueError ( 'ASCII, LOCALE and UNICODE flags are mutually incompatible' ) if ( not ( info . flags & _ALL_ENCODINGS ) ) : if isinstance ( pattern , unicode ) : info . flags |= UNICODE else : info . flags |= ASCII reverse = bool ( ( info . flags & REVERSE ) ) fuzzy = isinstance ( parsed , _Fuzzy ) _locale_sensitive [ locale_key ] = info . inline_locale caught_exception = None try : parsed . fix_groups ( pattern , reverse , False ) except error as e : caught_exception = e if caught_exception : raise error ( caught_exception . msg , caught_exception . pattern , caught_exception . pos ) if ( flags & DEBUG ) : parsed . dump ( indent = 0 , reverse = reverse ) parsed = parsed . optimise ( info , reverse ) parsed = parsed . pack_characters ( info ) ( req_offset , req_chars , req_flags ) = _get_required_string ( parsed , info . flags ) named_lists = { } named_list_indexes = ( [ None ] * len ( info . named_lists_used ) ) args_needed = set ( ) for ( key , index ) in info . named_lists_used . items ( ) : ( name , case_flags ) = key values = frozenset ( kwargs [ name ] ) if case_flags : items = frozenset ( ( _fold_case ( info , v ) for v in values ) ) else : items = values named_lists [ name ] = values named_list_indexes [ index ] = items args_needed . add ( ( name , values ) ) _check_group_features ( info , parsed ) code = parsed . compile ( reverse ) key = ( 0 , reverse , fuzzy ) ref = info . call_refs . get ( key ) if ( ref is not None ) : code = ( ( [ ( _OP . CALL_REF , ref ) ] + code ) + [ ( _OP . END , ) ] ) code += [ ( _OP . SUCCESS , ) ] for ( group , rev , fuz ) in info . additional_groups : code += group . compile ( rev , fuz ) code = _flatten_code ( code ) if ( not parsed . has_simple_start ( ) ) : try : fs_code = _compile_firstset ( info , parsed . get_firstset ( reverse ) ) fs_code = _flatten_code ( fs_code ) code = ( fs_code + code ) except _FirstSetError : pass index_group = dict ( ( ( v , n ) for ( n , v ) in info . group_index . items ( ) ) ) compiled_pattern = _regex . compile ( pattern , ( info . flags | version ) , code , info . group_index , index_group , named_lists , named_list_indexes , req_offset , req_chars , req_flags , info . group_count ) if ( len ( _cache ) >= _MAXCACHE ) : _cache_lock . acquire ( ) try : _shrink_cache ( _cache , _named_args , _locale_sensitive , _MAXCACHE ) finally : _cache_lock . release ( ) if ( not debugging ) : if ( ( info . flags & LOCALE ) == 0 ) : pattern_locale = None args_needed = frozenset ( args_needed ) pattern_key = ( pattern , type ( pattern ) , flags , args_needed , DEFAULT_VERSION , pattern_locale ) _cache [ pattern_key ] = compiled_pattern _named_args [ args_key ] = args_needed return compiled_pattern
compile a python source file .