@@ -146,15 +146,15 @@ def setup(sre_compiler, error_class, flags_table):
146
146
FLAG_DEBUG = 128
147
147
FLAG_ASCII = 256
148
148
FLAG_NAMES = [
149
- (FLAG_TEMPLATE , "re. TEMPLATE" ),
150
- (FLAG_IGNORECASE , "re. IGNORECASE" ),
151
- (FLAG_LOCALE , "re. LOCALE" ),
152
- (FLAG_MULTILINE , "re. MULTILINE" ),
153
- (FLAG_DOTALL , "re. DOTALL" ),
154
- (FLAG_UNICODE , "re. UNICODE" ),
155
- (FLAG_VERBOSE , "re. VERBOSE" ),
156
- (FLAG_DEBUG , "re. DEBUG" ),
157
- (FLAG_ASCII , "re. ASCII" ),
149
+ (FLAG_TEMPLATE , "TEMPLATE" ),
150
+ (FLAG_IGNORECASE , "IGNORECASE" ),
151
+ (FLAG_LOCALE , "LOCALE" ),
152
+ (FLAG_MULTILINE , "MULTILINE" ),
153
+ (FLAG_DOTALL , "DOTALL" ),
154
+ (FLAG_UNICODE , "UNICODE" ),
155
+ (FLAG_VERBOSE , "VERBOSE" ),
156
+ (FLAG_DEBUG , "DEBUG" ),
157
+ (FLAG_ASCII , "ASCII" ),
158
158
]
159
159
160
160
@@ -272,12 +272,12 @@ class Pattern():
272
272
def __init__ (self , pattern , flags ):
273
273
self .__binary = isinstance (pattern , bytes )
274
274
self .pattern = pattern
275
- self .flags = flags
275
+ self .__input_flags = flags
276
276
flags_str = []
277
- for char ,flag in FLAGS .items ():
277
+ for char , flag in FLAGS .items ():
278
278
if flags & flag :
279
279
flags_str .append (char )
280
- self .flags_str = "" .join (flags_str )
280
+ self .__flags_str = "" .join (flags_str )
281
281
self .__compiled_regexes = {}
282
282
compiled_regex = self .__tregex_compile (self .pattern )
283
283
self .groups = compiled_regex .groupCount - 1
@@ -288,6 +288,18 @@ def __init__(self, pattern, flags):
288
288
group_names = dir (groups )
289
289
self .groupindex = _mappingproxy ({name : groups [name ] for name in group_names })
290
290
291
+ @property
292
+ def flags (self ):
293
+ # Flags can be spcified both in the flag argument or inline in the regex. Extract them back from the regex
294
+ flags = self .__input_flags
295
+ for flag , name in FLAG_NAMES :
296
+ try :
297
+ if self .__tregex_compile (self .pattern ).flags [name ]:
298
+ flags |= flag
299
+ except KeyError :
300
+ pass
301
+ return flags
302
+
291
303
def __check_input_type (self , input ):
292
304
if not isinstance (input , str ) and not _is_bytes_like (input ):
293
305
raise TypeError ("expected string or bytes-like object" )
@@ -303,7 +315,7 @@ def __check_pos(pos):
303
315
304
316
def __tregex_compile (self , pattern , flags = None ):
305
317
if flags is None :
306
- flags = self .flags_str
318
+ flags = self .__flags_str
307
319
if (pattern , flags ) not in self .__compiled_regexes :
308
320
try :
309
321
self .__compiled_regexes [(pattern , flags )] = tregex_compile_internal (pattern , flags , fallback_compiler )
@@ -322,7 +334,7 @@ def __repr__(self):
322
334
for code , name in FLAG_NAMES :
323
335
if flags & code :
324
336
flags -= code
325
- flag_items .append (name )
337
+ flag_items .append (f're. { name } ' )
326
338
if flags != 0 :
327
339
flag_items .append ("0x%x" % flags )
328
340
if len (flag_items ) == 0 :
@@ -344,7 +356,7 @@ def __hash__(self):
344
356
return hash (self .pattern ) * 31 ^ hash (self .flags )
345
357
346
358
def _search (self , pattern , string , pos , endpos , sticky = False ):
347
- pattern = self .__tregex_compile (pattern , self .flags_str + ("y" if sticky else "" ))
359
+ pattern = self .__tregex_compile (pattern , self .__flags_str + ("y" if sticky else "" ))
348
360
input_str = string
349
361
if endpos == - 1 or endpos >= len (string ):
350
362
endpos = len (string )
0 commit comments