public void __del_derived__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__del__");
   if (impl != null) {
     impl.__get__(this, self_type).__call__();
   }
 }
 @Nullable
 @Override
 public PyType getReturnType(@NotNull TypeEvalContext context, @NotNull TypeEvalContext.Key key) {
   for (PyTypeProvider typeProvider : Extensions.getExtensions(PyTypeProvider.EP_NAME)) {
     final PyType returnType = typeProvider.getReturnType(this, context);
     if (returnType != null) {
       returnType.assertValid(typeProvider.toString());
       return returnType;
     }
   }
   if (context.maySwitchToAST(this)
       && LanguageLevel.forElement(this).isAtLeast(LanguageLevel.PYTHON30)) {
     PyAnnotation anno = getAnnotation();
     if (anno != null) {
       PyClass pyClass = anno.resolveToClass();
       if (pyClass != null) {
         return new PyClassTypeImpl(pyClass, false);
       }
     }
   }
   final PyType docStringType = getReturnTypeFromDocString();
   if (docStringType != null) {
     docStringType.assertValid("from docstring");
     return docStringType;
   }
   if (context.allowReturnTypes(this)) {
     final Ref<? extends PyType> yieldTypeRef = getYieldStatementType(context);
     if (yieldTypeRef != null) {
       return yieldTypeRef.get();
     }
     return getReturnStatementType(context);
   }
   return null;
 }
 @Nullable
 private PyType getReturnType(@NotNull TypeEvalContext context) {
   for (PyTypeProvider typeProvider : Extensions.getExtensions(PyTypeProvider.EP_NAME)) {
     final Ref<PyType> returnTypeRef = typeProvider.getReturnType(this, context);
     if (returnTypeRef != null) {
       final PyType returnType = returnTypeRef.get();
       if (returnType != null) {
         returnType.assertValid(typeProvider.toString());
       }
       return returnType;
     }
   }
   final PyType docStringType = getReturnTypeFromDocString();
   if (docStringType != null) {
     docStringType.assertValid("from docstring");
     return docStringType;
   }
   if (context.allowReturnTypes(this)) {
     final Ref<? extends PyType> yieldTypeRef = getYieldStatementType(context);
     if (yieldTypeRef != null) {
       return yieldTypeRef.get();
     }
     return getReturnStatementType(context);
   }
   return null;
 }
 @Nullable
 private PyType getGenericReturnType(
     @NotNull TypeEvalContext typeEvalContext, @Nullable PyQualifiedExpression callSite) {
   if (typeEvalContext.maySwitchToAST(this)
       && LanguageLevel.forElement(this).isAtLeast(LanguageLevel.PYTHON30)) {
     PyAnnotation anno = getAnnotation();
     if (anno != null) {
       PyClass pyClass = anno.resolveToClass();
       if (pyClass != null) {
         return new PyClassTypeImpl(pyClass, false);
       }
     }
   }
   for (PyTypeProvider typeProvider : Extensions.getExtensions(PyTypeProvider.EP_NAME)) {
     final PyType returnType = typeProvider.getReturnType(this, callSite, typeEvalContext);
     if (returnType != null) {
       returnType.assertValid(typeProvider.toString());
       return returnType;
     }
   }
   final PyType docStringType = getReturnTypeFromDocString();
   if (docStringType != null) {
     docStringType.assertValid("from docstring");
     return docStringType;
   }
   if (typeEvalContext.allowReturnTypes(this)) {
     final PyType yieldType = getYieldStatementType(typeEvalContext);
     if (yieldType != null) {
       return yieldType;
     }
     return getReturnStatementType(typeEvalContext);
   }
   return null;
 }
  /**
   * Adds type and description representation from function docstring
   *
   * @param parameter parameter of a function
   * @return true if type from docstring was added
   */
  private boolean addTypeAndDescriptionFromDocstring(@NotNull final PyNamedParameter parameter) {
    final PyFunction function = PsiTreeUtil.getParentOfType(parameter, PyFunction.class);
    if (function != null) {
      final String docString = PyPsiUtils.strValue(function.getDocStringExpression());
      final Pair<String, String> typeAndDescr = getTypeAndDescription(docString, parameter);

      final String type = typeAndDescr.first;
      final String description = typeAndDescr.second;

      if (type != null) {
        final PyType pyType = PyTypeParser.getTypeByName(parameter, type);
        if (pyType instanceof PyClassType) {
          myBody
              .addItem(": ")
              .addWith(
                  new LinkWrapper(PythonDocumentationProvider.LINK_TYPE_PARAM),
                  $(pyType.getName()));
        } else {
          myBody.addItem(": ").addItem(type);
        }
      }

      if (description != null) {
        myEpilog.addItem(BR).addItem(description);
      }

      return type != null;
    }

    return false;
  }
Example #6
0
 public PyObject __iter__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__iter__");
   if (impl != null) return impl.__get__(this, self_type).__call__();
   impl = self_type.lookup("__getitem__");
   if (impl == null) return super.__iter__();
   return new PySequenceIter(this);
 }
 public PyObject __call__(PyObject args[], String keywords[]) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__call__");
   if (impl != null) {
     return impl.__get__(this, self_type).__call__(args, keywords);
   }
   return super.__call__(args, keywords);
 }
 public combinationsWithReplacementDerived(PyType subtype) {
   super(subtype);
   slots = new PyObject[subtype.getNumSlots()];
   dict = subtype.instDict();
   if (subtype.needsFinalizer()) {
     FinalizeTrigger.ensureFinalizer(this);
   }
 }
Example #9
0
 public void __delitem__(PyObject key) { // ???
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__delitem__");
   if (impl != null) {
     impl.__get__(this, self_type).__call__(key);
     return;
   }
   super.__delitem__(key);
 }
Example #10
0
 public void __delattr__(String name) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__delattr__");
   if (impl != null) {
     impl.__get__(this, self_type).__call__(PyString.fromInterned(name));
     return;
   }
   super.__delattr__(name);
 }
 @Nullable
 private static PyType derefType(
     @NotNull Ref<PyType> typeRef, @NotNull PyTypeProvider typeProvider) {
   final PyType type = typeRef.get();
   if (type != null) {
     type.assertValid(typeProvider.toString());
   }
   return type;
 }
Example #12
0
 public void __delete__(PyObject obj) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__delete__");
   if (impl != null) {
     impl.__get__(this, self_type).__call__(obj);
     return;
   }
   super.__delete__(obj);
 }
 public int __len__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__len__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     return res.asInt();
   }
   return super.__len__();
 }
Example #14
0
 private static PyObject buildClass(
     PyObject dict, String classname, String superclass, PyObject classDict, String doc) {
   classDict.__setitem__("__doc__", Py.newString(doc));
   PyType type =
       (PyType) Py.makeClass("exceptions." + classname, dict.__finditem__(superclass), classDict);
   type.builtin = true;
   dict.__setitem__(classname, type);
   return type;
 }
Example #15
0
 public PyObject __ixor__(PyObject other) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__ixor__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__(other);
     if (res == Py.NotImplemented) return null;
     return res;
   }
   return super.__ixor__(other);
 }
Example #16
0
 public PyObject __int__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__int__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     if (res instanceof PyInteger || res instanceof PyLong) return res;
     throw Py.TypeError("__int__" + " should return an integer");
   }
   return super.__int__();
 }
Example #17
0
 public int __len__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__len__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     if (res instanceof PyInteger) return ((PyInteger) res).getValue();
     throw Py.TypeError("__len__ should return a int");
   }
   return super.__len__();
 }
Example #18
0
 public PyObject __get__(PyObject obj, PyObject type) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__get__");
   if (impl != null) {
     if (obj == null) obj = Py.None;
     if (type == null) type = Py.None;
     return impl.__get__(this, self_type).__call__(obj, type);
   }
   return super.__get__(obj, type);
 }
 public void __setattr__(String name, PyObject value) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__setattr__");
   if (impl != null) {
     impl.__get__(this, self_type).__call__(PyString.fromInterned(name), value);
     // CPython does not support instance-acquired finalizers.
     // So we don't check for __del__ here.
     return;
   }
   super.__setattr__(name, value);
 }
Example #20
0
 public PyFloat __float__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__float__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     if (res instanceof PyFloat) return (PyFloat) res;
     throw Py.TypeError(
         "__float__" + " returned non-" + "float" + " (type " + res.getType().fastGetName() + ")");
   }
   return super.__float__();
 }
Example #21
0
 public Object __coerce_ex__(PyObject o) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__coerce__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__(o);
     if (res == Py.NotImplemented) return Py.None;
     if (!(res instanceof PyTuple)) throw Py.TypeError("__coerce__ didn't return a 2-tuple");
     return ((PyTuple) res).getArray();
   }
   return super.__coerce_ex__(o);
 }
Example #22
0
 public PyUnicode __unicode__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__unicode__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     if (res instanceof PyUnicode) return (PyUnicode) res;
     if (res instanceof PyString) return new PyUnicode((PyString) res);
     throw Py.TypeError("__unicode__" + " should return a " + "unicode");
   }
   return super.__unicode__();
 }
Example #23
0
 public PyObject __getslice__(PyObject start, PyObject stop, PyObject step) { // ???
   if (step != null) {
     return __getitem__(new PySlice(start, stop, step));
   }
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__getslice__");
   if (impl != null) {
     PyObject[] indices = PySlice.indices2(this, start, stop);
     return impl.__get__(this, self_type).__call__(indices[0], indices[1]);
   }
   return super.__getslice__(start, stop, step);
 }
Example #24
0
 public PyObject __finditem__(int key) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__getitem__");
   if (impl != null)
     try {
       return impl.__get__(this, self_type).__call__(new PyInteger(key));
     } catch (PyException exc) {
       if (exc.match(Py.LookupError)) return null;
       throw exc;
     }
   return super.__finditem__(key);
 }
Example #25
0
 public String toString() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__repr__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     if (!(res instanceof PyString))
       throw Py.TypeError(
           "__repr__ returned non-string (type " + res.getType().fastGetName() + ")");
     return ((PyString) res).toString();
   }
   return super.toString();
 }
Example #26
0
 public PyObject __call__(PyObject args[], String keywords[]) {
   ThreadState ts = Py.getThreadState();
   if (ts.recursion_depth++ > ts.systemState.getrecursionlimit())
     throw Py.RuntimeError("maximum __call__ recursion depth exceeded");
   try {
     PyType self_type = getType();
     PyObject impl = self_type.lookup("__call__");
     if (impl != null) return impl.__get__(this, self_type).__call__(args, keywords);
     return super.__call__(args, keywords);
   } finally {
     --ts.recursion_depth;
   }
 }
Example #27
0
 public PyObject __iternext__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("next");
   if (impl != null) {
     try {
       return impl.__get__(this, self_type).__call__();
     } catch (PyException exc) {
       if (exc.match(Py.StopIteration)) return null;
       throw exc;
     }
   }
   return super.__iternext__(); // ???
 }
Example #28
0
 public PyObject __index__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__index__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     if (res instanceof PyInteger || res instanceof PyLong) {
       return res;
     }
     throw Py.TypeError(
         String.format(
             "__index__ returned non-(int,long) (type %s)", res.getType().fastGetName()));
   }
   return super.__index__();
 }
Example #29
0
 public boolean __nonzero__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__nonzero__");
   if (impl == null) {
     impl = self_type.lookup("__len__");
     if (impl == null) return super.__nonzero__();
   }
   PyObject o = impl.__get__(this, self_type).__call__();
   if (o.getClass() != PyInteger.class && o.getClass() != PyBoolean.class) {
     throw Py.TypeError(
         String.format("__nonzero__ should return bool or int, returned %s", self_type.getName()));
   }
   return o.__nonzero__();
 }
Example #30
0
 public void __setslice__(PyObject start, PyObject stop, PyObject step, PyObject value) {
   if (step != null) {
     __setitem__(new PySlice(start, stop, step), value);
     return;
   }
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__setslice__");
   if (impl != null) {
     PyObject[] indices = PySlice.indices2(this, start, stop);
     impl.__get__(this, self_type).__call__(indices[0], indices[1], value);
     return;
   }
   super.__setslice__(start, stop, step, value);
 }