mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	gh-105687: Remove deprecated objects from re module (#105688)
				
					
				
			This commit is contained in:
		
							parent
							
								
									fb655e0c45
								
							
						
					
					
						commit
						67f69dba0a
					
				
					 9 changed files with 13 additions and 56 deletions
				
			
		|  | @ -340,6 +340,10 @@ Removed | ||||||
|   attribute instead. |   attribute instead. | ||||||
|   (Contributed by Nikita Sobolev in :gh:`105546`.) |   (Contributed by Nikita Sobolev in :gh:`105546`.) | ||||||
| 
 | 
 | ||||||
|  | * Remove undocumented, never working, and deprecated ``re.template`` function | ||||||
|  |   and ``re.TEMPLATE`` flag (and ``re.T`` alias). | ||||||
|  |   (Contributed by Serhiy Storchaka and Nikita Sobolev in :gh:`105687`.) | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| Porting to Python 3.13 | Porting to Python 3.13 | ||||||
| ====================== | ====================== | ||||||
|  |  | ||||||
|  | @ -130,7 +130,7 @@ | ||||||
| # public symbols | # public symbols | ||||||
| __all__ = [ | __all__ = [ | ||||||
|     "match", "fullmatch", "search", "sub", "subn", "split", |     "match", "fullmatch", "search", "sub", "subn", "split", | ||||||
|     "findall", "finditer", "compile", "purge", "template", "escape", |     "findall", "finditer", "compile", "purge", "escape", | ||||||
|     "error", "Pattern", "Match", "A", "I", "L", "M", "S", "X", "U", |     "error", "Pattern", "Match", "A", "I", "L", "M", "S", "X", "U", | ||||||
|     "ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE", |     "ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE", | ||||||
|     "UNICODE", "NOFLAG", "RegexFlag", |     "UNICODE", "NOFLAG", "RegexFlag", | ||||||
|  | @ -150,7 +150,6 @@ class RegexFlag: | ||||||
|     DOTALL = S = _compiler.SRE_FLAG_DOTALL # make dot match newline |     DOTALL = S = _compiler.SRE_FLAG_DOTALL # make dot match newline | ||||||
|     VERBOSE = X = _compiler.SRE_FLAG_VERBOSE # ignore whitespace and comments |     VERBOSE = X = _compiler.SRE_FLAG_VERBOSE # ignore whitespace and comments | ||||||
|     # sre extensions (experimental, don't rely on these) |     # sre extensions (experimental, don't rely on these) | ||||||
|     TEMPLATE = T = _compiler.SRE_FLAG_TEMPLATE # unknown purpose, deprecated |  | ||||||
|     DEBUG = _compiler.SRE_FLAG_DEBUG # dump pattern after compilation |     DEBUG = _compiler.SRE_FLAG_DEBUG # dump pattern after compilation | ||||||
|     __str__ = object.__str__ |     __str__ = object.__str__ | ||||||
|     _numeric_repr_ = hex |     _numeric_repr_ = hex | ||||||
|  | @ -233,17 +232,6 @@ def purge(): | ||||||
|     _cache2.clear() |     _cache2.clear() | ||||||
|     _compile_template.cache_clear() |     _compile_template.cache_clear() | ||||||
| 
 | 
 | ||||||
| def template(pattern, flags=0): |  | ||||||
|     "Compile a template pattern, returning a Pattern object, deprecated" |  | ||||||
|     import warnings |  | ||||||
|     warnings.warn("The re.template() function is deprecated " |  | ||||||
|                   "as it is an undocumented function " |  | ||||||
|                   "without an obvious purpose. " |  | ||||||
|                   "Use re.compile() instead.", |  | ||||||
|                   DeprecationWarning) |  | ||||||
|     with warnings.catch_warnings(): |  | ||||||
|         warnings.simplefilter("ignore", DeprecationWarning)  # warn just once |  | ||||||
|         return _compile(pattern, flags|T) |  | ||||||
| 
 | 
 | ||||||
| # SPECIAL_CHARS | # SPECIAL_CHARS | ||||||
| # closing ')', '}' and ']' | # closing ')', '}' and ']' | ||||||
|  | @ -297,13 +285,6 @@ def _compile(pattern, flags): | ||||||
|             return pattern |             return pattern | ||||||
|         if not _compiler.isstring(pattern): |         if not _compiler.isstring(pattern): | ||||||
|             raise TypeError("first argument must be string or compiled pattern") |             raise TypeError("first argument must be string or compiled pattern") | ||||||
|         if flags & T: |  | ||||||
|             import warnings |  | ||||||
|             warnings.warn("The re.TEMPLATE/re.T flag is deprecated " |  | ||||||
|                     "as it is an undocumented flag " |  | ||||||
|                     "without an obvious purpose. " |  | ||||||
|                     "Don't use it.", |  | ||||||
|                     DeprecationWarning) |  | ||||||
|         p = _compiler.compile(pattern, flags) |         p = _compiler.compile(pattern, flags) | ||||||
|         if flags & DEBUG: |         if flags & DEBUG: | ||||||
|             return p |             return p | ||||||
|  |  | ||||||
|  | @ -101,8 +101,6 @@ def _compile(code, pattern, flags): | ||||||
|             else: |             else: | ||||||
|                 emit(ANY) |                 emit(ANY) | ||||||
|         elif op in REPEATING_CODES: |         elif op in REPEATING_CODES: | ||||||
|             if flags & SRE_FLAG_TEMPLATE: |  | ||||||
|                 raise error("internal: unsupported template operator %r" % (op,)) |  | ||||||
|             if _simple(av[2]): |             if _simple(av[2]): | ||||||
|                 emit(REPEATING_CODES[op][2]) |                 emit(REPEATING_CODES[op][2]) | ||||||
|                 skip = _len(code); emit(0) |                 skip = _len(code); emit(0) | ||||||
|  |  | ||||||
|  | @ -13,7 +13,7 @@ | ||||||
| 
 | 
 | ||||||
| # update when constants are added or removed | # update when constants are added or removed | ||||||
| 
 | 
 | ||||||
| MAGIC = 20221023 | MAGIC = 20230612 | ||||||
| 
 | 
 | ||||||
| from _sre import MAXREPEAT, MAXGROUPS | from _sre import MAXREPEAT, MAXGROUPS | ||||||
| 
 | 
 | ||||||
|  | @ -204,7 +204,6 @@ def _makecodes(*names): | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| # flags | # flags | ||||||
| SRE_FLAG_TEMPLATE = 1 # template mode (unknown purpose, deprecated) |  | ||||||
| SRE_FLAG_IGNORECASE = 2 # case insensitive | SRE_FLAG_IGNORECASE = 2 # case insensitive | ||||||
| SRE_FLAG_LOCALE = 4 # honour system locale | SRE_FLAG_LOCALE = 4 # honour system locale | ||||||
| SRE_FLAG_MULTILINE = 8 # treat target as multiline string | SRE_FLAG_MULTILINE = 8 # treat target as multiline string | ||||||
|  |  | ||||||
|  | @ -61,12 +61,11 @@ | ||||||
|     "x": SRE_FLAG_VERBOSE, |     "x": SRE_FLAG_VERBOSE, | ||||||
|     # extensions |     # extensions | ||||||
|     "a": SRE_FLAG_ASCII, |     "a": SRE_FLAG_ASCII, | ||||||
|     "t": SRE_FLAG_TEMPLATE, |  | ||||||
|     "u": SRE_FLAG_UNICODE, |     "u": SRE_FLAG_UNICODE, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| TYPE_FLAGS = SRE_FLAG_ASCII | SRE_FLAG_LOCALE | SRE_FLAG_UNICODE | TYPE_FLAGS = SRE_FLAG_ASCII | SRE_FLAG_LOCALE | SRE_FLAG_UNICODE | ||||||
| GLOBAL_FLAGS = SRE_FLAG_DEBUG | SRE_FLAG_TEMPLATE | GLOBAL_FLAGS = SRE_FLAG_DEBUG | ||||||
| 
 | 
 | ||||||
| class State: | class State: | ||||||
|     # keeps track of state for parsing |     # keeps track of state for parsing | ||||||
|  |  | ||||||
|  | @ -2398,30 +2398,6 @@ def test_bug_gh91616(self): | ||||||
|         self.assertTrue(re.fullmatch(r'(?s:(?>.*?\.).*)\Z', "a.txt")) # reproducer |         self.assertTrue(re.fullmatch(r'(?s:(?>.*?\.).*)\Z', "a.txt")) # reproducer | ||||||
|         self.assertTrue(re.fullmatch(r'(?s:(?=(?P<g0>.*?\.))(?P=g0).*)\Z', "a.txt")) |         self.assertTrue(re.fullmatch(r'(?s:(?=(?P<g0>.*?\.))(?P=g0).*)\Z', "a.txt")) | ||||||
| 
 | 
 | ||||||
|     def test_template_function_and_flag_is_deprecated(self): |  | ||||||
|         with self.assertWarns(DeprecationWarning) as cm: |  | ||||||
|             template_re1 = re.template(r'a') |  | ||||||
|         self.assertIn('re.template()', str(cm.warning)) |  | ||||||
|         self.assertIn('is deprecated', str(cm.warning)) |  | ||||||
|         self.assertIn('function', str(cm.warning)) |  | ||||||
|         self.assertNotIn('flag', str(cm.warning)) |  | ||||||
| 
 |  | ||||||
|         with self.assertWarns(DeprecationWarning) as cm: |  | ||||||
|             # we deliberately use more flags here to test that that still |  | ||||||
|             # triggers the warning |  | ||||||
|             # if paranoid, we could test multiple different combinations, |  | ||||||
|             # but it's probably not worth it |  | ||||||
|             template_re2 = re.compile(r'a', flags=re.TEMPLATE|re.UNICODE) |  | ||||||
|         self.assertIn('re.TEMPLATE', str(cm.warning)) |  | ||||||
|         self.assertIn('is deprecated', str(cm.warning)) |  | ||||||
|         self.assertIn('flag', str(cm.warning)) |  | ||||||
|         self.assertNotIn('function', str(cm.warning)) |  | ||||||
| 
 |  | ||||||
|         # while deprecated, is should still function |  | ||||||
|         self.assertEqual(template_re1, template_re2) |  | ||||||
|         self.assertTrue(template_re1.match('ahoy')) |  | ||||||
|         self.assertFalse(template_re1.match('nope')) |  | ||||||
| 
 |  | ||||||
|     @unittest.skipIf(multiprocessing is None, 'test requires multiprocessing') |     @unittest.skipIf(multiprocessing is None, 'test requires multiprocessing') | ||||||
|     def test_regression_gh94675(self): |     def test_regression_gh94675(self): | ||||||
|         pattern = re.compile(r'(?<=[({}])(((//[^\n]*)?[\n])([\000-\040])*)*' |         pattern = re.compile(r'(?<=[({}])(((//[^\n]*)?[\n])([\000-\040])*)*' | ||||||
|  | @ -2615,11 +2591,11 @@ def test_flags_repr(self): | ||||||
|                          "re.IGNORECASE|re.DOTALL|re.VERBOSE|0x100000") |                          "re.IGNORECASE|re.DOTALL|re.VERBOSE|0x100000") | ||||||
|         self.assertEqual( |         self.assertEqual( | ||||||
|                 repr(~re.I), |                 repr(~re.I), | ||||||
|                 "re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DOTALL|re.VERBOSE|re.TEMPLATE|re.DEBUG") |                 "re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DOTALL|re.VERBOSE|re.DEBUG|0x1") | ||||||
|         self.assertEqual(repr(~(re.I|re.S|re.X)), |         self.assertEqual(repr(~(re.I|re.S|re.X)), | ||||||
|                          "re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.TEMPLATE|re.DEBUG") |                          "re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DEBUG|0x1") | ||||||
|         self.assertEqual(repr(~(re.I|re.S|re.X|(1<<20))), |         self.assertEqual(repr(~(re.I|re.S|re.X|(1<<20))), | ||||||
|                          "re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.TEMPLATE|re.DEBUG|0xffe00") |                          "re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DEBUG|0xffe01") | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class ImplementationTest(unittest.TestCase): | class ImplementationTest(unittest.TestCase): | ||||||
|  |  | ||||||
|  | @ -0,0 +1,2 @@ | ||||||
|  | Remove deprecated ``re.template``, ``re.T``, ``re.TEMPLATE``, | ||||||
|  | ``sre_constans.SRE_FLAG_TEMPLATE``. | ||||||
|  | @ -1335,7 +1335,6 @@ pattern_repr(PatternObject *obj) | ||||||
|         const char *name; |         const char *name; | ||||||
|         int value; |         int value; | ||||||
|     } flag_names[] = { |     } flag_names[] = { | ||||||
|         {"re.TEMPLATE", SRE_FLAG_TEMPLATE}, |  | ||||||
|         {"re.IGNORECASE", SRE_FLAG_IGNORECASE}, |         {"re.IGNORECASE", SRE_FLAG_IGNORECASE}, | ||||||
|         {"re.LOCALE", SRE_FLAG_LOCALE}, |         {"re.LOCALE", SRE_FLAG_LOCALE}, | ||||||
|         {"re.MULTILINE", SRE_FLAG_MULTILINE}, |         {"re.MULTILINE", SRE_FLAG_MULTILINE}, | ||||||
|  |  | ||||||
|  | @ -11,7 +11,7 @@ | ||||||
|  * See the sre.c file for information on usage and redistribution. |  * See the sre.c file for information on usage and redistribution. | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| #define SRE_MAGIC 20221023 | #define SRE_MAGIC 20230612 | ||||||
| #define SRE_OP_FAILURE 0 | #define SRE_OP_FAILURE 0 | ||||||
| #define SRE_OP_SUCCESS 1 | #define SRE_OP_SUCCESS 1 | ||||||
| #define SRE_OP_ANY 2 | #define SRE_OP_ANY 2 | ||||||
|  | @ -85,7 +85,6 @@ | ||||||
| #define SRE_CATEGORY_UNI_NOT_WORD 15 | #define SRE_CATEGORY_UNI_NOT_WORD 15 | ||||||
| #define SRE_CATEGORY_UNI_LINEBREAK 16 | #define SRE_CATEGORY_UNI_LINEBREAK 16 | ||||||
| #define SRE_CATEGORY_UNI_NOT_LINEBREAK 17 | #define SRE_CATEGORY_UNI_NOT_LINEBREAK 17 | ||||||
| #define SRE_FLAG_TEMPLATE 1 |  | ||||||
| #define SRE_FLAG_IGNORECASE 2 | #define SRE_FLAG_IGNORECASE 2 | ||||||
| #define SRE_FLAG_LOCALE 4 | #define SRE_FLAG_LOCALE 4 | ||||||
| #define SRE_FLAG_MULTILINE 8 | #define SRE_FLAG_MULTILINE 8 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Nikita Sobolev
						Nikita Sobolev