1
1
"""Various utility functions."""
2
2
3
- import inspect
3
+ from sys import _getframe
4
+ from inspect import getframeinfo
4
5
5
6
import six
6
7
7
8
CONFIG_STACK = []
8
9
10
+ if six .PY2 :
11
+ from inspect import getargspec as _getargspec
9
12
10
- def get_args (func ):
11
- """Get a list of argument names for a function.
13
+ def get_args (func ):
14
+ """Get a list of argument names for a function.
12
15
13
- This is a wrapper around inspect.getargspec/inspect.signature because
14
- getargspec got deprecated in Python 3.5 and signature isn't available on
15
- Python 2.
16
+ :param func: The function to inspect.
16
17
17
- :param func: The function to inspect.
18
+ :return: A list of argument names.
19
+ :rtype: list
20
+ """
21
+ return _getargspec (func ).args
18
22
19
- :return: A list of argument names.
20
- :rtype: list
21
- """
22
- if six .PY2 :
23
- return inspect .getargspec (func ).args
24
23
25
- params = inspect .signature (func ).parameters .values ()
26
- return [param .name for param in params if param .kind == param .POSITIONAL_OR_KEYWORD ]
24
+ else :
25
+ from inspect import signature as _signature
26
+
27
+ def get_args (func ):
28
+ """Get a list of argument names for a function.
29
+
30
+ :param func: The function to inspect.
31
+
32
+ :return: A list of argument names.
33
+ :rtype: list
34
+ """
35
+ params = _signature (func ).parameters .values ()
36
+ return [param .name for param in params if param .kind == param .POSITIONAL_OR_KEYWORD ]
27
37
28
38
29
39
def get_parametrize_markers_args (node ):
@@ -36,11 +46,19 @@ def get_parametrize_markers_args(node):
36
46
37
47
38
48
def get_caller_module_locals (depth = 2 ):
39
- frame_info = inspect .stack ()[depth ]
40
- frame = frame_info [0 ] # frame_info.frame
41
- return frame .f_locals
49
+ """Get the caller module locals dictionary.
50
+
51
+ We use sys._getframe instead of inspect.stack(0) because the latter is way slower, since it iterates over
52
+ all the frames in the stack.
53
+ """
54
+ return _getframe (depth ).f_locals
42
55
43
56
44
57
def get_caller_module_path (depth = 2 ):
45
- frame_info = inspect .stack ()[depth ]
46
- return frame_info [1 ] # frame_info.filename
58
+ """Get the caller module path.
59
+
60
+ We use sys._getframe instead of inspect.stack(0) because the latter is way slower, since it iterates over
61
+ all the frames in the stack.
62
+ """
63
+ frame = _getframe (depth )
64
+ return getframeinfo (frame , context = 0 ).filename
0 commit comments