Message11057

Author stefan.richthofer
Recipients stefan.richthofer
Date 2017-02-02.00:18:06
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1485994687.67.0.40812232387.issue2546@psf.upfronthosting.co.za>
In-reply-to
Content
So, I figured out what needs to be done to support something like

public static final PyString __doc__fieldName = Py.newString("blah");

We would need to add a field to PyReflectedField:
public PyObject __doc__ = null;

In PyReflectedField.java append to _doget:

PyObject result = Py.java2py(value);
if (__doc__ != null) {
  try {
    result.__setattr__("__doc__", __doc__);
  } catch (PyException e) {}
}
return result;

Finally some modification in PyJavaType is required:
In init method change the part starting with for (Field field : fields) to the following:

        Map<String, CharSequence> doc_tmp = new HashMap<>();
        for (Field field : fields) {
            if (!declaredOnMember(baseClass, field)) {
                continue;
            }
            String fldname = field.getName();
            if (Modifier.isStatic(field.getModifiers())) {
                if (fldname.startsWith("__doc__") && fldname.length() > 7
                        && CharSequence.class.isAssignableFrom(field.getType())) {
                    String fname = fldname.substring(7).intern();
                    PyObject memb = dict.__finditem__(fname);
                    if (memb == null) {
                        memb = dict.__finditem__(normalize(fname));
                    }
                    if (memb != null) {// && memb instanceof PyReflectedFunction) {
                        CharSequence doc = null;
                        try {
                            doc = (CharSequence) field.get(null);
                        } catch (IllegalAccessException e) {
                            throw Py.JavaError(e);
                        }
                        if (memb instanceof PyReflectedFunction) {
                            ((PyReflectedFunction)memb).__doc__ = doc instanceof PyString ?
                                    (PyString) doc : new PyString(doc.toString());
                        } else if (memb instanceof PyReflectedField) {
                            try {
                                ((PyReflectedField)memb).__doc__ = doc instanceof PyString ?
                                        (PyString) doc : new PyString(doc.toString());
                            } catch (Exception e) {
                                System.out.println(444+"  "+e);
                            }
                        }
                    } else {
                        try {
                            doc_tmp.put(normalize(fname), (CharSequence) field.get(null));
                        } catch (IllegalAccessException e) {
                            throw Py.JavaError(e);
                        }
                    }
                }
            }
            if (dict.__finditem__(normalize(fldname)) == null) {
                PyReflectedField fld = new PyReflectedField(field);
                CharSequence doc = doc_tmp.get(normalize(fldname));
                if (doc != null) fld.__doc__ = doc instanceof PyString ?
                        (PyString) doc : new PyString(doc.toString());
                dict.__setitem__(normalize(fldname), fld);
            }
        }

If there are no concerns and tests go fine I'd be for adding this doc-feature.
History
Date User Action Args
2017-02-02 00:18:07stefan.richthofersetmessageid: <1485994687.67.0.40812232387.issue2546@psf.upfronthosting.co.za>
2017-02-02 00:18:07stefan.richthofersetrecipients: + stefan.richthofer
2017-02-02 00:18:07stefan.richthoferlinkissue2546 messages
2017-02-02 00:18:06stefan.richthofercreate