File: PyBuiltinMethodSet.java

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (60 lines) | stat: -rw-r--r-- 1,824 bytes parent folder | download | duplicates (4)
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
/* Copyright (c) Jython Developers */
package org.python.core;

public class PyBuiltinMethodSet extends PyBuiltinFunctionSet implements Cloneable {

    private Class<?> type;

    protected PyObject __self__ = Py.None;

    public PyBuiltinMethodSet(String name, int index, int minargs, int maxargs, String doc,
                              Class<?> type) {
        super(name, index, minargs, maxargs, doc);
        this.type = type;
    }

    @Override
    public PyObject __get__(PyObject obj, PyObject type) {
        if (obj != null) {
            if (this.type.isAssignableFrom(obj.getClass())) {
                return bind(obj);
            } else {
                throw Py.TypeError(String.format("descriptor '%s' for '%s' objects doesn't apply "
                                                 + "to '%s' object", info.getName(),
                                                 PyType.fromClass(this.type), obj.getType()));
            }
        }
        return this;
    }

    @Override
    public PyBuiltinCallable bind(PyObject bindTo) {
        if (__self__ != Py.None) {
            return this;
        }
        PyBuiltinMethodSet bindable;
        try {
            bindable = (PyBuiltinMethodSet)clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException("Didn't expect PyBuiltinMethodSet to throw "
                                       + "CloneNotSupported since it implements Cloneable", e);
        }
        bindable.__self__ = bindTo;
        return bindable;
    }

    @Override
    public PyObject getSelf() {
        return __self__;
    }

    @Override
    public boolean implementsDescrGet() {
        return true;
    }

    @Override
    public String toString() {
        return String.format("<built-in method %s>", info.getName());
    }
}