FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
vscode-python/pythonFiles/jedi/evaluate/stdlib.py at master · DaddyCal/vscode-python · GitHub
DaddyCal
/
vscode-python
Public
forked from
microsoft/vscode-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
vscode-python
/
pythonFiles
/
jedi
/
evaluate
/
stdlib.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
343 lines (292 loc) · 11.7 KB
Breadcrumbs
vscode-python
/
pythonFiles
/
jedi
/
evaluate
/
stdlib.py
Copy path
File metadata and controls
343 lines (292 loc) · 11.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""
Implementations of standard library functions, because it's not possible to
understand them with Jedi.
To add a new implementation, create a function and add it to the
``_implemented`` dict at the bottom of this module.
Note that this module exists only to implement very specific functionality in
the standard library. The usual way to understand the standard library is the
compiled module that returns the types for C-builtins.
"""
import
re
import
parso
from
jedi
.
_compatibility
import
force_unicode
from
jedi
import
debug
from
jedi
.
evaluate
.
arguments
import
ValuesArguments
from
jedi
.
evaluate
import
analysis
from
jedi
.
evaluate
import
compiled
from
jedi
.
evaluate
.
context
.
instance
import
InstanceFunctionExecution
, \
AbstractInstanceContext
,
CompiledInstance
,
BoundMethod
, \
AnonymousInstanceFunctionExecution
from
jedi
.
evaluate
.
base_context
import
ContextualizedNode
, \
NO_CONTEXTS
,
ContextSet
from
jedi
.
evaluate
.
context
import
ClassContext
,
ModuleContext
from
jedi
.
evaluate
.
context
import
iterable
from
jedi
.
evaluate
.
lazy_context
import
LazyTreeContext
from
jedi
.
evaluate
.
syntax_tree
import
is_string
# Now this is all part of fake tuples in Jedi. However super doesn't work on
# __init__ and __new__ doesn't work at all. So adding this to nametuples is
# just the easiest way.
_NAMEDTUPLE_INIT
=
"""
def __init__(_cls, {arg_list}):
'A helper function for namedtuple.'
self.__iterable = ({arg_list})
def __iter__(self):
for i in self.__iterable:
yield i
def __getitem__(self, y):
return self.__iterable[y]
"""
class
NotInStdLib
(
LookupError
):
pass
def
execute
(
evaluator
,
obj
,
arguments
):
if
isinstance
(
obj
,
BoundMethod
):
raise
NotInStdLib
()
try
:
obj_name
=
obj
.
name
.
string_name
except
AttributeError
:
pass
else
:
if
obj
.
parent_context
==
evaluator
.
builtins_module
:
module_name
=
'builtins'
elif
isinstance
(
obj
.
parent_context
,
ModuleContext
):
module_name
=
obj
.
parent_context
.
name
.
string_name
else
:
module_name
=
''
# for now we just support builtin functions.
try
:
func
=
_implemented
[
module_name
][
obj_name
]
except
KeyError
:
pass
else
:
return
func
(
evaluator
,
obj
,
arguments
)
raise
NotInStdLib
()
def
_follow_param
(
evaluator
,
arguments
,
index
):
try
:
key
,
lazy_context
=
list
(
arguments
.
unpack
())[
index
]
except
IndexError
:
return
NO_CONTEXTS
else
:
return
lazy_context
.
infer
()
def
argument_clinic
(
string
,
want_obj
=
False
,
want_context
=
False
,
want_arguments
=
False
):
"""
Works like Argument Clinic (PEP 436), to validate function params.
"""
clinic_args
=
[]
allow_kwargs
=
False
optional
=
False
while
string
:
# Optional arguments have to begin with a bracket. And should always be
# at the end of the arguments. This is therefore not a proper argument
# clinic implementation. `range()` for exmple allows an optional start
# value at the beginning.
match
=
re
.
match
(
'(?:(?:(\[),? ?|, ?|)(\w+)|, ?/)\]*'
,
string
)
string
=
string
[
len
(
match
.
group
(
0
)):]
if
not
match
.
group
(
2
):
# A slash -> allow named arguments
allow_kwargs
=
True
continue
optional
=
optional
or
bool
(
match
.
group
(
1
))
word
=
match
.
group
(
2
)
clinic_args
.
append
((
word
,
optional
,
allow_kwargs
))
def
f
(
func
):
def
wrapper
(
evaluator
,
obj
,
arguments
):
debug
.
dbg
(
'builtin start %s'
%
obj
,
color
=
'MAGENTA'
)
result
=
NO_CONTEXTS
try
:
lst
=
list
(
arguments
.
eval_argument_clinic
(
clinic_args
))
except
ValueError
:
pass
else
:
kwargs
=
{}
if
want_context
:
kwargs
[
'context'
]
=
arguments
.
context
if
want_obj
:
kwargs
[
'obj'
]
=
obj
if
want_arguments
:
kwargs
[
'arguments'
]
=
arguments
result
=
func
(
evaluator
,
*
lst
,
**
kwargs
)
finally
:
debug
.
dbg
(
'builtin end: %s'
,
result
,
color
=
'MAGENTA'
)
return
result
return
wrapper
return
f
@
argument_clinic
(
'iterator[, default], /'
)
def
builtins_next
(
evaluator
,
iterators
,
defaults
):
"""
TODO this function is currently not used. It's a stab at implementing next
in a different way than fake objects. This would be a bit more flexible.
"""
if
evaluator
.
environment
.
version_info
.
major
==
2
:
name
=
'next'
else
:
name
=
'__next__'
context_set
=
NO_CONTEXTS
for
iterator
in
iterators
:
if
isinstance
(
iterator
,
AbstractInstanceContext
):
context_set
=
ContextSet
.
from_sets
(
n
.
infer
()
for
filter
in
iterator
.
get_filters
(
include_self_names
=
True
)
for
n
in
filter
.
get
(
name
)
).
execute_evaluated
()
if
context_set
:
return
context_set
return
defaults
@
argument_clinic
(
'object, name[, default], /'
)
def
builtins_getattr
(
evaluator
,
objects
,
names
,
defaults
=
None
):
# follow the first param
for
obj
in
objects
:
for
name
in
names
:
if
is_string
(
name
):
return
obj
.
py__getattribute__
(
force_unicode
(
name
.
get_safe_value
()))
else
:
debug
.
warning
(
'getattr called without str'
)
continue
return
NO_CONTEXTS
@
argument_clinic
(
'object[, bases, dict], /'
)
def
builtins_type
(
evaluator
,
objects
,
bases
,
dicts
):
if
bases
or
dicts
:
# It's a type creation... maybe someday...
return
NO_CONTEXTS
else
:
return
objects
.
py__class__
()
class
SuperInstance
(
AbstractInstanceContext
):
"""To be used like the object ``super`` returns."""
def
__init__
(
self
,
evaluator
,
cls
):
su
=
cls
.
py_mro
()[
1
]
super
().
__init__
(
evaluator
,
su
and
su
[
0
]
or
self
)
@
argument_clinic
(
'[type[, obj]], /'
,
want_context
=
True
)
def
builtins_super
(
evaluator
,
types
,
objects
,
context
):
# TODO make this able to detect multiple inheritance super
if
isinstance
(
context
, (
InstanceFunctionExecution
,
AnonymousInstanceFunctionExecution
)):
su
=
context
.
instance
.
py__class__
().
py__bases__
()
return
su
[
0
].
infer
().
execute_evaluated
()
return
NO_CONTEXTS
@
argument_clinic
(
'sequence, /'
,
want_obj
=
True
,
want_arguments
=
True
)
def
builtins_reversed
(
evaluator
,
sequences
,
obj
,
arguments
):
# While we could do without this variable (just by using sequences), we
# want static analysis to work well. Therefore we need to generated the
# values again.
key
,
lazy_context
=
next
(
arguments
.
unpack
())
cn
=
None
if
isinstance
(
lazy_context
,
LazyTreeContext
):
# TODO access private
cn
=
ContextualizedNode
(
lazy_context
.
_context
,
lazy_context
.
data
)
ordered
=
list
(
sequences
.
iterate
(
cn
))
rev
=
list
(
reversed
(
ordered
))
# Repack iterator values and then run it the normal way. This is
# necessary, because `reversed` is a function and autocompletion
# would fail in certain cases like `reversed(x).__iter__` if we
# just returned the result directly.
seq
=
iterable
.
FakeSequence
(
evaluator
,
u'list'
,
rev
)
arguments
=
ValuesArguments
([
ContextSet
(
seq
)])
return
ContextSet
(
CompiledInstance
(
evaluator
,
evaluator
.
builtins_module
,
obj
,
arguments
))
@
argument_clinic
(
'obj, type, /'
,
want_arguments
=
True
)
def
builtins_isinstance
(
evaluator
,
objects
,
types
,
arguments
):
bool_results
=
set
()
for
o
in
objects
:
cls
=
o
.
py__class__
()
try
:
mro_func
=
cls
.
py__mro__
except
AttributeError
:
# This is temporary. Everything should have a class attribute in
# Python?! Maybe we'll leave it here, because some numpy objects or
# whatever might not.
bool_results
=
set
([
True
,
False
])
break
mro
=
mro_func
()
for
cls_or_tup
in
types
:
if
cls_or_tup
.
is_class
():
bool_results
.
add
(
cls_or_tup
in
mro
)
elif
cls_or_tup
.
name
.
string_name
==
'tuple'
\
and
cls_or_tup
.
get_root_context
()
==
evaluator
.
builtins_module
:
# Check for tuples.
classes
=
ContextSet
.
from_sets
(
lazy_context
.
infer
()
for
lazy_context
in
cls_or_tup
.
iterate
()
)
bool_results
.
add
(
any
(
cls
in
mro
for
cls
in
classes
))
else
:
_
,
lazy_context
=
list
(
arguments
.
unpack
())[
1
]
if
isinstance
(
lazy_context
,
LazyTreeContext
):
node
=
lazy_context
.
data
message
=
'TypeError: isinstance() arg 2 must be a '
\
'class, type, or tuple of classes and types, '
\
'not %s.'
%
cls_or_tup
analysis
.
add
(
lazy_context
.
_context
,
'type-error-isinstance'
,
node
,
message
)
return
ContextSet
.
from_iterable
(
compiled
.
builtin_from_name
(
evaluator
,
force_unicode
(
str
(
b
)))
for
b
in
bool_results
)
def
collections_namedtuple
(
evaluator
,
obj
,
arguments
):
"""
Implementation of the namedtuple function.
This has to be done by processing the namedtuple class template and
evaluating the result.
"""
collections_context
=
obj
.
parent_context
_class_template_set
=
collections_context
.
py__getattribute__
(
u'_class_template'
)
if
not
_class_template_set
:
# Namedtuples are not supported on Python 2.6, early 2.7, because the
# _class_template variable is not defined, there.
return
NO_CONTEXTS
# Process arguments
# TODO here we only use one of the types, we should use all.
# TODO this is buggy, doesn't need to be a string
name
=
list
(
_follow_param
(
evaluator
,
arguments
,
0
))[
0
].
get_safe_value
()
_fields
=
list
(
_follow_param
(
evaluator
,
arguments
,
1
))[
0
]
if
isinstance
(
_fields
,
compiled
.
CompiledObject
):
fields
=
_fields
.
get_safe_value
().
replace
(
','
,
' '
).
split
()
elif
isinstance
(
_fields
,
iterable
.
Sequence
):
fields
=
[
v
.
get_safe_value
()
for
lazy_context
in
_fields
.
py__iter__
()
for
v
in
lazy_context
.
infer
()
if
is_string
(
v
)
]
else
:
return
NO_CONTEXTS
def
get_var
(
name
):
x
,
=
collections_context
.
py__getattribute__
(
name
)
return
x
.
get_safe_value
()
base
=
next
(
iter
(
_class_template_set
)).
get_safe_value
()
base
+=
_NAMEDTUPLE_INIT
# Build source code
code
=
base
.
format
(
typename
=
name
,
field_names
=
tuple
(
fields
),
num_fields
=
len
(
fields
),
arg_list
=
repr
(
tuple
(
fields
)).
replace
(
"u'"
,
""
).
replace
(
"'"
,
""
)[
1
:
-
1
],
repr_fmt
=
', '
.
join
(
get_var
(
u'_repr_template'
).
format
(
name
=
name
)
for
name
in
fields
),
field_defs
=
'
\n
'
.
join
(
get_var
(
u'_field_template'
).
format
(
index
=
index
,
name
=
name
)
for
index
,
name
in
enumerate
(
fields
))
)
# Parse source code
module
=
evaluator
.
grammar
.
parse
(
code
)
generated_class
=
next
(
module
.
iter_classdefs
())
parent_context
=
ModuleContext
(
evaluator
,
module
,
None
,
code_lines
=
parso
.
split_lines
(
code
,
keepends
=
True
),
)
return
ContextSet
(
ClassContext
(
evaluator
,
parent_context
,
generated_class
))
@
argument_clinic
(
'first, /'
)
def
_return_first_param
(
evaluator
,
firsts
):
return
firsts
_implemented
=
{
'builtins'
: {
'getattr'
:
builtins_getattr
,
'type'
:
builtins_type
,
'super'
:
builtins_super
,
'reversed'
:
builtins_reversed
,
'isinstance'
:
builtins_isinstance
,
},
'copy'
: {
'copy'
:
_return_first_param
,
'deepcopy'
:
_return_first_param
,
},
'json'
: {
'load'
:
lambda
*
args
:
NO_CONTEXTS
,
'loads'
:
lambda
*
args
:
NO_CONTEXTS
,
},
'collections'
: {
'namedtuple'
:
collections_namedtuple
,
},
}
Back
|
FazBrowse Home
|
New Git URL