/**
   * 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;
  }
Ejemplo n.º 2
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__();
 }
Ejemplo n.º 3
0
 public PyObject __findattr_ex__(String name) {
   PyType self_type = getType();
   // TODO: We should speed this up. As the __getattribute__ slot almost never
   //       changes, it is a good candidate for caching, as PyClass does with
   //       __getattr__. See #1102.
   PyObject getattribute = self_type.lookup("__getattribute__");
   PyString py_name = null;
   PyException firstAttributeError = null;
   try {
     if (getattribute != null) {
       py_name = PyString.fromInterned(name);
       return getattribute.__get__(this, self_type).__call__(py_name);
     } else {
       Py.Warning(String.format("__getattribute__ not found on type %s", self_type.getName()));
       PyObject ret = super.__findattr_ex__(name);
       if (ret != null) {
         return ret;
       } // else: pass through to __getitem__ invocation
     }
   } catch (PyException e) {
     if (!Py.matchException(e, Py.AttributeError)) {
       throw e;
     } else {
       firstAttributeError = e; // saved to avoid swallowing custom AttributeErrors
       // and pass through to __getattr__ invocation.
     }
   }
   PyObject getattr = self_type.lookup("__getattr__");
   if (getattr != null) {
     if (py_name == null) {
       py_name = PyString.fromInterned(name);
     }
     return getattr.__get__(this, self_type).__call__(py_name);
   }
   if (firstAttributeError != null) {
     throw firstAttributeError;
   }
   return null;
 }
Ejemplo n.º 4
0
  public PythonRunner(
      Map<String, String> environment,
      Repositories repositories,
      Scheduler scheduler,
      Hierarchy loggerRepository,
      String pythonPath,
      BufferedWriter out,
      BufferedWriter err)
      throws Exception {

    init();

    this.staticContext = new StaticContext(scheduler, loggerRepository).repository(repositories);
    this.environment = environment;

    final PySystemState systemState = new PySystemState();
    interpreter = new PythonInterpreter(null, systemState);
    for (String path : pythonPath.split(":")) {
      systemState.path.add(new PyString(path));
    }
    interpreter.setOut(out);
    interpreter.setErr(err);
    scriptContext = staticContext.scriptContext();

    // XPM module
    final PyModule xpmModule = new PyModule("xpm", (PyObject) null);
    systemState.modules.__setitem__("xpm", xpmModule);

    // Add classes
    for (PyType type : TYPES.values()) {
      xpmModule.__setattr__(type.getName(), type);
    }

    // Add constants
    Scripting.forEachConstant(
        (name, value) -> {
          xpmModule.__setattr__(name, wrap(value));
        });

    // Add functions
    Scripting.forEachFunction(
        m -> {
          xpmModule.__setattr__(m.getKey(), new PythonMethod(null, m));
        });

    // Add Python specific functions
    for (MethodFunction m : Scripting.getMethodFunctions(PythonFunctions.class)) {
      xpmModule.__setattr__(m.getKey(), new PythonMethod(null, m));
    }

    // XPM object: wrap properties
    final XPM xpm = new XPM();
    ClassDescription xpmDescription = ClassDescription.analyzeClass(XPM.class);
    for (Map.Entry<Object, MethodFunction> x : xpmDescription.getMethods().entrySet()) {
      final Object key = x.getKey();
      if (key instanceof String) {
        xpmModule.__setattr__((String) key, new PythonMethod(xpm, x.getValue()));
      } else {
        throw new XPMRuntimeException("Could not handle key ", key);
      }
    }

    // Add properties
    xpmModule.__setattr__("tasks", wrap(new Tasks()));
    xpmModule.__setattr__("logger", wrap(new ScriptingLogger("xpm")));
    xpmModule.__setattr__("env", wrap(environment));
  }
 @Override
 public String getName() {
   PyType res = excludeNull(TypeEvalContext.codeInsightFallback());
   return res != null ? res.getName() : PyNames.UNKNOWN_TYPE;
 }