From: <fwi...@us...> - 2008-09-07 05:57:30
|
Revision: 5301 http://jython.svn.sourceforge.net/jython/?rev=5301&view=rev Author: fwierzbicki Date: 2008-09-07 05:57:26 +0000 (Sun, 07 Sep 2008) Log Message: ----------- Committing Nicholas Riley's patch after getting test_ast.py to work. See http://bugs.jython.org/issue1758279. Modified Paths: -------------- trunk/jython/Lib/test/test_ast.py trunk/jython/Lib/test/test_class_jy.py trunk/jython/src/org/python/core/PyJavaClass.java Modified: trunk/jython/Lib/test/test_ast.py =================================================================== --- trunk/jython/Lib/test/test_ast.py 2008-09-06 23:20:46 UTC (rev 5300) +++ trunk/jython/Lib/test/test_ast.py 2008-09-07 05:57:26 UTC (rev 5301) @@ -5,12 +5,12 @@ def get_class_name(t): result = t.__class__.__name__ if os.name.startswith('java'): - if result in ("org.python.antlr.ast.expr_contextType", - "org.python.antlr.ast.boolopType", - "org.python.antlr.ast.unaryopType", - "org.python.antlr.ast.cmpopType", - "org.python.antlr.ast.operatorType"): - result = str(t) + if result in ("expr_contextType", + "boolopType", + "unaryopType", + "cmpopType", + "operatorType"): + result = t.name() else: result = result.split(".")[-1] if result.endswith("Type"): Modified: trunk/jython/Lib/test/test_class_jy.py =================================================================== --- trunk/jython/Lib/test/test_class_jy.py 2008-09-06 23:20:46 UTC (rev 5300) +++ trunk/jython/Lib/test/test_class_jy.py 2008-09-07 05:57:26 UTC (rev 5301) @@ -273,12 +273,29 @@ self.assert_(isinstance(retro, OldStyle)) +class JavaClassNamingTestCase(unittest.TestCase): + """ + Tests for PyJavaClass naming. + """ + + def test_java_class_name(self): + """ + The __name__ and __module__ attributes of Java classes should be set + according to the same convention that Python uses. + """ + from java.lang import String + self.assertEqual(String.__name__, "String") + self.assertEqual(String.__module__, "java.lang") + + + def test_main(): test_support.run_unittest(ClassGeneralTestCase, ClassNamelessModuleTestCase, BrokenNameTestCase, ClassLocalsTestCase, - IsDescendentTestCase) + IsDescendentTestCase, + JavaClassNamingTestCase) if __name__ == "__main__": Modified: trunk/jython/src/org/python/core/PyJavaClass.java =================================================================== --- trunk/jython/src/org/python/core/PyJavaClass.java 2008-09-06 23:20:46 UTC (rev 5300) +++ trunk/jython/src/org/python/core/PyJavaClass.java 2008-09-07 05:57:26 UTC (rev 5301) @@ -75,8 +75,30 @@ init(c); } - protected PyJavaClass(String name,PackageManager mgr) { - __name__ = name; + public String __module__; + /** + * Set the full name of this class. + */ + private void setName(String name) { + int dotIndex = name.lastIndexOf("."); + if (dotIndex == -1) { + __name__ = name; + __module__ = ""; + } else { + __name__ = name.substring(name.lastIndexOf(".")+1, name.length()); + __module__ = name.substring(0, name.lastIndexOf(".")); + } + } + + private String fullName() { + if (__module__ == "") + return __name__; + else + return __module__ + "." + __name__; + } + + protected PyJavaClass(String name, PackageManager mgr) { + setName(name); this.__mgr__ = mgr; } @@ -97,7 +119,7 @@ } private static final void initLazy(PyJavaClass jc) { - Class c = jc.__mgr__.findClass(null,jc.__name__,"lazy java class"); + Class c = jc.__mgr__.findClass(null,jc.fullName(),"lazy java class"); check_lazy_allowed(c); // xxx jc.init(c); tbl.putCanonical(jc.proxyClass,jc); @@ -209,7 +231,7 @@ private void init(Class c) { proxyClass = c; - __name__ = c.getName(); + setName(c.getName()); } /** @@ -761,6 +783,8 @@ } if (name == "__name__") return new PyString(__name__); + if (name == "__module__") + return new PyString(__module__); if (name == "__bases__") { if (__bases__ == null) initialize(); @@ -851,11 +875,11 @@ if (PyObject.class.isAssignableFrom(proxyClass)) { if (Modifier.isAbstract(proxyClass.getModifiers())) { throw Py.TypeError("can't instantiate abstract class ("+ - __name__+")"); + fullName()+")"); } if (__init__ == null) { throw Py.TypeError("no public constructors for "+ - __name__); + fullName()); } return __init__.make(args,keywords); } @@ -871,7 +895,19 @@ return super.__tojava__(c); } + public int __cmp__(PyObject other) { + if (!(other instanceof PyJavaClass)) { + return -2; + } + int c = fullName().compareTo(((PyJavaClass) other).fullName()); + return c < 0 ? -1 : c > 0 ? 1 : 0; + } + + public PyString __str__() { + return new PyString(fullName()); + } + public String toString() { - return "<jclass "+__name__+" "+Py.idstr(this)+">"; + return "<jclass "+fullName()+" "+Py.idstr(this)+">"; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |