mirror of
				https://github.com/python/cpython.git
				synced 2025-11-01 06:01:29 +00:00 
			
		
		
		
	Issue #18157: stop using imp.load_module() in imp.
This commit is contained in:
		
							parent
							
								
									30b4131b41
								
							
						
					
					
						commit
						d5e6f2e200
					
				
					 2 changed files with 14 additions and 12 deletions
				
			
		
							
								
								
									
										24
									
								
								Lib/pydoc.py
									
										
									
									
									
								
							
							
						
						
									
										24
									
								
								Lib/pydoc.py
									
										
									
									
									
								
							|  | @ -53,6 +53,7 @@ class or function within a module or module in a package.  If the | |||
| 
 | ||||
| import builtins | ||||
| import imp | ||||
| import importlib._bootstrap | ||||
| import importlib.machinery | ||||
| import inspect | ||||
| import io | ||||
|  | @ -269,18 +270,17 @@ def importfile(path): | |||
|     """Import a Python source file or compiled file given its path.""" | ||||
|     magic = imp.get_magic() | ||||
|     with open(path, 'rb') as file: | ||||
|         if file.read(len(magic)) == magic: | ||||
|             kind = imp.PY_COMPILED | ||||
|         else: | ||||
|             kind = imp.PY_SOURCE | ||||
|         file.seek(0) | ||||
|         filename = os.path.basename(path) | ||||
|         name, ext = os.path.splitext(filename) | ||||
|         try: | ||||
|             module = imp.load_module(name, file, path, (ext, 'r', kind)) | ||||
|         except: | ||||
|             raise ErrorDuringImport(path, sys.exc_info()) | ||||
|     return module | ||||
|         is_bytecode = magic == file.read(len(magic)) | ||||
|     filename = os.path.basename(path) | ||||
|     name, ext = os.path.splitext(filename) | ||||
|     if is_bytecode: | ||||
|         loader = importlib._bootstrap.SourcelessFileLoader(name, path) | ||||
|     else: | ||||
|         loader = importlib._bootstrap.SourceFileLoader(name, path) | ||||
|     try: | ||||
|         return loader.load_module(name) | ||||
|     except: | ||||
|         raise ErrorDuringImport(path, sys.exc_info()) | ||||
| 
 | ||||
| def safeimport(path, forceload=0, cache={}): | ||||
|     """Import a module; handle errors; return None if the module isn't found. | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Brett Cannon
						Brett Cannon