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

17 statements  

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

1# encoding: utf-8 

2""" 

3Utilities for working with stack frames. 

4""" 

5 

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

7# Copyright (C) 2008-2011 The IPython Development Team 

8# 

9# Distributed under the terms of the BSD License. The full license is in 

10# the file COPYING, distributed as part of this software. 

11#----------------------------------------------------------------------------- 

12 

13#----------------------------------------------------------------------------- 

14# Imports 

15#----------------------------------------------------------------------------- 

16 

17import sys 

18 

19#----------------------------------------------------------------------------- 

20# Code 

21#----------------------------------------------------------------------------- 

22 

23def extract_vars(*names,**kw): 

24 """Extract a set of variables by name from another frame. 

25 

26 Parameters 

27 ---------- 

28 *names : str 

29 One or more variable names which will be extracted from the caller's 

30 frame. 

31 **kw : integer, optional 

32 How many frames in the stack to walk when looking for your variables. 

33 The default is 0, which will use the frame where the call was made. 

34 

35 Examples 

36 -------- 

37 :: 

38 

39 In [2]: def func(x): 

40 ...: y = 1 

41 ...: print(sorted(extract_vars('x','y').items())) 

42 ...: 

43 

44 In [3]: func('hello') 

45 [('x', 'hello'), ('y', 1)] 

46 """ 

47 

48 depth = kw.get('depth',0) 

49 

50 callerNS = sys._getframe(depth+1).f_locals 

51 return dict((k,callerNS[k]) for k in names) 

52 

53 

54def extract_vars_above(*names): 

55 """Extract a set of variables by name from another frame. 

56 

57 Similar to extractVars(), but with a specified depth of 1, so that names 

58 are extracted exactly from above the caller. 

59 

60 This is simply a convenience function so that the very common case (for us) 

61 of skipping exactly 1 frame doesn't have to construct a special dict for 

62 keyword passing.""" 

63 

64 callerNS = sys._getframe(2).f_locals 

65 return dict((k,callerNS[k]) for k in names) 

66 

67 

68def debugx(expr,pre_msg=''): 

69 """Print the value of an expression from the caller's frame. 

70 

71 Takes an expression, evaluates it in the caller's frame and prints both 

72 the given expression and the resulting value (as well as a debug mark 

73 indicating the name of the calling function. The input must be of a form 

74 suitable for eval(). 

75 

76 An optional message can be passed, which will be prepended to the printed 

77 expr->value pair.""" 

78 

79 cf = sys._getframe(1) 

80 print('[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr, 

81 eval(expr,cf.f_globals,cf.f_locals))) 

82 

83 

84# deactivate it by uncommenting the following line, which makes it a no-op 

85#def debugx(expr,pre_msg=''): pass 

86 

87def extract_module_locals(depth=0): 

88 """Returns (module, locals) of the function `depth` frames away from the caller""" 

89 f = sys._getframe(depth + 1) 

90 global_ns = f.f_globals 

91 module = sys.modules[global_ns['__name__']] 

92 return (module, f.f_locals)