[Jython-checkins] jython: Fixes issue #608632 by letting __doc__memberName accept anything that

stefan.richthofer jython-checkins at python.org
Thu Jan 12 10:20:36 EST 2017


https://hg.python.org/jython/rev/fcfff8fb886d
changeset:   8007:fcfff8fb886d
user:        Stefan Richthofer <stefan.richthofer at gmx.de>
date:        Thu Jan 12 16:19:40 2017 +0100
summary:
  Fixes issue #608632 by letting __doc__memberName accept anything that implements java.lang.CharSequence. This is a modified merge of #41 github.com/jythontools/jython/pull/41, just extended to accept any CharSequence, e.g. String, Stringbuffer, StringBuilder. Originally I recognized CharSequence as an elegant way to unify String and PyString and as of github.com/jythontools/jython/commit/5822a827cfcca9a0af4c427cd672a32d968ee8f4 PyBaseString and thus also PyString and PyUnicode implement CharSequence too. Using CharSequence seems like the ideal compromise between allowing any Java-Object and allowing only String and PyString.

files:
  NEWS                                |   4 ++
  src/org/python/core/PyJavaType.java |  30 ++++++++++------
  2 files changed, 22 insertions(+), 12 deletions(-)


diff --git a/NEWS b/NEWS
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,7 @@
 
 Jython 2.7.1rc1
   Bugs fixed
+    - [ 608632 ] __doc__foo should accept java String
     - [ 2523 ] defaultdict.__getitem__() does not propagate exceptions raised by calling
                default_factory
     - [ 2522 ] defaultdict.__getitem__(unhashable) raises KeyError instead of TypeError
@@ -46,6 +47,9 @@
     - [ 1767 ] Rich comparisons
 
   New Features
+    - Jython doc-entries in Java-code (__doc__foo) now accept anything that implements
+      java.lang.CharSequence (PyString implements CharSequence now as well).
+      This allows Jython-friendly Java-code without PyString-induced dependency on Jython.
     - Provided a painless way to deal with long-standing Issue 527524 - "Cannot compile to use
       methods exceeding JVM size restrictions": If a CPython 2.7 bytecode-file (.pyc) exists,
       Jython automatically uses that bytecode to represent oversized functions and methods,
diff --git a/src/org/python/core/PyJavaType.java b/src/org/python/core/PyJavaType.java
--- a/src/org/python/core/PyJavaType.java
+++ b/src/org/python/core/PyJavaType.java
@@ -178,7 +178,7 @@
             for (String method : type.modified) {
                 if (!allModified.add(method)) { // Another type in conflict has this method, possibly fail
                     PyList types = new PyList();
-                    Set<Class> proxySet = Generic.set();
+                    Set<Class<?>> proxySet = Generic.set();
                     for (PyJavaType othertype : conflictedAttributes) {
                         if (othertype.modified != null && othertype.modified.contains(method)) {
                             types.add(othertype);
@@ -283,7 +283,7 @@
 
         // Add methods and determine bean properties declared on this class
         Map<String, PyBeanProperty> props = Generic.map();
-        Map<String, PyBeanEvent> events = Generic.map();
+        Map<String, PyBeanEvent<?>> events = Generic.map();
         Method[] methods;
         if (Options.respectJavaAccessibility) {
             // returns just the public methods
@@ -344,7 +344,7 @@
                     ename = ename.substring(idot + 1);
                 }
                 ename = normalize(StringUtil.decapitalize(ename));
-                events.put(ename, new PyBeanEvent(ename, eventClass, meth));
+                events.put(ename, new PyBeanEvent<>(ename, eventClass, meth));
                 continue;
             }
 
@@ -378,7 +378,7 @@
                     // XXX: should we issue a warning if setX and getX have different
                     // types?
                     if (prop.myType == null) {
-                        Class[] params = meth.getParameterTypes();
+                        Class<?>[] params = meth.getParameterTypes();
                         if (params.length == 1) {
                             prop.myType = params[0];
                         }
@@ -423,17 +423,18 @@
             String fldname = field.getName();
             if (Modifier.isStatic(field.getModifiers())) {
                 if (fldname.startsWith("__doc__") && fldname.length() > 7
-                        && field.getType() == PyString.class) {
+                        && CharSequence.class.isAssignableFrom(field.getType())) {
                     String fname = fldname.substring(7).intern();
                     PyObject memb = dict.__finditem__(fname);
                     if (memb != null && memb instanceof PyReflectedFunction) {
-                        PyString doc = null;
+                        CharSequence doc = null;
                         try {
-                            doc = (PyString)field.get(null);
+                            doc = (CharSequence) field.get(null);
                         } catch (IllegalAccessException e) {
                             throw Py.JavaError(e);
                         }
-                        ((PyReflectedFunction)memb).__doc__ = doc;
+                        ((PyReflectedFunction)memb).__doc__ = doc instanceof PyString ?
+                                (PyString) doc : new PyString(doc.toString());
                     }
                 }
             }
@@ -442,7 +443,7 @@
             }
         }
 
-        for (PyBeanEvent ev : events.values()) {
+        for (PyBeanEvent<?> ev : events.values()) {
             if (dict.__finditem__(ev.__name__) == null) {
                 dict.__setitem__(ev.__name__, ev);
             }
@@ -726,6 +727,7 @@
 
         @SuppressWarnings("unchecked")  // thanks to Bas de Bakker for the tip!
         T clone = (T) cin.readObject();
+        cin.close();
         return clone;
     }
 
@@ -887,6 +889,7 @@
             super(name, numArgs);
         }
 
+        @SuppressWarnings("unchecked")
         @Override
         public PyObject __call__(PyObject arg) {
             Object asjava = arg.__tojava__(Object.class);
@@ -938,9 +941,10 @@
         final Map<Class<?>, PyBuiltinMethod[]> proxies = new HashMap<>();
 
         PyBuiltinMethodNarrow iterableProxy = new PyBuiltinMethodNarrow("__iter__") {
+            @SuppressWarnings("unchecked")
             @Override
             public PyObject __call__() {
-                return new JavaIterator(((Iterable) self.getJavaProxy()));
+                return new JavaIterator(((Iterable<Object>) self.getJavaProxy()));
             }
         };
         proxies.put(Iterable.class, new PyBuiltinMethod[]{iterableProxy});
@@ -974,17 +978,19 @@
         proxies.put(Collection.class, new PyBuiltinMethod[]{lenProxy, containsProxy});
 
         PyBuiltinMethodNarrow iteratorProxy = new PyBuiltinMethodNarrow("__iter__") {
+            @SuppressWarnings("unchecked")
             @Override
             public PyObject __call__() {
-                return new JavaIterator(((Iterator) self.getJavaProxy()));
+                return new JavaIterator(((Iterator<Object>) self.getJavaProxy()));
             }
         };
         proxies.put(Iterator.class, new PyBuiltinMethod[]{iteratorProxy});
 
         PyBuiltinMethodNarrow enumerationProxy = new PyBuiltinMethodNarrow("__iter__") {
+            @SuppressWarnings("unchecked")
             @Override
             public PyObject __call__() {
-                return new EnumerationIter(((Enumeration) self.getJavaProxy()));
+                return new EnumerationIter(((Enumeration<Object>) self.getJavaProxy()));
             }
         };
         proxies.put(Enumeration.class, new PyBuiltinMethod[]{enumerationProxy});

-- 
Repository URL: https://hg.python.org/jython


More information about the Jython-checkins mailing list