Use create_simple_object for a lot of use cases

This commit is contained in:
Dave Halter
2017-12-02 01:59:48 +01:00
parent 2aa2005502
commit 3c78aad8b1
6 changed files with 15 additions and 6 deletions

View File

@@ -22,6 +22,15 @@ def builtin_from_name(evaluator, string):
return create(evaluator, bltn_obj)
def create_simple_object(evaluator, obj):
"""
Only allows creations of objects that are easily picklable across Python
versions.
"""
assert isinstance(obj, (int, float, str, bytes, slice, complex, type(Ellipsis)))
return create(evaluator, obj)
def create(evaluator, obj):
return create_from_access(
evaluator, create_access(evaluator, obj)

View File

@@ -124,7 +124,7 @@ class AbstractInstanceContext(Context):
debug.warning('No __getitem__, cannot access the array.')
return NO_CONTEXTS
else:
index_obj = compiled.create(self.evaluator, index)
index_obj = compiled.create_simple_object(self.evaluator, index)
return self.execute_function_slots(names, index_obj)
def py__iter__(self):

View File

@@ -443,7 +443,7 @@ class FakeDict(_FakeArray):
def py__iter__(self):
for key in self._dct:
yield LazyKnownContext(compiled.create(self.evaluator, key))
yield LazyKnownContext(compiled.create_simple_object(self.evaluator, key))
def py__getitem__(self, index):
return self._dct[index].infer()

View File

@@ -140,7 +140,7 @@ class NameFinder(object):
def _check_getattr(self, inst):
"""Checks for both __getattr__ and __getattribute__ methods"""
# str is important, because it shouldn't be `Name`!
name = compiled.create(self._evaluator, self._string_name)
name = compiled.create_simple_object(self._evaluator, self._string_name)
# This is a little bit special. `__getattribute__` is in Python
# executed before `__getattr__`. But: I know no use case, where

View File

@@ -167,7 +167,7 @@ def py__getitem__(context, typ, node):
if isinstance(child, tree.Class))
if type_name not in valid_classnames:
return None
compiled_classname = compiled.create(context.evaluator, type_name)
compiled_classname = compiled.create_simple_object(context.evaluator, type_name)
from jedi.evaluate.context.iterable import FakeSequence
args = FakeSequence(

View File

@@ -174,7 +174,7 @@ def eval_atom(context, atom):
elif isinstance(atom, tree.Literal):
string = parser_utils.safe_literal_eval(atom.value)
return ContextSet(compiled.create(context.evaluator, string))
return ContextSet(compiled.create_simple_object(context.evaluator, string))
else:
c = atom.children
if c[0].type == 'string':
@@ -313,7 +313,7 @@ def eval_factor(context_set, operator):
value = context.py__bool__()
if value is None: # Uncertainty.
return
yield compiled.create(context.evaluator, not value)
yield compiled.create_simple_object(context.evaluator, not value)
else:
yield context