Ejemplo n.º 1
0
 def _getargs(self, node, args):
     self.match_args(node, args)
     sig, = self.signatures
     callargs = {
         name: var
         for name, var, _ in sig.signature.iter_args(args)
     }
     # typing.NamedTuple doesn't support rename or verbose
     name_var = callargs["typename"]
     fields_var = callargs["fields"]
     fields = abstract_utils.get_atomic_python_constant(fields_var)
     # The fields is a list of tuples, so we need to deeply unwrap them.
     fields = [abstract_utils.get_atomic_python_constant(t) for t in fields]
     # We need the actual string for the field names and the AtomicAbstractValue
     # for the field types.
     names = []
     types = []
     for field in fields:
         if (len(field) != 2 or any(not self._is_str_instance(v)
                                    for v in field[0].data)):
             # Note that we don't need to check field[1] because both 'str'
             # (forward reference) and 'type' are valid for it.
             sig, = self.signatures
             bad_param = function.BadParam(name="fields",
                                           expected=self._fields_type)
             raise function.WrongArgTypes(sig.signature, args, self.vm,
                                          bad_param)
         name, typ = field
         names.append(abstract_utils.get_atomic_python_constant(name))
         types.append(abstract_utils.get_atomic_value(typ))
     return name_var, names, types
Ejemplo n.º 2
0
 def call(self, node, _, args):
     result = self.vm.program.NewVariable()
     num_args = len(args.posargs)
     if num_args == 0 and self.vm.PY3:
         # The implicit type argument is available in a freevar named '__class__'.
         cls_var = None
         # If we are in a list comprehension we want the enclosing frame.
         index = -1
         while self.vm.frames[index].f_code.co_name == "<listcomp>":
             index -= 1
         frame = self.vm.frames[index]
         for i, free_var in enumerate(frame.f_code.co_freevars):
             if free_var == abstract.BuildClass.CLOSURE_NAME:
                 cls_var = frame.cells[len(frame.f_code.co_cellvars) + i]
                 break
         if not (cls_var and cls_var.bindings):
             self.vm.errorlog.invalid_super_call(
                 self.vm.frames,
                 message="Missing __class__ closure for super call.",
                 details=
                 "Is 'super' being called from a method defined in a class?"
             )
             return node, self.vm.new_unsolvable(node)
         # The implicit super object argument is the first positional argument to
         # the function calling 'super'.
         self_arg = frame.first_posarg
         if not self_arg:
             self.vm.errorlog.invalid_super_call(
                 self.vm.frames,
                 message="Missing 'self' argument to 'super' call.")
             return node, self.vm.new_unsolvable(node)
         super_objects = self_arg.bindings
     elif 1 <= num_args <= 2:
         cls_var = args.posargs[0]
         super_objects = args.posargs[1].bindings if num_args == 2 else [
             None
         ]
     else:
         raise function.WrongArgCount(self._SIGNATURE, args, self.vm)
     for cls in cls_var.bindings:
         if not isinstance(
                 cls.data,
             (class_mixin.Class, abstract.AMBIGUOUS_OR_EMPTY)):
             bad = function.BadParam(name="cls",
                                     expected=self.vm.convert.type_type)
             raise function.WrongArgTypes(self._SIGNATURE,
                                          args,
                                          self.vm,
                                          bad_param=bad)
         for obj in super_objects:
             if obj:
                 result.AddBinding(
                     SuperInstance(cls.data, obj.data, self.vm), [cls, obj],
                     node)
             else:
                 result.AddBinding(SuperInstance(cls.data, None, self.vm),
                                   [cls], node)
     return node, result
Ejemplo n.º 3
0
 def _getargs(self, node, args):
     self.match_args(node, args)
     sig, = self.signatures
     callargs = {
         name: var
         for name, var, _ in sig.signature.iter_args(args)
     }
     # typing.NamedTuple doesn't support rename or verbose
     name_var = callargs["typename"]
     fields_var = callargs["fields"]
     fields = abstract_utils.get_atomic_python_constant(fields_var)
     if isinstance(fields, six.string_types):
         # Since str matches Sequence, we have to manually check for it.
         raise function.WrongArgTypes(sig.signature, args, self.vm,
                                      self._fields_param)
     # The fields is a list of tuples, so we need to deeply unwrap them.
     fields = [abstract_utils.get_atomic_python_constant(t) for t in fields]
     # We need the actual string for the field names and the AtomicAbstractValue
     # for the field types.
     names = []
     types = []
     for field in fields:
         if isinstance(field, six.string_types):
             # Since str matches Sequence, we have to manually check for it.
             raise function.WrongArgTypes(sig.signature, args, self.vm,
                                          self._fields_param)
         if (len(field) != 2 or any(not self._is_str_instance(v)
                                    for v in field[0].data)):
             # Note that we don't need to check field[1] because both 'str'
             # (forward reference) and 'type' are valid for it.
             raise function.WrongArgTypes(sig.signature, args, self.vm,
                                          self._fields_param)
         name, typ = field
         name_py_constant = abstract_utils.get_atomic_python_constant(name)
         if name_py_constant.__class__ is compat.UnicodeType:
             # Unicode values should be ASCII.
             name_py_constant = compat.native_str(
                 name_py_constant.encode("ascii"))
         names.append(name_py_constant)
         types.append(abstract_utils.get_atomic_value(typ))
     return name_var, names, types