Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(20)

Side by Side Diff: Lib/inspect.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/importlib/test/source/test_file_loader.py ('k') | Lib/lib2to3/fixer_base.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Get useful information from live Python objects. 1 """Get useful information from live Python objects.
2 2
3 This module encapsulates the interface provided by the internal special 3 This module encapsulates the interface provided by the internal special
4 attributes (co_*, im_*, tb_*, etc.) in a friendlier fashion. 4 attributes (co_*, im_*, tb_*, etc.) in a friendlier fashion.
5 It also provides some help for examining source code and class layout. 5 It also provides some help for examining source code and class layout.
6 6
7 Here are some of the useful functions provided by this module: 7 Here are some of the useful functions provided by this module:
8 8
9 ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(), 9 ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(),
10 isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(), 10 isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(),
(...skipping 15 matching lines...) Expand all
26 26
27 # This module is in the public domain. No warranties. 27 # This module is in the public domain. No warranties.
28 28
29 __author__ = 'Ka-Ping Yee <ping@lfw.org>' 29 __author__ = 'Ka-Ping Yee <ping@lfw.org>'
30 __date__ = '1 Jan 2001' 30 __date__ = '1 Jan 2001'
31 31
32 import sys 32 import sys
33 import os 33 import os
34 import types 34 import types
35 import itertools 35 import itertools
36 import string
36 import re 37 import re
37 import imp 38 import imp
38 import tokenize 39 import tokenize
39 import linecache 40 import linecache
40 from operator import attrgetter 41 from operator import attrgetter
41 from collections import namedtuple 42 from collections import namedtuple
42 43
43 # Create constants for the compiler flags in Include/code.h 44 # Create constants for the compiler flags in Include/code.h
44 # We try to get them from dis to avoid duplication, but fall 45 # We try to get them from dis to avoid duplication, but fall
45 # back to hardcording so the dependency is optional 46 # back to hardcording so the dependency is optional
(...skipping 864 matching lines...) Expand 10 before | Expand all | Expand 10 after
910 return formatarg(name) + formatvalue(locals[name]) 911 return formatarg(name) + formatvalue(locals[name])
911 specs = [] 912 specs = []
912 for i in range(len(args)): 913 for i in range(len(args)):
913 specs.append(convert(args[i])) 914 specs.append(convert(args[i]))
914 if varargs: 915 if varargs:
915 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs])) 916 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
916 if varkw: 917 if varkw:
917 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw])) 918 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
918 return '(' + ', '.join(specs) + ')' 919 return '(' + ', '.join(specs) + ')'
919 920
920 def _missing_arguments(f_name, argnames, pos, values): 921 def _positional_error(f_name, args, kwonly, varargs, defcount, given, values):
921 names = [repr(name) for name in argnames if name not in values]
922 missing = len(names)
923 if missing == 1:
924 s = names[0]
925 elif missing == 2:
926 s = "{} and {}".format(*names)
927 else:
928 tail = ", {} and {}".format(names[-2:])
929 del names[-2:]
930 s = ", ".join(names) + tail
931 raise TypeError("%s() missing %i required %s argument%s: %s" %
932 (f_name, missing,
933 "positional" if pos else "keyword-only",
934 "" if missing == 1 else "s", s))
935
936 def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
937 atleast = len(args) - defcount 922 atleast = len(args) - defcount
923 if given is None:
924 given = len([arg for arg in args if arg in values])
938 kwonly_given = len([arg for arg in kwonly if arg in values]) 925 kwonly_given = len([arg for arg in kwonly if arg in values])
939 if varargs: 926 if varargs:
940 plural = atleast != 1 927 plural = atleast != 1
941 sig = "at least %d" % (atleast,) 928 sig = "at least %d" % (atleast,)
942 elif defcount: 929 elif defcount:
943 plural = True 930 plural = True
944 sig = "from %d to %d" % (atleast, len(args)) 931 sig = "from %d to %d" % (atleast, len(args))
945 else: 932 else:
946 plural = len(args) != 1 933 plural = len(args) != 1
947 sig = str(len(args)) 934 sig = str(len(args))
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
986 if not varkw: 973 if not varkw:
987 raise TypeError("%s() got an unexpected keyword argument %r" % 974 raise TypeError("%s() got an unexpected keyword argument %r" %
988 (f_name, kw)) 975 (f_name, kw))
989 arg2value[varkw][kw] = value 976 arg2value[varkw][kw] = value
990 continue 977 continue
991 if kw in arg2value: 978 if kw in arg2value:
992 raise TypeError("%s() got multiple values for argument %r" % 979 raise TypeError("%s() got multiple values for argument %r" %
993 (f_name, kw)) 980 (f_name, kw))
994 arg2value[kw] = value 981 arg2value[kw] = value
995 if num_pos > num_args and not varargs: 982 if num_pos > num_args and not varargs:
996 _too_many(f_name, args, kwonlyargs, varargs, num_defaults, 983 _positional_error(f_name, args, kwonlyargs, varargs, num_defaults,
997 num_pos, arg2value) 984 num_pos, arg2value)
998 if num_pos < num_args: 985 if num_pos < num_args:
999 req = args[:num_args - num_defaults] 986 for arg in args[:num_args - num_defaults]:
1000 for arg in req:
1001 if arg not in arg2value: 987 if arg not in arg2value:
1002 _missing_arguments(f_name, req, True, arg2value) 988 _positional_error(f_name, args, kwonlyargs, varargs,
989 num_defaults, None, arg2value)
1003 for i, arg in enumerate(args[num_args - num_defaults:]): 990 for i, arg in enumerate(args[num_args - num_defaults:]):
1004 if arg not in arg2value: 991 if arg not in arg2value:
1005 arg2value[arg] = defaults[i] 992 arg2value[arg] = defaults[i]
1006 missing = 0
1007 for kwarg in kwonlyargs: 993 for kwarg in kwonlyargs:
1008 if kwarg not in arg2value: 994 if kwarg not in arg2value:
1009 if kwarg in kwonlydefaults: 995 if kwarg not in kwonlydefaults:
1010 arg2value[kwarg] = kwonlydefaults[kwarg] 996 raise TypeError("%s() requires keyword-only argument %r" %
1011 else: 997 (f_name, kwarg))
1012 missing += 1 998 arg2value[kwarg] = kwonlydefaults[kwarg]
1013 if missing:
1014 _missing_arguments(f_name, kwonlyargs, False, arg2value)
1015 return arg2value 999 return arg2value
1016 1000
1017 # -------------------------------------------------- stack frame extraction 1001 # -------------------------------------------------- stack frame extraction
1018 1002
1019 Traceback = namedtuple('Traceback', 'filename lineno function code_context index ') 1003 Traceback = namedtuple('Traceback', 'filename lineno function code_context index ')
1020 1004
1021 def getframeinfo(frame, context=1): 1005 def getframeinfo(frame, context=1):
1022 """Get information about a frame or traceback object. 1006 """Get information about a frame or traceback object.
1023 1007
1024 A tuple of five things is returned: the filename, the line number of 1008 A tuple of five things is returned: the filename, the line number of
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
1194 GEN_SUSPENDED: Currently suspended at a yield expression. 1178 GEN_SUSPENDED: Currently suspended at a yield expression.
1195 GEN_CLOSED: Execution has completed. 1179 GEN_CLOSED: Execution has completed.
1196 """ 1180 """
1197 if generator.gi_running: 1181 if generator.gi_running:
1198 return GEN_RUNNING 1182 return GEN_RUNNING
1199 if generator.gi_frame is None: 1183 if generator.gi_frame is None:
1200 return GEN_CLOSED 1184 return GEN_CLOSED
1201 if generator.gi_frame.f_lasti == -1: 1185 if generator.gi_frame.f_lasti == -1:
1202 return GEN_CREATED 1186 return GEN_CREATED
1203 return GEN_SUSPENDED 1187 return GEN_SUSPENDED
OLDNEW
« no previous file with comments | « Lib/importlib/test/source/test_file_loader.py ('k') | Lib/lib2to3/fixer_base.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+