mirror of
				https://github.com/python/cpython.git
				synced 2025-10-30 21:21:22 +00:00 
			
		
		
		
	 d687900f98
			
		
	
	
		d687900f98
		
			
		
	
	
	
	
		
			
			Make `warnings.catch_warnings()` use a context variable for holding the warning filtering state if the `sys.flags.context_aware_warnings` flag is set to true. This makes using the context manager thread-safe in multi-threaded programs. Add the `sys.flags.thread_inherit_context` flag. If true, starting a new thread with `threading.Thread` will use a copy of the context from the caller of `Thread.start()`. Both these flags are set to true by default for the free-threaded build and false for the default build. Move the Python implementation of warnings.py into _py_warnings.py. Make _contextvars a builtin module. Co-authored-by: Kumar Aditya <kumaraditya@python.org>
		
			
				
	
	
		
			99 lines
		
	
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			99 lines
		
	
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| import sys
 | |
| 
 | |
| __all__ = [
 | |
|     "warn",
 | |
|     "warn_explicit",
 | |
|     "showwarning",
 | |
|     "formatwarning",
 | |
|     "filterwarnings",
 | |
|     "simplefilter",
 | |
|     "resetwarnings",
 | |
|     "catch_warnings",
 | |
|     "deprecated",
 | |
| ]
 | |
| 
 | |
| from _py_warnings import (
 | |
|     WarningMessage,
 | |
|     _DEPRECATED_MSG,
 | |
|     _OptionError,
 | |
|     _add_filter,
 | |
|     _deprecated,
 | |
|     _filters_mutated,
 | |
|     _filters_mutated_lock_held,
 | |
|     _filters_version,
 | |
|     _formatwarning_orig,
 | |
|     _formatwarnmsg,
 | |
|     _formatwarnmsg_impl,
 | |
|     _get_context,
 | |
|     _get_filters,
 | |
|     _getaction,
 | |
|     _getcategory,
 | |
|     _is_filename_to_skip,
 | |
|     _is_internal_filename,
 | |
|     _is_internal_frame,
 | |
|     _lock,
 | |
|     _new_context,
 | |
|     _next_external_frame,
 | |
|     _processoptions,
 | |
|     _set_context,
 | |
|     _set_module,
 | |
|     _setoption,
 | |
|     _setup_defaults,
 | |
|     _showwarning_orig,
 | |
|     _showwarnmsg,
 | |
|     _showwarnmsg_impl,
 | |
|     _use_context,
 | |
|     _warn_unawaited_coroutine,
 | |
|     _warnings_context,
 | |
|     catch_warnings,
 | |
|     defaultaction,
 | |
|     deprecated,
 | |
|     filters,
 | |
|     filterwarnings,
 | |
|     formatwarning,
 | |
|     onceregistry,
 | |
|     resetwarnings,
 | |
|     showwarning,
 | |
|     simplefilter,
 | |
|     warn,
 | |
|     warn_explicit,
 | |
| )
 | |
| 
 | |
| try:
 | |
|     # Try to use the C extension, this will replace some parts of the
 | |
|     # _py_warnings implementation imported above.
 | |
|     from _warnings import (
 | |
|         _acquire_lock,
 | |
|         _defaultaction as defaultaction,
 | |
|         _filters_mutated_lock_held,
 | |
|         _onceregistry as onceregistry,
 | |
|         _release_lock,
 | |
|         _warnings_context,
 | |
|         filters,
 | |
|         warn,
 | |
|         warn_explicit,
 | |
|     )
 | |
| 
 | |
|     _warnings_defaults = True
 | |
| 
 | |
|     class _Lock:
 | |
|         def __enter__(self):
 | |
|             _acquire_lock()
 | |
|             return self
 | |
| 
 | |
|         def __exit__(self, *args):
 | |
|             _release_lock()
 | |
| 
 | |
|     _lock = _Lock()
 | |
| except ImportError:
 | |
|     _warnings_defaults = False
 | |
| 
 | |
| 
 | |
| # Module initialization
 | |
| _set_module(sys.modules[__name__])
 | |
| _processoptions(sys.warnoptions)
 | |
| if not _warnings_defaults:
 | |
|     _setup_defaults()
 | |
| 
 | |
| del _warnings_defaults
 | |
| del _setup_defaults
 |