|
16 | 16 | # under the License. |
17 | 17 |
|
18 | 18 | import asyncio |
| 19 | +import inspect |
19 | 20 | import sys |
| 21 | +from pathlib import Path |
20 | 22 | from urllib.parse import quote as _quote |
21 | 23 | from urllib.parse import urlencode, urlparse |
22 | 24 |
|
@@ -67,6 +69,50 @@ def __enter__(self) -> None: |
67 | 69 | def __exit__(self, *_) -> None: |
68 | 70 | pass |
69 | 71 |
|
| 72 | + def acquire(self, blocking: bool = True) -> bool: |
| 73 | + return True |
| 74 | + |
| 75 | + def release(self) -> None: |
| 76 | + pass |
| 77 | + |
| 78 | + |
| 79 | +def warn_stacklevel() -> int: |
| 80 | + """Dynamically determine warning stacklevel for warnings based on the call stack""" |
| 81 | + try: |
| 82 | + # Grab the root module from the current module '__name__' |
| 83 | + module_name = __name__.partition(".")[0] |
| 84 | + module_path = Path(sys.modules[module_name].__file__) |
| 85 | + |
| 86 | + # If the module is a folder we're looking at |
| 87 | + # subdirectories, otherwise we're looking for |
| 88 | + # an exact match. |
| 89 | + module_is_folder = module_path.name == "__init__.py" |
| 90 | + if module_is_folder: |
| 91 | + module_path = module_path.parent |
| 92 | + |
| 93 | + # Look through frames until we find a file that |
| 94 | + # isn't a part of our module, then return that stacklevel. |
| 95 | + for level, frame in enumerate(inspect.stack()): |
| 96 | + # Garbage collecting frames |
| 97 | + frame_filename = Path(frame.filename) |
| 98 | + del frame |
| 99 | + |
| 100 | + if ( |
| 101 | + # If the module is a folder we look at subdirectory |
| 102 | + module_is_folder |
| 103 | + and module_path not in frame_filename.parents |
| 104 | + ) or ( |
| 105 | + # Otherwise we're looking for an exact match. |
| 106 | + not module_is_folder |
| 107 | + and module_path != frame_filename |
| 108 | + ): |
| 109 | + return level |
| 110 | + except KeyError: |
| 111 | + pass |
| 112 | + except Exception: |
| 113 | + return 2 |
| 114 | + return 0 |
| 115 | + |
70 | 116 |
|
71 | 117 | __all__ = [ |
72 | 118 | "get_running_loop", |
|
0 commit comments