Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/IPython/utils/module_paths.py: 23%

13 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 06:09 +0000

1"""Utility functions for finding modules 

2 

3Utility functions for finding modules on sys.path. 

4 

5""" 

6#----------------------------------------------------------------------------- 

7# Copyright (c) 2011, the IPython Development Team. 

8# 

9# Distributed under the terms of the Modified BSD License. 

10# 

11# The full license is in the file COPYING.txt, distributed with this software. 

12#----------------------------------------------------------------------------- 

13 

14#----------------------------------------------------------------------------- 

15# Imports 

16#----------------------------------------------------------------------------- 

17 

18# Stdlib imports 

19import importlib 

20import sys 

21 

22# Third-party imports 

23 

24# Our own imports 

25 

26 

27#----------------------------------------------------------------------------- 

28# Globals and constants 

29#----------------------------------------------------------------------------- 

30 

31#----------------------------------------------------------------------------- 

32# Local utilities 

33#----------------------------------------------------------------------------- 

34 

35#----------------------------------------------------------------------------- 

36# Classes and functions 

37#----------------------------------------------------------------------------- 

38 

39def find_mod(module_name): 

40 """ 

41 Find module `module_name` on sys.path, and return the path to module `module_name`. 

42 

43 - If `module_name` refers to a module directory, then return path to __init__ file. 

44 - If `module_name` is a directory without an __init__file, return None. 

45 - If module is missing or does not have a `.py` or `.pyw` extension, return None. 

46 - Note that we are not interested in running bytecode. 

47 - Otherwise, return the fill path of the module. 

48 

49 Parameters 

50 ---------- 

51 module_name : str 

52 

53 Returns 

54 ------- 

55 module_path : str 

56 Path to module `module_name`, its __init__.py, or None, 

57 depending on above conditions. 

58 """ 

59 spec = importlib.util.find_spec(module_name) 

60 module_path = spec.origin 

61 if module_path is None: 

62 if spec.loader in sys.meta_path: 

63 return spec.loader 

64 return None 

65 else: 

66 split_path = module_path.split(".") 

67 if split_path[-1] in ["py", "pyw"]: 

68 return module_path 

69 else: 

70 return None