All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.org.python.modules._weakref.AbstractReference Maven / Gradle / Ivy

Go to download

Jython is an implementation of the high-level, dynamic, object-oriented language Python written in 100% Pure Java, and seamlessly integrated with the Java platform. It thus allows you to run Python on any Java platform.

There is a newer version: 2.7.4
Show newest version
/* Copyright (c) Jython Developers */
package org.python.modules._weakref;

import org.python.core.Py;
import org.python.core.PyObject;
import org.python.core.PyType;

/**
 * Base class for weakref types.
 */
public abstract class AbstractReference extends PyObject {

    PyObject callback;

    protected GlobalRef gref;

    public AbstractReference(PyType subType, GlobalRef gref, PyObject callback) {
        super(subType);
        this.gref = gref;
        this.callback = callback;
        gref.add(this);
    }

    void call() {
        if (callback == null)
            return;
        try {
            callback.__call__(this);
        } catch (Exception exc) {
            // XXX: Should trigger the equiv. of CPython's
            // PyErr_WriteUnraisable
            exc.printStackTrace();
        }
    }

    protected PyObject py() {
        PyObject o = (PyObject)gref.get();
        if (o == null) {
            throw Py.ReferenceError("weakly-referenced object no longer exists");
        }
        return o;
    }

    public int hashCode() {
        return gref.pythonHashCode();
    }

    public PyObject __eq__(PyObject other) {
        if (other.getClass() != getClass()) {
            return null;
        }
        PyObject pythis = (PyObject)gref.get();
        PyObject pyother = (PyObject)((AbstractReference)other).gref.get();
        if (pythis == null || pyother == null) {
            return this == other ? Py.True : Py.False;
        }
        return pythis._eq(pyother);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy