Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/dateutil/tz/_tzpath.py: 13%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

53 statements  

1try: 

2 import zoneinfo 

3 

4 reset_tzpath = zoneinfo.reset_tzpath 

5 InvalidTZPathWarning = zoneinfo.InvalidTZPathWarning 

6 

7 def __getattr__(name): 

8 if name == "TZPATH": 

9 return zoneinfo.TZPATH 

10 else: 

11 raise AttributeError( 

12 "Module %s has no attribute %s" % (repr(__name__), repr(name)) 

13 ) 

14 

15except ImportError: 

16 import os 

17 import sys 

18 import warnings 

19 

20 import six 

21 

22 class InvalidTZPathWarning(RuntimeWarning): 

23 """Warning raised if an invalid path is specified in PYTHONTZPATH.""" 

24 

25 def _parse_python_tzpath(env_var, stacklevel): 

26 if not env_var: 

27 return () 

28 

29 raw_tzpath = env_var.split(os.pathsep) 

30 new_tzpath = tuple(filter(os.path.isabs, raw_tzpath)) 

31 

32 # If anything has been filtered out, we will warn about it 

33 if len(new_tzpath) != len(raw_tzpath): 

34 msg = _get_invalid_paths_message(raw_tzpath) 

35 

36 warnings.warn( 

37 "Invalid paths specified in PYTHONTZPATH environment variable. " 

38 + msg, 

39 InvalidTZPathWarning, 

40 stacklevel=stacklevel, 

41 ) 

42 

43 return new_tzpath 

44 

45 def _get_invalid_paths_message(tzpaths): 

46 invalid_paths = (path for path in tzpaths if not os.path.isabs(path)) 

47 

48 prefix = "\n " 

49 indented_str = prefix + prefix.join(invalid_paths) 

50 

51 return ( 

52 "Paths should be absolute but found the following relative paths:" 

53 + indented_str 

54 ) 

55 

56 def _reset_tzpath(to=None, stacklevel=4): 

57 global TZPATH 

58 tzpaths = to 

59 if tzpaths is not None: 

60 if isinstance(tzpaths, six.string_types + (bytes,)): 

61 raise TypeError( 

62 "tzpaths must be a sequence, not %s: %s" 

63 % ( 

64 type(tzpaths), 

65 repr(tzpaths), 

66 ) 

67 ) 

68 

69 # Path-like objects not supported by os.path operations before 3.6 

70 if sys.version_info < (3, 6): 

71 tzpaths = tuple(map(str, tzpaths)) 

72 

73 if not all(map(os.path.isabs, tzpaths)): 

74 raise ValueError(_get_invalid_paths_message(tzpaths)) 

75 base_tzpath = tzpaths 

76 else: 

77 env_var = os.environ.get("PYTHONTZPATH", None) 

78 if env_var is not None: 

79 base_tzpath = _parse_python_tzpath(env_var, stacklevel) 

80 elif sys.platform != "win32": 

81 base_tzpath = ( 

82 "/usr/share/zoneinfo", 

83 "/usr/lib/zoneinfo", 

84 "/usr/share/lib/zoneinfo", 

85 "/etc/zoneinfo", 

86 ) 

87 else: 

88 base_tzpath = () 

89 

90 TZPATH = tuple(base_tzpath) 

91 if TZPATH_CALLBACKS: 

92 for callback in TZPATH_CALLBACKS: 

93 callback(TZPATH) 

94 

95 def reset_tzpath(to=None): 

96 # As in CPython, the warning about PYTHONTZPATH is raised from a 

97 # helper that is used both at import time and by this function, so 

98 # that each can pass the stacklevel that points at its caller. 

99 _reset_tzpath(to) 

100 

101 TZPATH = () 

102 TZPATH_CALLBACKS = [] 

103 

104 _reset_tzpath(stacklevel=5)