SF.net SVN: jython:[7251] trunk/jython/src/org/python

[email protected] Sun, 20 Mar 2011 19:41:31 +0000
Newsgroups gmane.comp.lang.jython.cvs
Message-ID <[email protected]>
Revision: 7251
          http://jython.svn.sourceforge.net/jython/?rev=7251&view=rev
Author:   pjenvey
Date:     2011-03-20 19:41:31 +0000 (Sun, 20 Mar 2011)

Log Message:
-----------
o add Py.writeUnraisable
o remove the no longer needed formatException <oldclass>.__module__ workaround
o cleanup class/instance __repr__

Modified Paths:
--------------
    trunk/jython/src/org/python/core/Py.java
    trunk/jython/src/org/python/core/PyClass.java
    trunk/jython/src/org/python/core/PyFinalizableInstance.java
    trunk/jython/src/org/python/core/PyInstance.java
    trunk/jython/src/org/python/modules/_weakref/AbstractReference.java

Modified: trunk/jython/src/org/python/core/Py.java
===================================================================
--- trunk/jython/src/org/python/core/Py.java	2011-03-20 06:07:17 UTC (rev 7250)
+++ trunk/jython/src/org/python/core/Py.java	2011-03-20 19:41:31 UTC (rev 7251)
@@ -1168,6 +1168,10 @@
     }
 
     public static String formatException(PyObject type, PyObject value) {
+        return formatException(type, value, false);
+    }
+
+    public static String formatException(PyObject type, PyObject value, boolean useRepr) {
         StringBuilder buf = new StringBuilder();
 
         if (PyException.isExceptionClass(type)) {
@@ -1178,10 +1182,7 @@
             }
             PyObject moduleName = type.__findattr__("__module__");
             if (moduleName == null) {
-                // XXX: Workaround the fact that PyClass lacks __module__
-                if (!(type instanceof PyClass)) {
-                    buf.append("<unknown>");
-                }
+                buf.append("<unknown>");
             } else {
                 String moduleStr = moduleName.toString();
                 if (!moduleStr.equals("exceptions")) {
@@ -1191,11 +1192,11 @@
             }
             buf.append(className);
         } else {
-            buf.append(type.__str__());
+            buf.append(useRepr ? type.__repr__() : type.__str__());
         }
         if (value != null && value != Py.None) {
             // only print colon if the str() of the object is not the empty string
-            PyObject s = value.__str__();
+            PyObject s = useRepr ? value.__repr__() : value.__str__();
             if (!(s instanceof PyString) || s.__len__() != 0) {
                 buf.append(": ");
             }
@@ -1204,7 +1205,18 @@
         return buf.toString();
     }
 
+    public static void writeUnraisable(Exception exc, PyObject obj) {
+        if (exc instanceof PyException) {
+            PyException pye = (PyException) exc;
+            stderr.println(String.format("Exception %s in %s ignored",
+                                         formatException(pye.type, pye.value, true), obj));
+        } else {
+            // XXX: this could be better handled
+            exc.printStackTrace();
+        }
+    }
 
+
     /* Equivalent to Python's assert statement */
     public static void assert_(PyObject test, PyObject message) {
         if (!test.__nonzero__()) {

Modified: trunk/jython/src/org/python/core/PyClass.java
===================================================================
--- trunk/jython/src/org/python/core/PyClass.java	2011-03-20 06:07:17 UTC (rev 7250)
+++ trunk/jython/src/org/python/core/PyClass.java	2011-03-20 19:41:31 UTC (rev 7251)
@@ -229,13 +229,8 @@
     @Override
     public String toString() {
         PyObject mod = __dict__.__finditem__("__module__");
-        String smod;
-        if (mod == null || !(mod instanceof PyString)) {
-            smod = "<unknown>";
-        } else {
-            smod = ((PyString) mod).toString();
-        }
-        return "<class " + smod + "." + __name__ + " at " + Py.idstr(this) + ">";
+        String modStr = (mod == null || !Py.isInstance(mod, PyString.TYPE)) ? "?" : mod.toString();
+        return String.format("<class %s.%s at %s>", modStr, __name__, Py.idstr(this));
     }
 
     public boolean isSubClass(PyClass superclass) {

Modified: trunk/jython/src/org/python/core/PyFinalizableInstance.java
===================================================================
--- trunk/jython/src/org/python/core/PyFinalizableInstance.java	2011-03-20 06:07:17 UTC (rev 7250)
+++ trunk/jython/src/org/python/core/PyFinalizableInstance.java	2011-03-20 19:41:31 UTC (rev 7251)
@@ -12,14 +12,14 @@
  * This is a special class due to performance. Defining
  * finalize() on a class, makes the class a lot slower.
  */
+public class PyFinalizableInstance extends PyInstance {
 
-public class PyFinalizableInstance extends PyInstance
-{
     public PyFinalizableInstance(PyClass iclass) {
         super(iclass);
     }
 
     // __del__ method is invoked upon object finalization.
+    @Override
     protected void finalize() {
         try {
             instclass.__del__.__call__(this);
@@ -28,12 +28,10 @@
             PyObject method = instclass.__del__;
             try {
                 method = __findattr__("__del__");
-            } catch (PyException e) { ; }
-
-            Py.stderr.println("Exception " +
-                Py.formatException(exc.type, exc.value) +
-                " in " + method +
-                " ignored");
+            } catch (PyException e) {
+                // nothing we can do
+            }
+            Py.writeUnraisable(exc, method);
         }
     }
 }

Modified: trunk/jython/src/org/python/core/PyInstance.java
===================================================================
--- trunk/jython/src/org/python/core/PyInstance.java	2011-03-20 06:07:17 UTC (rev 7250)
+++ trunk/jython/src/org/python/core/PyInstance.java	2011-03-20 19:41:31 UTC (rev 7251)
@@ -381,18 +381,9 @@
      */
     protected PyString makeDefaultRepr() {
         PyObject mod = instclass.__dict__.__finditem__("__module__");
-        String smod;
-        if(mod == Py.None) {
-            smod = "";
-        } else {
-            if(mod == null || !(mod instanceof PyString)) {
-                smod = "<unknown>.";
-            } else {
-                smod = ((PyString)mod).toString() + '.';
-            }
-        }
-        return new PyString("<" + smod + instclass.__name__ + " instance at " +
-                            Py.idstr(this) + ">");
+        String modStr = (mod == null || !Py.isInstance(mod, PyString.TYPE)) ? "?" : mod.toString();
+        return new PyString(String.format("<%s.%s instance at %s>", modStr, instclass.__name__,
+                                          Py.idstr(this)));
     }
 
     @Override

Modified: trunk/jython/src/org/python/modules/_weakref/AbstractReference.java
===================================================================
--- trunk/jython/src/org/python/modules/_weakref/AbstractReference.java	2011-03-20 06:07:17 UTC (rev 7250)
+++ trunk/jython/src/org/python/modules/_weakref/AbstractReference.java	2011-03-20 19:41:31 UTC (rev 7251)
@@ -27,9 +27,7 @@
         try {
             callback.__call__(this);
         } catch (Exception exc) {
-            // XXX: Should trigger the equiv. of CPython's
-            // PyErr_WriteUnraisable
-            exc.printStackTrace();
+            Py.writeUnraisable(exc, callback);
         }
     }
 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.

------------------------------------------------------------------------------
Colocation vs. Managed Hosting
A question and answer guide to determining the best fit
for your organization - today and in the future.
http://p.sf.net/sfu/internap-sfd2d