From: <cg...@us...> - 2009-01-05 03:56:32
|
Revision: 5845 http://jython.svn.sourceforge.net/jython/?rev=5845&view=rev Author: cgroves Date: 2009-01-05 03:56:25 +0000 (Mon, 05 Jan 2009) Log Message: ----------- test292 - Moved to test_java_integration test293 - Importing no longer auto-imports submodules; deleted test294 - Moved to test_java_subclasses test295 - Moved to test_traceback test296 - Moved to test_import_jy test297,301,310,313,314,315,317 - Testing jythonc; deleted test298 - Moved to test_import_jy test299,300 - Tested by test_file test303 - Moved to test_java_visibility test307,308 - Moved to test_zipimport_jy test309 - Tested by test_scope test311 - Already disabled; deleted test312 - Moved to test_sax_jy test316 - Moved to test_java_integration test318 - Tested by test_syntax test319 - Moved to test_java_visibility Modified Paths: -------------- trunk/jython/Lib/test/test_import_jy.py trunk/jython/Lib/test/test_java_integration.py trunk/jython/Lib/test/test_java_subclasses.py trunk/jython/Lib/test/test_java_visibility.py trunk/jython/Lib/test/test_traceback.py trunk/jython/Lib/test/test_traceback_jy.py trunk/jython/bugtests/test302.py trunk/jython/src/org/python/core/APIReader.java trunk/jython/src/org/python/core/imp.java trunk/jython/src/org/python/core/util/importer.java trunk/jython/tests/java/org/python/tests/BeanInterface.java Added Paths: ----------- trunk/jython/Lib/test/anygui.py trunk/jython/Lib/test/import_nonexistent.py trunk/jython/Lib/test/python_home.policy trunk/jython/Lib/test/syspath_import.jar trunk/jython/Lib/test/test_sax_jy.py trunk/jython/Lib/test/test_zipimport_jy.py trunk/jython/tests/java/org/python/tests/Matryoshka.java Removed Paths: ------------- trunk/jython/bugtests/test292.policy trunk/jython/bugtests/test292.py trunk/jython/bugtests/test293.py trunk/jython/bugtests/test293p/ trunk/jython/bugtests/test294.py trunk/jython/bugtests/test294j.java trunk/jython/bugtests/test295.py trunk/jython/bugtests/test296.py trunk/jython/bugtests/test296p/ trunk/jython/bugtests/test297.py trunk/jython/bugtests/test297c.py trunk/jython/bugtests/test298.py trunk/jython/bugtests/test298m1.py trunk/jython/bugtests/test299.py trunk/jython/bugtests/test300.py trunk/jython/bugtests/test301.py trunk/jython/bugtests/test301c.py trunk/jython/bugtests/test303.py trunk/jython/bugtests/test303j.java trunk/jython/bugtests/test307.py trunk/jython/bugtests/test307foobar.template trunk/jython/bugtests/test307m.template trunk/jython/bugtests/test307p.template trunk/jython/bugtests/test308.py trunk/jython/bugtests/test308d/ trunk/jython/bugtests/test309.py trunk/jython/bugtests/test310.py trunk/jython/bugtests/test310c.py trunk/jython/bugtests/test311.py trunk/jython/bugtests/test312.py trunk/jython/bugtests/test313.py trunk/jython/bugtests/test313c.py trunk/jython/bugtests/test314.py trunk/jython/bugtests/test314c.py trunk/jython/bugtests/test315.py trunk/jython/bugtests/test315c.py trunk/jython/bugtests/test316.py trunk/jython/bugtests/test317.py trunk/jython/bugtests/test317c.py trunk/jython/bugtests/test318.py trunk/jython/bugtests/test319.py trunk/jython/bugtests/test319j.java Added: trunk/jython/Lib/test/anygui.py =================================================================== --- trunk/jython/Lib/test/anygui.py (rev 0) +++ trunk/jython/Lib/test/anygui.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -0,0 +1,40 @@ +'''Used by test_import_jy/test_getattr_module''' +import sys + +class anygui: + __all__ = ['Window'] # Etc... + + def __init__(self): + self.__backend = None + self.__backends = ['msw', 'x', 'mac', 'wx', 'tk', 'java'] + + def __try_to_get(self, modulename): + import imp + try: + module = imp.find_module(modulename) + except (IOError, ImportError, AttributeError, AssertionError): + return None + else: + return module + + def __import_backend(self, wishlist): + candidates = self.__backends[:] + for wish in wishlist: + if wish in candidates: + candidates.remove(wish) + else: + wishlist.remove(wish) + candidates = wishlist + candidates + + for name in candidates: + backend = self.__try_to_get('%sgui' % name) + if not backend: + raise Exception, 'not able to import any GUI backends' + self.__backend = backend + + def __getattr__(self, name): + if not self.__backend: + self.__import_backend(self.__dict__.get('wishlist', [])) + return self.__backend.__dict__[name] + +sys.modules[__name__] = anygui() Added: trunk/jython/Lib/test/import_nonexistent.py =================================================================== --- trunk/jython/Lib/test/import_nonexistent.py (rev 0) +++ trunk/jython/Lib/test/import_nonexistent.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -0,0 +1,7 @@ +try: + import nonexistent_module +except ImportError: + pass # This should cause an import error, but as there's a security manager in place it hasn't + # always done so +else: + raise Error("Should've caused an import error!") Added: trunk/jython/Lib/test/python_home.policy =================================================================== --- trunk/jython/Lib/test/python_home.policy (rev 0) +++ trunk/jython/Lib/test/python_home.policy 2009-01-05 03:56:25 UTC (rev 5845) @@ -0,0 +1,5 @@ +grant codeBase "file:${python.home}/-" { + permission java.util.PropertyPermission "*", "read,write"; + permission java.io.FilePermission "<<ALL FILES>>", "read,write"; +}; + Added: trunk/jython/Lib/test/syspath_import.jar =================================================================== (Binary files differ) Property changes on: trunk/jython/Lib/test/syspath_import.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Modified: trunk/jython/Lib/test/test_import_jy.py =================================================================== --- trunk/jython/Lib/test/test_import_jy.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/Lib/test/test_import_jy.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -89,8 +89,14 @@ # Again ensure we didn't recompile self.assertEquals(bytecode, read(init_compiled), 'bytecode was recompiled') + def test_corrupt_bytecode(self): + f = open("empty$py.class", "w") + f.close() + self.assertRaises(ImportError, __import__, "empty") + f = open("empty.py", "w") + f.close() + self.assertRaises(ImportError, __import__, "empty") - class OverrideBuiltinsImportTestCase(unittest.TestCase): def test_override(self): tests = [ @@ -136,7 +142,15 @@ (None, '__builtin__', ('', '', 6))) self.assertEqual(imp.find_module('imp'), (None, 'imp', ('', '', 6))) + def test_getattr_module(self): + '''Replacing __getattr__ in a class shouldn't lead to calls to __getitem__ + http://bugs.jython.org/issue438108''' + from test import anygui + # causes a stack overflow if the bug occurs + self.assertRaises(Exception, getattr, anygui, 'abc') + + def test_main(): test_support.run_unittest(MislabeledImportTestCase, OverrideBuiltinsImportTestCase, Modified: trunk/jython/Lib/test/test_java_integration.py =================================================================== --- trunk/jython/Lib/test/test_java_integration.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/Lib/test/test_java_integration.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,5 +1,6 @@ import os import unittest +import subprocess import sys import re @@ -279,6 +280,7 @@ def test_list_delegation(self): for c in ArrayList, Vector: a = c() + self.assertRaises(IndexError, a.__getitem__, 0) a.add("blah") self.assertTrue("blah" in a) self.assertEquals(1, len(a)) @@ -318,6 +320,14 @@ for i in v: pass +class SecurityManagerTest(unittest.TestCase): + def test_nonexistent_import_with_security(self): + policy = test_support.findfile("python_home.policy") + script = test_support.findfile("import_nonexistent.py") + self.assertEquals(subprocess.call([sys.executable, "-J-Dpython.cachedir.skip=true", + "-J-Djava.security.manager", "-J-Djava.security.policy=%s" % policy, script]), + 0) + def test_main(): test_support.run_unittest(InstantiationTest, BeanTest, @@ -331,7 +341,7 @@ BigDecimalTest, JavaStringTest, JavaDelegationTest, - ) + SecurityManagerTest) if __name__ == "__main__": test_main() Modified: trunk/jython/Lib/test/test_java_subclasses.py =================================================================== --- trunk/jython/Lib/test/test_java_subclasses.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/Lib/test/test_java_subclasses.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -12,7 +12,7 @@ from java.awt import Color, Component, Dimension, Rectangle from javax.swing.table import AbstractTableModel -from org.python.tests import Callbacker +from org.python.tests import BeanInterface, Callbacker class InterfaceTest(unittest.TestCase): def test_java_calling_python_interface_implementation(self): @@ -146,7 +146,34 @@ ht.put("a", fv) self.failUnless(fv is ht.get("a")) + def test_proxy_generates_protected_methods(self): + """Jython proxies should generate methods for protected methods on their superclasses + Tests for bug #416871""" + output = [] + class RegularBean(BeanInterface): + def __init__(self): + output.append("init") + + def getName(self): + output.append("getName") + class FinalizingBean(RegularBean): + def finalize(self): + pass + def clone(self): + return self.__class__() + + for a in FinalizingBean(), RegularBean(): + self.assertEquals("init", output.pop()) + a.getName() + self.assertEquals("getName", output.pop()) + aa = a.clone() + if isinstance(a, FinalizingBean): + self.assertEquals("init", output.pop()) + aa.name + self.assertEquals("getName", output.pop()) + + """ public abstract class Abstract { public Abstract() { Modified: trunk/jython/Lib/test/test_java_visibility.py =================================================================== --- trunk/jython/Lib/test/test_java_visibility.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/Lib/test/test_java_visibility.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -5,8 +5,8 @@ from test import test_support from java.lang import Byte, Class from java.util import HashMap, Observable, Observer -from org.python.tests import (Coercions, HiddenSuper, InterfaceCombination, Invisible, OnlySubclassable, - OtherSubVisible, SomePyMethods, SubVisible, Visible, VisibleOverride) +from org.python.tests import (Coercions, HiddenSuper, InterfaceCombination, Invisible, Matryoshka, + OnlySubclassable, OtherSubVisible, SomePyMethods, SubVisible, Visible, VisibleOverride) from org.python.tests import VisibilityResults as Results class VisibilityTest(unittest.TestCase): @@ -131,6 +131,17 @@ '''Bug #222847 - Can't access public member of package private base class''' self.assertEquals("hi", HiddenSuper().hi()) + def test_nested_classes(self): + """Test deeply nested classes + + Bug #440660 - using nested java cls @ level >2 fails""" + + Matryoshka.Outermost.Middle.Innermost + + def test_inner_class_identity(self): + """Bug #452947 - Class of innerclass inst <> innerclass""" + self.assertEquals(id(Matryoshka.Outermost), id(Matryoshka.makeOutermost().__class__)) + class JavaClassTest(unittest.TestCase): def test_class_methods_visible(self): self.assertFalse(HashMap.isInterface(), @@ -212,7 +223,7 @@ test_support.run_unittest(VisibilityTest, JavaClassTest, CoercionTest, -# RespectJavaAccessibilityTest, + RespectJavaAccessibilityTest, ClassloaderTest) if __name__ == "__main__": Added: trunk/jython/Lib/test/test_sax_jy.py =================================================================== --- trunk/jython/Lib/test/test_sax_jy.py (rev 0) +++ trunk/jython/Lib/test/test_sax_jy.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -0,0 +1,50 @@ +import sys +import StringIO +import unittest + +from xml.sax import saxutils +from xml.sax import make_parser +from xml.sax.handler import feature_namespaces + +from test import test_support + +file = StringIO.StringIO("""<collection> + <comic title="Sandman" number='62'> + <writer>Neil Gaiman</writer> + <penciller pages='1-9,18-24'>Glyn Dillon</penciller> + <penciller pages="10-17">Charles Vess</penciller> + </comic> + <comic title="Shade, the Changing Man" number="7"> + <writer>Peter Milligan</writer> + <penciller>Chris Bachalo</penciller> + </comic> +</collection>""") + +class FindIssue(saxutils.DefaultHandler): + def __init__(self, title, number): + self.search_title, self.search_number = title, number + self.match = 0 + + def startElement(self,name,attrs): + global match + if name != 'comic' : return + + title = attrs.get('title', None) + number = attrs.get('number',None) + if title == self.search_title and number == self.search_number: + self.match += 1 + +class SimpleSaxTest(unittest.TestCase): + def test_find_issue(self): + parser = make_parser() + parser.setFeature(feature_namespaces,0) + dh = FindIssue('Sandman', '62') + parser.setContentHandler(dh) + parser.parse(file) + self.assertEquals(1, dh.match) +def test_main(): + test_support.run_unittest(SimpleSaxTest) + +if __name__ == "__main__": + test_main() + Modified: trunk/jython/Lib/test/test_traceback.py =================================================================== --- trunk/jython/Lib/test/test_traceback.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/Lib/test/test_traceback.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -158,7 +158,6 @@ err = traceback.format_exception_only(None, None) self.assertEqual(err, ['None\n']) - def test_main(): run_unittest(TracebackCases) Modified: trunk/jython/Lib/test/test_traceback_jy.py =================================================================== --- trunk/jython/Lib/test/test_traceback_jy.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/Lib/test/test_traceback_jy.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -46,7 +46,12 @@ else: self.fail('Expected Exception') + def test_extract_stack(self): + # http://bugs.jython.org/issue437809 + traceback.extract_stack() + + try: raise Exception('foo') except Exception: Added: trunk/jython/Lib/test/test_zipimport_jy.py =================================================================== --- trunk/jython/Lib/test/test_zipimport_jy.py (rev 0) +++ trunk/jython/Lib/test/test_zipimport_jy.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -0,0 +1,31 @@ +import unittest +import sys + +from test import test_support + +class SyspathZipimportTest(unittest.TestCase): + def setUp(self): + self.orig_path = sys.path + sys.path.insert(0, test_support.findfile("syspath_import.jar")) + + def tearDown(self): + sys.path = self.orig_path + + def test_load_class_from_syspath_zip(self): + from syspathonly import Syspath + self.assertEquals(Syspath.staticCall(), "result") + + def test_load_pkg_from_syspath(self): + import syspathpkg + self.assertEquals(syspathpkg.__name__, 'syspathpkg') + self.assert_('syspath_import.jar' in syspathpkg.__file__) + from syspathpkg import module + self.assertEquals(module.__name__, 'syspathpkg.module') + +def test_main(): + test_support.run_unittest(SyspathZipimportTest) + +if __name__ == "__main__": + test_main() + + Deleted: trunk/jython/bugtests/test292.policy =================================================================== --- trunk/jython/bugtests/test292.policy 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test292.policy 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,11 +0,0 @@ - -grant codeBase "file:./classes/-" { - permission java.util.PropertyPermission "*", "read,write"; - permission java.io.FilePermission "<<ALL FILES>>", "read,write"; -}; - -grant codeBase "file:${python.home}/-" { - permission java.util.PropertyPermission "*", "read,write"; - permission java.io.FilePermission "<<ALL FILES>>", "read,write"; -}; - Deleted: trunk/jython/bugtests/test292.py =================================================================== --- trunk/jython/bugtests/test292.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test292.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,16 +0,0 @@ -""" -Test sys.path import when a security profile prevents -creating classloaders. -""" - -import support, java - -support.compileJava("classes/test292j.java") - -home = java.lang.System.getProperty("python.home") -cmd = """\ --Djava.security.manager -Djava.security.policy=test292.policy \ --Dpython.home=%s test292j""" % home - -support.runJava(cmd) - Deleted: trunk/jython/bugtests/test293.py =================================================================== --- trunk/jython/bugtests/test293.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test293.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,13 +0,0 @@ -""" -Test lost syntax error when auto importing submodules. -""" - -import support - -import test293p -try: - test293p.submod.func() -except SyntaxError: - pass -else: - raise support.TestError('should raise a syntax error') Deleted: trunk/jython/bugtests/test294.py =================================================================== --- trunk/jython/bugtests/test294.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test294.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,48 +0,0 @@ -""" -[ #416871 ] proxy fails to have all needed methods -""" - -import support - -support.compileJava('test294j.java') - -import test294j - - -class PyTestA(test294j): - def __init__(self): - output.append( "inited") - def doStart(self): - output.append( "started") - def doEnd(self): - output.append( "completed") - def finalize(self): - pass - def clone(self): - return self.__class__() - -class PyTestB(test294j): - def __init__(self): - output.append( "inited") - def doStart(self): - output.append( "started") - def doEnd(self): - output.append( "completed") - -output = [] - -a = PyTestA() -a.doStart() -aa = a.clone() -aa.doStart() - -assert output == ['inited', 'started', 'inited', 'started'] - -output = [] - -a = PyTestB() -a.doStart() -aa = a.clone() -aa.doStart() - -assert output == ['inited', 'started', 'started'] Deleted: trunk/jython/bugtests/test294j.java =================================================================== --- trunk/jython/bugtests/test294j.java 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test294j.java 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,10 +0,0 @@ -/** - * Just a simple interface to demonstrate how subclassing - * from Jython breaks because of the two protected - * methods 'finalize' and 'clone'. - */ -public interface test294j extends Cloneable { - public void doStart(); - public void doEnd(); -} - Deleted: trunk/jython/bugtests/test295.py =================================================================== --- trunk/jython/bugtests/test295.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test295.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,9 +0,0 @@ -""" -[ #437809 ] traceback error -""" - -import support - -import traceback -traceback.extract_stack() - Deleted: trunk/jython/bugtests/test296.py =================================================================== --- trunk/jython/bugtests/test296.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test296.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,15 +0,0 @@ -""" -[ #438108 ] __getitem__ called when it shouldn't? -""" - -import support -import java - -import test296p -try: - print test296p.abc -except java.lang.StackOverflowError: - raise support.TestError("Shouldn't raise a Stack overflow") -except Exception: - pass - Deleted: trunk/jython/bugtests/test297.py =================================================================== --- trunk/jython/bugtests/test297.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test297.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,11 +0,0 @@ -""" - -""" - -import support - -support.compileJPythonc("test297c.py", output="test297.err") - -if support.grep("jpywork/test297c.java", - r'TestFactory extends .*HTMLFactory', count=1) != 1: - raise support.TestError('Subclassing an inner class should be possible') Deleted: trunk/jython/bugtests/test297c.py =================================================================== --- trunk/jython/bugtests/test297c.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test297c.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,23 +0,0 @@ -from javax.swing.text.html import HTMLEditorKit, FormView - -class TestEditorKit(HTMLEditorKit): - def getViewFactory(self): - return self.TestFactory() - - class TestFactory(HTMLEditorKit.HTMLFactory): - def create(self,e): - o = e.getAttributes().getAttribute(StyleConstants.NameAttribute) - if o == HTML.Tag.INPUT: - return TestFormView(e) - return HTMLEditorKit.HTMLFactory.create(self,e) - -class TestFormView(FormView): - def __init__(self,e): - FormView.__init__(self,e) - - def test(self): - print 'hello' - - def actionPerformed(self,e): - self.test() - Deleted: trunk/jython/bugtests/test298.py =================================================================== --- trunk/jython/bugtests/test298.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test298.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,22 +0,0 @@ -""" -Test a obviously corrupt $py.class file. -""" - -import support -import java - -f = open("test298m1.py", "w") -f.close() - -f = open("test298m1$py.class", "w") -f.close() - -try: - import test298m1 -except ImportError, e: - pass -except java.lang.ArrayIndexOutOfBoundsException: - raise support.TestWarning('Should not throw an ArratIndexOutOfBound') -else: - raise support.TestError('Should throw an import error') - Deleted: trunk/jython/bugtests/test299.py =================================================================== --- trunk/jython/bugtests/test299.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test299.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,24 +0,0 @@ -""" -Test that open append position filepointer at the end -""" - -import support - -s1 = "abcdefghijklmnopqrstuvwxyz" -s2 = "0123456789" -f = open("test299.out", "wb") -f.write(s1) -f.close() -f = open("test299.out", "ab") -f.write(s2) -f.close() - -f = open("test299.out", "rb") -res = f.read() -f.close() - -if res != s1 + s2: - raise support.TestError('File did not append correctly') - - - Deleted: trunk/jython/bugtests/test300.py =================================================================== --- trunk/jython/bugtests/test300.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test300.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,39 +0,0 @@ -""" -test of double seek -""" - -import support - -s1 = "abcdefghijklmnopqrstuvwxyz" -s2 = "0123456789" -f = open("test299.out", "wb") -f.write(s1) -f.close() - -def verify(pos, res): - #print pos, res - if pos != res: - raise support.TestError, "Wrong filepos #1 (%d, %d)" % (pos, res) - - -f = open("test299.out", "rb") -f.read() -verify(f.tell(), 26) -f.seek(-10, 1) -verify(f.tell(), 16) -f.seek(-10, 1) -verify(f.tell(), 6) -f.seek(-1, 1) -verify(f.tell(), 5) -f.seek(-1, 1) -verify(f.tell(), 4) -f.close() - -#raise support.TestWarning('A test of TestWarning. It is not an error') - - - - - - - Deleted: trunk/jython/bugtests/test301.py =================================================================== --- trunk/jython/bugtests/test301.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test301.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,14 +0,0 @@ -""" -[ #434324 ] jythonc -A and dead java vars -""" - -import support - -support.compileJava("classes/test301p/B.java") -support.compileJava("classes/test301p/A.java") -support.compileJPythonc("test301c.py", jar="test301.jar", core=1, - addpackages="test301p", - output="test301.err") - -support.runJava("test301c", classpath="test301.jar", output="test301c.err") - Deleted: trunk/jython/bugtests/test301c.py =================================================================== --- trunk/jython/bugtests/test301c.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test301c.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,9 +0,0 @@ - -import test301p - -class test301c(test301p.A): - pass - -#print test301c() -#print test301p.A() - Modified: trunk/jython/bugtests/test302.py =================================================================== --- trunk/jython/bugtests/test302.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test302.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -2,16 +2,13 @@ In bug #439688 the value 0x99 does not survive the JavaCC parser. """ -import support - +import sys +print sys.defaultencoding f = open("test302s.py", "wb") f.write('v = "\x99"\n') f.close() -try: - import test302s -except SyntaxError: - raise support.TestError('Importing a file with str byte > 128 should not raise a Syntaxerror') +import test302s f = open("test302.out", "w") f.write("\x99") @@ -20,6 +17,4 @@ from java.io import FileInputStream, InputStreamReader readval = InputStreamReader(FileInputStream("test302.out"), 'ISO-8859-1').read() -if ord(test302s.v) != readval: - raise support.TestError("Module source was not decoded correctly %x %x" % - (ord(test302s.v), readval)) +print ord(test302s.v) == readval Deleted: trunk/jython/bugtests/test303.py =================================================================== --- trunk/jython/bugtests/test303.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test303.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,10 +0,0 @@ -""" -Test deeply nested classes -[ #440660 ] using nested java cls @ level >2 fails -""" - -import support -support.compileJava("test303j.java") - -import test303j.A -import test303j.A.B Deleted: trunk/jython/bugtests/test303j.java =================================================================== --- trunk/jython/bugtests/test303j.java 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test303j.java 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,10 +0,0 @@ -public class test303j { - public static class A { - public static class B { - public static class C { - } - } - } -} - - Deleted: trunk/jython/bugtests/test307.py =================================================================== --- trunk/jython/bugtests/test307.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test307.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,42 +0,0 @@ -""" -Test import of modules and packages from sys.path zipfile. -""" - -import support -import zipfile, time -from java.io import File - - -def addZipEntry(zip, name, templateName): - template = open('test307%s.template' % templateName) - entry = zipfile.ZipInfo() - entry.filename = name - entry.date_time = time.gmtime(time.time()) - zip.writestr(entry, template.read()) - template.close() - -zip = zipfile.ZipFile("test307.zip", "w") - -addZipEntry(zip, 'test307m.py', 'm') -addZipEntry(zip, 'test307p/__init__.py', 'p') -addZipEntry(zip, "foo/bar/foobar.py", "foobar") - -zip.close() - -import sys -sys.path.append("test307.zip") - -import test307m -assert test307m.__name__ == "test307m" -assert "test307.zip" in test307m.__file__ -import test307p - -sys.path.pop() -del test307p, test307m -del sys.modules['test307p'], sys.modules['test307m'] - -import java -java.lang.System.gc() -time.sleep(4) - - Deleted: trunk/jython/bugtests/test307foobar.template =================================================================== --- trunk/jython/bugtests/test307foobar.template 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test307foobar.template 2009-01-05 03:56:25 UTC (rev 5845) @@ -1 +0,0 @@ -assert __name__ == 'test307p.foobar', "__name__ should've been test307p.foobar but was %s" % __name__ Deleted: trunk/jython/bugtests/test307m.template =================================================================== --- trunk/jython/bugtests/test307m.template 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test307m.template 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,4 +0,0 @@ -assert __name__ == 'test307m', " __name__ should've been test307m but was %s" % __name__ -from java.io import File -expected = "test307.zip%stest307m.py" % File.separator -assert expected in __file__, "%s should've been in __file__ but was %s" % (expected, __file__) Deleted: trunk/jython/bugtests/test307p.template =================================================================== --- trunk/jython/bugtests/test307p.template 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test307p.template 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,7 +0,0 @@ -assert __name__ == 'test307p', " __name__ should've been test307p but was %s" % __name__ - -#compare("__file__", __file__, "test307p/__init__.py") -#compare("__path__", __path__, ["test307.zip/test307p"]) -#import test307m -__path__.append("test307.zip/foo/bar") -import foobar Deleted: trunk/jython/bugtests/test308.py =================================================================== --- trunk/jython/bugtests/test308.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test308.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,32 +0,0 @@ -""" -Test import of java class from sys.path zipfile. -""" - -import support -import zipfile, time - -support.compileJava("test308d/test308j.java") - -def addZipEntry(zip, name, data): - entry = zipfile.ZipInfo() - entry.filename = name - entry.date_time = time.gmtime(time.time()) - zip.writestr(entry, data) - - -zip = zipfile.ZipFile("test308.zip", "w", zipfile.ZIP_DEFLATED) - -addZipEntry(zip, "test308m.py", """ -import test308j -assert test308j().foo() == "bar" -""") - -zip.write("test308d/test308j.class", "test308j.class") - -zip.close() - -import sys -sys.path.append("test308.zip") - -import test308m - Deleted: trunk/jython/bugtests/test309.py =================================================================== --- trunk/jython/bugtests/test309.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test309.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,13 +0,0 @@ -""" -[ #444292 ] local var binding overrides local import -""" - -import support - -def foo(pickle): - assert pickle == 1 - import pickle - assert pickle != 1 - -foo(1) - Deleted: trunk/jython/bugtests/test310.py =================================================================== --- trunk/jython/bugtests/test310.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test310.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,10 +0,0 @@ -""" -[ #444292 ] local var binding overrides local import with jythonc -""" - -import support - -support.compileJPythonc("test310c.py", output="test310.err", - jar="test310.jar", core=1) -support.runJava("test310c", classpath="test310.jar") - Deleted: trunk/jython/bugtests/test310c.py =================================================================== --- trunk/jython/bugtests/test310c.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test310c.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,13 +0,0 @@ -""" -[ #444292 ] local var binding overrides local import -""" - -import support - -def foo(pickle): - assert pickle == 1 - import pickle - assert pickle != 1 - -foo(1) - Deleted: trunk/jython/bugtests/test311.py =================================================================== --- trunk/jython/bugtests/test311.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test311.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,22 +0,0 @@ -""" -Test DST for time.mktime -""" - -import support -import time - -def check(tup, facit): - #print tup, long(time.mktime(tup)), facit, (long(time.mktime(tup))-facit) - assert time.mktime(tup) == facit - -# These tests fail for CPython also. - -''' -check((1998, 6, 13, 0, 0, 0, 0, 0, 0), 897692400) -check((1998, 6, 13, 0, 0, 0, 0, 0, -1), 897688800) -check((1998, 6, 13, 0, 0, 0, 0, 0, 0), 897692400) -check((1998, 6, 13, 0, 0, 0, 0, 0, 1), 897688800) -check((1998, 1, 13, 0, 0, 0, 0, 0, -1), 884646000) -check((1998, 1, 13, 0, 0, 0, 0, 0, 0), 884646000) -check((1998, 1, 13, 0, 0, 0, 0, 0, 1), 884642400) -''' Deleted: trunk/jython/bugtests/test312.py =================================================================== --- trunk/jython/bugtests/test312.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test312.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,47 +0,0 @@ -""" -Simple test of xml. -""" - -import support -import sys, StringIO - -from xml.sax import saxutils -from xml.sax import make_parser -from xml.sax.handler import feature_namespaces - -#print sys.modules - -file = StringIO.StringIO("""<collection> - <comic title="Sandman" number='62'> - <writer>Neil Gaiman</writer> - <penciller pages='1-9,18-24'>Glyn Dillon</penciller> - <penciller pages="10-17">Charles Vess</penciller> - </comic> - <comic title="Shade, the Changing Man" number="7"> - <writer>Peter Milligan</writer> - <penciller>Chris Bachalo</penciller> - </comic> -</collection>""") - -class FindIssue(saxutils.DefaultHandler): - def __init__(self, title, number): - self.search_title, self.search_number = title, number - - def startElement(self,name,attrs): - global match - if name != 'comic' : return - - title = attrs.get('title', None) - number = attrs.get('number',None) - if title == self.search_title and number == self.search_number: - match += 1 - -parser = make_parser() -parser.setFeature(feature_namespaces,0) -dh = FindIssue('Sandman', '62') -parser.setContentHandler(dh) - -match = 0 -parser.parse(file) -assert match == 1 - Deleted: trunk/jython/bugtests/test313.py =================================================================== --- trunk/jython/bugtests/test313.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test313.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,10 +0,0 @@ -""" -Jythonc test of xml -""" - -import support - -support.compileJPythonc("test313c.py", output="test313.err", - jar="test313.jar", core=1) -support.runJava("test313c", classpath="test313.jar") - Deleted: trunk/jython/bugtests/test313c.py =================================================================== --- trunk/jython/bugtests/test313c.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test313c.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,46 +0,0 @@ - -import sys, StringIO - -from xml.sax import saxutils -from xml.sax import make_parser -from xml.sax.handler import feature_namespaces - -def dummy_jythonc(): - import xml.sax.drivers2.drv_xmlproc - import encodings.utf_16_be - import dumbdbm - -file = StringIO.StringIO("""<collection> - <comic title="Sandman" number='62'> - <writer>Neil Gaiman</writer> - <penciller pages='1-9,18-24'>Glyn Dillon</penciller> - <penciller pages="10-17">Charles Vess</penciller> - </comic> - <comic title="Shade, the Changing Man" number="7"> - <writer>Peter Milligan</writer> - <penciller>Chris Bachalo</penciller> - </comic> -</collection>""") - -class FindIssue(saxutils.DefaultHandler): - def __init__(self, title, number): - self.search_title, self.search_number = title, number - - def startElement(self,name,attrs): - global match - if name != 'comic' : return - - title = attrs.get('title', None) - number = attrs.get('number',None) - if title == self.search_title and number == self.search_number: - match += 1 - -parser = make_parser() -parser.setFeature(feature_namespaces,0) -dh = FindIssue('Sandman', '62') -parser.setContentHandler(dh) - -match = 0 -parser.parse(file) -assert match == 1 - Deleted: trunk/jython/bugtests/test314.py =================================================================== --- trunk/jython/bugtests/test314.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test314.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,19 +0,0 @@ -""" -[ #448485 ] Tuple unpacking raises KeyError -""" - -import support - -import string -s = "elem1 elem2" -try: - (a, b, c) = string.split(s) -except ValueError: - pass -else: - print support.TestError("Should raise a ValueError") - -support.compileJPythonc("test314c.py", output="test314.err", - jar="test314.jar", core=1) -support.runJava("test314c", classpath="test314.jar") - Deleted: trunk/jython/bugtests/test314c.py =================================================================== --- trunk/jython/bugtests/test314c.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test314c.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,13 +0,0 @@ - -import string -s = "elem1 elem2" -try: - (a, b, c) = string.split(s) - (d, e, f) = string.split(s) - pass -except ValueError: - pass -else: - print support.TestError("Should raise a ValueError") - - Deleted: trunk/jython/bugtests/test315.py =================================================================== --- trunk/jython/bugtests/test315.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test315.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,11 +0,0 @@ -""" -Basic test, just raises an TestError -""" - -import support -support.compileJPythonc("test315c.py", output="test315.err", - jar="test315.jar", core=1) -support.runJava("test315c", classpath="test315.jar") - - - Deleted: trunk/jython/bugtests/test315c.py =================================================================== --- trunk/jython/bugtests/test315c.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test315c.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,2 +0,0 @@ - -"abc".encode("ASCII") Deleted: trunk/jython/bugtests/test316.py =================================================================== --- trunk/jython/bugtests/test316.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test316.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,14 +0,0 @@ -""" -[ #449316 ] ArrayList()[0] should raise IndexError -""" - -import support - -from java.util import ArrayList -al = ArrayList() -try: - foo = al[0] -except KeyError: - pass -else: - raise support.TestError("Should raise a KeyError") Deleted: trunk/jython/bugtests/test317.py =================================================================== --- trunk/jython/bugtests/test317.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test317.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,11 +0,0 @@ -""" -[ #448485 ] Tuple unpacking raises KeyError -""" - -import support -support.compileJPythonc("test317c.py", output="test317.err", - jar="test317.jar", core=1) -support.runJava("test317c", classpath="test317.jar") - - - Deleted: trunk/jython/bugtests/test317c.py =================================================================== --- trunk/jython/bugtests/test317c.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test317c.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,7 +0,0 @@ - -import string -try: - (a, b, c) = string.split("elem1 elem2") -except ValueError: - pass - Deleted: trunk/jython/bugtests/test318.py =================================================================== --- trunk/jython/bugtests/test318.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test318.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,14 +0,0 @@ -""" -Test against AIOOB exceptions. -""" - -import support - -def go2( a, b ): - pass - -try: - go2( 1, 2, 3 ) -except TypeError: - pass - Deleted: trunk/jython/bugtests/test319.py =================================================================== --- trunk/jython/bugtests/test319.py 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test319.py 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,18 +0,0 @@ -""" -Test class identity for inner classes -[ #452947 ] Class of innerclass inst <> innerclas -""" - -import support - -support.compileJava('test319j.java') - -import test319j - -id1 = id(test319j.inner) -id2 = id(test319j.mkinner().__class__) - -if id1 != id2: - print "innerclass different", test319j.inner, test319j.mkinner().__class__ - raise support.TestWarning("innerclass different %s %s" % ( - test319j.inner, test319j.mkinner().__class__)) Deleted: trunk/jython/bugtests/test319j.java =================================================================== --- trunk/jython/bugtests/test319j.java 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/bugtests/test319j.java 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,9 +0,0 @@ - -public class test319j { - public static class inner { } - - public static inner mkinner() { - return new inner(); - } -} - Modified: trunk/jython/src/org/python/core/APIReader.java =================================================================== --- trunk/jython/src/org/python/core/APIReader.java 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/src/org/python/core/APIReader.java 2009-01-05 03:56:25 UTC (rev 5845) @@ -4,22 +4,19 @@ */ package org.python.core; +import java.io.IOException; + import org.python.objectweb.asm.AnnotationVisitor; import org.python.objectweb.asm.ClassReader; import org.python.objectweb.asm.commons.EmptyVisitor; -import java.io.InputStream; -import java.io.IOException; - /** - * This class reads a classfile from a byte array and pulls out the value of - * the class annotation for APIVersion, which can then be retrieved by a call - * to getVersion(). + * This class reads a classfile from a byte array and pulls out the value of the class annotation + * for APIVersion, which can then be retrieved by a call to getVersion(). * - * Hopefully the use of ClassReader in this implementation is not too - * expensive. I suspect it is not since EmptyVisitor is just a bag of empty - * methods so shouldn't cost too much. If it turns out to cost too much, we - * will want to implement a special purpose ClassReader that only reads out the + * Hopefully the use of ClassReader in this implementation is not too expensive. I suspect it is not + * since EmptyVisitor is just a bag of empty methods so shouldn't cost too much. If it turns out to + * cost too much, we will want to implement a special purpose ClassReader that only reads out the * APIVersion annotation I think. */ public class APIReader extends EmptyVisitor { @@ -28,17 +25,29 @@ private int version = -1; + /** + * Reads the classfile bytecode in data and to extract the version. + * @throws IOException - if the classfile is malformed. + */ public APIReader(byte[] data) throws IOException { - ClassReader r = new ClassReader(data); - r.accept(this, 0); + ClassReader r; + try { + r = new ClassReader(data); + } catch (ArrayIndexOutOfBoundsException e) { + IOException ioe = new IOException("Malformed bytecode: not enough data"); + ioe.initCause(e);// IOException didn't grow a constructor that could take a cause till + // 1.6, so do it the old fashioned way + throw ioe; + } + r.accept(this, 0); } - public AnnotationVisitor visitAnnotation(String desc, boolean visible) { + public AnnotationVisitor visitAnnotation(String desc, boolean visible) { nextVisitIsVersion = desc.equals("Lorg/python/compiler/APIVersion;"); return this; } - public void visit(String name, Object value) { + public void visit(String name, Object value) { if (nextVisitIsVersion) { version = (Integer)value; nextVisitIsVersion = false; Modified: trunk/jython/src/org/python/core/imp.java =================================================================== --- trunk/jython/src/org/python/core/imp.java 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/src/org/python/core/imp.java 2009-01-05 03:56:25 UTC (rev 5845) @@ -87,7 +87,15 @@ static PyObject createFromPyClass(String name, InputStream fp, boolean testing, String sourceName, String compiledName) { - byte[] data = readCode(name, fp, testing); + byte[] data = null; + try { + data = readCode(name, fp, testing); + } catch (IOException ioe) { + if (!testing) { + throw Py.ImportError(ioe.getMessage() + "[name=" + name + ", source=" + sourceName + + ", compiled=" + compiledName + "]"); + } + } if (testing && data == null) { return null; } @@ -108,15 +116,11 @@ return createFromCode(name, code, compiledName); } - public static byte[] readCode(String name, InputStream fp, boolean testing) { + public static byte[] readCode(String name, InputStream fp, boolean testing) throws IOException { byte[] data = readBytes(fp); int api; - try { - APIReader ar = new APIReader(data); - api = ar.getVersion(); - } catch (IOException i) { - api = -1; - } + APIReader ar = new APIReader(data); + api = ar.getVersion(); if (api != APIVersion) { if (testing) { return null; Modified: trunk/jython/src/org/python/core/util/importer.java =================================================================== --- trunk/jython/src/org/python/core/util/importer.java 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/src/org/python/core/util/importer.java 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,5 +1,6 @@ package org.python.core.util; +import java.io.IOException; import java.io.InputStream; import java.util.EnumSet; import org.python.core.BytecodeLoader; @@ -182,7 +183,11 @@ Bundle bundle = makeBundle(searchPath, tocEntry); byte[] codeBytes; if (isbytecode) { - codeBytes = imp.readCode(fullname, bundle.inputStream, true); + try { + codeBytes = imp.readCode(fullname, bundle.inputStream, true); + } catch (IOException ioe) { + throw Py.ImportError(ioe.getMessage() + "[path=" + fullPath + searchPath + "]"); + } } else { codeBytes = imp.compileSource(fullname, bundle.inputStream, fullSearchPath); } Modified: trunk/jython/tests/java/org/python/tests/BeanInterface.java =================================================================== --- trunk/jython/tests/java/org/python/tests/BeanInterface.java 2009-01-04 23:20:34 UTC (rev 5844) +++ trunk/jython/tests/java/org/python/tests/BeanInterface.java 2009-01-05 03:56:25 UTC (rev 5845) @@ -1,6 +1,6 @@ package org.python.tests; -public interface BeanInterface { +public interface BeanInterface extends Cloneable { String getName(); } Added: trunk/jython/tests/java/org/python/tests/Matryoshka.java =================================================================== --- trunk/jython/tests/java/org/python/tests/Matryoshka.java (rev 0) +++ trunk/jython/tests/java/org/python/tests/Matryoshka.java 2009-01-05 03:56:25 UTC (rev 5845) @@ -0,0 +1,16 @@ +package org.python.tests; + +public class Matryoshka { + + public static Outermost makeOutermost() { + return new Outermost(); + } + + public static class Outermost { + + public static class Middle { + + public static class Innermost {} + } + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2009-01-05 07:12:46
|
Revision: 5847 http://jython.svn.sourceforge.net/jython/?rev=5847&view=rev Author: cgroves Date: 2009-01-05 07:12:43 +0000 (Mon, 05 Jan 2009) Log Message: ----------- test302 - Move to test_pep263_jy This adds a check for the encoding declared by the source file actually matching up with what we were getting out of it, and switches the default encoding to ascii to follow CPython. CPython assumes utf-8 in some single compilation contexts, and this uses the same parser algorithm everywhere, so I made a small change to test_doctest since Jython is throwing a SyntaxError on utf-8 strings to a single compilation. Modified Paths: -------------- trunk/jython/Lib/test/test_doctest.py trunk/jython/src/org/python/core/ParserFacade.java Added Paths: ----------- trunk/jython/Lib/test/invalid_utf_8_declared_encoding.py trunk/jython/Lib/test/latin1_no_encoding.py trunk/jython/Lib/test/test_pep263_jy.py Removed Paths: ------------- trunk/jython/bugtests/test302.py Added: trunk/jython/Lib/test/invalid_utf_8_declared_encoding.py =================================================================== (Binary files differ) Property changes on: trunk/jython/Lib/test/invalid_utf_8_declared_encoding.py ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: trunk/jython/Lib/test/latin1_no_encoding.py =================================================================== (Binary files differ) Property changes on: trunk/jython/Lib/test/latin1_no_encoding.py ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Modified: trunk/jython/Lib/test/test_doctest.py =================================================================== --- trunk/jython/Lib/test/test_doctest.py 2009-01-05 07:08:51 UTC (rev 5846) +++ trunk/jython/Lib/test/test_doctest.py 2009-01-05 07:12:43 UTC (rev 5847) @@ -2265,17 +2265,18 @@ File "...", line 7, in test_doctest4.txt Failed example: u'...' - Expected: - u'f\xf6\xf6' - Got: - u'f\xc3\xb6\xc3\xb6' + ... ********************************************************************** ... ********************************************************************** + ... + ********************************************************************** + ... + ********************************************************************** 1 items had failures: - 2 of 4 in test_doctest4.txt - ***Test Failed*** 2 failures. - (2, 4) + 4 of 4 in test_doctest4.txt + ***Test Failed*** 4 failures. + (4, 4) >>> doctest.master = None # Reset master. >>> doctest.testfile('test_doctest4.txt', encoding='utf-8') Added: trunk/jython/Lib/test/test_pep263_jy.py =================================================================== --- trunk/jython/Lib/test/test_pep263_jy.py (rev 0) +++ trunk/jython/Lib/test/test_pep263_jy.py 2009-01-05 07:12:43 UTC (rev 5847) @@ -0,0 +1,16 @@ +import unittest +from test import test_support + +class BadEncodingTest(unittest.TestCase): + + def test_invalid_default(self): + self.assertRaises(SyntaxError, __import__, "test.latin1_no_encoding") + + def test_invalid_declared_encoding(self): + self.assertRaises(SyntaxError, __import__, "test.invalid_utf_8_declared_encoding") + +def test_main(): + test_support.run_unittest(BadEncodingTest) + +if __name__=="__main__": + test_main() Deleted: trunk/jython/bugtests/test302.py =================================================================== --- trunk/jython/bugtests/test302.py 2009-01-05 07:08:51 UTC (rev 5846) +++ trunk/jython/bugtests/test302.py 2009-01-05 07:12:43 UTC (rev 5847) @@ -1,20 +0,0 @@ -""" -In bug #439688 the value 0x99 does not survive the JavaCC parser. -""" - -import sys -print sys.defaultencoding -f = open("test302s.py", "wb") -f.write('v = "\x99"\n') -f.close() - -import test302s - -f = open("test302.out", "w") -f.write("\x99") -f.close(); - -from java.io import FileInputStream, InputStreamReader -readval = InputStreamReader(FileInputStream("test302.out"), 'ISO-8859-1').read() - -print ord(test302s.v) == readval Modified: trunk/jython/src/org/python/core/ParserFacade.java =================================================================== --- trunk/jython/src/org/python/core/ParserFacade.java 2009-01-05 07:08:51 UTC (rev 5846) +++ trunk/jython/src/org/python/core/ParserFacade.java 2009-01-05 07:12:43 UTC (rev 5847) @@ -8,7 +8,11 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; -import java.io.UnsupportedEncodingException; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CodingErrorAction; +import java.nio.charset.UnsupportedCharsetException; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -56,9 +60,9 @@ } // if reader != null, reset it - public static PyException fixParseError(BufferedReader reader, Throwable t, - String filename) - { + public static PyException fixParseError(ExpectedEncodingBufferedReader reader, + Throwable t, + String filename) { if (reader != null) { try { reader.reset(); @@ -82,6 +86,16 @@ return new PyIndentationError(msg, line, col, text, filename); } return new PySyntaxError(msg, line, col, text, filename); + } else if (t instanceof CharacterCodingException) { + String msg; + if (reader.encoding == null) { + msg = "Non-ASCII character in file '" + filename + "', but no encoding declared" + + "; see http://www.python.org/peps/pep-0263.html for details"; + } else { + msg = "Illegal character in file '" + filename + "' for encoding '" + + reader.encoding + "'"; + } + throw Py.SyntaxError(msg); } else return Py.JavaError(t); } @@ -93,7 +107,7 @@ * from it, to translate ParserExceptions into PySyntaxErrors or * PyIndentationErrors. */ - private static mod parse(BufferedReader reader, + private static mod parse(ExpectedEncodingBufferedReader reader, String kind, String filename, CompilerFlags cflags) throws Throwable { @@ -120,7 +134,7 @@ String kind, String filename, CompilerFlags cflags) { - BufferedReader bufReader = null; + ExpectedEncodingBufferedReader bufReader = null; try { // prepBufReader takes care of encoding detection and universal // newlines: @@ -137,7 +151,7 @@ String kind, String filename, CompilerFlags cflags) { - BufferedReader bufReader = null; + ExpectedEncodingBufferedReader bufReader = null; try { bufReader = prepBufReader(string, cflags, filename); return parse(bufReader, kind, filename, cflags); @@ -154,7 +168,7 @@ CompilerFlags cflags, boolean stdprompt) { // XXX: What's the idea of the stdprompt argument? - BufferedReader reader = null; + ExpectedEncodingBufferedReader reader = null; try { reader = prepBufReader(string, cflags, filename); return parse(reader, kind, filename, cflags); @@ -194,7 +208,21 @@ return true; } - private static BufferedReader prepBufReader(InputStream input, CompilerFlags cflags, + private static class ExpectedEncodingBufferedReader extends BufferedReader { + + /** + * The encoding from the source file, or null if none was specified and ascii is being used. + */ + public final String encoding; + + public ExpectedEncodingBufferedReader(Reader in, String encoding) { + super(in); + this.encoding = encoding; + } + } + + private static ExpectedEncodingBufferedReader prepBufReader(InputStream input, + CompilerFlags cflags, String filename) throws IOException { input = new BufferedInputStream(input); boolean bom = adjustForBOM(input); @@ -222,17 +250,19 @@ UniversalIOWrapper textIO = new UniversalIOWrapper(bufferedIO); input = new TextIOInputStream(textIO); - Reader reader; + CharsetDecoder dec; try { - // Using iso-8859-1 for the raw bytes when no encoding was specified - reader = new InputStreamReader(input, encoding == null ? "iso-8859-1" : encoding); - } catch (UnsupportedEncodingException exc) { + // Use ascii for the raw bytes when no encoding was specified + dec = Charset.forName(encoding == null ? "ascii" : encoding).newDecoder(); + } catch (UnsupportedCharsetException exc) { throw new PySyntaxError("Unknown encoding: " + encoding, 1, 0, "", filename); } - return new BufferedReader(reader); + dec.onMalformedInput(CodingErrorAction.REPORT); + dec.onUnmappableCharacter(CodingErrorAction.REPORT); + return new ExpectedEncodingBufferedReader(new InputStreamReader(input, dec), encoding); } - private static BufferedReader prepBufReader(String string, CompilerFlags cflags, + private static ExpectedEncodingBufferedReader prepBufReader(String string, CompilerFlags cflags, String filename) throws IOException { if (cflags.source_is_utf8) { // Passed unicode, re-encode the String to raw bytes @@ -246,7 +276,7 @@ } /** - * Check for a BOM mark at the begginning of stream. If there is a BOM + * Check for a BOM mark at the beginning of stream. If there is a BOM * mark, advance the stream passed it. If not, reset() to start at the * beginning of the stream again. * This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-01-06 20:57:44
|
Revision: 5858 http://jython.svn.sourceforge.net/jython/?rev=5858&view=rev Author: fwierzbicki Date: 2009-01-06 20:57:41 +0000 (Tue, 06 Jan 2009) Log Message: ----------- Making the package names point to the raw original package names in source and making the jarjar step process everything in one step so the package mangling happens on our source as well as the included jars for jython-complete.jar. Modified Paths: -------------- trunk/jython/Lib/os.py trunk/jython/build.xml trunk/jython/src/org/python/compiler/AdapterMaker.java trunk/jython/src/org/python/compiler/ClassFile.java trunk/jython/src/org/python/compiler/Code.java trunk/jython/src/org/python/compiler/CodeCompiler.java trunk/jython/src/org/python/compiler/Constant.java trunk/jython/src/org/python/compiler/Module.java trunk/jython/src/org/python/compiler/ProxyMaker.java trunk/jython/src/org/python/core/APIReader.java trunk/jython/src/org/python/core/BytecodeLoader.java trunk/jython/src/org/python/core/Py.java trunk/jython/src/org/python/core/io/FileIO.java trunk/jython/src/org/python/core/io/IOBase.java trunk/jython/src/org/python/core/io/ServerSocketIO.java trunk/jython/src/org/python/expose/generate/ClassMethodExposer.java trunk/jython/src/org/python/expose/generate/DescriptorExposer.java trunk/jython/src/org/python/expose/generate/ExposeTask.java trunk/jython/src/org/python/expose/generate/ExposedFieldFinder.java trunk/jython/src/org/python/expose/generate/ExposedMethodFinder.java trunk/jython/src/org/python/expose/generate/ExposedTypeProcessor.java trunk/jython/src/org/python/expose/generate/ExposedTypeVisitor.java trunk/jython/src/org/python/expose/generate/Exposer.java trunk/jython/src/org/python/expose/generate/InstanceMethodExposer.java trunk/jython/src/org/python/expose/generate/MethodExposer.java trunk/jython/src/org/python/expose/generate/NewExposer.java trunk/jython/src/org/python/expose/generate/OverridableNewExposer.java trunk/jython/src/org/python/expose/generate/PyTypes.java trunk/jython/src/org/python/expose/generate/RestrictiveAnnotationVisitor.java trunk/jython/src/org/python/expose/generate/TypeExposer.java trunk/jython/src/org/python/modules/_py_compile.java trunk/jython/src/org/python/modules/errno.java trunk/jython/tests/java/org/python/expose/generate/DescriptorExposerTest.java trunk/jython/tests/java/org/python/expose/generate/ExposeMethodFinderTest.java trunk/jython/tests/java/org/python/expose/generate/ExposedTypeVisitorTest.java trunk/jython/tests/java/org/python/expose/generate/MethodExposerTest.java trunk/jython/tests/java/org/python/expose/generate/NewExposerTest.java trunk/jython/tests/java/org/python/expose/generate/OverridableNewExposerTest.java trunk/jython/tests/java/org/python/expose/generate/TypeExposerTest.java Modified: trunk/jython/Lib/os.py =================================================================== --- trunk/jython/Lib/os.py 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/Lib/os.py 2009-01-06 20:57:41 UTC (rev 5858) @@ -47,10 +47,14 @@ import stat as _stat import sys from java.io import File -from org.python.constantine.platform import Errno from org.python.core.io import FileDescriptors, FileIO, IOBase from org.python.core.Py import newString as asPyString +try: + from org.python.constantine.platform import Errno +except ImportError: + from com.kenai.constantine.platform import Errno + # Mapping of: os._name: [name list, shell command list] _os_map = dict(nt=[ ['Windows 95', 'Windows 98', 'Windows ME', 'Windows NT', @@ -96,7 +100,10 @@ # should *NOT* use it _name = get_os_type() -from org.python.posix import JavaPOSIX, POSIXHandler, POSIXFactory +try: + from org.python.posix import JavaPOSIX, POSIXHandler, POSIXFactory +except ImportError: + from org.jruby.ext.posix import JavaPOSIX, POSIXHandler, POSIXFactory class PythonPOSIXHandler(POSIXHandler): def error(self, error, msg): @@ -420,7 +427,10 @@ if constant.name() == constant.description(): # XXX: have constantine handle this fallback # Fake constant or just lacks a description, fallback to Linux's - from org.python.constantine.platform.linux import Errno as LinuxErrno + try: + from org.python.constantine.platform.linux import Errno as LinuxErrno + except ImportError: + from com.kenai.constantine.platform.linux import Errno as LinuxErrno constant = getattr(LinuxErrno, constant.name(), None) if not constant: return 'Unknown error: %d' % code @@ -558,7 +568,10 @@ global _time_t if _time_t is None: from java.lang import Integer, Long - from org.python.posix.util import Platform + try: + from org.python.posix import Platform + except ImportError: + from org.jruby.ext.posix.util import Platform _time_t = Integer if Platform.IS_32_BIT else Long try: Modified: trunk/jython/build.xml =================================================================== --- trunk/jython/build.xml 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/build.xml 2009-01-06 20:57:41 UTC (rev 5858) @@ -108,20 +108,9 @@ <target name="full-build" depends="full-check, install" description="a full build with svn checkout" /> <target name="needed-check" unless="full-build"> - <uptodate property="antlr.notneeded" targetfile="${output.dir}/jarjar.jar"> + <uptodate property="antlr.notneeded" targetfile="${dest.dir}/jython.jar"> <srcfiles dir="grammar" includes="*.g" /> </uptodate> - <!-- When antlr.notneeded isn't set, a clean is run after this, so jarjar's running depends - on both that and its own jar --> - <condition property="jarjar.notneeded"> - <and> - <isset property="antlr.notneeded"/> - <uptodate targetfile="${output.dir}/jarjar.jar"> - <srcfiles dir="extlibs" includes="*" /> - <srcfiles dir="grammar" includes="*.g" /> - </uptodate> - </and> - </condition> </target> <target name="init" depends="version-init"> @@ -166,6 +155,10 @@ <pathelement path="${extlibs.dir}/antlr-2.7.7.jar" /> <pathelement path="${extlibs.dir}/antlr-3.1.1.jar" /> <pathelement path="${extlibs.dir}/stringtemplate-3.2.jar" /> + + <pathelement path="${extlibs.dir}/asm-3.1.jar" /> + <pathelement path="${extlibs.dir}/asm-commons-3.1.jar" /> + <pathelement path="${extlibs.dir}/constantine-0.4.jar" /> </path> <available property="informix.present" classname="com.informix.jdbc.IfxDriver" classpath="${informix.jar}" /> @@ -452,7 +445,7 @@ </target> - <target name="compile" depends="init,antlr_gen,jarjar,brand-version"> + <target name="compile" depends="init,antlr_gen,brand-version"> <javac destdir="${compile.dir}" target="${jdk.target.version}" source="${jdk.source.version}" @@ -512,10 +505,12 @@ includesfile="${jython.base.dir}/CoreExposed.includes"/> </target> - <target name="jarjar" depends="init,needed-check" unless="jarjar.notneeded"> + <target name="jar-complete" depends="jar"> <taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask" classpath="extlibs/jarjar-0.7.jar"/> - <jarjar destfile="${output.dir}/jarjar.jar"> + <jarjar destfile="${output.dir}/jython-complete.jar"> + <zipfileset src="${dist.dir}/jython.jar"/> <zipfileset src="extlibs/antlr-3.1.1-runtime.jar"/> + <rule pattern="org.antlr.runtime.**" result="org.python.antlr.runtime.@1"/> <zipfileset src="extlibs/asm-3.1.jar"/> <zipfileset src="extlibs/asm-commons-3.1.jar"/> <zipfileset src="extlibs/asm-util-3.1.jar"/> @@ -526,6 +521,21 @@ <rule pattern="org.jruby.ext.posix.**" result="org.python.posix.@1"/> <zipfileset src="extlibs/constantine-0.4.jar"/> <rule pattern="com.kenai.constantine.**" result="org.python.constantine.@1"/> + <fileset dir="${dist.dir}" includes="Lib/**/*" /> + <manifest> + <attribute name="Main-Class" value="org.python.util.jython" /> + <attribute name="Built-By" value="${user.name}" /> + <!-- info section. ATTN: no blanks, no '.' in the names --> + <section name="Build-Info"> + <attribute name="version" value="${jython.version}" /> + <attribute name="svn-build" value="${do.checkout}" /> + <attribute name="oracle" value="${oracle.present}" /> + <attribute name="informix" value="${informix.present}" /> + <attribute name="build-compiler" value="${build.compiler}" /> + <attribute name="jdk-target-version" value="${jdk.target.version}" /> + <attribute name="debug" value="${debug}" /> + </section> + </manifest> </jarjar> <unjar src="${output.dir}/jarjar.jar" dest="${jarjar.dir}"> <patternset> @@ -534,30 +544,7 @@ </unjar> </target> - <target name="jar-complete" depends="compile,expose,jarjar"> - <jar destfile="${dist.dir}/jython-complete.jar" duplicate="preserve"> - <fileset dir="${exposed.dir}"/> - <fileset dir="${compile.dir}"/> - <fileset dir="${jarjar.dir}"> - </fileset> - <manifest> - <attribute name="Main-Class" value="org.python.util.jython" /> - <attribute name="Built-By" value="${user.name}" /> - <!-- info section. ATTN: no blanks, no '.' in the names --> - <section name="Build-Info"> - <attribute name="version" value="${jython.version}" /> - <attribute name="svn-build" value="${do.checkout}" /> - <attribute name="oracle" value="${oracle.present}" /> - <attribute name="informix" value="${informix.present}" /> - <attribute name="build-compiler" value="${build.compiler}" /> - <attribute name="jdk-target-version" value="${jdk.target.version}" /> - <attribute name="debug" value="${debug}" /> - </section> - </manifest> - </jar> - </target> - - <target name="jar" depends="compile,expose,jarjar"> + <target name="jar" depends="compile,expose"> <typedef name="nameunion" classname="org.python.util.NameUnionAntType"> <classpath> <path refid="main.classpath" /> @@ -572,9 +559,6 @@ <fileset dir="${exposed.dir}"/> <fileset dir="${compile.dir}" excludes="org/python/expose/generate/**"/> </nameunion> - <fileset dir="${jarjar.dir}"> - <include name="org/python/objectweb/asm/ClassReader.class" /> - </fileset> <manifest> <attribute name="Main-Class" value="org.python.util.jython" /> <attribute name="Built-By" value="${user.name}" /> Modified: trunk/jython/src/org/python/compiler/AdapterMaker.java =================================================================== --- trunk/jython/src/org/python/compiler/AdapterMaker.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/compiler/AdapterMaker.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -6,8 +6,8 @@ import java.lang.reflect.Method; import java.util.HashSet; -import org.python.objectweb.asm.Label; -import org.python.objectweb.asm.Opcodes; +import org.objectweb.asm.Label; +import org.objectweb.asm.Opcodes; import org.python.util.Generic; Modified: trunk/jython/src/org/python/compiler/ClassFile.java =================================================================== --- trunk/jython/src/org/python/compiler/ClassFile.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/compiler/ClassFile.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -9,11 +9,11 @@ import java.util.Collections; import java.util.List; -import org.python.objectweb.asm.AnnotationVisitor; -import org.python.objectweb.asm.ClassWriter; -import org.python.objectweb.asm.FieldVisitor; -import org.python.objectweb.asm.MethodVisitor; -import org.python.objectweb.asm.Opcodes; +import org.objectweb.asm.AnnotationVisitor; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.FieldVisitor; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; public class ClassFile { Modified: trunk/jython/src/org/python/compiler/Code.java =================================================================== --- trunk/jython/src/org/python/compiler/Code.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/compiler/Code.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -3,11 +3,11 @@ import java.util.BitSet; import java.util.Vector; -import org.python.objectweb.asm.AnnotationVisitor; -import org.python.objectweb.asm.Attribute; -import org.python.objectweb.asm.Label; -import org.python.objectweb.asm.MethodVisitor; -import org.python.objectweb.asm.Opcodes; +import org.objectweb.asm.AnnotationVisitor; +import org.objectweb.asm.Attribute; +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; class Code implements MethodVisitor, Opcodes { MethodVisitor mv; Modified: trunk/jython/src/org/python/compiler/CodeCompiler.java =================================================================== --- trunk/jython/src/org/python/compiler/CodeCompiler.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/compiler/CodeCompiler.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -79,11 +79,11 @@ import org.python.core.PyObject; import org.python.core.PyString; import org.python.core.PyUnicode; -import org.python.objectweb.asm.ClassWriter; -import org.python.objectweb.asm.Label; -import org.python.objectweb.asm.Opcodes; -import org.python.objectweb.asm.Type; -import org.python.objectweb.asm.commons.Method; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.Label; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; +import org.objectweb.asm.commons.Method; public class CodeCompiler extends Visitor implements Opcodes, ClassConstants //, PythonGrammarTreeConstants { Modified: trunk/jython/src/org/python/compiler/Constant.java =================================================================== --- trunk/jython/src/org/python/compiler/Constant.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/compiler/Constant.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -4,7 +4,7 @@ import java.io.IOException; -import org.python.objectweb.asm.Opcodes; +import org.objectweb.asm.Opcodes; abstract class Constant implements Opcodes{ public Module module; Modified: trunk/jython/src/org/python/compiler/Module.java =================================================================== --- trunk/jython/src/org/python/compiler/Module.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/compiler/Module.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -10,8 +10,8 @@ import java.util.List; import java.util.Map; -import org.python.objectweb.asm.Label; -import org.python.objectweb.asm.Opcodes; +import org.objectweb.asm.Label; +import org.objectweb.asm.Opcodes; import org.python.core.CompilerFlags; import org.python.core.Py; import org.python.core.PyException; Modified: trunk/jython/src/org/python/compiler/ProxyMaker.java =================================================================== --- trunk/jython/src/org/python/compiler/ProxyMaker.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/compiler/ProxyMaker.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -16,8 +16,8 @@ import org.python.core.PyObject; import org.python.core.PyProxy; import org.python.core.PyReflectedFunction; -import org.python.objectweb.asm.Label; -import org.python.objectweb.asm.Opcodes; +import org.objectweb.asm.Label; +import org.objectweb.asm.Opcodes; import org.python.util.Generic; public class ProxyMaker implements ClassConstants, Opcodes Modified: trunk/jython/src/org/python/core/APIReader.java =================================================================== --- trunk/jython/src/org/python/core/APIReader.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/core/APIReader.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -6,9 +6,9 @@ import java.io.IOException; -import org.python.objectweb.asm.AnnotationVisitor; -import org.python.objectweb.asm.ClassReader; -import org.python.objectweb.asm.commons.EmptyVisitor; +import org.objectweb.asm.AnnotationVisitor; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.commons.EmptyVisitor; /** * This class reads a classfile from a byte array and pulls out the value of the class annotation Modified: trunk/jython/src/org/python/core/BytecodeLoader.java =================================================================== --- trunk/jython/src/org/python/core/BytecodeLoader.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/core/BytecodeLoader.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -4,7 +4,7 @@ import java.security.SecureClassLoader; import java.util.List; -import org.python.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassReader; import org.python.util.Generic; /** Modified: trunk/jython/src/org/python/core/Py.java =================================================================== --- trunk/jython/src/org/python/core/Py.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/core/Py.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -20,7 +20,7 @@ import java.util.Set; import org.python.antlr.base.mod; -import org.python.constantine.platform.Errno; +import com.kenai.constantine.platform.Errno; import org.python.compiler.Module; import org.python.core.adapter.ClassicPyObjectAdapter; import org.python.core.adapter.ExtensiblePyObjectAdapter; Modified: trunk/jython/src/org/python/core/io/FileIO.java =================================================================== --- trunk/jython/src/org/python/core/io/FileIO.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/core/io/FileIO.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -12,7 +12,7 @@ import java.nio.channels.Channels; import java.nio.channels.FileChannel; -import org.python.constantine.platform.Errno; +import com.kenai.constantine.platform.Errno; import org.python.core.imp; import org.python.core.Py; import org.python.core.util.RelativeFile; Modified: trunk/jython/src/org/python/core/io/IOBase.java =================================================================== --- trunk/jython/src/org/python/core/io/IOBase.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/core/io/IOBase.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -1,7 +1,7 @@ /* Copyright (c) 2007 Jython Developers */ package org.python.core.io; -import org.python.constantine.platform.Errno; +import com.kenai.constantine.platform.Errno; import java.io.InputStream; import java.io.OutputStream; Modified: trunk/jython/src/org/python/core/io/ServerSocketIO.java =================================================================== --- trunk/jython/src/org/python/core/io/ServerSocketIO.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/core/io/ServerSocketIO.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -6,7 +6,7 @@ import java.nio.channels.Channel; import java.nio.channels.ServerSocketChannel; -import org.python.constantine.platform.Errno; +import com.kenai.constantine.platform.Errno; import org.python.core.Py; /** Modified: trunk/jython/src/org/python/expose/generate/ClassMethodExposer.java =================================================================== --- trunk/jython/src/org/python/expose/generate/ClassMethodExposer.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/expose/generate/ClassMethodExposer.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -1,6 +1,6 @@ package org.python.expose.generate; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.Type; import org.python.core.PyBuiltinClassMethodNarrow; public class ClassMethodExposer extends MethodExposer { Modified: trunk/jython/src/org/python/expose/generate/DescriptorExposer.java =================================================================== --- trunk/jython/src/org/python/expose/generate/DescriptorExposer.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/expose/generate/DescriptorExposer.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -1,6 +1,6 @@ package org.python.expose.generate; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.Type; import org.python.core.PyDataDescr; /** Modified: trunk/jython/src/org/python/expose/generate/ExposeTask.java =================================================================== --- trunk/jython/src/org/python/expose/generate/ExposeTask.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/expose/generate/ExposeTask.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -11,7 +11,7 @@ import org.apache.tools.ant.types.Path; import org.apache.tools.ant.util.GlobPatternMapper; import org.apache.tools.ant.util.SourceFileScanner; -import org.python.objectweb.asm.ClassWriter; +import org.objectweb.asm.ClassWriter; import org.python.util.Generic; public class ExposeTask extends MatchingTask { Modified: trunk/jython/src/org/python/expose/generate/ExposedFieldFinder.java =================================================================== --- trunk/jython/src/org/python/expose/generate/ExposedFieldFinder.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/expose/generate/ExposedFieldFinder.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -1,8 +1,8 @@ package org.python.expose.generate; -import org.python.objectweb.asm.AnnotationVisitor; -import org.python.objectweb.asm.Attribute; -import org.python.objectweb.asm.FieldVisitor; +import org.objectweb.asm.AnnotationVisitor; +import org.objectweb.asm.Attribute; +import org.objectweb.asm.FieldVisitor; public abstract class ExposedFieldFinder implements FieldVisitor, PyTypes { Modified: trunk/jython/src/org/python/expose/generate/ExposedMethodFinder.java =================================================================== --- trunk/jython/src/org/python/expose/generate/ExposedMethodFinder.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/expose/generate/ExposedMethodFinder.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -3,11 +3,11 @@ import java.util.List; import org.python.expose.MethodType; -import org.python.objectweb.asm.AnnotationVisitor; -import org.python.objectweb.asm.MethodAdapter; -import org.python.objectweb.asm.MethodVisitor; -import org.python.objectweb.asm.Opcodes; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.AnnotationVisitor; +import org.objectweb.asm.MethodAdapter; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; import org.python.util.Generic; /** Modified: trunk/jython/src/org/python/expose/generate/ExposedTypeProcessor.java =================================================================== --- trunk/jython/src/org/python/expose/generate/ExposedTypeProcessor.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/expose/generate/ExposedTypeProcessor.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -7,16 +7,16 @@ import java.util.Map; import org.python.expose.ExposedType; -import org.python.objectweb.asm.AnnotationVisitor; -import org.python.objectweb.asm.ClassAdapter; -import org.python.objectweb.asm.ClassReader; -import org.python.objectweb.asm.ClassVisitor; -import org.python.objectweb.asm.ClassWriter; -import org.python.objectweb.asm.FieldVisitor; -import org.python.objectweb.asm.MethodAdapter; -import org.python.objectweb.asm.MethodVisitor; -import org.python.objectweb.asm.Opcodes; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.AnnotationVisitor; +import org.objectweb.asm.ClassAdapter; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.FieldVisitor; +import org.objectweb.asm.MethodAdapter; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; import org.python.util.Generic; /** Modified: trunk/jython/src/org/python/expose/generate/ExposedTypeVisitor.java =================================================================== --- trunk/jython/src/org/python/expose/generate/ExposedTypeVisitor.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/expose/generate/ExposedTypeVisitor.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -1,7 +1,7 @@ package org.python.expose.generate; -import org.python.objectweb.asm.AnnotationVisitor; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.AnnotationVisitor; +import org.objectweb.asm.Type; /** * Visits an ExposedType annotation and passes the values it gathers to handleResult. Modified: trunk/jython/src/org/python/expose/generate/Exposer.java =================================================================== --- trunk/jython/src/org/python/expose/generate/Exposer.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/expose/generate/Exposer.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -3,12 +3,12 @@ import java.util.HashMap; import java.util.Map; -import org.python.objectweb.asm.ClassVisitor; -import org.python.objectweb.asm.ClassWriter; -import org.python.objectweb.asm.Label; -import org.python.objectweb.asm.MethodVisitor; -import org.python.objectweb.asm.Opcodes; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; import org.python.core.BytecodeLoader; /** Modified: trunk/jython/src/org/python/expose/generate/InstanceMethodExposer.java =================================================================== --- trunk/jython/src/org/python/expose/generate/InstanceMethodExposer.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/expose/generate/InstanceMethodExposer.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -1,7 +1,7 @@ package org.python.expose.generate; -import org.python.objectweb.asm.Label; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.Label; +import org.objectweb.asm.Type; import org.python.core.PyBuiltinMethod; import org.python.core.PyBuiltinMethodNarrow; import org.python.expose.ExposedMethod; Modified: trunk/jython/src/org/python/expose/generate/MethodExposer.java =================================================================== --- trunk/jython/src/org/python/expose/generate/MethodExposer.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/expose/generate/MethodExposer.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -1,6 +1,6 @@ package org.python.expose.generate; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.Type; public abstract class MethodExposer extends Exposer { Modified: trunk/jython/src/org/python/expose/generate/NewExposer.java =================================================================== --- trunk/jython/src/org/python/expose/generate/NewExposer.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/expose/generate/NewExposer.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -2,8 +2,8 @@ import java.lang.reflect.Method; -import org.python.objectweb.asm.Opcodes; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; import org.python.core.PyNewWrapper; import org.python.core.PyObject; import org.python.core.PyType; Modified: trunk/jython/src/org/python/expose/generate/OverridableNewExposer.java =================================================================== --- trunk/jython/src/org/python/expose/generate/OverridableNewExposer.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/expose/generate/OverridableNewExposer.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -1,8 +1,8 @@ package org.python.expose.generate; import org.python.core.PyOverridableNew; -import org.python.objectweb.asm.Label; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.Label; +import org.objectweb.asm.Type; public class OverridableNewExposer extends Exposer { Modified: trunk/jython/src/org/python/expose/generate/PyTypes.java =================================================================== --- trunk/jython/src/org/python/expose/generate/PyTypes.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/expose/generate/PyTypes.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -1,6 +1,6 @@ package org.python.expose.generate; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.Type; import org.python.core.Py; import org.python.core.PyBoolean; import org.python.core.PyBuiltinCallable; Modified: trunk/jython/src/org/python/expose/generate/RestrictiveAnnotationVisitor.java =================================================================== --- trunk/jython/src/org/python/expose/generate/RestrictiveAnnotationVisitor.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/expose/generate/RestrictiveAnnotationVisitor.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -1,6 +1,6 @@ package org.python.expose.generate; -import org.python.objectweb.asm.AnnotationVisitor; +import org.objectweb.asm.AnnotationVisitor; /** * An Annotation visitor that throws an IllegalArgumentException if it visits anything other than Modified: trunk/jython/src/org/python/expose/generate/TypeExposer.java =================================================================== --- trunk/jython/src/org/python/expose/generate/TypeExposer.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/expose/generate/TypeExposer.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -7,7 +7,7 @@ import org.python.expose.BaseTypeBuilder; import org.python.expose.ExposedType; import org.python.expose.TypeBuilder; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.Type; import org.python.util.Generic; /** Modified: trunk/jython/src/org/python/modules/_py_compile.java =================================================================== --- trunk/jython/src/org/python/modules/_py_compile.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/modules/_py_compile.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -3,7 +3,7 @@ import java.io.File; -import org.python.constantine.platform.Errno; +import com.kenai.constantine.platform.Errno; import org.python.core.Py; import org.python.core.PyList; import org.python.core.PyString; Modified: trunk/jython/src/org/python/modules/errno.java =================================================================== --- trunk/jython/src/org/python/modules/errno.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/src/org/python/modules/errno.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -1,8 +1,8 @@ /* Copyright (c) Jython Developers */ package org.python.modules; -import org.python.constantine.Constant; -import org.python.constantine.ConstantSet; +import com.kenai.constantine.Constant; +import com.kenai.constantine.ConstantSet; import org.python.core.ClassDictInit; import org.python.core.Py; import org.python.core.PyDictionary; @@ -14,7 +14,7 @@ * The Python errno module. * * Errno constants can be accessed from Java code via - * {@link org.python.constantine.platform.Errno}, e.g. Errno.ENOENT. + * {@link com.kenai.constantine.platform.Errno}, e.g. Errno.ENOENT. */ public class errno implements ClassDictInit { Modified: trunk/jython/tests/java/org/python/expose/generate/DescriptorExposerTest.java =================================================================== --- trunk/jython/tests/java/org/python/expose/generate/DescriptorExposerTest.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/tests/java/org/python/expose/generate/DescriptorExposerTest.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -1,6 +1,6 @@ package org.python.expose.generate; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.Type; import org.python.core.BytecodeLoader; import org.python.core.Py; import org.python.core.PyDataDescr; Modified: trunk/jython/tests/java/org/python/expose/generate/ExposeMethodFinderTest.java =================================================================== --- trunk/jython/tests/java/org/python/expose/generate/ExposeMethodFinderTest.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/tests/java/org/python/expose/generate/ExposeMethodFinderTest.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -2,10 +2,10 @@ import junit.framework.TestCase; -import org.python.objectweb.asm.AnnotationVisitor; -import org.python.objectweb.asm.Opcodes; -import org.python.objectweb.asm.Type; -import org.python.objectweb.asm.commons.EmptyVisitor; +import org.objectweb.asm.AnnotationVisitor; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; +import org.objectweb.asm.commons.EmptyVisitor; import org.python.expose.ExposedMethod; import org.python.expose.ExposedNew; Modified: trunk/jython/tests/java/org/python/expose/generate/ExposedTypeVisitorTest.java =================================================================== --- trunk/jython/tests/java/org/python/expose/generate/ExposedTypeVisitorTest.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/tests/java/org/python/expose/generate/ExposedTypeVisitorTest.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -2,7 +2,7 @@ import junit.framework.TestCase; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.Type; import org.python.core.PyObject; import org.python.expose.generate.ExposedTypeVisitor; Modified: trunk/jython/tests/java/org/python/expose/generate/MethodExposerTest.java =================================================================== --- trunk/jython/tests/java/org/python/expose/generate/MethodExposerTest.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/tests/java/org/python/expose/generate/MethodExposerTest.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -1,7 +1,7 @@ package org.python.expose.generate; -import org.python.objectweb.asm.Opcodes; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; import org.python.core.BytecodeLoader; import org.python.core.Py; import org.python.core.PyBuiltinCallable; Modified: trunk/jython/tests/java/org/python/expose/generate/NewExposerTest.java =================================================================== --- trunk/jython/tests/java/org/python/expose/generate/NewExposerTest.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/tests/java/org/python/expose/generate/NewExposerTest.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -1,7 +1,7 @@ package org.python.expose.generate; -import org.python.objectweb.asm.Opcodes; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; import org.python.core.BytecodeLoader; import org.python.core.Py; import org.python.core.PyNewWrapper; Modified: trunk/jython/tests/java/org/python/expose/generate/OverridableNewExposerTest.java =================================================================== --- trunk/jython/tests/java/org/python/expose/generate/OverridableNewExposerTest.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/tests/java/org/python/expose/generate/OverridableNewExposerTest.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -1,7 +1,7 @@ package org.python.expose.generate; -import org.python.objectweb.asm.Opcodes; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; import org.python.core.BytecodeLoader; import org.python.core.Py; import org.python.core.PyDictionary; Modified: trunk/jython/tests/java/org/python/expose/generate/TypeExposerTest.java =================================================================== --- trunk/jython/tests/java/org/python/expose/generate/TypeExposerTest.java 2009-01-06 02:11:04 UTC (rev 5857) +++ trunk/jython/tests/java/org/python/expose/generate/TypeExposerTest.java 2009-01-06 20:57:41 UTC (rev 5858) @@ -4,7 +4,7 @@ import junit.framework.TestCase; -import org.python.objectweb.asm.Type; +import org.objectweb.asm.Type; import org.python.core.Py; import org.python.core.PyNewWrapper; import org.python.core.PyObject; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <otm...@us...> - 2009-01-07 07:35:09
|
Revision: 5863 http://jython.svn.sourceforge.net/jython/?rev=5863&view=rev Author: otmarhumbel Date: 2009-01-07 07:35:05 +0000 (Wed, 07 Jan 2009) Log Message: ----------- - added asm and constantine .jar files to the eclipse build path - jarjar directory is not needed any more Modified Paths: -------------- trunk/jython/.classpath trunk/jython/build.xml Modified: trunk/jython/.classpath =================================================================== --- trunk/jython/.classpath 2009-01-07 04:02:38 UTC (rev 5862) +++ trunk/jython/.classpath 2009-01-07 07:35:05 UTC (rev 5863) @@ -12,8 +12,10 @@ <classpathentry kind="lib" path="extlibs/mysql-connector-java-5.1.6.jar"/> <classpathentry kind="lib" path="extlibs/postgresql-8.3-603.jdbc4.jar"/> <classpathentry kind="lib" path="extlibs/servlet-api-2.5.jar"/> - <classpathentry kind="lib" path="build/jarjar"/> <classpathentry kind="var" path="ANT_HOME/lib/ant.jar"/> <classpathentry kind="lib" path="extlibs/antlr-3.1.1-runtime.jar"/> + <classpathentry kind="lib" path="extlibs/asm-3.1.jar"/> + <classpathentry kind="lib" path="extlibs/asm-commons-3.1.jar"/> + <classpathentry kind="lib" path="extlibs/constantine-0.4.jar"/> <classpathentry kind="output" path="bugtests/classes"/> </classpath> Modified: trunk/jython/build.xml =================================================================== --- trunk/jython/build.xml 2009-01-07 04:02:38 UTC (rev 5862) +++ trunk/jython/build.xml 2009-01-07 07:35:05 UTC (rev 5863) @@ -135,7 +135,6 @@ <property name="extlibs.dir" value="${jython.base.dir}/extlibs" /> <property name="output.dir" value="${work.dir}/build" /> <property name="compile.dir" value="${output.dir}/classes" /> - <property name="jarjar.dir" value="${output.dir}/jarjar" /> <property name="exposed.dir" value="${output.dir}/exposed" /> <property name="gensrc.dir" value="${output.dir}/gensrc" /> <property name="dist.dir" value="${work.dir}/dist" /> @@ -144,7 +143,6 @@ <!-- classpaths --> <path id="main.classpath"> - <pathelement path="${jarjar.dir}" /> <pathelement path="${extlibs.dir}/libreadline-java-0.8.jar" /> <pathelement path="${extlibs.dir}/jline-0.9.94.jar" /> <pathelement path="${extlibs.dir}/servlet-api-2.5.jar" /> @@ -325,7 +323,6 @@ <!-- create output directories --> <target name ="prepare-output" depends="init,needed-check,clean-if-antlr-needed"> <mkdir dir="${compile.dir}" /> - <mkdir dir="${jarjar.dir}" /> <mkdir dir="${gensrc.dir}/org/python/antlr" /> <mkdir dir="${exposed.dir}" /> <mkdir dir="${dist.dir}" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2009-01-07 18:12:52
|
Revision: 5869 http://jython.svn.sourceforge.net/jython/?rev=5869&view=rev Author: cgroves Date: 2009-01-07 18:12:46 +0000 (Wed, 07 Jan 2009) Log Message: ----------- Use the path to the imported item as __file__, not the package path Modified Paths: -------------- trunk/jython/Lib/test/test_classpathimporter.py trunk/jython/src/org/python/core/ClasspathPyImporter.java trunk/jython/src/org/python/core/util/importer.java trunk/jython/src/org/python/modules/zipimport/zipimporter.java Modified: trunk/jython/Lib/test/test_classpathimporter.py =================================================================== --- trunk/jython/Lib/test/test_classpathimporter.py 2009-01-07 18:04:18 UTC (rev 5868) +++ trunk/jython/Lib/test/test_classpathimporter.py 2009-01-07 18:12:46 UTC (rev 5869) @@ -18,22 +18,24 @@ except KeyError: pass - def setClassLoaderAndCheck(self, jar): + def setClassLoaderAndCheck(self, jar, prefix): Thread.currentThread().contextClassLoader = test_support.make_jar_classloader(jar) import flat_in_jar self.assertEquals(flat_in_jar.value, 7) import jar_pkg + self.assertEquals(prefix + '/jar_pkg/__init__$py.class', jar_pkg.__file__) from jar_pkg import prefer_compiled + self.assertEquals(prefix + '/jar_pkg/prefer_compiled$py.class', prefer_compiled.__file__) self.assert_(prefer_compiled.compiled) self.assertRaises(NameError, __import__, 'flat_bad') self.assertRaises(NameError, __import__, 'jar_pkg.bad') def test_default_pyclasspath(self): - self.setClassLoaderAndCheck("classimport.jar") + self.setClassLoaderAndCheck("classimport.jar", "__pyclasspath__") def test_path_in_pyclasspath(self): sys.path = ['__pyclasspath__/Lib'] - self.setClassLoaderAndCheck("classimport_Lib.jar") + self.setClassLoaderAndCheck("classimport_Lib.jar", "__pyclasspath__/Lib") def test_main(): test_support.run_unittest(ClasspathImporterTestCase) Modified: trunk/jython/src/org/python/core/ClasspathPyImporter.java =================================================================== --- trunk/jython/src/org/python/core/ClasspathPyImporter.java 2009-01-07 18:04:18 UTC (rev 5868) +++ trunk/jython/src/org/python/core/ClasspathPyImporter.java 2009-01-07 18:12:46 UTC (rev 5869) @@ -110,6 +110,11 @@ } @Override + protected String makeFilePath(String fullname) { + return path + fullname.replace('.', '/'); + } + + @Override protected String makePackagePath(String fullname) { return path; } Modified: trunk/jython/src/org/python/core/util/importer.java =================================================================== --- trunk/jython/src/org/python/core/util/importer.java 2009-01-07 18:04:18 UTC (rev 5868) +++ trunk/jython/src/org/python/core/util/importer.java 2009-01-07 18:12:46 UTC (rev 5869) @@ -55,6 +55,12 @@ protected abstract String makeFilename(String fullname); /** + * Given a full module name, return the potential file path including the archive (without + * extension). + */ + protected abstract String makeFilePath(String fullname); + + /** * Returns an entry for a filename from makeFilename with a potential suffix such that this * importer can make a bundle with it, or null if fullFilename doesn't exist in this importer. */ @@ -156,7 +162,7 @@ */ protected final ModuleCodeData getModuleCode(String fullname) { String path = makeFilename(fullname); - String fullPath = makePackagePath(fullname); + String fullPath = makeFilePath(fullname); if (path.length() < 0) { return null; @@ -186,7 +192,7 @@ try { codeBytes = imp.readCode(fullname, bundle.inputStream, true); } catch (IOException ioe) { - throw Py.ImportError(ioe.getMessage() + "[path=" + fullPath + searchPath + "]"); + throw Py.ImportError(ioe.getMessage() + "[path=" + fullSearchPath + "]"); } } else { codeBytes = imp.compileSource(fullname, bundle.inputStream, fullSearchPath); Modified: trunk/jython/src/org/python/modules/zipimport/zipimporter.java =================================================================== --- trunk/jython/src/org/python/modules/zipimport/zipimporter.java 2009-01-07 18:04:18 UTC (rev 5868) +++ trunk/jython/src/org/python/modules/zipimport/zipimporter.java 2009-01-07 18:12:46 UTC (rev 5869) @@ -423,6 +423,11 @@ } @Override + protected String makeFilePath(String fullname) { + return makePackagePath(fullname); + } + + @Override protected PyObject makeEntry(String fullFilename) { return files.__finditem__(fullFilename); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-01-08 02:09:02
|
Revision: 5875 http://jython.svn.sourceforge.net/jython/?rev=5875&view=rev Author: fwierzbicki Date: 2009-01-08 02:08:58 +0000 (Thu, 08 Jan 2009) Log Message: ----------- Change to Raise and ExceptHandler so that Python sees the properly named "type" field, but the getters and setters are mangled to getExceptType so PyObject's getType method is not in conflict. Modified Paths: -------------- trunk/jython/ast/Python.asdl trunk/jython/ast/asdl_antlr.py trunk/jython/src/org/python/antlr/ast/ExceptHandler.java trunk/jython/src/org/python/antlr/ast/Raise.java trunk/jython/src/org/python/compiler/CodeCompiler.java Modified: trunk/jython/ast/Python.asdl =================================================================== --- trunk/jython/ast/Python.asdl 2009-01-08 00:34:35 UTC (rev 5874) +++ trunk/jython/ast/Python.asdl 2009-01-08 02:08:58 UTC (rev 5875) @@ -28,8 +28,7 @@ | With(expr context_expr, expr? optional_vars, stmt* body) -- 'type' is a bad name - -- changed to 'type' to excepttype for Jython - | Raise(expr? excepttype, expr? inst, expr? tback) + | Raise(expr? type, expr? inst, expr? tback) | TryExcept(stmt* body, excepthandler* handlers, stmt* orelse) | TryFinally(stmt* body, stmt* finalbody) | Assert(expr test, expr? msg) @@ -99,8 +98,7 @@ comprehension = (expr target, expr iter, expr* ifs) -- not sure what to call the first argument for raise and except - -- changed to 'type' to excepttype for Jython - excepthandler = ExceptHandler(expr? excepttype, expr? name, stmt* body) + excepthandler = ExceptHandler(expr? type, expr? name, stmt* body) attributes (int lineno, int col_offset) arguments = (expr* args, identifier? vararg, Modified: trunk/jython/ast/asdl_antlr.py =================================================================== --- trunk/jython/ast/asdl_antlr.py 2009-01-08 00:34:35 UTC (rev 5874) +++ trunk/jython/ast/asdl_antlr.py 2009-01-08 02:08:58 UTC (rev 5875) @@ -484,7 +484,7 @@ self.emit('{%s}, %s);' % (fpargs, len(fields)), depth + 2) i = 0 for f in fields: - self.emit("set%s(ap.getPyObject(%s));" % (str(f.name).capitalize(), + self.emit("set%s(ap.getPyObject(%s));" % (self.processFieldName(f.name), str(i)), depth+1) i += 1 if str(name) in ('stmt', 'expr', 'excepthandler'): @@ -506,8 +506,7 @@ fpargs = ", ".join(["PyObject %s" % f.name for f in fields]) self.emit("public %s(%s) {" % (clsname, fpargs), depth) for f in fields: - self.emit("set%s(%s);" % (str(f.name).capitalize(), - f.name), depth+1) + self.emit("set%s(%s);" % (self.processFieldName(f.name), f.name), depth+1) self.emit("}", depth) self.emit("", 0) @@ -539,6 +538,14 @@ self.emit("", 0) + #This is mainly a kludge to turn get/setType -> get/setExceptType because + #getType conflicts with a method on PyObject. + def processFieldName(self, name): + name = str(name).capitalize() + if name == "Type": + name = "ExceptType" + return name + def visitField(self, field, depth): self.emit("private %s;" % self.fieldDef(field), depth) self.emit("public %s getInternal%s() {" % (self.javaType(field), @@ -546,7 +553,7 @@ self.emit("return %s;" % field.name, depth+1) self.emit("}", depth) self.emit('@ExposedGet(name = "%s")' % field.name, depth) - self.emit("public PyObject get%s() {" % (str(field.name).capitalize()), depth) + self.emit("public PyObject get%s() {" % self.processFieldName(field.name), depth) if field.seq: self.emit("return new AstList(%s, AstAdapters.%sAdapter);" % (field.name, field.type), depth+1) else: @@ -567,8 +574,7 @@ #self.emit("return Py.None;", depth+1) self.emit("}", depth) self.emit('@ExposedSet(name = "%s")' % field.name, depth) - self.emit("public void set%s(PyObject %s) {" % (str(field.name).capitalize(), - field.name), depth) + self.emit("public void set%s(PyObject %s) {" % (self.processFieldName(field.name), field.name), depth) if field.seq: #self.emit("this.%s = new %s(" % (field.name, self.javaType(field)), depth+1) self.emit("this.%s = AstAdapters.py2%sList(%s);" % (field.name, str(field.type), field.name), depth+1) Modified: trunk/jython/src/org/python/antlr/ast/ExceptHandler.java =================================================================== --- trunk/jython/src/org/python/antlr/ast/ExceptHandler.java 2009-01-08 00:34:35 UTC (rev 5874) +++ trunk/jython/src/org/python/antlr/ast/ExceptHandler.java 2009-01-08 02:08:58 UTC (rev 5875) @@ -28,17 +28,17 @@ @ExposedType(name = "_ast.ExceptHandler", base = AST.class) public class ExceptHandler extends excepthandler { public static final PyType TYPE = PyType.fromClass(ExceptHandler.class); - private expr excepttype; - public expr getInternalExcepttype() { - return excepttype; + private expr type; + public expr getInternalType() { + return type; } - @ExposedGet(name = "excepttype") - public PyObject getExcepttype() { - return excepttype; + @ExposedGet(name = "type") + public PyObject getExceptType() { + return type; } - @ExposedSet(name = "excepttype") - public void setExcepttype(PyObject excepttype) { - this.excepttype = AstAdapters.py2expr(excepttype); + @ExposedSet(name = "type") + public void setExceptType(PyObject type) { + this.type = AstAdapters.py2expr(type); } private expr name; @@ -69,7 +69,7 @@ private final static PyString[] fields = - new PyString[] {new PyString("excepttype"), new PyString("name"), new PyString("body")}; + new PyString[] {new PyString("type"), new PyString("name"), new PyString("body")}; @ExposedGet(name = "_fields") public PyString[] get_fields() { return fields; } @@ -88,8 +88,8 @@ @ExposedMethod public void ExceptHandler___init__(PyObject[] args, String[] keywords) { ArgParser ap = new ArgParser("ExceptHandler", args, keywords, new String[] - {"excepttype", "name", "body", "lineno", "col_offset"}, 3); - setExcepttype(ap.getPyObject(0)); + {"type", "name", "body", "lineno", "col_offset"}, 3); + setExceptType(ap.getPyObject(0)); setName(ap.getPyObject(1)); setBody(ap.getPyObject(2)); int lin = ap.getInt(3, -1); @@ -104,16 +104,16 @@ } - public ExceptHandler(PyObject excepttype, PyObject name, PyObject body) { - setExcepttype(excepttype); + public ExceptHandler(PyObject type, PyObject name, PyObject body) { + setExceptType(type); setName(name); setBody(body); } - public ExceptHandler(Token token, expr excepttype, expr name, java.util.List<stmt> body) { + public ExceptHandler(Token token, expr type, expr name, java.util.List<stmt> body) { super(token); - this.excepttype = excepttype; - addChild(excepttype); + this.type = type; + addChild(type); this.name = name; addChild(name); this.body = body; @@ -125,11 +125,11 @@ } } - public ExceptHandler(Integer ttype, Token token, expr excepttype, expr name, - java.util.List<stmt> body) { + public ExceptHandler(Integer ttype, Token token, expr type, expr name, java.util.List<stmt> + body) { super(ttype, token); - this.excepttype = excepttype; - addChild(excepttype); + this.type = type; + addChild(type); this.name = name; addChild(name); this.body = body; @@ -141,10 +141,10 @@ } } - public ExceptHandler(PythonTree tree, expr excepttype, expr name, java.util.List<stmt> body) { + public ExceptHandler(PythonTree tree, expr type, expr name, java.util.List<stmt> body) { super(tree); - this.excepttype = excepttype; - addChild(excepttype); + this.type = type; + addChild(type); this.name = name; addChild(name); this.body = body; @@ -163,8 +163,8 @@ public String toStringTree() { StringBuffer sb = new StringBuffer("ExceptHandler("); - sb.append("excepttype="); - sb.append(dumpThis(excepttype)); + sb.append("type="); + sb.append(dumpThis(type)); sb.append(","); sb.append("name="); sb.append(dumpThis(name)); @@ -181,8 +181,8 @@ } public void traverse(VisitorIF visitor) throws Exception { - if (excepttype != null) - excepttype.accept(visitor); + if (type != null) + type.accept(visitor); if (name != null) name.accept(visitor); if (body != null) { Modified: trunk/jython/src/org/python/antlr/ast/Raise.java =================================================================== --- trunk/jython/src/org/python/antlr/ast/Raise.java 2009-01-08 00:34:35 UTC (rev 5874) +++ trunk/jython/src/org/python/antlr/ast/Raise.java 2009-01-08 02:08:58 UTC (rev 5875) @@ -28,17 +28,17 @@ @ExposedType(name = "_ast.Raise", base = AST.class) public class Raise extends stmt { public static final PyType TYPE = PyType.fromClass(Raise.class); - private expr excepttype; - public expr getInternalExcepttype() { - return excepttype; + private expr type; + public expr getInternalType() { + return type; } - @ExposedGet(name = "excepttype") - public PyObject getExcepttype() { - return excepttype; + @ExposedGet(name = "type") + public PyObject getExceptType() { + return type; } - @ExposedSet(name = "excepttype") - public void setExcepttype(PyObject excepttype) { - this.excepttype = AstAdapters.py2expr(excepttype); + @ExposedSet(name = "type") + public void setExceptType(PyObject type) { + this.type = AstAdapters.py2expr(type); } private expr inst; @@ -69,7 +69,7 @@ private final static PyString[] fields = - new PyString[] {new PyString("excepttype"), new PyString("inst"), new PyString("tback")}; + new PyString[] {new PyString("type"), new PyString("inst"), new PyString("tback")}; @ExposedGet(name = "_fields") public PyString[] get_fields() { return fields; } @@ -88,8 +88,8 @@ @ExposedMethod public void Raise___init__(PyObject[] args, String[] keywords) { ArgParser ap = new ArgParser("Raise", args, keywords, new String[] - {"excepttype", "inst", "tback", "lineno", "col_offset"}, 3); - setExcepttype(ap.getPyObject(0)); + {"type", "inst", "tback", "lineno", "col_offset"}, 3); + setExceptType(ap.getPyObject(0)); setInst(ap.getPyObject(1)); setTback(ap.getPyObject(2)); int lin = ap.getInt(3, -1); @@ -104,36 +104,36 @@ } - public Raise(PyObject excepttype, PyObject inst, PyObject tback) { - setExcepttype(excepttype); + public Raise(PyObject type, PyObject inst, PyObject tback) { + setExceptType(type); setInst(inst); setTback(tback); } - public Raise(Token token, expr excepttype, expr inst, expr tback) { + public Raise(Token token, expr type, expr inst, expr tback) { super(token); - this.excepttype = excepttype; - addChild(excepttype); + this.type = type; + addChild(type); this.inst = inst; addChild(inst); this.tback = tback; addChild(tback); } - public Raise(Integer ttype, Token token, expr excepttype, expr inst, expr tback) { + public Raise(Integer ttype, Token token, expr type, expr inst, expr tback) { super(ttype, token); - this.excepttype = excepttype; - addChild(excepttype); + this.type = type; + addChild(type); this.inst = inst; addChild(inst); this.tback = tback; addChild(tback); } - public Raise(PythonTree tree, expr excepttype, expr inst, expr tback) { + public Raise(PythonTree tree, expr type, expr inst, expr tback) { super(tree); - this.excepttype = excepttype; - addChild(excepttype); + this.type = type; + addChild(type); this.inst = inst; addChild(inst); this.tback = tback; @@ -147,8 +147,8 @@ public String toStringTree() { StringBuffer sb = new StringBuffer("Raise("); - sb.append("excepttype="); - sb.append(dumpThis(excepttype)); + sb.append("type="); + sb.append(dumpThis(type)); sb.append(","); sb.append("inst="); sb.append(dumpThis(inst)); @@ -165,8 +165,8 @@ } public void traverse(VisitorIF visitor) throws Exception { - if (excepttype != null) - excepttype.accept(visitor); + if (type != null) + type.accept(visitor); if (inst != null) inst.accept(visitor); if (tback != null) Modified: trunk/jython/src/org/python/compiler/CodeCompiler.java =================================================================== --- trunk/jython/src/org/python/compiler/CodeCompiler.java 2009-01-08 00:34:35 UTC (rev 5874) +++ trunk/jython/src/org/python/compiler/CodeCompiler.java 2009-01-08 02:08:58 UTC (rev 5875) @@ -751,10 +751,10 @@ @Override public Object visitRaise(Raise node) throws Exception { setline(node); - if (node.getInternalExcepttype() != null) { visit(node.getInternalExcepttype()); stackProduce(); } + if (node.getInternalType() != null) { visit(node.getInternalType()); stackProduce(); } if (node.getInternalInst() != null) { visit(node.getInternalInst()); stackProduce(); } if (node.getInternalTback() != null) { visit(node.getInternalTback()); stackProduce(); } - if (node.getInternalExcepttype() == null) { + if (node.getInternalType() == null) { code.invokestatic("org/python/core/Py", "makeException", "()" + $pyExc); } else if (node.getInternalInst() == null) { stackConsume(); @@ -1117,10 +1117,10 @@ //setline(name); Label end_of_self = new Label(); - if (handler.getInternalExcepttype() != null) { + if (handler.getInternalType() != null) { code.aload(exc); //get specific exception - visit(handler.getInternalExcepttype()); + visit(handler.getInternalType()); code.invokestatic("org/python/core/Py", "matchException", "(" + $pyExc + $pyObj + ")Z"); code.ifeq(end_of_self); } else { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-01-08 21:23:34
|
Revision: 5889 http://jython.svn.sourceforge.net/jython/?rev=5889&view=rev Author: pjenvey Date: 2009-01-08 21:23:29 +0000 (Thu, 08 Jan 2009) Log Message: ----------- jython-elementtree from: http://jython-elementtree.googlecode.com/svn/trunk@69 provides a partial emulation of pyexpat via xerces 2.9.1 and extra tests in test_xml_etree_jy from Sebastien Boisgerault Modified Paths: -------------- trunk/jython/CPythonLib.includes trunk/jython/Lib/test/regrtest.py trunk/jython/Lib/test/test_xml_etree.py trunk/jython/Lib/test/test_xml_etree_c.py Added Paths: ----------- trunk/jython/Lib/pyexpat.py trunk/jython/Lib/test/test_xml_etree_jy.py trunk/jython/Lib/xml/parsers/ trunk/jython/Lib/xml/parsers/__init__.py trunk/jython/Lib/xml/parsers/expat.py Modified: trunk/jython/CPythonLib.includes =================================================================== --- trunk/jython/CPythonLib.includes 2009-01-08 21:14:30 UTC (rev 5888) +++ trunk/jython/CPythonLib.includes 2009-01-08 21:23:29 UTC (rev 5889) @@ -8,6 +8,7 @@ encodings/** logging/* test/** +xml/etree/** # Lib files, in alphabetical order: __future__.py Added: trunk/jython/Lib/pyexpat.py =================================================================== --- trunk/jython/Lib/pyexpat.py (rev 0) +++ trunk/jython/Lib/pyexpat.py 2009-01-08 21:23:29 UTC (rev 5889) @@ -0,0 +1 @@ +from xml.parsers.expat import * Modified: trunk/jython/Lib/test/regrtest.py =================================================================== --- trunk/jython/Lib/test/regrtest.py 2009-01-08 21:14:30 UTC (rev 5888) +++ trunk/jython/Lib/test/regrtest.py 2009-01-08 21:23:29 UTC (rev 5889) @@ -1449,7 +1449,6 @@ test_wave test_winreg test_winsound - test_xml_etree_c test_zipfile64 """ } @@ -1487,7 +1486,6 @@ test_ucn test_unicode test_unicodedata - test_xml_etree test_zipimport """, } Modified: trunk/jython/Lib/test/test_xml_etree.py =================================================================== --- trunk/jython/Lib/test/test_xml_etree.py 2009-01-08 21:14:30 UTC (rev 5888) +++ trunk/jython/Lib/test/test_xml_etree.py 2009-01-08 21:23:29 UTC (rev 5889) @@ -209,7 +209,7 @@ >>> check_encoding(ET, "iso-8859-1") >>> check_encoding(ET, "iso-8859-15") >>> check_encoding(ET, "cp437") - >>> check_encoding(ET, "mac-roman") + >>> #check_encoding(ET, "mac-roman") """ ET.XML("<?xml version='1.0' encoding='%s'?><xml />" % encoding) Modified: trunk/jython/Lib/test/test_xml_etree_c.py =================================================================== --- trunk/jython/Lib/test/test_xml_etree_c.py 2009-01-08 21:14:30 UTC (rev 5888) +++ trunk/jython/Lib/test/test_xml_etree_c.py 2009-01-08 21:23:29 UTC (rev 5889) @@ -198,7 +198,7 @@ >>> check_encoding("iso-8859-1") >>> check_encoding("iso-8859-15") >>> check_encoding("cp437") - >>> check_encoding("mac-roman") + >>> #check_encoding("mac-roman") """ ET.XML( "<?xml version='1.0' encoding='%s'?><xml />" % encoding Added: trunk/jython/Lib/test/test_xml_etree_jy.py =================================================================== --- trunk/jython/Lib/test/test_xml_etree_jy.py (rev 0) +++ trunk/jython/Lib/test/test_xml_etree_jy.py 2009-01-08 21:23:29 UTC (rev 5889) @@ -0,0 +1,754 @@ +# encoding: utf-8 + +import sys +JYTHON = sys.platform.startswith("java") + +import doctest + +import xml.parsers.expat as expat +from xml.etree.ElementTree import * + +def jython(function): + if JYTHON: + return function + else: + return None + +class sortdict(dict): + def __repr__(self): + items = self.items() + items.sort() + pairs = ["%r: %r" % pair for pair in items] + return "{%s}" % ", ".join(pairs) + __str__ = __repr__ + + +class Outputter: + def StartElementHandler(self, name, attrs): + print 'Start element:\n ', repr(name), sortdict(attrs) + + def EndElementHandler(self, name): + print 'End element:\n ', repr(name) + + def CharacterDataHandler(self, data): + data = data.strip() + if data: + print 'Character data:' + print ' ', repr(data) + + def ProcessingInstructionHandler(self, target, data): + print 'PI:\n ', repr(target), repr(data) + + def StartNamespaceDeclHandler(self, prefix, uri): + print 'NS decl:\n ', repr(prefix), repr(uri) + + def EndNamespaceDeclHandler(self, prefix): + print 'End of NS decl:\n ', repr(prefix) + + def StartCdataSectionHandler(self): + print 'Start of CDATA section' + + def EndCdataSectionHandler(self): + print 'End of CDATA section' + + def CommentHandler(self, text): + print 'Comment:\n ', repr(text) + + def NotationDeclHandler(self, *args): + name, base, sysid, pubid = args + print 'Notation declared:', args + + def UnparsedEntityDeclHandler(self, *args): + entityName, base, systemId, publicId, notationName = args + print 'Unparsed entity decl:\n ', args + + def NotStandaloneHandler(self, userData): + print 'Not standalone' + return 1 + + def ExternalEntityRefHandler(self, *args): + context, base, sysId, pubId = args + print 'External entity ref:', args[1:] + return 1 + + def DefaultHandler(self, userData): + pass + + def DefaultHandlerExpand(self, userData): + pass + +_= """ + >>> data = '''\ + ... <?xml version="1.0" encoding="iso-8859-1" standalone="no"?> + ... <?xml-stylesheet href="stylesheet.css"?> + ... <!-- comment data --> + ... <!DOCTYPE quotations SYSTEM "quotations.dtd" [ + ... <!ELEMENT root ANY> + ... <!NOTATION notation SYSTEM "notation.jpeg"> + ... <!ENTITY acirc "â"> + ... <!ENTITY external_entity SYSTEM "entity.file"> + ... <!ENTITY unparsed_entity SYSTEM "entity.file" NDATA notation> + ... %unparsed_entity; + ... ]> + ... + ... <root attr1="value1" attr2="value2ὀ"> + ... <myns:subelement xmlns:myns="http://www.python.org/namespace"> + ... Contents of subelements + ... </myns:subelement> + ... <sub2><![CDATA[contents of CDATA section]]></sub2> + ... &external_entity; + ... </root> + ... ''' + """ + +def test_utf8(): + """ + Source: test_pyexpat.py + Changes: replaced tabs with spaces in Outputter to ease doctest integration + + >>> out = Outputter() + >>> parser = expat.ParserCreate(namespace_separator='!') + >>> HANDLER_NAMES = [ + ... 'StartElementHandler', 'EndElementHandler', + ... 'CharacterDataHandler', + ... 'ProcessingInstructionHandler', + ... 'UnparsedEntityDeclHandler', 'NotationDeclHandler', + ... 'StartNamespaceDeclHandler', 'EndNamespaceDeclHandler', + ... 'CommentHandler', 'StartCdataSectionHandler', + ... 'EndCdataSectionHandler', + ... 'DefaultHandler', 'DefaultHandlerExpand', + ... #'NotStandaloneHandler', + ... 'ExternalEntityRefHandler' + ... ] + >>> for name in HANDLER_NAMES: + ... setattr(parser, name, getattr(out, name)) + + >>> data = '''\\ + ... <?xml version="1.0" encoding="iso-8859-1" standalone="no"?> + ... <?xml-stylesheet href="stylesheet.css"?> + ... <!-- comment data --> + ... <!DOCTYPE quotations SYSTEM "quotations.dtd" [ + ... <!ELEMENT root ANY> + ... <!NOTATION notation SYSTEM "notation.jpeg"> + ... <!ENTITY acirc "â"> + ... <!ENTITY external_entity SYSTEM "entity.file"> + ... <!ENTITY unparsed_entity SYSTEM "entity.file" NDATA notation> + ... %unparsed_entity; + ... ]> + ... + ... <root attr1="value1" attr2="value2ὀ"> + ... <myns:subelement xmlns:myns="http://www.python.org/namespace"> + ... Contents of subelements + ... </myns:subelement> + ... <sub2><![CDATA[contents of CDATA section]]></sub2> + ... &external_entity; + ... </root> + ... ''' + + #Produce UTF-8 output + #>>> parser.returns_unicode = 0 + #>>> try: + #... parser.Parse(data, 1) + #... except expat.error: + #... print '** Error', parser.ErrorCode, expat.ErrorString(parser.ErrorCode) + #... print '** Line', parser.ErrorLineNumber + #... print '** Column', parser.ErrorColumnNumber + #... print '** Byte', parser.ErrorByteIndex + #PI: + #'xml-stylesheet' 'href="stylesheet.css"' + #Comment: + #' comment data ' + #Notation declared: ('notation', None, 'notation.jpeg', None) + #Unparsed entity decl: + #('unparsed_entity', None, 'entity.file', None, 'notation') + #Start element: + #'root' {'attr1': 'value1', 'attr2': 'value2\\xe1\\xbd\\x80'} + #NS decl: + #'myns' 'http://www.python.org/namespace' + #Start element: + #'http://www.python.org/namespace!subelement' {} + #Character data: + #'Contents of subelements' + #End element: + #'http://www.python.org/namespace!subelement' + #End of NS decl: + #'myns' + #Start element: + #'sub2' {} + #Start of CDATA section + #Character data: + #'contents of CDATA section' + #End of CDATA section + #End element: + #'sub2' + #External entity ref: (None, 'entity.file', None) + #End element: + #'root' + #1 + + >>> parser = expat.ParserCreate(namespace_separator='!') + >>> parser.returns_unicode = 1 + >>> for name in HANDLER_NAMES: + ... setattr(parser, name, getattr(out, name)) + >>> try: + ... parser.Parse(data, 1) + ... except expat.error: + ... print '** Line', parser.ErrorLineNumber + ... print '** Column', parser.ErrorColumnNumber + ... print '** Byte', parser.ErrorByteIndex #doctest: +REPORT_UDIFF + PI: + u'xml-stylesheet' u'href="stylesheet.css"' + Comment: + u' comment data ' + Notation declared: (u'notation', None, u'notation.jpeg', None) + Unparsed entity decl: + (u'unparsed_entity', None, u'entity.file', None, u'notation') + Start element: + u'root' {u'attr1': u'value1', u'attr2': u'value2\u1f40'} + NS decl: + u'myns' u'http://www.python.org/namespace' + Start element: + u'http://www.python.org/namespace!subelement' {} + Character data: + u'Contents of subelements' + End element: + u'http://www.python.org/namespace!subelement' + End of NS decl: + u'myns' + Start element: + u'sub2' {} + Start of CDATA section + Character data: + u'contents of CDATA section' + End of CDATA section + End element: + u'sub2' + External entity ref: (None, u'entity.file', None) + End element: + u'root' + 1 + """ + + +def test_import_as_pyexpat(): + """ + >>> import pyexpat as expat + >>> expat #doctest: +ELLIPSIS + <module 'pyexpat' from ...> + """ + + +def test_errors_submodule(): + """ + >>> import xml.parsers.expat as expat + >>> expat.errors + <module 'pyexpat.errors' (built-in)> + >>> dir(expat.errors) #doctest: +ELLIPSIS + ['XML_ERROR_ABORTED', ..., 'XML_ERROR_XML_DECL', '__doc__', '__name__'] + >>> expat.errors.XML_ERROR_ABORTED + 'parsing aborted' + >>> expat.errors.XML_ERROR_XML_DECL + 'XML declaration not well-formed' + """ + +def test_model_submodule(): + """ + >>> import xml.parsers.expat as expat + >>> expat.model + <module 'pyexpat.model' (built-in)> + >>> print sortdict(expat.model.__dict__) + {'XML_CQUANT_NONE': 0, 'XML_CQUANT_OPT': 1, 'XML_CQUANT_PLUS': 3, 'XML_CQUANT_REP': 2, 'XML_CTYPE_ANY': 2, 'XML_CTYPE_CHOICE': 5, 'XML_CTYPE_EMPTY': 1, 'XML_CTYPE_MIXED': 3, 'XML_CTYPE_NAME': 4, 'XML_CTYPE_SEQ': 6, '__doc__': 'Constants used to interpret content model information.', '__name__': 'pyexpat.model'} + """ + +def test_parse_only_xml_data(): + """ + Source: test_pyexpat.py, see also: http://python.org/sf/1296433 + Changes: + - replaced 'iso8859' encoding with 'ISO-8859-1', + - added isfinal=True keyword argument to Parse call (as in this port, + the data is not processed until it is fully available). + With these changes, the test still crashes CPython 2.5. + + >>> import xml.parsers.expat as expat + >>> # xml = "<?xml version='1.0' encoding='iso8859'?><s>%s</s>" % ('a' * 1025) + + This one doesn't crash: + >>> xml = "<?xml version='1.0'?><s>%s</s>" % ('a' * 10000) + + >>> def handler(text): + ... raise Exception + >>> parser = expat.ParserCreate() + >>> parser.CharacterDataHandler = handler + >>> try: + ... parser.Parse(xml, True) + ... except: + ... pass + """ + + +def test_namespace_separator(): + """ + Source: test_pyexpat.py + + Tests that make sure we get errors when the namespace_separator value + is illegal, and that we don't for good values: + + >>> from xml.parsers.expat import ParserCreate + + >>> p = ParserCreate() + >>> p = ParserCreate(namespace_separator=None) + >>> p = ParserCreate(namespace_separator=' ') + >>> p = ParserCreate(namespace_separator=42) #doctest: +ELLIPSIS + Traceback (most recent call last): + ... + TypeError: ... + >>> p = ParserCreate(namespace_separator='too long') #doctest: +ELLIPSIS + Traceback (most recent call last): + ... + ValueError: ... + + ParserCreate() needs to accept a namespace_separator of zero length + to satisfy the requirements of RDF applications that are required + to simply glue together the namespace URI and the localname. Though + considered a wart of the RDF specifications, it needs to be supported. + + See XML-SIG mailing list thread starting with + http://mail.python.org/pipermail/xml-sig/2001-April/005202.html + + >>> p = ParserCreate(namespace_separator='') # too short +""" + + +def test_interning_machinery(): + """ + Source: test_pyexpat.py + + >>> from xml.parsers.expat import ParserCreate + + >>> p = ParserCreate() + >>> L = [] + >>> def collector(name, *args): + ... L.append(name) + >>> p.StartElementHandler = collector + >>> p.EndElementHandler = collector + >>> p.Parse("<e> <e/> <e></e> </e>", 1) + 1 + >>> tag = L[0] + >>> len(L) + 6 + >>> all(tag is entry for entry in L) + True + """ + + +def test_exception_from_callback(): + """ + Source: test_pyexpat.py + + >>> from xml.parsers.expat import ParserCreate + + >>> def StartElementHandler(name, attrs): + ... raise RuntimeError(name) + + >>> parser = ParserCreate() + >>> parser.StartElementHandler = StartElementHandler + >>> try: + ... parser.Parse("<a><b><c/></b></a>", 1) + ... except RuntimeError, e: + ... pass + >>> e.args[0] == "a" + True + """ + + +def test_with_and_without_namespace(): + """ + >>> from xml.parsers.expat import ParserCreate + + >>> xml = '''<root + ... xmlns="http://www.python.org" + ... xmlns:python="http://www.python.org" + ... python:a="1" b="2"> + ... <python:sub1/> + ... <sub2 xmlns=""/> + ... </root>''' + >>> def handler(name, attributes): + ... attributes = sorted(attributes.items()) + ... print name + ... for attr in attributes: + ... print " %s = %r" % attr + + >>> parser = ParserCreate() + >>> parser.StartElementHandler = handler + >>> _ = parser.Parse(xml, True) + root + b = u'2' + python:a = u'1' + xmlns = u'http://www.python.org' + xmlns:python = u'http://www.python.org' + python:sub1 + sub2 + xmlns = u'' + + >>> parser = ParserCreate(namespace_separator="|") + >>> parser.StartElementHandler = handler + >>> _ = parser.Parse(xml, True) + http://www.python.org|root + b = u'2' + http://www.python.org|a = u'1' + http://www.python.org|sub1 + sub2 + """ + +def test_unicode_bug(): + """ + Regression introduced by revision 28 + + >>> doc = XML("<doc>舰</doc>") + >>> doc.text + u'\u8230' + """ + +def test_DTD(): + """ + >>> xml = '''<!DOCTYPE doc [ + ... <!ELEMENT doc (any|empty|text|mixed|opt|many|plus)> + ... <!ELEMENT any ANY> + ... <!ELEMENT empty EMPTY> + ... <!ELEMENT text (#PCDATA)> + ... <!ELEMENT sequence (_sequence)> + ... <!ELEMENT _sequence (any,any)> + ... <!ELEMENT mixed (#PCDATA|any)*> + ... <!ELEMENT opt (empty)?> + ... <!ELEMENT many (empty)*> + ... <!ELEMENT plus (empty)+> + ... ]> + ... <doc><text>content</text></doc> + ... ''' + >>> parser = expat.ParserCreate() + >>> def handler(header, *args): + ... def _handler(*args): + ... print header + ":", args + ... return _handler + >>> parser.ElementDeclHandler = handler("ELEMENT") + >>> parser.AttlistDeclHandler = handler("ATTRIBUTE") + >>> parser.EntityDeclHandler = handler("ENTITY") + >>> parser.NotationDeclHandler = handler("NOTATION") + >>> parser.UnparsedEntityDeclHandler = handler("UNPARSED") + >>> parser.Parse(xml, True) + ELEMENT: (u'doc', (5, 0, None, ((4, 0, u'any', ()), (4, 0, u'empty', ()), (4, 0, u'text', ()), (4, 0, u'mixed', ()), (4, 0, u'opt', ()), (4, 0, u'many', ()), (4, 0, u'plus', ())))) + ELEMENT: (u'any', (2, 0, None, ())) + ELEMENT: (u'empty', (1, 0, None, ())) + ELEMENT: (u'text', (3, 0, None, ())) + ELEMENT: (u'sequence', (6, 0, None, ((4, 0, u'_sequence', ()),))) + ELEMENT: (u'_sequence', (6, 0, None, ((4, 0, u'any', ()), (4, 0, u'any', ())))) + ELEMENT: (u'mixed', (3, 2, None, ((4, 0, u'any', ()),))) + ELEMENT: (u'opt', (6, 1, None, ((4, 0, u'empty', ()),))) + ELEMENT: (u'many', (6, 2, None, ((4, 0, u'empty', ()),))) + ELEMENT: (u'plus', (6, 3, None, ((4, 0, u'empty', ()),))) + 1 + """ + +def test_entity(): + """ + + TODO: need a fallback for entity-resolver so that empty source is returned. + + >>> xml = ''' <!DOCTYPE doc SYSTEM "external.dtd" [ + ... <!ENTITY ext-entity SYSTEM "external-entity"> + ... ]> + ... <doc>&ext-entity;&in-ext-dtd-entity;</doc>''' + >>> parser = expat.ParserCreate() + >>> parser.Parse(xml, True) + 1 + + EXPAT OH MY ! When applicable (internal entities), the CharacterDataHandler + callback will override DefaultHandlerExpand, but it WON'T override + DefaultHandler. On the other hand, the DefaultHandlerExpand callback WILL + override DefaultHandler ... More tests todo here ... + + >>> xml = '''<!DOCTYPE doc SYSTEM "external.dtd" [ + ... <!ENTITY ext-entity SYSTEM "external-entity"> + ... <!ENTITY int-entity "internal"> + ... ]> + ... <doc>&int-entity;&ext-entity;&in-ext-dtd-entity;</doc>''' + >>> parser = expat.ParserCreate() + >>> def handler(header): + ... def _handler(*args): + ... print header + ":", args + ... return 1 + ... return _handler + >>> parser.CharacterDataHandler = handler("text") + >>> parser.DefaultHandler = handler("default") + >>> parser.Parse(xml, True) #doctest: +ELLIPSIS + default: ... + default: (u'&int-entity;',) + default: (u'&ext-entity;',) + default: (u'&in-ext-dtd-entity;',) + ... + 1 + + EXPAT OH MY ! When applicable (internal entities), the CharacterDataHandler + callback will override DefaultHandlerExpand, but it WON'T override + DefaultHandler. On the other hand, the DefaultHandlerExpand callback WILL + override DefaultHandler ... More tests todo here ... + """ + +def test_resolve_entity_handlers(): + """ + >>> xml = '''<!DOCTYPE doc [ + ... <!ENTITY entity SYSTEM "entity"> + ... ]> + ... <doc>&entity;</doc>''' + >>> def handler(header): + ... def _handler(*args): + ... print header + ":", args + ... return 1 + ... return _handler + + >>> parser = expat.ParserCreate() + >>> parser.ExternalEntityRefHandler = handler("ExternalEntityRefHandler") + >>> parser.Parse(xml, True) + ExternalEntityRefHandler: (u'entity', None, u'entity', None) + 1 + """ + +def handler(name, header="XML>", returns=None): + def _handler(*args): + if len(args) == 1: + args = "(%r)" % args[0] + else: + args = str(args) + print header, name + "%s" % args + return returns + return _handler + +def parse(xml, *handlers): + parser = expat.ParserCreate() + for name in handlers: + if name == "ExternalEntityRefHandler": + returns = 1 + else: + returns = None + setattr(parser, name, handler(name, returns=returns)) + parser.Parse(xml, True) + +def test_internal_entities(): + """ + >>> xml = '''<!DOCTYPE doc [ + ... <!ENTITY entity "entity-content"> + ... ]> + ... <doc>&entity;</doc>''' + + >>> parse(xml) + + >>> parse(xml, "CharacterDataHandler") + XML> CharacterDataHandler(u'entity-content') + + >>> parse(xml, "DefaultHandler") #doctest: +ELLIPSIS + XML> ...DefaultHandler(u'&entity;')... + + >>> parse(xml, "DefaultHandlerExpand") #doctest: +ELLIPSIS + XML> ...DefaultHandlerExpand(u'entity-content')... + + # Uhu ? + >>> parse(xml, "CharacterDataHandler", + ... "DefaultHandler") #doctest: +ELLIPSIS + XML> ...DefaultHandler(u'&entity;')... + + >>> parse(xml, "CharacterDataHandler", + ... "DefaultHandlerExpand") #doctest: +ELLIPSIS + XML> ...CharacterDataHandler(u'entity-content')... + + >>> parse(xml, "DefaultHandler", + ... "DefaultHandlerExpand") #doctest: +ELLIPSIS + XML> ...DefaultHandlerExpand(u'entity-content')... + + >>> parse(xml, "CharacterDataHandler", + ... "DefaultHandler", + ... "DefaultHandlerExpand") #doctest: +ELLIPSIS + XML> ...CharacterDataHandler(u'entity-content')... + """ + +def test_external_entities(): + """ + >>> xml = '''<!DOCTYPE doc [ + ... <!ENTITY entity PUBLIC "http://entity-web" "entity-file"> + ... ]> + ... <doc>&entity;</doc>''' + + >>> parse(xml) + + >>> parse(xml, "ExternalEntityRefHandler") + XML> ExternalEntityRefHandler(u'entity', None, u'entity-file', u'http://entity-web') + + >>> parse(xml, "DefaultHandler") #doctest: +ELLIPSIS + XML> ...DefaultHandler(u'&entity;')... + + >>> parse(xml, "DefaultHandlerExpand") #doctest: +ELLIPSIS + XML> ...DefaultHandlerExpand(u'&entity;')... + + >>> parse(xml, "ExternalEntityRefHandler", + ... "DefaultHandler") #doctest: +ELLIPSIS + XML> ...ExternalEntityRefHandler(u'entity', None, u'entity-file', u'http://entity-web')... + + >>> parse(xml, "ExternalEntityRefHandler", + ... "DefaultHandlerExpand") #doctest: +ELLIPSIS + XML> ...ExternalEntityRefHandler(u'entity', None, u'entity-file', u'http://entity-web')... + + >>> parse(xml, "DefaultHandler", + ... "DefaultHandlerExpand") #doctest: +ELLIPSIS + XML> ...DefaultHandlerExpand(u'&entity;')... + + >>> parse(xml, "ExternalEntityRefHandler", + ... "DefaultHandler", + ... "DefaultHandlerExpand") #doctest: +ELLIPSIS + XML> ...ExternalEntityRefHandler(u'entity', None, u'entity-file', u'http://entity-web')... + """ + +def test_undefined_entities(): + """ + >>> xml = "<doc>&entity;</doc>" + >>> parse(xml) + Traceback (most recent call last): + ... + ExpatError: undefined entity: line 1, column 5 + """ + +def locate(parser, name): + def _handler(*args): + print name, parser.CurrentLineNumber, parser.CurrentColumnNumber + return _handler + +def test_current_location(): + """ + >>> xml = '''<doc>text<tag/>text<tag></tag> + ... <tag></tag> + ... text<tag/> + ... </doc>''' + >>> parser = expat.ParserCreate() + >>> parser.CharacterDataHandler = locate(parser, "TEXT:") + >>> parser.StartElementHandler = locate(parser, "START:") + >>> parser.EndElementHandler = locate(parser, "END:") + >>> _ = parser.Parse(xml, True) #doctest: +ELLIPSIS + START: 1 0 + TEXT: 1 5... + START: 1 9 + END: 1 15 + TEXT: 1 15... + START: 1 19 + END: 1 24 + TEXT: 1 30... + START: 2 0 + END: 2 5 + TEXT: 2 11... + START: 3 4 + END: 3 10 + TEXT: 3 10... + END: 4 0 + + >>> xml = '''<doc> + ... start tag after some text<tag/> + ... <elt></elt><tag/> + ... <elt/><tag/> + ... </doc>''' + >>> parser = expat.ParserCreate() + >>> parser.CharacterDataHandler = locate(parser, "TEXT:") + >>> parser.StartElementHandler = locate(parser, "START:") + >>> parser.EndElementHandler = locate(parser, "END:") + >>> _ = parser.Parse(xml, True) #doctest: +ELLIPSIS + START: 1 0 + TEXT: 1 5... + START: 2 25 + END: 2 31 + TEXT: 2 31... + START: 3 0 + END: 3 5 + START: 3 11 + END: 3 17 + TEXT: 3 17... + START: 4 0 + END: 4 6 + START: 4 6 + END: 4 12 + TEXT: 4 12... + END: 5 0 + """ + + +def test_error_location(): + """ + Source: selftest.py, ElementTree 1.3a3 + Changes: removed dependencies in ElementTree, added one extra test + + >>> def error(xml): + ... p = expat.ParserCreate() + ... try: + ... p.Parse(xml, True) + ... except expat.ExpatError, e: + ... return e.lineno, e.offset + + >>> error("foo") + (1, 0) + >>> error("<tag>&foo;</tag>") + (1, 5) + >>> error("foobar<") + (1, 6) + >>> error("<doc>text<doc") + (1, 9) + """ + +@jython +def test_resolveEntity(): + """ + # TODO: test that 'skipEntity' works. + + >>> # Jython + >>> from org.python.core.util import StringUtil + >>> from jarray import array + + >>> # Java Standard Edition + >>> from org.xml.sax import * + >>> from org.xml.sax.ext import * + >>> from org.xml.sax.helpers import * + >>> from java.io import ByteArrayInputStream + + >>> xml = '''<!DOCTYPE doc + ... [<!ENTITY entity SYSTEM "entity-file"> + ... ]> + ... <doc>&entity;</doc> + ... ''' + + >>> def empty_source(): + ... _source = InputSource() + ... byte_stream = ByteArrayInputStream(array([], "b")) + ... _source.setByteStream(byte_stream) + ... return _source + + >>> class Handler(EntityResolver2): + ... def getExternalSubset(self, name, baseURI): + ... return None + ... def resolveEntity(self, name, publicId, baseURI, systemId): + ... print "Entity name:", name + ... return empty_source() + + >>> def main(): + ... sax_parser = "org.apache.xerces.parsers.SAXParser" + ... reader = XMLReaderFactory.createXMLReader(sax_parser) + ... entity_resolver2 = "http://xml.org/sax/features/use-entity-resolver2" + ... enabled = reader.getFeature(entity_resolver2) + ... print "Entity-Resolver2 enabled:", enabled + ... handler = Handler() + ... reader.setEntityResolver(handler) + ... bytes = StringUtil.toBytes(xml) + ... byte_stream = ByteArrayInputStream(bytes) + ... source = InputSource(byte_stream) + ... reader.parse(source) + + >>> main() + Entity-Resolver2 enabled: True + Entity name: entity + """ + +if __name__ == "__main__": + doctest.testmod() Property changes on: trunk/jython/Lib/test/test_xml_etree_jy.py ___________________________________________________________________ Added: svn:executable + * Added: trunk/jython/Lib/xml/parsers/expat.py =================================================================== --- trunk/jython/Lib/xml/parsers/expat.py (rev 0) +++ trunk/jython/Lib/xml/parsers/expat.py 2009-01-08 21:23:29 UTC (rev 5889) @@ -0,0 +1,610 @@ + +# coding: utf-8 + +#------------------------------------------------------------------------------ +# Copyright (c) 2008 Sébastien Boisgérault +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# ----------------------------------------------------------------------------- + +__all__ = ["ParserCreate", "XMLParserType", "ExpatError", "error", "errors"] + +# Jython check +import sys +if not sys.platform.startswith('java'): + raise ImportError("this version of expat requires the jython interpreter") + +# Standard Python Library +import re +import types + +# Jython +from org.python.core import Py +from org.python.core.util import StringUtil +from jarray import array + +# Java Standard Edition +from java.io import ByteArrayInputStream +from java.lang import String, StringBuilder +from org.xml.sax import InputSource +from org.xml.sax import SAXNotRecognizedException, SAXParseException +from org.xml.sax.helpers import DefaultHandler, XMLReaderFactory +from org.xml.sax.ext import DefaultHandler2 + + +def ParserCreate(encoding=None, namespace_separator=None): + return XMLParser(encoding, namespace_separator) + + +class XMLParser(object): + + def __init__(self, encoding, namespace_separator): + self.encoding = encoding + self.CurrentLineNumber = 1 + self.CurrentColumnNumber = 0 + self._NextLineNumber = 1 + self._NextColumnNumber = 0 + self.ErrorLineNumber = -1 + self.ErrorColumnNumber = -1 + self.ErrorCode = None + + if namespace_separator is None: + self.namespace_separator = namespace_separator + elif isinstance(namespace_separator, basestring): + self.namespace_separator = str(namespace_separator) + if len(self.namespace_separator) > 1: + error = ("namespace_separator must be at most one character, " + "omitted, or None") + raise ValueError(error) + else: + error = ("ParserCreate() argument 2 must be string or None, " + "not %s" % type(namespace_separator).__name__) + raise TypeError(error) + + XMLReader = XMLReaderFactory.createXMLReader + xerces_parser = "org.apache.xerces.parsers.SAXParser" + self._reader = XMLReader(xerces_parser) + + if self.namespace_separator is None: + try: + feature = "http://xml.org/sax/features/namespaces" + self._reader.setFeature(feature, False) + except SAXNotRecognizedException: + error = ("namespace support cannot be disabled; " + "set namespace_separator to a string of length 1.") + raise ValueError(error) + + self._base = None + self._buffer_text = True + self._returns_unicode = True + + self._data = StringBuilder() + + self._handler = XMLEventHandler(self) + self._reader.setContentHandler(self._handler) + self._reader.setErrorHandler(self._handler) + self._reader.setDTDHandler(self._handler) + self._reader.setEntityResolver(self._handler) + + sax_properties = ("lexical-handler", "declaration-handler") + for name in sax_properties: + try: + name = "http://xml.org/sax/properties/" + name + self._reader.setProperty(name, self._handler) + except SAXNotRecognizedException: + error = "can't set property %r" % name + raise NotImplementedError(error) + + apache_features = (("nonvalidating/load-external-dtd", False),) + for name, value in apache_features: + try: + name = "http://apache.org/xml/features/" + name + self._reader.setFeature(name, value) + except SAXNotRecognizedException: + error = "can't set feature %r" % name + raise NotImplementedError(error) + + # experimental + # f = "http://xml.org/sax/features/external-general-entities" + f = "http://xml.org/sax/features/external-parameter-entities" + # self._reader.setFeature(f, False) + + # check + f = "http://xml.org/sax/features/use-entity-resolver2" + assert self._reader.getFeature(f) is True + + + def GetBase(self): + return self._base + + def SetBase(self, base): + self._base = base + + def _error(self, value=None): + raise AttributeError("'XMLParser' has no such attribute") + + def _get_buffer_text(self): + return self._buffer_text + + def _set_buffer_text(self, value): + self._buffer_text = bool(value) + + def _get_returns_unicode(self): + return bool(self._returns_unicode) + + def _set_returns_unicode(self, value): + self._returns_unicode = value + + # 'ordered' and 'specified' attributes are not supported + ordered_attributes = property(_error, _error) + specified_attributes = property(_error, _error) + # any setting is allowed, but it won't make a difference + buffer_text = property(_get_buffer_text, _set_buffer_text) + # non-significant read-only values + buffer_used = property(lambda self: None) + buffer_size = property(lambda self: None) + # 'returns_unicode' attribute is properly supported + returns_unicode = property(_get_returns_unicode, _set_returns_unicode) + + def _expat_error(self, sax_error): + sax_message = sax_error.getMessage() + pattern = 'The entity ".*" was referenced, but not declared\.' + if re.match(pattern, sax_message): + expat_message = "undefined entity: line %s, column %s" % \ + (self.ErrorLineNumber, self.ErrorColumnNumber) + else: + expat_message = sax_message + error = ExpatError(expat_message) + error.lineno = self.ErrorLineNumber + error.offset = self.ErrorColumnNumber + error.code = self.ErrorCode + return error + + def Parse(self, data, isfinal=False): + # The 'data' argument should be an encoded text: a str instance that + # represents an array of bytes. If instead it is a unicode string, + # only the us-ascii range is considered safe enough to be silently + # converted. + if isinstance(data, unicode): + data = data.encode(sys.getdefaultencoding()) + + self._data.append(data) + + if isfinal: + bytes = StringUtil.toBytes(self._data.toString()) + byte_stream = ByteArrayInputStream(bytes) + source = InputSource(byte_stream) + if self.encoding is not None: + source.setEncoding(self.encoding) + try: + self._reader.parse(source) + except SAXParseException, sax_error: + # Experiments tend to show that the '_Next*' parser locations + # match more closely expat behavior than the 'Current*' or sax + # error locations. + self.ErrorLineNumber = self._NextLineNumber + self.ErrorColumnNumber = self._NextColumnNumber + self.ErrorCode = None + raise self._expat_error(sax_error) + return 1 + + def ParseFile(self, file): + # TODO: pseudo-buffering if a read without argument is not supported. + # document parse / parsefile usage. + return self.Parse(file.read(), isfinal=True) + + +XMLParserType = XMLParser + + +def _encode(arg, encoding): + if isinstance(arg, unicode): + return arg.encode(encoding) + else: + if isinstance(arg, dict): + iterator = arg.iteritems() + else: + iterator = iter(arg) + return type(arg)(_encode(_arg, encoding) for _arg in iterator) + + +def expat(callback=None, guard="True", force=False, returns="None"): + global _register + try: + _ = _register + except NameError: + _register = {} + + def _expat(method): + name = method.__name__ + context = id(sys._getframe(1)) + key = name, context + append = _register.setdefault(key, []).append + append((method, callback, guard, force, returns)) + + def new_method(*args): + self = args[0] + parser = self.parser + self._update_location(event=name) # bug if multiple method def + for (method, callback, guard, force, returns) in _register[key]: + _callback = callback and eval(guard) and \ + getattr(parser, callback, None) + if _callback or force: + results = method(*args) + if _callback: + if not isinstance(results, tuple): + results = (results,) + if not parser.returns_unicode: + results = _encode(results, "utf-8") + _callback(*results) + return_ = eval(returns) + if callable(return_): + return return_(*args[1:]) + else: + return return_ + break + new_method.__name__ = name + #new_method.__doc__ = method.__doc__ # what to do with multiple docs ? + return new_method + return _expat + + +class XMLEventHandler(DefaultHandler2): + + def __init__(self, parser): + self.parser = parser + self._tags = {} + self.dtd = False + self._entity = {} + self._previous_event = None + + # --- Helpers ------------------------------------------------------------- + + def _intern(self, tag): + return self._tags.setdefault(tag, tag) + + def _qualify(self, local_name, qname, namespace=None): + namespace_separator = self.parser.namespace_separator + if namespace_separator is None: + return qname + if not namespace: + return local_name + else: + return namespace + namespace_separator + local_name + + def _char_slice_to_unicode(self, characters, start, length): + """Convert a char[] slice to a PyUnicode instance""" + text = Py.newUnicode(String(characters[start:start + length])) + return text + + def _expat_content_model(self, name, model_): + # TODO : implement a model parser + return (name, model_) # does not fit expat conventions + + def _update_location(self, event=None): + parser = self.parser + locator = self._locator + + # ugly hack that takes care of a xerces-specific (?) locator issue: + # locate start and end elements at the '<' instead of the first tag + # type character. + if event == "startElement" and self._previous_event == "characters": + parser._NextColumnNumber = max(parser._NextColumnNumber - 1, 0) + if event == "endElement" and self._previous_event == "characters": + parser._NextColumnNumber = max(parser._NextColumnNumber - 2, 0) + # TODO: use the same trick to report accurate error locations ? + + parser.CurrentLineNumber = parser._NextLineNumber + parser.CurrentColumnNumber = parser._NextColumnNumber + parser._NextLineNumber = locator.getLineNumber() + parser._NextColumnNumber = locator.getColumnNumber() - 1 + + self._previous_event = event + + # --- ContentHandler Interface -------------------------------------------- + + @expat("ProcessingInstructionHandler") + def processingInstruction(self, target, data): + return target, data + + @expat("StartElementHandler") + def startElement(self, namespace, local_name, qname, attributes): + tag = self._qualify(local_name, qname, namespace) + attribs = {} + length = attributes.getLength() + for index in range(length): + local_name = attributes.getLocalName(index) + qname = attributes.getQName(index) + namespace = attributes.getURI(index) + name = self._qualify(local_name, qname, namespace) + value = attributes.getValue(index) + attribs[name] = value + return self._intern(tag), attribs + + @expat("EndElementHandler") + def endElement(self, namespace, local_name, qname): + return self._intern(self._qualify(local_name, qname, namespace)) + + @expat("CharacterDataHandler") + def characters(self, characters, start, length): + return self._char_slice_to_unicode(characters, start, length) + + @expat("DefaultHandlerExpand") + def characters(self, characters, start, length): + return self._char_slice_to_unicode(characters, start, length) + + @expat("DefaultHandler") + def characters(self, characters, start, length): + # TODO: make a helper function here + if self._entity["location"] == (self.parser.CurrentLineNumber, + self.parser.CurrentColumnNumber): + return "&%s;" % self._entity["name"] + else: + return self._char_slice_to_unicode(characters, start, length) + + @expat("StartNamespaceDeclHandler") + def startPrefixMapping(self, prefix, uri): + return prefix, uri + + @expat("EndNamespaceDeclHandler") + def endPrefixMapping(self, prefix): + return prefix + + def _empty_source(self, *args): + name, publicId, baseURI, systemId = args + source = InputSource() + byte_stream = ByteArrayInputStream(array([], "b")) + source.setByteStream(byte_stream) + source.setPublicId(publicId) + source.setSystemId(systemId) + return source + + @expat("ExternalEntityRefHandler", guard="not self.dtd", + returns="self._empty_source") + def resolveEntity(self, name, publicId, baseURI, systemId): + context = name # wrong. see expat headers documentation. + base = self.parser.GetBase() + return context, base, systemId, publicId + + @expat("DefaultHandlerExpand", guard="not self.dtd", + returns="self._empty_source") + def resolveEntity(self, name, publicId, baseURI, systemId): + return "&%s;" % name + + @expat("DefaultHandler", guard="not self.dtd", + returns="self._empty_source") + def resolveEntity(self, name, publicId, baseURI, systemId): + return "&%s;" % name + + @expat(force=True, returns="self._empty_source") + def resolveEntity(self, name, publicId, baseURI, systemId): + pass + + def setDocumentLocator(self, locator): + self._locator = locator + + def skippedEntity(self, name): + error = ExpatError() + error.lineno = self.ErrorLineNumber = self.parser._NextLineNumber + error.offset = self.ErrorColumnNumber = self.parser._NextColumnNumber + error.code = self.ErrorCode = None + message = "undefined entity &%s;: line %s, column %s" + message = message % (name, error.lineno, error.offset) + error.__init__(message) + raise error + + # --- LexicalHandler Interface -------------------------------------------- + + @expat("CommentHandler") + def comment(self, characters, start, length): + return self._char_slice_to_unicode(characters, start, length) + + @expat("StartCdataSectionHandler") + def startCDATA(self): + return () + + @expat("EndCdataSectionHandler") + def endCDATA(self): + return () + + @expat("StartDoctypeDeclHandler", force=True) + def startDTD(self, name, publicId, systemId): + self.dtd = True + has_internal_subset = 0 # don't know this ... + return name, systemId, publicId, has_internal_subset + + @expat("EndDoctypeDeclHandler", force=True) + def endDTD(self): + self.dtd = False + + def startEntity(self, name): + self._entity = {} + self._entity["location"] = (self.parser._NextLineNumber, + self.parser._NextColumnNumber) + self._entity["name"] = name + + def endEntity(self, name): + pass + + # --- DTDHandler Interface ------------------------------------------------ + + @expat("NotationDeclHandler") + def notationDecl(self, name, publicId, systemId): + base = self.parser.GetBase() + return name, base, systemId, publicId + + @expat("UnparsedEntityDeclHandler") # deprecated + def unparsedEntityDecl(self, name, publicId, systemId, notationName): + base = self.parser.GetBase() + return name, base, systemId, publicId, notationName + + # --- DeclHandler Interface ----------------------------------------------- + + @expat("AttlistDeclHandler") + def attributeDecl(self, eName, aName, type, mode, value): + # TODO: adapt mode, required, etc. + required = False + return eName, aName, type, value, required + + @expat("ElementDeclHandler") + def elementDecl(self, name, model): + return self._expat_content_model(name, model) + + @expat("EntityDeclHandler") + def externalEntityDecl(self, name, publicId, systemId): + base = self.parser.GetBase() + value = None + is_parameter_entity = None + notation_name = None + return (name, is_parameter_entity, value, base, systemId, publicId, + notation_name) + + @expat("EntityDeclHandler") + def internalEntityDecl(self, name, value): + base = self.parser.GetBase() + is_parameter_entity = None + notation_name = None + systemId, publicId = None, None + return (name, is_parameter_entity, value, base, systemId, publicId, + notation_name) + + +def _init_model(): + global model + model = types.ModuleType("pyexpat.model") + model.__doc__ = "Constants used to interpret content model information." + quantifiers = "NONE, OPT, REP, PLUS" + for i, quantifier in enumerate(quantifiers.split(", ")): + setattr(model, "XML_CQUANT_" + quantifier, i) + types_ = "EMPTY, ANY, MIXED, NAME, CHOICE, SEQ" + for i, type_ in enumerate(types_.split(", ")): + setattr(model, "XML_CTYPE_" + type_, i+1) + +_init_model(); del _init_model + + +class ExpatError(Exception): + pass + + +error = ExpatError + + +def _init_error_strings(): + global ErrorString + error_strings = ( + None, + "out of memory", + "syntax error", + "no element found", + "not well-formed (invalid token)", + "unclosed token", + "partial character", + "mismatched tag", + "duplicate attribute", + "junk after document element", + "illegal parameter entity reference", + "undefined entity", + "recursive entity reference", + "asynchronous entity", + "reference to invalid character number", + "reference to binary entity", + "reference to external entity in attribute", + "XML or text declaration not at start of entity", + "unknown encoding", + "encoding specified in XML declaration is incorrect", + "unclosed CDATA section", + "error in processing external entity reference", + "document is not standalone", + "unexpected parser state - please send a bug report", + "entity declared in parameter entity", + "requested feature requires XML_DTD support in Expat", + "cannot change setting once parsing has begun", + "unbound prefix", + "must not undeclare prefix", + "incomplete markup in parameter entity", + "XML declaration not well-formed", + "text declaration not well-formed", + "illegal character(s) in public id", + "parser suspended", + "parser not suspended", + "parsing aborted", + "parsing finished", + "cannot suspend in external parameter entity") + def ErrorString(code): + try: + return error_strings[code] + except IndexError: + return None + +_init_error_strings(); del _init_error_strings + + +def _init_errors(): + global errors + + errors = types.ModuleType("pyexpat.errors") + errors.__doc__ = "Constants used to describe error conditions." + + error_names = """ + XML_ERROR_NONE + XML_ERROR_NONE, + XML_ERROR_NO_MEMORY, + XML_ERROR_SYNTAX, + XML_ERROR_NO_ELEMENTS, + XML_ERROR_INVALID_TOKEN, + XML_ERROR_UNCLOSED_TOKEN, + XML_ERROR_PARTIAL_CHAR, + XML_ERROR_TAG_MISMATCH, + XML_ERROR_DUPLICATE_ATTRIBUTE, + XML_ERROR_JUNK_AFTER_DOC_ELEMENT, + XML_ERROR_PARAM_ENTITY_REF, + XML_ERROR_UNDEFINED_ENTITY, + XML_ERROR_RECURSIVE_ENTITY_REF, + XML_ERROR_ASYNC_ENTITY, + XML_ERROR_BAD_CHAR_REF, + XML_ERROR_BINARY_ENTITY_REF, + XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF, + XML_ERROR_MISPLACED_XML_PI, + XML_ERROR_UNKNOWN_ENCODING, + XML_ERROR_INCORRECT_ENCODING, + XML_ERROR_UNCLOSED_CDATA_SECTION, + XML_ERROR_EXTERNAL_ENTITY_HANDLING, + XML_ERROR_NOT_STANDALONE, + XML_ERROR_UNEXPECTED_STATE, + XML_ERROR_ENTITY_DECLARED_IN_PE, + XML_ERROR_FEATURE_REQUIRES_XML_DTD, + XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING, + XML_ERROR_UNBOUND_PREFIX, + XML_ERROR_UNDECLARING_PREFIX, + XML_ERROR_INCOMPLETE_PE, + XML_ERROR_XML_DECL, + XML_ERROR_TEXT_DECL, + XML_ERROR_PUBLICID, + XML_ERROR_SUSPENDED, + XML_ERROR_NOT_SUSPENDED, + XML_ERROR_ABORTED, + XML_ERROR_FINISHED, + XML_ERROR_SUSPEND_PE + """ + error_names = [name.strip() for name in error_names.split(',')] + for i, name in enumerate(error_names[1:]): + setattr(errors, name, ErrorString(i+1)) + +_init_errors(); del _init_errors This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-01-08 22:22:10
|
Revision: 5891 http://jython.svn.sourceforge.net/jython/?rev=5891&view=rev Author: pjenvey Date: 2009-01-08 21:49:36 +0000 (Thu, 08 Jan 2009) Log Message: ----------- jarjar xerces into jython-complete and check for the mangled name Modified Paths: -------------- trunk/jython/Lib/xml/parsers/expat.py trunk/jython/build.xml Modified: trunk/jython/Lib/xml/parsers/expat.py =================================================================== --- trunk/jython/Lib/xml/parsers/expat.py 2009-01-08 21:26:19 UTC (rev 5890) +++ trunk/jython/Lib/xml/parsers/expat.py 2009-01-08 21:49:36 UTC (rev 5891) @@ -47,7 +47,15 @@ from org.xml.sax.helpers import DefaultHandler, XMLReaderFactory from org.xml.sax.ext import DefaultHandler2 +# Xerces +try: + # Name mangled by jarjar? + import org.python.apache.xerces.parsers.SAXParser + _xerces_parser = "org.python.apache.xerces.parsers.SAXParser" +except ImportError: + _xerces_parser = "org.apache.xerces.parsers.SAXParser" + def ParserCreate(encoding=None, namespace_separator=None): return XMLParser(encoding, namespace_separator) @@ -77,9 +85,7 @@ "not %s" % type(namespace_separator).__name__) raise TypeError(error) - XMLReader = XMLReaderFactory.createXMLReader - xerces_parser = "org.apache.xerces.parsers.SAXParser" - self._reader = XMLReader(xerces_parser) + self._reader = XMLReaderFactory.createXMLReader(_xerces_parser) if self.namespace_separator is None: try: Modified: trunk/jython/build.xml =================================================================== --- trunk/jython/build.xml 2009-01-08 21:26:19 UTC (rev 5890) +++ trunk/jython/build.xml 2009-01-08 21:49:36 UTC (rev 5891) @@ -520,6 +520,8 @@ <rule pattern="org.jruby.ext.posix.**" result="org.python.posix.@1"/> <zipfileset src="extlibs/constantine-0.4.jar"/> <rule pattern="com.kenai.constantine.**" result="org.python.constantine.@1"/> + <zipfileset src="extlibs/xercesImpl.jar"/> + <rule pattern="org.apache.**" result="org.python.apache.@1"/> <manifest> <attribute name="Main-Class" value="org.python.util.jython" /> <attribute name="Built-By" value="${user.name}" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-01-09 15:16:25
|
Revision: 5900 http://jython.svn.sourceforge.net/jython/?rev=5900&view=rev Author: fwierzbicki Date: 2009-01-09 15:16:08 +0000 (Fri, 09 Jan 2009) Log Message: ----------- Fix for wrong error line number when file ends with open paren. Modified Paths: -------------- trunk/jython/Lib/test/test_eof_jy.py trunk/jython/src/org/python/antlr/PythonTokenSource.java Added Paths: ----------- trunk/jython/Lib/test/eof_fodder7.py Added: trunk/jython/Lib/test/eof_fodder7.py =================================================================== --- trunk/jython/Lib/test/eof_fodder7.py (rev 0) +++ trunk/jython/Lib/test/eof_fodder7.py 2009-01-09 15:16:08 UTC (rev 5900) @@ -0,0 +1,5 @@ +def hi(): + pass + +def bye(): + hi( Modified: trunk/jython/Lib/test/test_eof_jy.py =================================================================== --- trunk/jython/Lib/test/test_eof_jy.py 2009-01-09 04:50:22 UTC (rev 5899) +++ trunk/jython/Lib/test/test_eof_jy.py 2009-01-09 15:16:08 UTC (rev 5900) @@ -45,6 +45,12 @@ except ImportError, cause: self.fail(cause) + def test_trailing_paren(self): + try: + import eof_fodder7 + except SyntaxError, cause: + self.assertEquals(cause.lineno, 5) + #============================================================================== def test_main(verbose=None): Modified: trunk/jython/src/org/python/antlr/PythonTokenSource.java =================================================================== --- trunk/jython/src/org/python/antlr/PythonTokenSource.java 2009-01-09 04:50:22 UTC (rev 5899) +++ trunk/jython/src/org/python/antlr/PythonTokenSource.java 2009-01-09 15:16:08 UTC (rev 5900) @@ -146,6 +146,7 @@ } private void generateNewline(Token t) { + //System.out.println("generating newline from token: " + t); CommonToken newline = new CommonToken(PythonLexer.NEWLINE, "\n"); newline.setLine(t.getLine()); newline.setCharPositionInLine(t.getCharPositionInLine()); @@ -153,9 +154,12 @@ } private void handleEOF(CommonToken eof, CommonToken prev) { + //System.out.println("processing eof with token: " + prev); if (prev != null) { eof.setStartIndex(prev.getStopIndex()); eof.setStopIndex(prev.getStopIndex()); + eof.setLine(prev.getLine()); + eof.setCharPositionInLine(prev.getCharPositionInLine()); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ped...@us...> - 2009-01-10 12:39:27
|
Revision: 5908 http://jython.svn.sourceforge.net/jython/?rev=5908&view=rev Author: pedronis Date: 2009-01-10 12:39:15 +0000 (Sat, 10 Jan 2009) Log Message: ----------- yet another try at binop rule, with more exhaustive tests (verified against cpython) that now pass see also PyPy issue 412 Modified Paths: -------------- trunk/jython/Lib/test/test_descr_jy.py trunk/jython/src/org/python/core/PyObject.java trunk/jython/src/org/python/core/PyType.java Modified: trunk/jython/Lib/test/test_descr_jy.py =================================================================== --- trunk/jython/Lib/test/test_descr_jy.py 2009-01-10 03:53:36 UTC (rev 5907) +++ trunk/jython/Lib/test/test_descr_jy.py 2009-01-10 12:39:15 UTC (rev 5908) @@ -368,13 +368,148 @@ except AttributeError, e: self.assertEquals("Custom message", str(e)) +# try to test more exhaustively binop overriding combination cases +class Base(object): + def __init__(self, name): + self.name = name + +def lookup_where(obj, name): + mro = type(obj).__mro__ + for t in mro: + if name in t.__dict__: + return t.__dict__[name], t + return None, None + +def refop(x, y, opname, ropname): + # this has been validated by running the tests on top of cpython + # so for the space of possibilities that the tests touch it is known + # to behave like cpython as long as the latter doesn't change its own + # algorithm + t1 = type(x) + t2 = type(y) + op, where1 = lookup_where(x, opname) + rop, where2 = lookup_where(y, ropname) + if op is None and rop is not None: + return rop(y, x) + if rop and where1 is not where2: + if (issubclass(t2, t1) and not issubclass(where1, where2) + and not issubclass(t1, where2) + ): + return rop(y, x) + if op is None: + return "TypeError" + return op(x,y) + +def do_test(X, Y, name, impl): + x = X('x') + y = Y('y') + opname = '__%s__' % name + ropname = '__r%s__' % name + + count = [0] + fail = [] + + def check(z1, z2): + ref = refop(z1, z2, opname, ropname) + try: + v = impl(z1, z2) + except TypeError: + v = "TypeError" + if v != ref: + fail.append(count[0]) + + def override_in_hier(n=6): + if n == 0: + count[0] += 1 + check(x, y) + check(y, x) + return + + f = lambda self, other: (n, self.name, other.name) + if n%2 == 0: + name = opname + else: + name = ropname + + for C in Y.__mro__: + if name in C.__dict__: + continue + if C is not object: + setattr(C, name, f) + override_in_hier(n-1) + if C is not object: + delattr(C, name) + + override_in_hier() + #print count[0] + return fail + +class BinopCombinationsTestCase(unittest.TestCase): + + def test_binop_combinations_mul(self): + class X(Base): + pass + class Y(X): + pass + + fail = do_test(X, Y, 'mul', lambda x,y: x*y) + #print len(fail) + self.assert_(not fail) + + def test_binop_combinations_sub(self): + class X(Base): + pass + class Y(X): + pass + + fail = do_test(X, Y, 'sub', lambda x,y: x-y) + #print len(fail) + self.assert_(not fail) + + def test_binop_combinations_pow(self): + class X(Base): + pass + class Y(X): + pass + + fail = do_test(X, Y, 'pow', lambda x,y: x**y) + #print len(fail) + self.assert_(not fail) + + def test_binop_combinations_more_exhaustive(self): + class X(Base): + pass + + class B1(object): + pass + + class B2(object): + pass + + class X1(B1, X, B2): + pass + + class C1(object): + pass + + class C2(object): + pass + + class Y(C1, X1, C2): + pass + + fail = do_test(X, Y, 'sub', lambda x,y: x-y) + #print len(fail) + self.assert_(not fail) + def test_main(): test_support.run_unittest(TestDescrTestCase, SubclassDescrTestCase, InPlaceTestCase, DescrExceptionsTestCase, - GetAttrTestCase) + GetAttrTestCase, + BinopCombinationsTestCase) if __name__ == '__main__': test_main() Modified: trunk/jython/src/org/python/core/PyObject.java =================================================================== --- trunk/jython/src/org/python/core/PyObject.java 2009-01-10 03:53:36 UTC (rev 5907) +++ trunk/jython/src/org/python/core/PyObject.java 2009-01-10 12:39:15 UTC (rev 5908) @@ -1888,14 +1888,16 @@ * test_descr.subclass_right_op. */ PyObject o1 = this; - int[] where = new int[1]; - int where1, where2; - PyObject impl1 = t1.lookup_where_index(left, where); + PyObject[] where = new PyObject[1]; + PyObject where1 = null, where2 = null; + PyObject impl1 = t1.lookup_where(left, where); where1 = where[0]; - PyObject impl2 = t2.lookup_where_index(right, where); + PyObject impl2 = t2.lookup_where(right, where); where2 = where[0]; - if (impl2 != null && where1 < where2 && (t2.isSubType(t1) || - isStrUnicodeSpecialCase(t1, t2, op))) { + if (impl2 != null && impl1 != null && where1 != where2 && + (t2.isSubType(t1) && !Py.isSubClass(where1, where2) + && !Py.isSubClass(t1, where2) || + isStrUnicodeSpecialCase(t1, t2, op))) { PyObject tmp = o1; o1 = o2; o2 = tmp; Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2009-01-10 03:53:36 UTC (rev 5907) +++ trunk/jython/src/org/python/core/PyType.java 2009-01-10 12:39:15 UTC (rev 5908) @@ -915,34 +915,6 @@ return null; } - /** - * Like lookup but also provides (in where[0]) the index of the type in the reversed - * mro -- that is, how many subtypes away from the base object the type is. - * - * @param name attribute name (must be interned) - * @param where an int[] with a length of at least 1 - * @return found PyObject or null - */ - public PyObject lookup_where_index(String name, int[] where) { - PyObject[] mro = this.mro; - if (mro == null) { - return null; - } - int i = mro.length; - for (PyObject t : mro) { - i--; - PyObject dict = t.fastGetDict(); - if (dict != null) { - PyObject obj = dict.__finditem__(name); - if (obj != null) { - where[0] = i; - return obj; - } - } - } - return null; - } - public PyObject super_lookup(PyType ref, String name) { PyObject[] mro = this.mro; if (mro == null) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2009-01-11 00:54:06
|
Revision: 5912 http://jython.svn.sourceforge.net/jython/?rev=5912&view=rev Author: cgroves Date: 2009-01-11 00:53:55 +0000 (Sun, 11 Jan 2009) Log Message: ----------- * Add xerces' version number to its file name for future explorers. * Add a skip-brand property to build.xml that allows version branding to be skipped, which keeps jython-dev.jar from being built even if no other files change and shaves a couple seconds off a dev build. Modified Paths: -------------- trunk/jython/build.xml trunk/jython/src/org/python/util/NameUnionAntType.java Added Paths: ----------- trunk/jython/extlibs/xercesImpl-2.9.1.jar Removed Paths: ------------- trunk/jython/extlibs/xercesImpl.jar Modified: trunk/jython/build.xml =================================================================== --- trunk/jython/build.xml 2009-01-10 20:50:36 UTC (rev 5911) +++ trunk/jython/build.xml 2009-01-11 00:53:55 UTC (rev 5912) @@ -362,7 +362,9 @@ <exec executable="svnversion" failifexecutionfails="false" outputproperty="build.svn.revision"/> </target> - <target name="brand-version" depends="version-init, svnversion"> + <!-- skip-brand can be set in ant.properties or as a system property to keep from updating the + version.properties file and making the jar on every developer build. --> + <target name="brand-version" depends="version-init, svnversion" unless="skip-brand"> <condition property="build.svn.revision" value=""> <not> <isset property="build.svn.revision"/> @@ -520,7 +522,7 @@ <rule pattern="org.jruby.ext.posix.**" result="org.python.posix.@1"/> <zipfileset src="extlibs/constantine-0.4.jar"/> <rule pattern="com.kenai.constantine.**" result="org.python.constantine.@1"/> - <zipfileset src="extlibs/xercesImpl.jar"/> + <zipfileset src="extlibs/xercesImpl-2.9.1.jar"/> <rule pattern="org.apache.**" result="org.python.apache.@1"/> <manifest> <attribute name="Main-Class" value="org.python.util.jython" /> Copied: trunk/jython/extlibs/xercesImpl-2.9.1.jar (from rev 5907, trunk/jython/extlibs/xercesImpl.jar) =================================================================== (Binary files differ) Property changes on: trunk/jython/extlibs/xercesImpl-2.9.1.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: svn:mergeinfo + /branches/newstyle-java-types/extlibs/xercesImpl.jar:5564-5663,5666-5729 Deleted: trunk/jython/extlibs/xercesImpl.jar =================================================================== (Binary files differ) Modified: trunk/jython/src/org/python/util/NameUnionAntType.java =================================================================== --- trunk/jython/src/org/python/util/NameUnionAntType.java 2009-01-10 20:50:36 UTC (rev 5911) +++ trunk/jython/src/org/python/util/NameUnionAntType.java 2009-01-11 00:53:55 UTC (rev 5912) @@ -2,7 +2,6 @@ import java.util.ArrayList; import java.util.Collection; -import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2009-01-11 03:21:30
|
Revision: 5915 http://jython.svn.sourceforge.net/jython/?rev=5915&view=rev Author: cgroves Date: 2009-01-11 03:21:25 +0000 (Sun, 11 Jan 2009) Log Message: ----------- Cleanup Modified Paths: -------------- trunk/jython/src/org/python/core/PyDictionary.java trunk/jython/src/org/python/core/PyObjectList.java trunk/jython/src/org/python/core/PySequenceList.java trunk/jython/tests/java/javatests/ListTest.java trunk/jython/tests/java/javatests/TestSupport.java Modified: trunk/jython/src/org/python/core/PyDictionary.java =================================================================== --- trunk/jython/src/org/python/core/PyDictionary.java 2009-01-11 01:12:16 UTC (rev 5914) +++ trunk/jython/src/org/python/core/PyDictionary.java 2009-01-11 03:21:25 UTC (rev 5915) @@ -12,7 +12,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; -import org.python.core.PyMapSet.PySetIter; import org.python.expose.ExposedClassMethod; import org.python.expose.ExposedMethod; import org.python.expose.ExposedNew; @@ -27,7 +26,7 @@ public class PyDictionary extends PyObject implements ConcurrentMap { public static final PyType TYPE = PyType.fromClass(PyDictionary.class); - + protected final ConcurrentMap<PyObject, PyObject> table; /** @@ -39,7 +38,6 @@ /** * For derived types - * @param subtype */ public PyDictionary(PyType subtype) { super(subtype); @@ -47,31 +45,27 @@ } /** - * Create an new dictionary which is based on the hashtable. - * @param t the hashtable used. The supplied hashtable is used as - * is and must only contain PyObject key:value pairs. + * Create a new dictionary which is based on given map. */ public PyDictionary(Map<PyObject, PyObject> t) { table = new ConcurrentHashMap<PyObject, PyObject>(t); } - /** - * Create an new dictionary which is based on the map and for derived types. - * @param subtype - * @param t the hashtable used. The supplied hashtable is used as - * is and must only contain PyObject key:value pairs. + /** + * Create a new derived dictionary which is based on the given map. */ public PyDictionary(PyType subtype, Map<PyObject, PyObject> t) { super(subtype); table = new ConcurrentHashMap<PyObject, PyObject>(t); } - + /** * Create a new dictionary with the element as content. - * @param elements The initial elements that is inserted in the - * dictionary. Even numbered elements are keys, - * odd numbered elements are values. + * + * @param elements + * The initial elements that is inserted in the dictionary. Even numbered elements + * are keys, odd numbered elements are values. */ public PyDictionary(PyObject elements[]) { this(); @@ -85,7 +79,7 @@ protected final void dict___init__(PyObject[] args, String[] keywords) { updateCommon(args, keywords, "dict"); } - + public static PyObject fromkeys(PyObject keys) { return fromkeys(keys, Py.None); } @@ -210,10 +204,10 @@ } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.dict___eq___doc) - final PyObject dict___eq__(PyObject ob_other) { + final PyObject dict___eq__(PyObject ob_other) { PyType thisType = getType(); PyType otherType = ob_other.getType(); - if (otherType != thisType && !thisType.isSubType(otherType) + if (otherType != thisType && !thisType.isSubType(otherType) && !otherType.isSubType(thisType)) { return null; } @@ -291,7 +285,7 @@ final int dict___cmp__(PyObject ob_other) { PyType thisType = getType(); PyType otherType = ob_other.getType(); - if (otherType != thisType && !thisType.isSubType(otherType) + if (otherType != thisType && !thisType.isSubType(otherType) && !otherType.isSubType(thisType)) { return -2; } @@ -435,7 +429,7 @@ } for (int i = 0; i < keywords.length; i++) { dict___setitem__(Py.newString(keywords[i]), args[nargs + i]); - } + } } /** @@ -539,7 +533,7 @@ } } - + /** * Return a value based on key * from the dictionary. @@ -578,18 +572,17 @@ @ExposedMethod(doc = BuiltinDocs.dict_popitem_doc) final PyObject dict_popitem() { - Iterator it = table.entrySet().iterator(); + Iterator<Entry<PyObject, PyObject>> it = table.entrySet().iterator(); if (!it.hasNext()) throw Py.KeyError("popitem(): dictionary is empty"); - Entry entry = (Entry)it.next(); - PyTuple tuple = new PyTuple( - (PyObject)entry.getKey(), (PyObject)entry.getValue()); + Entry<PyObject, PyObject> entry = it.next(); + PyTuple tuple = new PyTuple(entry.getKey(), entry.getValue()); it.remove(); return tuple; } /** - * Return a copy of the dictionarys list of (key, value) tuple + * Return a copy of the dictionary's list of (key, value) tuple * pairs. */ public PyList items() { @@ -606,7 +599,7 @@ } /** - * Return a copy of the dictionarys list of keys. + * Return a copy of the dictionary's list of keys. */ public PyList keys() { return dict_keys(); @@ -623,7 +616,7 @@ } /** - * Return an interator over (key, value) pairs. + * Returns an iterator over (key, value) pairs. */ public PyObject iteritems() { return dict_iteritems(); @@ -635,7 +628,7 @@ } /** - * Return an interator over (key, value) pairs. + * Returns an iterator over the dictionary's keys. */ public PyObject iterkeys() { return dict_iterkeys(); @@ -647,7 +640,7 @@ } /** - * Return an interator over (key, value) pairs. + * Returns an iterator over the dictionary's values. */ public PyObject itervalues() { return dict_itervalues(); @@ -703,7 +696,7 @@ iterator = items.iterator(); size = items.size(); } - + public PyObject __iternext__() { if (table.size() != size) { throw Py.RuntimeError("dictionary changed size during iteration"); @@ -716,13 +709,12 @@ } } - /* The following methods implement the java.util.Map interface - which allows PyDictionary to be passed to java methods that take - java.util.Map as a parameter. Basically, the Map methods are a - wrapper around the PyDictionary's Map container stored in member - variable 'table'. These methods simply convert java Object to - PyObjects on insertion, and PyObject to Objects on retrieval. */ - + /* + * The following methods implement the java.util.Map interface which allows PyDictionary to be + * passed to java methods that take java.util.Map as a parameter. Basically, the Map methods are + * a wrapper around the PyDictionary's Map container stored in member variable 'table'. These + * methods convert java Object to PyObjects on insertion, and PyObject to Objects on retrieval. + */ /** @see java.util.Map#entrySet() */ public Set entrySet() { return new PyMapEntrySet(table.entrySet()); @@ -733,17 +725,15 @@ return new PyMapKeyValSet(table.keySet()); } - /** Return a copy of the dictionarys list of values. */ + /** @see java.util.Map#values() */ public Collection values() { return new PyMapKeyValSet(table.values()); } - /** @see java.util.Map#putAll(Map map) */ public void putAll(Map map) { - Iterator i = map.entrySet().iterator(); - while (i.hasNext()) { - Entry entry = (Entry)i.next(); + for (Object o : map.entrySet()) { + Entry entry = (Entry)o; table.put(Py.java2py(entry.getKey()), Py.java2py(entry.getValue())); } } @@ -772,19 +762,19 @@ public boolean containsKey(Object key) { return table.containsKey(Py.java2py(key)); } - - /** @see java.util.Map#isEmpty(Object key) */ + + /** @see java.util.Map#isEmpty() */ public boolean isEmpty() { return table.isEmpty(); } - - /** @see java.util.Map#size(Object key) */ + + /** @see java.util.Map#size() */ public int size() { return table.size(); } /** Convert return values to java objects */ - static final Object tojava(Object val) { + final static Object tojava(Object val) { return val == null ? null : ((PyObject)val).__tojava__(Object.class); } @@ -805,19 +795,20 @@ } } + /** Basic implementation of Entry that just holds onto a key and value and returns them. */ -class SimpleEntry<K, V> implements Entry<K, V> { - - public SimpleEntry(K key, V value){ +class SimpleEntry implements Entry { + + public SimpleEntry(Object key, Object value){ this.key = key; this.value = value; } - - public K getKey() { + + public Object getKey() { return key; } - public V getValue() { + public Object getValue() { return value; } @@ -841,31 +832,29 @@ return key + "=" + value; } - public V setValue(V val) { + public Object setValue(Object val) { throw new UnsupportedOperationException("Not supported by this view"); } - protected K key; + protected Object key; - protected V value; + protected Object value; } /** - * Wrapper for a Entry object returned from the java.util.Set - * object which in turn is returned by the entrySet method of - * java.util.Map. This is needed to correctly convert from PyObjects - * to java Objects. Note that we take care in the equals and hashCode - * methods to make sure these methods are consistent with Entry - * objects that contain java Objects for a value so that on the java - * side they can be reliable compared. + * Wrapper for a Entry object returned from the java.util.Set object which in turn is returned by + * the entrySet method of java.util.Map. This is needed to correctly convert from PyObjects to java + * Objects. Note that we take care in the equals and hashCode methods to make sure these methods are + * consistent with Entry objects that contain java Objects for a value so that on the java side they + * can be reliable compared. */ class PyToJavaMapEntry extends SimpleEntry { - /** Create a copy of the Entry with Py.None coverted to null */ + /** Create a copy of the Entry with Py.None converted to null */ PyToJavaMapEntry(Entry entry) { super(entry.getKey(), entry.getValue()); } - + public boolean equals(Object o) { if (o == null || !(o instanceof Entry)) return false; Entry me = new JavaToPyMapEntry((Entry)o); @@ -877,7 +866,7 @@ public Object getKey() { return PyDictionary.tojava(key); } - + public Object getValue() { return PyDictionary.tojava(value); } @@ -892,24 +881,22 @@ } /** - * MapEntry Object for java MapEntry objects passed to the java.util.Set - * interface which is returned by the entrySet method of PyDictionary. - * Essentially like PyTojavaMapEntry, but going the other way converting java - * Objects to PyObjects. + * MapEntry Object for java MapEntry objects passed to the java.util.Set interface which is returned + * by the entrySet method of PyDictionary. Essentially like PyTojavaMapEntry, but going the other + * way converting java Objects to PyObjects. */ class JavaToPyMapEntry extends SimpleEntry { - + public JavaToPyMapEntry(Entry entry) { super(Py.java2py(entry.getKey()), Py.java2py(entry.getValue())); } } /** - * Wrapper collection class for the keySet and values methods of - * java.util.Map + * Wrapper collection class for the keySet and values methods of java.util.Map */ class PyMapKeyValSet extends PyMapSet { - + PyMapKeyValSet(Collection coll) { super(coll); } @@ -917,21 +904,18 @@ Object toJava(Object o) { return PyDictionary.tojava(o); } - + Object toPython(Object o) { return Py.java2py(o); } } /** - * Set wrapper for the java.util.EntrySet method. Entry - * objects are wrapped further in JavaToPyMapEntry and - * PyToJavaMapEntry. Note - The set interface is reliable for - * standard objects like strings and integers, but may be inconstant - * for other types of objects since the equals method may return false - * for Entry object that hold more elaborate PyObject types. - * However, We insure that this iterface works when the Entry - * object originates from a Set object retrieved from a PyDictionary. + * Set wrapper for the entrySet method. Entry objects are wrapped further in JavaToPyMapEntry and + * PyToJavaMapEntry. Note - The set interface is reliable for standard objects like strings and + * integers, but may be inconsistent for other types of objects since the equals method may return + * false for Entry object that hold more elaborate PyObject types. However, we insure that this + * interface works when the Entry object originates from a Set object retrieved from a PyDictionary. */ class PyMapEntrySet extends PyMapSet { @@ -939,13 +923,13 @@ super(coll); } - // We know that PyMapEntrySet will only contains Entrys, so - // if the object being passed in is null or not a Entry, then - // return null which will match nothing for remove and contains methods. + // We know that PyMapEntrySet will only contains entries, so if the object being passed in is + // null or not an Entry, then return null which will match nothing for remove and contains + // methods. Object toPython(Object o) { if(o == null || !(o instanceof Entry)) return null; - if(o instanceof PyToJavaMapEntry) { + if (o instanceof PyToJavaMapEntry) { // Use the original entry from PyDictionary return ((PyToJavaMapEntry)o).getEntry(); } else { @@ -959,15 +943,12 @@ } /** - * PyMapSet serves as a wrapper around Set Objects returned by the - * java.util.Map interface of PyDictionary. entrySet, values and - * keySet methods return this type for the java.util.Map - * implementation. This class is necessary as a wrapper to convert - * PyObjects to java Objects for methods that return values, and - * convert Objects to PyObjects for methods that take values. The - * translation is necessary to provide java access to jython - * dictionary objects. This wrapper also provides the expected backing - * functionality such that changes to the wrapper set or reflected in + * PyMapSet serves as a wrapper around Set Objects returned by the java.util.Map interface of + * PyDictionary. entrySet, values and keySet methods return this type for the java.util.Map + * implementation. This class is necessary as a wrapper to convert PyObjects to java Objects for + * methods that return values, and convert Objects to PyObjects for methods that take values. The + * translation is necessary to provide java access to jython dictionary objects. This wrapper also + * provides the expected backing functionality such that changes to the wrapper set or reflected in * PyDictionary. */ abstract class PyMapSet extends AbstractSet { @@ -983,15 +964,15 @@ public int size() { return coll.size(); } - + public boolean contains(Object o) { return coll.contains(toPython(o)); } - + public boolean remove(Object o) { return coll.remove(toPython(o)); } - + public void clear() { coll.clear(); } @@ -1013,15 +994,15 @@ public Object next() { return toJava(itr.next()); } - + public void remove() { itr.remove(); } } - + public Iterator iterator() { return new PySetIter(coll.iterator()); } - + private final Collection coll; } Modified: trunk/jython/src/org/python/core/PyObjectList.java =================================================================== --- trunk/jython/src/org/python/core/PyObjectList.java 2009-01-11 01:12:16 UTC (rev 5914) +++ trunk/jython/src/org/python/core/PyObjectList.java 2009-01-11 03:21:25 UTC (rev 5915) @@ -4,7 +4,6 @@ import java.io.Serializable; import java.util.AbstractList; import java.util.Collection; -import java.util.Iterator; import java.util.RandomAccess; /** @@ -20,17 +19,15 @@ * @author Clark Updike */ public class PyObjectList - extends AbstractList implements RandomAccess, Cloneable, Serializable { + extends AbstractList<Object> implements RandomAccess, Cloneable, Serializable { - /* Design note: - * This class let's PySequenceList implement java.util.List by delegating - * to an instance of this. The major distinction is that the backing array - * is PyObject[], not Object[] (as you'd get by delegating to ArrayList). - * There are 2 major benefits: 1) A lot of casting can be avoided - * internally (although use of PySequenceList descendants as java - * collections does involve some casting); 2) PySequenceList descendants - * can still do bulk array operations, allowing better performance and - * reuse of much of the pre-collections bulk operation implementation. + /* + * Design note: This class lets PySequenceList implement java.util.List by delegating to it. The + * major distinction is that the backing array is PyObject[], not Object[] (as you'd get by + * delegating to ArrayList). There are 2 major benefits: 1) A lot of casting can be avoided + * internally (although use of PySequenceList descendants as java collections does involve some + * casting); 2) PySequenceList descendants can still do bulk array operations, allowing better + * performance and reuse of much of the pre-collections bulk operation implementation. */ @@ -49,7 +46,7 @@ array.baseArray = pyObjArr; } - public PyObjectList(Collection c) { + public PyObjectList(Collection<PyObject> c) { array = new PyObjectArray(); array.appendArray(c.toArray()); } @@ -62,8 +59,7 @@ * For internal jython usage, use {@link #pyadd(int, PyObject)}. */ public void add(int index, Object element) { - array.add(index, Py.java2py(element)); - modCount += array.getModCountIncr(); + pyadd(index, Py.java2py(element)); } public void pyadd(int index, PyObject element) { @@ -75,8 +71,7 @@ * For internal jython usage, use {@link #pyadd(PyObject)}. */ public boolean add(Object o) { - array.add(Py.java2py(o)); - modCount += array.getModCountIncr(); + pyadd(Py.java2py(o)); return true; } @@ -90,7 +85,7 @@ try { PyObjectList tol = (PyObjectList) super.clone(); tol.array = (PyObjectArray) array.clone(); - modCount = 0; + tol.modCount = 0; return tol; } catch (CloneNotSupportedException eCNSE) { throw new InternalError("Unexpected CloneNotSupportedException.\n" @@ -113,8 +108,7 @@ * Use <code>pyget(int)</code> for internal jython usage. */ public Object get(int index) { - PyObject obj = array.get(index); - return obj.__tojava__(Object.class); + return array.get(index).__tojava__(Object.class); } PyObject pyget(int index) { @@ -137,7 +131,7 @@ * Use <code>pyset(int, PyObject)</code> for internal jython usage. */ public Object set(int index, Object element) { - return array.set(index, Py.java2py(element) ).__tojava__(Object.class); + return pyset(index, Py.java2py(element)).__tojava__(Object.class); } PyObject pyset(int index, PyObject element) { @@ -148,11 +142,11 @@ return array.getSize(); } - public boolean addAll(Collection c) { + public boolean addAll(Collection<? extends Object> c) { return addAll(size(), c); } - public boolean addAll(int index, Collection c) { + public boolean addAll(int index, Collection<? extends Object> c) { if (c instanceof PySequenceList) { PySequenceList cList = (PySequenceList)c; PyObject[] cArray = cList.getArray(); @@ -160,21 +154,20 @@ array.makeInsertSpace(index, cOrigSize); array.replaceSubArray(index, index + cOrigSize, cArray, 0, cOrigSize); } else { - // need to use add to convert anything pulled from a collection - // into a PyObject - for (Iterator i = c.iterator(); i.hasNext(); ) { - add(i.next()); + // need to use add to convert anything pulled from a collection into a PyObject + for (Object element : c) { + add(element); } } return c.size() > 0; } - /** - * Get the backing array. The array should generally not be modified. - * To get a copy of the array, see {@link #toArray()} which returns a copy. - * - * @return backing array object - */ + /** + * Get the backing array. The array should generally not be modified. To get a copy of the + * array, see {@link #toArray()} which returns a copy. + * + * @return backing array object + */ protected PyObject[] getArray() { return (PyObject[])array.getArray(); } Modified: trunk/jython/src/org/python/core/PySequenceList.java =================================================================== --- trunk/jython/src/org/python/core/PySequenceList.java 2009-01-11 01:12:16 UTC (rev 5914) +++ trunk/jython/src/org/python/core/PySequenceList.java 2009-01-11 03:21:25 UTC (rev 5915) @@ -30,7 +30,7 @@ list = new PyObjectList(elements); } - public PySequenceList(PyType type, Collection c) { + public PySequenceList(PyType type, Collection<PyObject> c) { super(type); list = new PyObjectList(c); } Modified: trunk/jython/tests/java/javatests/ListTest.java =================================================================== --- trunk/jython/tests/java/javatests/ListTest.java 2009-01-11 01:12:16 UTC (rev 5914) +++ trunk/jython/tests/java/javatests/ListTest.java 2009-01-11 03:21:25 UTC (rev 5915) @@ -1,151 +1,140 @@ -//Copyright (c) Corporation for National Research Initiatives +// Copyright (c) Corporation for National Research Initiatives package javatests; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.NoSuchElementException; +import org.python.util.Generic; + /** * @author updikca1 */ public abstract class ListTest { - + public static ListTest getArrayListTest(final boolean makeReadOnly) { return new ListTest() { - public List newInstance(Collection c) { - List l = null; - if(c == null) { - l = new ArrayList(); + + public List<Object> newInstance(Collection<Object> c) { + List<Object> l = null; + if (c == null) { + l = Generic.list(); } else { - l = new ArrayList(c); + l = new ArrayList<Object>(c); } - return (makeReadOnly) - ? Collections.unmodifiableList(l) - : l; + return (makeReadOnly) ? Collections.unmodifiableList(l) : l; } + public boolean isReadOnly() { return makeReadOnly; - } + } }; } - - public static void verifyImutability(List l) { - + + public static void verifyImutability(List<Object> l) { String message = "Expected UnsupportedOperationException."; - try { - l.add(0, new Integer(0)); - TestSupport.assertThat(false, message); + l.add(0, 0); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} - try { - l.add(new Integer(0)); - TestSupport.assertThat(false, message); + l.add(0); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} - try { - l.addAll(null); - TestSupport.assertThat(false, message); + l.addAll(null); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} - try { - l.addAll(0, null); - TestSupport.assertThat(false, message); + l.addAll(0, null); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} - try { - l.clear(); - TestSupport.assertThat(false, message); + l.clear(); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} - try { - l.remove(0); - TestSupport.assertThat(false, message); + l.remove(0); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} - try { l.remove(new Object()); - TestSupport.assertThat(false, message); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} - try { - l.removeAll(null); - TestSupport.assertThat(false, message); + l.removeAll(null); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} - try { l.retainAll(null); - TestSupport.assertThat(false, message); + TestSupport.fail(message); } catch (UnsupportedOperationException e) {} try { - l.set(0,new Integer(0)); - TestSupport.assertThat(false, message); - } catch (UnsupportedOperationException e) {} + l.set(0, 0); + TestSupport.fail(message); + } catch (UnsupportedOperationException e) {} } - - private final List nullList; - - protected List defaultList() { - List l = new ArrayList(); + + private final List<Object> nullList; + + protected List<Object> defaultList() { + List<Object> l = Generic.list(); for (int i = 0; i < 4; i++) { - l.add(new Integer(i)); + l.add(i); } return newInstance(l); } - + /** * Implementations must supply an empty list if the collection is null. - * @param c Initial collection or null for empty. + * + * @param c + * Initial collection or null for empty. * @return the List instance */ - public List newInstance(Collection c) { + public List<Object> newInstance(Collection<Object> c) { throw new UnsupportedOperationException("This method must be overridden"); } - + /** * @return true if the list is read-only (like PyTuple) */ public boolean isReadOnly() { throw new UnsupportedOperationException("This method must be overridden"); } - + { nullList = newInstance(Arrays.asList(new Object[] {null})); } - + public void testAll() { - test_get(); test_equals(); test_size(); test_contains(); test_containsAll(); - try { defaultList().hashCode(); test_hashCode(); } catch (Exception e) { // skip unhashable types } - test_subList(); test_lastIndexOf(); test_listIterator(); test_toArray(); test_toArray_typed(); - - if(!isReadOnly()) { + if (!isReadOnly()) { test_add(); test_add_index(); test_set(); test_clear(); test_addAll(); - test_addAll_index(); + test_addAll_index(); test_remove(); test_remove_index(); test_removeAll(); @@ -154,481 +143,404 @@ verifyImutability(newInstance(null)); } } - + /** Tests get(int index) */ public void test_get() { - TestSupport.assertThat(defaultList().get(0).equals(new Integer(0)), - "get() did not return expected value of Integer(0)"); + List<Object> l = defaultList(); + TestSupport.assertThat(l.get(0).equals(0), + "get() did not return expected value of Integer(0)"); try { - defaultList().get(-1); - TestSupport.assertThat(false, - "get(<negative index>) did not throw IndexOutOfBoundsException"); + l.get(-1); + TestSupport.fail("get(<negative index>) did not throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) {} - try { - defaultList().get(-1); - TestSupport.assertThat(false, - "get(<index too big>) did not throw IndexOutOfBoundsException"); + l.get(l.size()); + TestSupport.fail("get(<index too big>) did not throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) {} } - + /** Tests set(int index, Object element) */ public void test_set() { - try { newInstance(null).set(-1, "spam"); - TestSupport.assertThat(false, - "get(<negative index>) did not throw IndexOutOfBoundsException"); + TestSupport.fail("get(<negative index>) did not throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) {} - try { newInstance(null).set(0, "spam"); - TestSupport.assertThat(false, - "set(<index too big>) did not throw IndexOutOfBoundsException"); + TestSupport.fail("set(<index too big>) did not throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) {} - - List a = defaultList(); - a.set(a.size() - 1 , "spam"); - TestSupport.assertThat(a.get(a.size() - 1).equals("spam"), - "set() object was not retrieved via get()"); + List<Object> a = defaultList(); + a.set(a.size() - 1, "spam"); + TestSupport.assertThat(a.get(a.size() - 1).equals("spam"), + "set() object was not retrieved via get()"); } - - /** Tests add(Object o) */ - public void test_add() { - List a = newInstance(null); + + /** Tests add(Object o) */ + public void test_add() { + List<Object> a = newInstance(null); for (int i = 0; i < 4; i++) { - a.add(new Integer(i)); + a.add(i); } TestSupport.assertEquals(a, defaultList(), "add(Object o) failed"); } - + /** Tests isEmpty() */ public void test_isEmpty() { - List a = newInstance(null); - TestSupport.assertThat(a.isEmpty(), - "isEmpty() is false on an emtpy List"); + List<Object> a = newInstance(null); + TestSupport.assertThat(a.isEmpty(), "isEmpty() is false on an emtpy List"); a.addAll(defaultList()); - TestSupport.assertThat(!a.isEmpty(), - "isEmpty() is true on a non-empty List)" ); + TestSupport.assertThat(!a.isEmpty(), "isEmpty() is true on a non-empty List)"); a.clear(); - TestSupport.assertThat(a.isEmpty(), - "isEmpty() is false on an emtpy List"); + TestSupport.assertThat(a.isEmpty(), "isEmpty() is false on an emtpy List"); } - + /** Tests size() */ - public void test_size() { - List b = newInstance(null); + public void test_size() { + List<Object> b = newInstance(null); TestSupport.assertThat(b.size() == 0, "empty list size was not 0"); - TestSupport.assertThat(defaultList().size() == 4, - "default list did not have a size of 4"); + TestSupport.assertThat(defaultList().size() == 4, "default list did not have a size of 4"); } - - /** Tests add(int index, Object element) */ + + /** Tests add(int index, Object element) */ public void test_add_index() { - List a = newInstance(null); - List b = defaultList(); + List<Object> a = newInstance(null); + List<Object> b = defaultList(); for (int i = 0; i < b.size(); i++) { a.add(i, b.get(i)); } - try { - a.add(a.size() + 1, new Integer(a.size() + 1)); - TestSupport.assertThat(false, "expected IndexOutOfBoundsException"); + a.add(a.size() + 1, new Integer(a.size() + 1)); + TestSupport.fail("expected IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) {} - try { - a.add(-1, new Integer(-1)); - TestSupport.assertThat(false, "expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) {} + a.add(-1, new Integer(-1)); + TestSupport.fail("expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) {} } - - /** Tests equals(Object o)*/ + + /** Tests equals(Object o) */ public void test_equals() { - TestSupport.assertEquals(defaultList(), defaultList(), - "Identical lists weren't equal()"); - TestSupport.assertNotEquals(newInstance(null), defaultList(), - "Different lists were equal()"); - TestSupport.assertNotEquals(newInstance(null), new Object(), - "List was equal to a non-List type"); + TestSupport.assertEquals(defaultList(), defaultList(), "Identical lists weren't equal()"); + TestSupport.assertNotEquals(newInstance(null), + defaultList(), + "Different lists were equal()"); + TestSupport.assertNotEquals(newInstance(null), + new Object(), + "List was equal to a non-List type"); } - + /** Tests addAll(Collection c) */ public void test_addAll() { - List a = defaultList(); - List b = defaultList(); - - TestSupport.assertThat(a.addAll(b) == true, - "Mutating addAll(Collection) returned false"); - TestSupport.assertThat(a.addAll(newInstance(null)) == false, - "Idempotent addAll(Collection) returned true"); - TestSupport.assertThat(b.addAll(b) == true, - "Mutating addAll(Collection) returned false"); - TestSupport.assertEquals(a, b, - "Expected equal objects from addAll(collection)"); - TestSupport.assertThat(a.size() == 8, - "Expected List to have size 8 after addAll(Collection)"); + List<Object> a = defaultList(); + List<Object> b = defaultList(); + TestSupport.assertThat(a.addAll(b) == true, "Mutating addAll(Collection) returned false"); + TestSupport.assertThat(a.addAll(newInstance(null)) == false, + "Idempotent addAll(Collection) returned true"); + TestSupport.assertThat(b.addAll(b) == true, "Mutating addAll(Collection) returned false"); + TestSupport.assertEquals(a, b, "Expected equal objects from addAll(collection)"); + TestSupport.assertThat(a.size() == 8, + "Expected List to have size 8 after addAll(Collection)"); } - - /** Tests indexOf(Object o) */ + + /** Tests indexOf(Object o) */ public void indexOf() { - TestSupport.assertThat(defaultList().indexOf(new Integer(3)) == 3, - "indexOf(3) did not return 3"); - TestSupport.assertThat(defaultList().indexOf(new Integer(42)) == -1, - "indexOf() non-existing entry did not return -1"); - TestSupport.assertThat(defaultList().indexOf(null) == -1, - "indexOf() non-existing null did not return -1"); - + TestSupport.assertThat(defaultList().indexOf(3) == 3, "indexOf(3) did not return 3"); + TestSupport.assertThat(defaultList().indexOf(42) == -1, + "indexOf() non-existing entry did not return -1"); + TestSupport.assertThat(defaultList().indexOf(null) == -1, + "indexOf() non-existing null did not return -1"); } - - /** Tests contains(Object o) */ - public void test_contains() { - TestSupport.assertThat(defaultList().contains(new Integer(42)) == false, - "contains() returned true for non-existing entry"); - TestSupport.assertThat(defaultList().contains(new Integer(0)) == true, - "contains() returned false for existing entry"); - TestSupport.assertThat(nullList.contains(null) == true, - "contains() returned false for existing null entry"); - TestSupport.assertThat(defaultList().contains(null) == false, - "contains() returned true for non-existing null entry"); + + /** Tests contains(Object o) */ + public void test_contains() { + TestSupport.assertThat(defaultList().contains(42) == false, + "contains() returned true for non-existing entry"); + TestSupport.assertThat(defaultList().contains(0) == true, + "contains() returned false for existing entry"); + TestSupport.assertThat(nullList.contains(null) == true, + "contains() returned false for existing null entry"); + TestSupport.assertThat(defaultList().contains(null) == false, + "contains() returned true for non-existing null entry"); } - + /** Tests remove(Object o) */ public void test_remove() { - List a = defaultList(); + List<Object> a = defaultList(); a.add(null); - TestSupport.assertThat(a.remove(null) == true, - "remove() existing null entry returned false"); - TestSupport.assertThat(a.remove(null) == false, - "remove() non-existing null entry returned false"); + TestSupport.assertThat(a.remove(null) == true, + "remove() existing null entry returned false"); + TestSupport.assertThat(a.remove(null) == false, + "remove() non-existing null entry returned false"); a.add("spam"); - TestSupport.assertThat(a.remove("spam") == true, - "remove() existing entry returned false"); - TestSupport.assertThat(a.remove("spam") == false, - "remove() non-existing entry returned true"); + TestSupport.assertThat(a.remove("spam") == true, "remove() existing entry returned false"); + TestSupport.assertThat(a.remove("spam") == false, + "remove() non-existing entry returned true"); } - - + /** Tests remove(int index) */ - public void test_remove_index() { - - List a = defaultList(); + public void test_remove_index() { + List<Object> a = defaultList(); for (int i = 0, n = a.size(); i < n; i++) { a.remove(0); } - TestSupport.assertThat(a.size() == 0, - "remove()-d all entries but size() not 0"); - + TestSupport.assertThat(a.size() == 0, "remove()-d all entries but size() not 0"); try { a.remove(0); - TestSupport.assertThat(false, - "removing a non-existing index did not throw exception"); - } catch(IndexOutOfBoundsException e) {} + TestSupport.fail("removing a non-existing index did not throw exception"); + } catch (IndexOutOfBoundsException e) {} } - + /** Tests lastIndexOf(Object o) */ public void test_lastIndexOf() { - // avoid calling any mutable methods - List l = new ArrayList(defaultList()); - l.add(new Integer(0)); - + // avoid calling any mutable methods + List<Object> l = new ArrayList<Object>(defaultList()); + l.add(0); // now get the immutable version - List a = newInstance(l); - - TestSupport.assertThat(a.lastIndexOf(new Integer(0)) == 4, - "lastIndexOf() did not return 4"); - TestSupport.assertThat(a.lastIndexOf(new Integer(42)) == -1, - "lastIndexOf() non-existing value did not return -1"); + List<Object> a = newInstance(l); + TestSupport.assertThat(a.lastIndexOf(0) == 4, "lastIndexOf() did not return 4"); + TestSupport.assertThat(a.lastIndexOf(42) == -1, + "lastIndexOf() non-existing value did not return -1"); } - - /** Tests removeAll(Collection c) */ + + /** Tests removeAll(Collection c) */ public void test_removeAll() { - List a = defaultList(); - TestSupport.assertThat(a.removeAll(a) == true, - "mutating removeAll() did not return true"); - TestSupport.assertThat(a.removeAll(a) == false, - "idempotent removeAll did not return false"); - TestSupport.assertThat(a.removeAll(nullList) == false, - "idempotent removeAll did not return false"); - - List yanl = newInstance(null); + List<Object> a = defaultList(); + TestSupport.assertThat(a.removeAll(a) == true, "mutating removeAll() did not return true"); + TestSupport.assertThat(a.removeAll(a) == false, "idempotent removeAll did not return false"); + TestSupport.assertThat(a.removeAll(nullList) == false, + "idempotent removeAll did not return false"); + List<Object> yanl = newInstance(null); yanl.addAll(nullList); - TestSupport.assertThat(yanl.removeAll(nullList) == true, - "mutating removeAll() did not return true"); - TestSupport.assertThat(yanl.size() == 0, - "empty list had non-zero size"); + TestSupport.assertThat(yanl.removeAll(nullList) == true, + "mutating removeAll() did not return true"); + TestSupport.assertThat(yanl.size() == 0, "empty list had non-zero size"); TestSupport.assertThat(yanl.removeAll(newInstance(null)) == false, - "idempotent removeAll did not return false"); - + "idempotent removeAll did not return false"); } - - /** Tests addAll(int index, Collection c) */ + + /** Tests addAll(int index, Collection c) */ public void test_addAll_index() { - List a = defaultList(); - List b = newInstance(null); - TestSupport.assertThat(b.addAll(0,a) == true, - "mutating addAll(index, Collection) did not return true"); - TestSupport.assertEquals(a, b, - "addAll(index, Collection) instances failed equals test"); - TestSupport.assertThat(a.addAll(0, newInstance(null)) == false, - "idempotent addAll(index, Collection) did not return false"); - TestSupport.assertThat(b.addAll(0,b) == true, - "mutating addAll(index, Collection) did not return true"); - + List<Object> a = defaultList(); + List<Object> b = newInstance(null); + TestSupport.assertThat(b.addAll(0, a) == true, + "mutating addAll(index, Collection) did not return true"); + TestSupport.assertEquals(a, b, "addAll(index, Collection) instances failed equals test"); + TestSupport.assertThat(a.addAll(0, newInstance(null)) == false, + "idempotent addAll(index, Collection) did not return false"); + TestSupport.assertThat(b.addAll(0, b) == true, + "mutating addAll(index, Collection) did not return true"); // Since PyObjectList has some specific handling when it detects // addAll on a PySequenceList, make sure the general case works. b = newInstance(null); - b.addAll(new ArrayList(defaultList())); - TestSupport.assertEquals(defaultList(), b, - "addAll(index, <ArrayList>) failed equals test"); + b.addAll(new ArrayList<Object>(defaultList())); + TestSupport.assertEquals(defaultList(), b, "addAll(index, <ArrayList>) failed equals test"); } - - /** Tests hashCode() */ + /** Tests hashCode() */ public void test_hashCode() { - List a = defaultList(); - TestSupport.assertThat(a.hashCode() == defaultList().hashCode(), - "Instances with same internal state have different hashcode"); - TestSupport.assertThat(a.hashCode() != newInstance(null).hashCode(), - "Instances with different internal state have the same hashcode"); - + List<Object> a = defaultList(); + TestSupport.assertThat(a.hashCode() == defaultList().hashCode(), + "Instances with same internal state have different hashcode"); + TestSupport.assertThat(a.hashCode() != newInstance(null).hashCode(), + "Instances with different internal state have the same hashcode"); if (isReadOnly() == false) { - List b = newInstance(null); + List<Object> b = newInstance(null); b.addAll(a); b.remove(0); - TestSupport.assertThat(a.hashCode()!= b.hashCode(), - "Instances with different internal state have the same hashcode"); + TestSupport.assertThat(a.hashCode() != b.hashCode(), + "Instances with different internal state have the same hashcode"); } - } - - /** Tests clear() */ + + /** Tests clear() */ public void test_clear() { - List a = defaultList(); + List<Object> a = defaultList(); a.clear(); - TestSupport.assertThat(a.size() == 0, - "clear()-ed list did not have size of 0"); + TestSupport.assertThat(a.size() == 0, "clear()-ed list did not have size of 0"); } - - /** Tests subList(int fromIndex, int toIndex) */ + + /** Tests subList(int fromIndex, int toIndex) */ public void test_subList() { - List a = defaultList(); - TestSupport.assertThat((a.subList(0, a.size()) != a), - "subList() returned the same instance"); - TestSupport.assertEquals(a.subList(0, a.size()), a, - "Complete subList() did not equal original List"); - TestSupport.assertThat(a.subList(0,0).size() == 0, - "empty subList had non-zero size"); - + List<Object> a = defaultList(); + TestSupport.assertThat((a.subList(0, a.size()) != a), + "subList() returned the same instance"); + TestSupport.assertEquals(a.subList(0, a.size()), + a, + "Complete subList() did not equal original List"); + TestSupport.assertThat(a.subList(0, 0).size() == 0, "empty subList had non-zero size"); try { - a.subList(-1,1); - TestSupport.assertThat(false, "Expected IndexOutOfBoundsException"); + a.subList(-1, 1); + TestSupport.fail("Expected IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) {} - try { - a.subList(1,0); - TestSupport.assertThat(false, "Expected IllegalArgumentException"); + a.subList(1, 0); + TestSupport.fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) {} - try { - a.subList(0,a.size() + 1); - TestSupport.assertThat(false, "Expected IndexOutOfBoundsException"); + a.subList(0, a.size() + 1); + TestSupport.fail("Expected IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) {} - if (!isReadOnly()) { - a.subList(0, a.size()).clear(); - TestSupport.assertThat(a.size() == 0, - "clear()-ed sublist did not have zero size"); - List c = newInstance(null); + TestSupport.assertThat(a.size() == 0, "clear()-ed sublist did not have zero size"); + List<Object> c = newInstance(null); c.addAll(defaultList()); - List d = c.subList(1,3); - TestSupport.assertThat(d.size() == 2, - "Expected subList to have size of 2"); - TestSupport.assertThat(c.set(1,"canned").equals(new Integer(1)), - "subList.set() did not return Integer(1) from index 1" + - " of defaultList"); + List<Object> d = c.subList(1, 3); + TestSupport.assertThat(d.size() == 2, "Expected subList to have size of 2"); + TestSupport.assertThat(c.set(1, "canned").equals(1), + "subList.set() did not return Integer(1) from index 1" + + " of defaultList"); TestSupport.assertThat(d.get(0).equals("canned"), - "subList does not update with changes to parent"); - d.set(0,"spam"); - TestSupport.assertThat(c.get(1).equals("spam"), - "parent does not update with changes to subList child"); + "subList does not update with changes to parent"); + d.set(0, "spam"); + TestSupport.assertThat(c.get(1).equals("spam"), + "parent does not update with changes to subList child"); } else { - List b = a.subList(0, a.size()); + List<Object> b = a.subList(0, a.size()); verifyImutability(b); } - } - + /** Tests retainAll(Collection c) */ - public void test_retainAll() { - List a = defaultList(); + public void test_retainAll() { + List<Object> a = defaultList(); a.retainAll(defaultList()); - TestSupport.assertEquals(a, defaultList(), - "retainAll(<equal List>) does not equal original list"); + TestSupport.assertEquals(a, + defaultList(), + "retainAll(<equal List>) does not equal original list"); a = defaultList(); a.retainAll(newInstance(null)); - TestSupport.assertThat(a.size() == 0, - "retainAll(<empty List>))does not have size of zero"); - + TestSupport.assertThat(a.size() == 0, "retainAll(<empty List>))does not have size of zero"); a = defaultList(); a.remove(0); a.remove(0); - a.add(new Integer(4)); - a.add(new Integer(5)); - List b = newInstance(null); - b.add(new Integer(2)); - b.add(new Integer(3)); + a.add(4); + a.add(5); + List<Object> b = newInstance(null); + b.add(2); + b.add(3); a.retainAll(b); - TestSupport.assertEquals(a, b, - "retainAll() on overlap of indices [2,3] did not return that List"); + TestSupport.assertEquals(a, + b, + "retainAll() on overlap of indices [2,3] did not return that List"); } - + /** Tests containsAll(Collection c) */ public void test_containsAll() { TestSupport.assertThat(defaultList().containsAll(defaultList()), - "containsAll(<identical List> was false"); - TestSupport.assertThat(defaultList().containsAll(newInstance(null)), - "containsAll(<empty List>) was false"); + "containsAll(<identical List> was false"); + TestSupport.assertThat(defaultList().containsAll(newInstance(null)), + "containsAll(<empty List>) was false"); TestSupport.assertThat(newInstance(null).containsAll(defaultList()) == false, - "containsAll(<disjoint List>) returned true"); - TestSupport.assertThat(defaultList().containsAll(defaultList().subList(1,3)), - "containsAll(<subList>) was false"); + "containsAll(<disjoint List>) returned true"); + TestSupport.assertThat(defaultList().containsAll(defaultList().subList(1, 3)), + "containsAll(<subList>) was false"); } - + /** Tests iterator() */ public void test_iterator() { - - TestSupport.assertThat(newInstance(null).iterator().hasNext() == false, - "Iterator for empty list thinks it hasNext()"); + TestSupport.assertThat(newInstance(null).iterator().hasNext() == false, + "Iterator for empty list thinks it hasNext()"); try { newInstance(null).iterator().next(); - TestSupport.assertThat(false, "expected NoSuchElementException"); + TestSupport.fail("expected NoSuchElementException"); } catch (NoSuchElementException e) {} - - List a = defaultList(); + List<Object> a = defaultList(); int i = 0; - for (Iterator iter = a.iterator(); iter.hasNext(); ) { - TestSupport.assertThat(iter.next() == a.get(i++), - "Iterator next() failed identity test"); + for (Object element : a) { + TestSupport.assertThat(element == a.get(i++), "Iterator next() failed identity test"); } - TestSupport.assertThat(i == a.size(), - "Iterator did not iterator over entire list"); + TestSupport.assertThat(i == a.size(), "Iterator did not iterator over entire list"); } - + public void test_listIterator() { - - ListIterator li = newInstance(null).listIterator(); - TestSupport.assertThat(li.hasNext() == false, - "ListIterator.hasNext() is true for empty List"); - - TestSupport.assertThat(li.hasPrevious() == false, - "ListIterator.hasPrevious() is true for empty List"); - + ListIterator<Object> li = newInstance(null).listIterator(); + TestSupport.assertThat(!li.hasNext(), "ListIterator.hasNext() is true for empty List"); + TestSupport.assertThat(!li.hasPrevious(), + "ListIterator.hasPrevious() is true for empty List"); try { li.next(); - TestSupport.assertThat(false, "expected NoSuchElementException"); + TestSupport.fail("expected NoSuchElementException"); } catch (NoSuchElementException e) {} - try { li.previous(); - TestSupport.assertThat(false, "expected NoSuchElementException"); + TestSupport.fail("expected NoSuchElementException"); } catch (NoSuchElementException e) {} - int nextIndex = li.nextIndex(); - TestSupport.assertThat(nextIndex == 0, - "ListIterator.nextIndex() on empty List did not return 0"); - + TestSupport.assertThat(nextIndex == 0, + "ListIterator.nextIndex() on empty List did not return 0"); int prevIndex = li.previousIndex(); TestSupport.assertThat(prevIndex == -1, - "ListIterator.previousIndex() on empty List did not return -1"); - - List l = new ArrayList(); - l.add(new Integer(1)); + "ListIterator.previousIndex() on empty List did not return -1"); + List<Object> l = Generic.list(); + l.add(1); li = newInstance(l).listIterator(); - TestSupport.assertThat(li.hasPrevious() == false, - "ListIterator.hasPrevious() is true with nothing previous"); - - TestSupport.assertThat(li.hasNext() == true, - "ListIterator.hasNext() is false with next present"); - TestSupport.assertThat(li.next().equals(new Integer(1)), - "ListIterator.next() did not return expected Integer(1)"); - + TestSupport.assertThat(!li.hasPrevious(), + "ListIterator.hasPrevious() is true with nothing previous"); + TestSupport.assertThat(li.hasNext(), "ListIterator.hasNext() is false with next present"); + TestSupport.assertThat(li.next().equals(1), + "ListIterator.next() did not return expected Integer(1)"); if (!isReadOnly()) { - li.remove(); - TestSupport.assertThat(li.hasNext() == false, - "ListIterator.hasNext() is true for empty List"); - - TestSupport.assertThat(li.hasPrevious() == false, - "ListIterator.hasPrevious() is true for empty List"); - try { - li.set(new Integer(42)); - TestSupport.assertThat(false, "expected IllegalStateException"); - } catch (IllegalStateException e) {} - - try { - li.remove(); - TestSupport.assertThat(false, "expected IllegalStateException"); - } catch (IllegalStateException e) {} + li.remove(); + TestSupport.assertThat(!li.hasNext(), "ListIterator.hasNext() is true for empty List"); + TestSupport.assertThat(!li.hasPrevious(), + "ListIterator.hasPrevious() is true for empty List"); + try { + li.set(42); + TestSupport.fail("expected IllegalStateException"); + } catch (IllegalStateException e) {} + try { + li.remove(); + TestSupport.fail("expected IllegalStateException"); + } catch (IllegalStateException e) {} } - - l = new ArrayList(); - l.add(new Integer(0)); - l.add(new Integer(1)); - l.add(new Integer(2)); - + l = Generic.list(); + l.add(0); + l.add(1); + l.add(2); li = newInstance(l).listIterator(); - for (int i = 0, n = l.size(); i < n; i++) { - TestSupport.assertThat(li.next().equals(new Integer(i)), - "ListIterator.previous did not return expected value"); + TestSupport.assertThat(li.next().equals(i), + "ListIterator.previous did not return expected value"); } - while (!isReadOnly() && li.hasNext()) { li.next(); - li.set(new Integer(42)); - TestSupport.assertThat(li.previous().equals(new Integer(42)), - "ListIterator.previous() did not return the value that was set()"); + li.set(42); + TestSupport.assertThat(li.previous().equals(42), + "ListIterator.previous() did not return the value that was set()"); li.remove(); } - - if(isReadOnly()) { + if (isReadOnly()) { li = newInstance(null).listIterator(); } - li = defaultList().listIterator(2); - TestSupport.assertThat(li.next().equals(new Integer(2)), - "List.listIteraor(index) did not return expected value"); - TestSupport.assertThat(li.next().equals(new Integer(3)), - "List.listIteraor(index) did not return expected value"); - TestSupport.assertThat(li.hasNext() == false, - "listIterator.hasNext() at end of list returned true"); - + TestSupport.assertThat(li.next().equals(2), + "List.listIteraor(index) did not return expected value"); + ... [truncated message content] |
From: <cg...@us...> - 2009-01-11 06:04:17
|
Revision: 5916 http://jython.svn.sourceforge.net/jython/?rev=5916&view=rev Author: cgroves Date: 2009-01-11 06:04:11 +0000 (Sun, 11 Jan 2009) Log Message: ----------- Convert to PyObject going into and Object coming out of java.util.Set methods on PySet like PyDictionary does for Map methods and PyList does for List methods. Without this, Java code can add non-PyObjects to the underlying set causing the Python access to it to throw ClassCastExceptions. Modified Paths: -------------- trunk/jython/src/org/python/core/BaseSet.java trunk/jython/src/org/python/core/PyFrozenSet.java trunk/jython/src/org/python/core/PyIterator.java trunk/jython/src/org/python/core/PyJavaType.java trunk/jython/src/org/python/core/PySet.java trunk/jython/src/org/python/core/PyType.java trunk/jython/src/org/python/util/Generic.java trunk/jython/tests/java/javatests/ListTest.java Added Paths: ----------- trunk/jython/Lib/test/test_set_jy.py trunk/jython/tests/java/javatests/PySetInJavaTest.java Removed Paths: ------------- trunk/jython/src/org/python/core/PySetIterator.java Added: trunk/jython/Lib/test/test_set_jy.py =================================================================== --- trunk/jython/Lib/test/test_set_jy.py (rev 0) +++ trunk/jython/Lib/test/test_set_jy.py 2009-01-11 06:04:11 UTC (rev 5916) @@ -0,0 +1,35 @@ +from test import test_support +import unittest + +from java.util import Random +from javatests import PySetInJavaTest + +class SetInJavaTest(unittest.TestCase): + "Tests for derived dict behaviour" + def test_using_PySet_as_Java_Set(self): + PySetInJavaTest.testPySetAsJavaSet() + + def test_accessing_items_added_in_java(self): + s = PySetInJavaTest.createPySetContainingJavaObjects() + for v in s: + self.assert_(v in s) + if isinstance(v, unicode): + self.assertEquals("value", v) + else: + v.nextInt()#Should be a java.util.Random; ensure we can call it + + def test_java_accessing_items_added_in_python(self): + # Test a type that should be coerced into a Java type, a Java instance + # that should be wrapped, and a Python instance that should pass + # through as itself with str, Random and tuple respectively. + s = set(["value", Random(), ("tuple", "of", "stuff")]) + PySetInJavaTest.accessAndRemovePySetItems(s) + self.assertEquals(0, len(s))# Check that the Java removal affected the underlying set + + + +def test_main(): + test_support.run_unittest(SetInJavaTest) + +if __name__ == '__main__': + test_main() Modified: trunk/jython/src/org/python/core/BaseSet.java =================================================================== --- trunk/jython/src/org/python/core/BaseSet.java 2009-01-11 03:21:25 UTC (rev 5915) +++ trunk/jython/src/org/python/core/BaseSet.java 2009-01-11 06:04:11 UTC (rev 5916) @@ -1,41 +1,35 @@ package org.python.core; +import java.lang.reflect.Array; import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; import java.util.Iterator; import java.util.Set; public abstract class BaseSet extends PyObject implements Set { /** The underlying Set. */ - protected Set _set; + protected Set<PyObject> _set; /** * Create a new Python set instance from the specified Set object. - * - * @param set An Set object. */ - protected BaseSet(Set set) { + protected BaseSet(Set<PyObject> set) { _set = set; } - protected BaseSet(PyType type, Set set) { + protected BaseSet(PyType type, Set<PyObject> set) { super(type); _set = set; } - protected void _update(PyObject data) throws PyIgnoreMethodTag { + protected void _update(PyObject data) { _update(_set, data); } /** * Update the underlying set with the contents of the iterable. - * - * @param data An iterable instance. - * @throws PyIgnoreMethodTag Ignore. */ - protected static Set _update(Set set, PyObject data) throws PyIgnoreMethodTag { + protected static Set<PyObject> _update(Set<PyObject> set, PyObject data) { if (data == null) { return set; } @@ -53,7 +47,7 @@ /** * The union of <code>this</code> with <code>other</code>. <p/> <br/> (I.e. all elements * that are in either set) - * + * * @param other * A <code>BaseSet</code> instance. * @return The union of the two sets as a new set. @@ -115,9 +109,9 @@ final PyObject baseset_difference(PyObject other) { BaseSet bs = (other instanceof BaseSet) ? (BaseSet)other : new PySet(other); - Set set = bs._set; + Set<PyObject> set = bs._set; BaseSet o = BaseSet.makeNewSet(getType()); - for (Object p : _set) { + for (PyObject p : _set) { if (!set.contains(p)) { o._set.add(p); } @@ -153,12 +147,12 @@ final PyObject baseset_symmetric_difference(PyObject other) { BaseSet bs = (other instanceof BaseSet) ? (BaseSet)other : new PySet(other); BaseSet o = BaseSet.makeNewSet(getType()); - for (Object p : _set) { + for (PyObject p : _set) { if (!bs._set.contains(p)) { o._set.add(p); } } - for (Object p : bs._set) { + for (PyObject p : bs._set) { if (!_set.contains(p)) { o._set.add(p); } @@ -206,7 +200,22 @@ } final PyObject baseset___iter__() { - return new PySetIterator(_set); + return new PyIterator() { + private int size = _set.size(); + + private Iterator<PyObject> iterator = _set.iterator(); + + @Override + public PyObject __iternext__() { + if (_set.size() != size) { + throw Py.RuntimeError("set changed size during iteration"); + } + if (iterator.hasNext()) { + return iterator.next(); + } + return null; + } + }; } public boolean __contains__(PyObject other) { @@ -257,7 +266,7 @@ } final PyObject baseset___le__(PyObject other) { - BaseSet bs = _binary_sanity_check(other); + _binary_sanity_check(other); return baseset_issubset(other); } @@ -266,7 +275,7 @@ } final PyObject baseset___ge__(PyObject other) { - BaseSet bs = _binary_sanity_check(other); + _binary_sanity_check(other); return baseset_issuperset(other); } @@ -299,7 +308,7 @@ public PyObject __reduce__() { return baseset___reduce__(); } - + final PyObject baseset___reduce__(){ PyObject args = new PyTuple(new PyList((PyObject)this)); PyObject dict = __findattr__("__dict__"); @@ -309,19 +318,6 @@ return new PyTuple(getType(), args, dict); } - /** - * Return this instance as a Java object. Only coerces to Collection and subinterfaces. - * - * @param c The Class to coerce to. - * @return the underlying HashSet (not a copy) - */ - public Object __tojava__(Class c) { - if (Collection.class.isAssignableFrom(c)) { - return Collections.unmodifiableSet(_set); - } - return super.__tojava__(c); - } - final PyObject baseset_union(PyObject other) { BaseSet result = BaseSet.makeNewSet(getType(), this); result._update(other); @@ -388,8 +384,8 @@ return name + "(...)"; } StringBuilder buf = new StringBuilder(name).append("(["); - for (Iterator i = _set.iterator(); i.hasNext();) { - buf.append(((PyObject)i.next()).__repr__().toString()); + for (Iterator<PyObject> i = _set.iterator(); i.hasNext();) { + buf.append((i.next()).__repr__().toString()); if (i.hasNext()) { buf.append(", "); } @@ -408,20 +404,20 @@ } /** - * Return a PyFrozenSet whose contents are shared with value when - * value is a BaseSet and pye is a TypeError. + * Return a PyFrozenSet whose contents are shared with value when value is a BaseSet and pye is + * a TypeError. * - * WARNING: The PyFrozenSet returned is only intended to be used - * temporarily (and internally); since its contents are shared - * with value, it could be mutated! - * - * This is better than special-casing behavior based on - * isinstance, because a Python subclass can override, say, - * __hash__ and all of a sudden you can't assume that a - * non-PyFrozenSet is unhashable anymore. + * WARNING: The PyFrozenSet returned is only intended to be used temporarily (and internally); + * since its contents are shared with value, it could be mutated! * - * @param pye The exception thrown from a hashable operation. - * @param value The object which was unhashable. + * This is better than special-casing behavior based on isinstance, because a Python subclass + * can override, say, __hash__ and all of a sudden you can't assume that a non-PyFrozenSet is + * unhashable anymore. + * + * @param pye + * The exception thrown from a hashable operation. + * @param value + * The object which was unhashable. * @return A PyFrozenSet if appropriate, otherwise the pye is rethrown */ protected final PyFrozenSet asFrozen(PyException pye, PyObject value) { @@ -442,7 +438,7 @@ protected static BaseSet makeNewSet(PyType type) { return makeNewSet(type, null); } - + /** * Create a new <et of type from iterable. * @@ -477,43 +473,89 @@ return _set.isEmpty(); } - public Object[] toArray() { - return _set.toArray(); - } - public boolean add(Object o) { - return _set.add(o); + return _set.add(Py.java2py(o)); } public boolean contains(Object o) { - return _set.contains(o); + return _set.contains(Py.java2py(o)); } public boolean remove(Object o) { - return _set.remove(o); + return _set.remove(Py.java2py(o)); } public boolean addAll(Collection c) { - return _set.addAll(c); + boolean added = false; + for (Object object : c) { + added |= add(object); + } + return added; } public boolean containsAll(Collection c) { - return _set.containsAll(c); + for (Object object : c) { + if (!_set.contains(Py.java2py(object))) { + return false; + } + } + return true; } public boolean removeAll(Collection c) { - return _set.removeAll(c); + boolean removed = false; + for (Object object : c) { + removed |= _set.remove(Py.java2py(object)); + } + return removed; } public boolean retainAll(Collection c) { - return _set.retainAll(c); + boolean modified = false; + Iterator e = iterator(); + while (e.hasNext()) { + if (!c.contains(e.next())) { + e.remove(); + modified = true; + } + } + return modified; } public Iterator iterator() { - return _set.iterator(); + return new Iterator() { + Iterator<PyObject> real = _set.iterator(); + + public boolean hasNext() { + return real.hasNext(); + } + + public Object next() { + return Py.tojava(real.next(), Object.class); + } + + public void remove() { + real.remove(); + } + }; } + public Object[] toArray() { + return toArray(new Object[size()]); + } + public Object[] toArray(Object a[]) { - return _set.toArray(a); + int size = size(); + if (a.length < size) { + a = (Object[])Array.newInstance(a.getClass().getComponentType(), size); + } + Iterator<PyObject> it = iterator(); + for (int i = 0; i < size; i++) { + a[i] = it.next(); + } + if (a.length > size) { + a[size] = null; + } + return a; } } Modified: trunk/jython/src/org/python/core/PyFrozenSet.java =================================================================== --- trunk/jython/src/org/python/core/PyFrozenSet.java 2009-01-11 03:21:25 UTC (rev 5915) +++ trunk/jython/src/org/python/core/PyFrozenSet.java 2009-01-11 06:04:11 UTC (rev 5916) @@ -11,9 +11,9 @@ @ExposedType(name = "frozenset", base = PyObject.class) public class PyFrozenSet extends BaseSet { - + public static final PyType TYPE = PyType.fromClass(PyFrozenSet.class); - + public PyFrozenSet() { super(new HashSet<PyObject>()); } @@ -204,8 +204,8 @@ } public Iterator iterator() { + final Iterator i = super.iterator(); return new Iterator() { - Iterator i = _set.iterator(); public boolean hasNext() { return i.hasNext(); Modified: trunk/jython/src/org/python/core/PyIterator.java =================================================================== --- trunk/jython/src/org/python/core/PyIterator.java 2009-01-11 03:21:25 UTC (rev 5915) +++ trunk/jython/src/org/python/core/PyIterator.java 2009-01-11 06:04:11 UTC (rev 5916) @@ -2,21 +2,18 @@ package org.python.core; /** - * An abstract helper class usefull when implementing an iterator object. This - * implementation supply a correct __iter__() and a next() method based on the - * __iternext__() implementation. The __iternext__() method must be supplied by - * the subclass. + * An abstract helper class useful when implementing an iterator object. This implementation supply + * a correct __iter__() and a next() method based on the __iternext__() implementation. The + * __iternext__() method must be supplied by the subclass. * - * If the implementation raises a StopIteration exception, it should be stored - * in stopException so the correct exception can be thrown to preserve the line - * numbers in the traceback. + * If the implementation raises a StopIteration exception, it should be stored in stopException so + * the correct exception can be thrown to preserve the line numbers in the traceback. */ public abstract class PyIterator extends PyObject { protected PyException stopException; - public PyIterator() { - } + public PyIterator() {} public PyIterator(PyType subType) { super(subType); Modified: trunk/jython/src/org/python/core/PyJavaType.java =================================================================== --- trunk/jython/src/org/python/core/PyJavaType.java 2009-01-11 03:21:25 UTC (rev 5915) +++ trunk/jython/src/org/python/core/PyJavaType.java 2009-01-11 06:04:11 UTC (rev 5916) @@ -87,8 +87,7 @@ bases[i] = PyType.fromClass(underlying_class.getInterfaces()[i - 1]); } Set<PyObject> seen = Generic.set(); - List<PyObject> mros = Generic.list(); - mros.add(this); + List<PyObject> mros = Generic.list(this); for (PyObject obj : bases) { for (PyObject mroObj : ((PyType)obj).mro) { if (seen.add(mroObj)) { Modified: trunk/jython/src/org/python/core/PySet.java =================================================================== --- trunk/jython/src/org/python/core/PySet.java 2009-01-11 03:21:25 UTC (rev 5915) +++ trunk/jython/src/org/python/core/PySet.java 2009-01-11 06:04:11 UTC (rev 5916) @@ -13,7 +13,7 @@ public class PySet extends BaseSet { public static final PyType TYPE = PyType.fromClass(PySet.class); - + public PySet() { super(new ConcurrentHashSet<PyObject>()); } @@ -25,7 +25,7 @@ public PySet(PyObject data) { super(_update(new ConcurrentHashSet<PyObject>(), data)); } - + @ExposedNew @ExposedMethod final void set___init__(PyObject[] args, String[] kwds) { @@ -40,7 +40,7 @@ _set.clear(); _update(args[0]); } - + @ExposedMethod(type = MethodType.BINARY) final PyObject set___cmp__(PyObject o) { return new PyInteger(baseset___cmp__(o)); @@ -181,7 +181,7 @@ return set___iand__(other); } - @ExposedMethod(type = MethodType.BINARY) + @ExposedMethod(type = MethodType.BINARY) final PyObject set___iand__(PyObject other) { if (!(other instanceof BaseSet)) { return null; @@ -281,8 +281,7 @@ } BaseSet bs = (other instanceof BaseSet) ? (BaseSet)other : new PySet(other); - for (Iterator iterator = bs._set.iterator(); iterator.hasNext();) { - Object o = iterator.next(); + for (PyObject o : bs._set) { if (_set.contains(o)) { _set.remove(o); } else { Deleted: trunk/jython/src/org/python/core/PySetIterator.java =================================================================== --- trunk/jython/src/org/python/core/PySetIterator.java 2009-01-11 03:21:25 UTC (rev 5915) +++ trunk/jython/src/org/python/core/PySetIterator.java 2009-01-11 06:04:11 UTC (rev 5916) @@ -1,52 +0,0 @@ -package org.python.core; - -import java.util.Iterator; -import java.util.Set; - -public class PySetIterator extends PyObject { - - private Set set; - - private int size; - - private Iterator<PyObject> iterator; - - public PySetIterator(Set set) { - super(); - this.set = set; - size = set.size(); - iterator = set.iterator(); - } - - public PyObject __iter__() { - return this; - } - - /** - * Returns the next item in the iteration or raises a StopIteration. - * - * @return the next item in the iteration - */ - public PyObject next() { - PyObject o = this.__iternext__(); - if (o == null) { - throw Py.StopIteration(""); - } - return o; - } - - /** - * Returns the next item in the iteration. - * - * @return the next item in the iteration or null to signal the end of the iteration - */ - public PyObject __iternext__() { - if (set.size() != size) { - throw Py.RuntimeError("set changed size during iteration"); - } - if (iterator.hasNext()) { - return iterator.next(); - } - return null; - } -} Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2009-01-11 03:21:25 UTC (rev 5915) +++ trunk/jython/src/org/python/core/PyType.java 2009-01-11 06:04:11 UTC (rev 5916) @@ -722,8 +722,7 @@ to_merge[n] = bases; remain[n] = 0; - List<PyObject> acc = Generic.list(); - acc.add(this); + List<PyObject> acc = Generic.list(this); int empty_cnt = 0; Modified: trunk/jython/src/org/python/util/Generic.java =================================================================== --- trunk/jython/src/org/python/util/Generic.java 2009-01-11 03:21:25 UTC (rev 5915) +++ trunk/jython/src/org/python/util/Generic.java 2009-01-11 06:04:11 UTC (rev 5916) @@ -15,11 +15,22 @@ */ public class Generic { /** - * Makes a List with its generic type inferred from whatever its being assigned to. + * Makes a List with its generic type inferred from whatever it's being assigned to. */ public static <T> List<T> list() { return new ArrayList<T>(); } + /** + * Makes a List with its generic type inferred from whatever it's being assigned to filled with + * the items in <code>contents</code>. + */ + public static <T, U extends T> List<T> list(U...contents) { + List<T> l = new ArrayList<T>(contents.length); + for (T t : contents) { + l.add(t); + } + return l; + } /** * Makes a Map using generic types inferred from whatever this is being assigned to. Modified: trunk/jython/tests/java/javatests/ListTest.java =================================================================== --- trunk/jython/tests/java/javatests/ListTest.java 2009-01-11 03:21:25 UTC (rev 5915) +++ trunk/jython/tests/java/javatests/ListTest.java 2009-01-11 06:04:11 UTC (rev 5916) @@ -472,8 +472,7 @@ int prevIndex = li.previousIndex(); TestSupport.assertThat(prevIndex == -1, "ListIterator.previousIndex() on empty List did not return -1"); - List<Object> l = Generic.list(); - l.add(1); + List<Object> l = Generic.list(1); li = newInstance(l).listIterator(); TestSupport.assertThat(!li.hasPrevious(), "ListIterator.hasPrevious() is true with nothing previous"); @@ -494,10 +493,7 @@ TestSupport.fail("expected IllegalStateException"); } catch (IllegalStateException e) {} } - l = Generic.list(); - l.add(0); - l.add(1); - l.add(2); + l = Generic.list(0, 1, 2); li = newInstance(l).listIterator(); for (int i = 0, n = l.size(); i < n; i++) { TestSupport.assertThat(li.next().equals(i), Added: trunk/jython/tests/java/javatests/PySetInJavaTest.java =================================================================== --- trunk/jython/tests/java/javatests/PySetInJavaTest.java (rev 0) +++ trunk/jython/tests/java/javatests/PySetInJavaTest.java 2009-01-11 06:04:11 UTC (rev 5916) @@ -0,0 +1,92 @@ +package javatests; + +import java.util.Iterator; +import java.util.Random; +import java.util.Set; + +import org.python.core.PySet; +import org.python.core.PyTuple; +import org.python.util.Generic; + +public class PySetInJavaTest { + + public static Set<Object> createPySetContainingJavaObjects() { + PySet s = new PySet(); + s.add("value"); + s.add(new Random()); + return s; + } + + public static void testPySetAsJavaSet() { + PySet s = new PySet(); + String v = "value"; + check(s.add(v));// Add a String as it should be wrapped in PyString + check(!s.add(v)); + String[] asArray = (String[])s.toArray(new String[0]);// The array type should be the same + // and it should be resized properly + check(asArray.length == 1); + check(asArray[0] == v); + Object[] naiveArray = s.toArray(); + check(naiveArray.length == 1); + check(naiveArray[0] == v); + // Add a Random as it should be wrapped in a generic PyObject; go through addAll to give it + // a little exercise + Random rand = new Random(); + check(s.addAll(Generic.list(rand))); + check(!s.addAll(Generic.list(rand, v))); + naiveArray = s.toArray(); + check(naiveArray.length == 2); + for (Object object : naiveArray) { + if (object instanceof String) { + check(object == v); + } else { + check(object == rand, "Should be 'value' or rand, not " + object); + } + } + check(!s.remove(new Random()), "The Random in the set shouldn't match a new Random"); + check(s.remove(rand)); + check(s.removeAll(Generic.list(rand, v)), + "The set should contain v and indicate it removed it"); + check(s.isEmpty()); + check(s.addAll(Generic.list(rand, v))); + check(2 == s.size(), "There should be 2 items, not " + s.size()); + check(s.containsAll(Generic.list(rand, v))); + check(!s.containsAll(Generic.list(rand, v, "other"))); + check(s.retainAll(Generic.list(rand))); + check(!s.retainAll(Generic.list(rand))); + check(s.addAll(Generic.list(rand, v))); + check(2 == s.size(), "There should be 2 items, not " + s.size()); + check(!s.addAll(Generic.list(rand, v))); + check(2 == s.size(), "There should be 2 items, not " + s.size()); + } + + public static void accessAndRemovePySetItems(Set<Object> items) { + check(items instanceof PySet, "The set shouldn't be __tojava'd into " + items.getClass()); + check(items.size() == 3, "Should be 3 items, not " + items.size()); + check(items.contains("value"), "The set from Python should contain 'value'"); + check(!items.contains(new Random()), "The set contains a particular Random"); + Iterator<Object> it = items.iterator(); + while (it.hasNext()) { + Object object = it.next(); + check(items.contains(object), "The set should contain all items from its iterator"); + if (object instanceof String) { + check(object.equals("value"), "The string should be 'value', not '" + object + "'"); + } else { + check(object instanceof Random || object instanceof PyTuple, + "The objects in the set should be a String, a Random or a PyTuple, not a " + + object.getClass()); + } + it.remove(); // Tests that removing on the iterator works + } + } + + private static void check(boolean testVal) { + check(testVal, ""); + } + + private static void check(boolean testVal, String failMsg) { + if (!testVal) { + throw new RuntimeException(failMsg); + } + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2009-01-11 06:24:51
|
Revision: 5917 http://jython.svn.sourceforge.net/jython/?rev=5917&view=rev Author: cgroves Date: 2009-01-11 06:24:45 +0000 (Sun, 11 Jan 2009) Log Message: ----------- Huh, I guess ECJ can handle inferring these generics, but javac sure can't. Modified Paths: -------------- trunk/jython/src/org/python/core/PyJavaType.java trunk/jython/src/org/python/core/PyType.java trunk/jython/tests/java/javatests/ListTest.java Modified: trunk/jython/src/org/python/core/PyJavaType.java =================================================================== --- trunk/jython/src/org/python/core/PyJavaType.java 2009-01-11 06:04:11 UTC (rev 5916) +++ trunk/jython/src/org/python/core/PyJavaType.java 2009-01-11 06:24:45 UTC (rev 5917) @@ -87,7 +87,8 @@ bases[i] = PyType.fromClass(underlying_class.getInterfaces()[i - 1]); } Set<PyObject> seen = Generic.set(); - List<PyObject> mros = Generic.list(this); + List<PyObject> mros = Generic.list(); + mros.add(this); for (PyObject obj : bases) { for (PyObject mroObj : ((PyType)obj).mro) { if (seen.add(mroObj)) { Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2009-01-11 06:04:11 UTC (rev 5916) +++ trunk/jython/src/org/python/core/PyType.java 2009-01-11 06:24:45 UTC (rev 5917) @@ -722,7 +722,8 @@ to_merge[n] = bases; remain[n] = 0; - List<PyObject> acc = Generic.list(this); + List<PyObject> acc = Generic.list(); + acc.add(this); int empty_cnt = 0; Modified: trunk/jython/tests/java/javatests/ListTest.java =================================================================== --- trunk/jython/tests/java/javatests/ListTest.java 2009-01-11 06:04:11 UTC (rev 5916) +++ trunk/jython/tests/java/javatests/ListTest.java 2009-01-11 06:24:45 UTC (rev 5917) @@ -472,7 +472,8 @@ int prevIndex = li.previousIndex(); TestSupport.assertThat(prevIndex == -1, "ListIterator.previousIndex() on empty List did not return -1"); - List<Object> l = Generic.list(1); + List<Object> l = Generic.list(); + l.add(1); li = newInstance(l).listIterator(); TestSupport.assertThat(!li.hasPrevious(), "ListIterator.hasPrevious() is true with nothing previous"); @@ -493,7 +494,7 @@ TestSupport.fail("expected IllegalStateException"); } catch (IllegalStateException e) {} } - l = Generic.list(0, 1, 2); + l = Generic.list(new Object[]{0, 1, 2}); li = newInstance(l).listIterator(); for (int i = 0, n = l.size(); i < n; i++) { TestSupport.assertThat(li.next().equals(i), This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2009-01-11 10:35:15
|
Revision: 5918 http://jython.svn.sourceforge.net/jython/?rev=5918&view=rev Author: cgroves Date: 2009-01-11 10:35:10 +0000 (Sun, 11 Jan 2009) Log Message: ----------- In String compilation contexts, we can assume the bytes have already been decoded and just pass them through when parsing instead of using ascii. This gets test_doctest back to passing as it was before and keeps the pep 263 checks. Modified Paths: -------------- trunk/jython/Lib/test/test_doctest.py trunk/jython/src/org/python/core/CompilerFlags.java trunk/jython/src/org/python/core/ParserFacade.java Modified: trunk/jython/Lib/test/test_doctest.py =================================================================== --- trunk/jython/Lib/test/test_doctest.py 2009-01-11 06:24:45 UTC (rev 5917) +++ trunk/jython/Lib/test/test_doctest.py 2009-01-11 10:35:10 UTC (rev 5918) @@ -2265,18 +2265,17 @@ File "...", line 7, in test_doctest4.txt Failed example: u'...' - ... + Expected: + u'f\xf6\xf6' + Got: + u'f\xc3\xb6\xc3\xb6' ********************************************************************** ... ********************************************************************** - ... - ********************************************************************** - ... - ********************************************************************** 1 items had failures: - 4 of 4 in test_doctest4.txt - ***Test Failed*** 4 failures. - (4, 4) + 2 of 4 in test_doctest4.txt + ***Test Failed*** 2 failures. + (2, 4) >>> doctest.master = None # Reset master. >>> doctest.testfile('test_doctest4.txt', encoding='utf-8') Modified: trunk/jython/src/org/python/core/CompilerFlags.java =================================================================== --- trunk/jython/src/org/python/core/CompilerFlags.java 2009-01-11 06:24:45 UTC (rev 5917) +++ trunk/jython/src/org/python/core/CompilerFlags.java 2009-01-11 10:35:10 UTC (rev 5918) @@ -3,54 +3,44 @@ public class CompilerFlags { + private int co_flags; + public boolean nested_scopes = true; public boolean division; public boolean generator_allowed = true; - public boolean with_statement = false; - public boolean absolute_import = false; + public boolean with_statement; + public boolean absolute_import; - public boolean only_ast = false; - public boolean dont_imply_dedent = false; - public boolean source_is_utf8 = false; + public boolean only_ast; + public boolean dont_imply_dedent; + public boolean source_is_utf8; public String encoding; - - public CompilerFlags(){} + public CompilerFlags() {} + public CompilerFlags(int co_flags) { - if ((co_flags & org.python.core.PyTableCode.CO_NESTED) != 0) { - this.nested_scopes = true; - } - if ((co_flags & org.python.core.PyTableCode.CO_FUTUREDIVISION) != 0) { - this.division = true; - } - if ((co_flags & org.python.core.PyTableCode.CO_GENERATOR_ALLOWED) != 0) { - this.generator_allowed = true; - } - if ((co_flags & org.python.core.PyTableCode.CO_FUTURE_ABSOLUTE_IMPORT) != 0) { - this.absolute_import = true; - } - if ((co_flags & org.python.core.PyTableCode.CO_WITH_STATEMENT) != 0) { - this.with_statement = true; - } - if ((co_flags & org.python.core.PyTableCode.PyCF_ONLY_AST) != 0) { - this.only_ast = true; - } - if ((co_flags & org.python.core.PyTableCode.PyCF_DONT_IMPLY_DEDENT) != 0) { - this.dont_imply_dedent = true; - } - if ((co_flags & org.python.core.PyTableCode.PyCF_SOURCE_IS_UTF8) != 0) { - this.source_is_utf8 = true; - } - + this.co_flags = co_flags; + nested_scopes = isEnabled(PyTableCode.CO_NESTED); + division = isEnabled(PyTableCode.CO_FUTUREDIVISION); + generator_allowed = isEnabled(PyTableCode.CO_GENERATOR_ALLOWED); + absolute_import = isEnabled(PyTableCode.CO_FUTURE_ABSOLUTE_IMPORT); + with_statement = isEnabled(PyTableCode.CO_WITH_STATEMENT); + only_ast = isEnabled(PyTableCode.PyCF_ONLY_AST); + dont_imply_dedent = isEnabled(PyTableCode.PyCF_DONT_IMPLY_DEDENT); + source_is_utf8 = isEnabled(PyTableCode.PyCF_SOURCE_IS_UTF8); } + private boolean isEnabled(int codeConstant) { + return (co_flags & codeConstant) != 0; + } + public String toString() { return String.format("CompilerFlags[division=%s nested_scopes=%s generators=%s " + "with_statement=%s absolute_import=%s only_ast=%s " + "dont_imply_dedent=%s source_is_utf8=%s]", division, nested_scopes, - generator_allowed, with_statement, absolute_import, only_ast, + generator_allowed, with_statement, absolute_import, only_ast, dont_imply_dedent, source_is_utf8); } - + } Modified: trunk/jython/src/org/python/core/ParserFacade.java =================================================================== --- trunk/jython/src/org/python/core/ParserFacade.java 2009-01-11 06:24:45 UTC (rev 5917) +++ trunk/jython/src/org/python/core/ParserFacade.java 2009-01-11 10:35:10 UTC (rev 5918) @@ -4,10 +4,13 @@ import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import java.io.Reader; +import java.io.Writer; import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; @@ -138,7 +141,7 @@ try { // prepBufReader takes care of encoding detection and universal // newlines: - bufReader = prepBufReader(stream, cflags, filename); + bufReader = prepBufReader(stream, cflags, filename, false); return parse(bufReader, kind, filename, cflags ); } catch (Throwable t) { throw fixParseError(bufReader, t, filename); @@ -223,7 +226,9 @@ private static ExpectedEncodingBufferedReader prepBufReader(InputStream input, CompilerFlags cflags, - String filename) throws IOException { + String filename, + boolean fromString) + throws IOException { input = new BufferedInputStream(input); boolean bom = adjustForBOM(input); String encoding = readEncoding(input); @@ -250,29 +255,45 @@ UniversalIOWrapper textIO = new UniversalIOWrapper(bufferedIO); input = new TextIOInputStream(textIO); - CharsetDecoder dec; + Charset cs; try { // Use ascii for the raw bytes when no encoding was specified - dec = Charset.forName(encoding == null ? "ascii" : encoding).newDecoder(); + if (encoding == null) { + if (fromString) { + cs = Charset.forName("ISO-8859-1"); + } else { + cs = Charset.forName("ascii"); + } + } else { + cs = Charset.forName(encoding); + } } catch (UnsupportedCharsetException exc) { throw new PySyntaxError("Unknown encoding: " + encoding, 1, 0, "", filename); } + CharsetDecoder dec = cs.newDecoder(); dec.onMalformedInput(CodingErrorAction.REPORT); dec.onUnmappableCharacter(CodingErrorAction.REPORT); return new ExpectedEncodingBufferedReader(new InputStreamReader(input, dec), encoding); } - private static ExpectedEncodingBufferedReader prepBufReader(String string, CompilerFlags cflags, - String filename) throws IOException { + private static ExpectedEncodingBufferedReader prepBufReader(String string, + CompilerFlags cflags, + String filename) throws IOException { + byte[] stringBytes; if (cflags.source_is_utf8) { // Passed unicode, re-encode the String to raw bytes // NOTE: This could be more efficient if we duplicate // prepBufReader/adjustForBOM/readEncoding to work on Readers, instead of // encoding - string = new PyUnicode(string).encode("utf-8"); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + Writer w = new OutputStreamWriter(out, "utf-8"); + w.write(string); + w.close(); + stringBytes = out.toByteArray(); + } else { + stringBytes = StringUtil.toBytes(string); } - InputStream input = new ByteArrayInputStream(StringUtil.toBytes(string)); - return prepBufReader(input, cflags, filename); + return prepBufReader(new ByteArrayInputStream(stringBytes), cflags, filename, true); } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2009-01-11 22:26:43
|
Revision: 5920 http://jython.svn.sourceforge.net/jython/?rev=5920&view=rev Author: cgroves Date: 2009-01-11 22:26:38 +0000 (Sun, 11 Jan 2009) Log Message: ----------- test320 - Moved to test_traceback_jy test366,373 - Moved to test_java_integration test391 - Moved to test_java_visibility test395 - Tested by test_java_visibility test396 - Tested by test_java_subclasses Modified Paths: -------------- trunk/jython/Lib/test/test_java_integration.py trunk/jython/Lib/test/test_java_visibility.py trunk/jython/Lib/test/test_traceback_jy.py trunk/jython/src/org/python/compiler/ProxyMaker.java trunk/jython/src/org/python/core/PyLong.java Added Paths: ----------- trunk/jython/Lib/test/except_in_raising_code.py Removed Paths: ------------- trunk/jython/bugtests/classes/test395j1.java trunk/jython/bugtests/classes/test395j2.java trunk/jython/bugtests/classes/test396j.java trunk/jython/bugtests/test320.py trunk/jython/bugtests/test366.py trunk/jython/bugtests/test373.py trunk/jython/bugtests/test391.py trunk/jython/bugtests/test395.py trunk/jython/bugtests/test396.py Added: trunk/jython/Lib/test/except_in_raising_code.py =================================================================== --- trunk/jython/Lib/test/except_in_raising_code.py (rev 0) +++ trunk/jython/Lib/test/except_in_raising_code.py 2009-01-11 22:26:38 UTC (rev 5920) @@ -0,0 +1,8 @@ +def test(): + print noname + +def foo(): + try: + test() + except ValueError: + raise RuntimeError("Accessing a undefined name should raise a NameError") Modified: trunk/jython/Lib/test/test_java_integration.py =================================================================== --- trunk/jython/Lib/test/test_java_integration.py 2009-01-11 14:49:43 UTC (rev 5919) +++ trunk/jython/Lib/test/test_java_integration.py 2009-01-11 22:26:38 UTC (rev 5920) @@ -7,7 +7,7 @@ from test import test_support from java.lang import (ExceptionInInitializerError, String, Runnable, System, Runtime, Math, Byte) -from java.math import BigDecimal +from java.math import BigDecimal, BigInteger from java.io import (FileInputStream, FileNotFoundException, FileOutputStream, FileWriter, OutputStreamWriter, UnsupportedEncodingException) from java.util import ArrayList, Date, HashMap, Hashtable, StringTokenizer, Vector @@ -56,6 +56,11 @@ b = BeanImplementation() self.assertEquals("name", b.getName()) self.assertEquals("name", b.name) + # Tests for #610576 + class SubBean(BeanImplementation): + def __init__(bself): + self.assertEquals("name", bself.getName()) + SubBean() class SysIntegrationTest(unittest.TestCase): @@ -262,7 +267,7 @@ self.assertEquals(len(treePath.path), 3, "Object[] not passed correctly") self.assertEquals(TreePath(treePath.path).path, treePath.path, "Object[] not passed and returned correctly") -class BigDecimalTest(unittest.TestCase): +class BigNumberTest(unittest.TestCase): def test_coerced_bigdecimal(self): from javatests import BigDecimalTest x = BigDecimal("123.4321") @@ -271,6 +276,11 @@ self.assertEqual(type(x), type(y), "BigDecimal coerced") self.assertEqual(x, y, "coerced BigDecimal not equal to directly created version") + def test_biginteger_in_long(self): + '''Checks for #608628, that long can take a BigInteger in its constructor''' + ns = '10000000000' + self.assertEquals(ns, str(long(BigInteger(ns)))) + class JavaStringTest(unittest.TestCase): def test_string_not_iterable(self): x = String('test') @@ -338,7 +348,7 @@ ImportTest, ColorTest, TreePathTest, - BigDecimalTest, + BigNumberTest, JavaStringTest, JavaDelegationTest, SecurityManagerTest) Modified: trunk/jython/Lib/test/test_java_visibility.py =================================================================== --- trunk/jython/Lib/test/test_java_visibility.py 2009-01-11 14:49:43 UTC (rev 5919) +++ trunk/jython/Lib/test/test_java_visibility.py 2009-01-11 22:26:38 UTC (rev 5920) @@ -4,7 +4,7 @@ import sys from test import test_support from java.lang import Byte, Class -from java.util import HashMap, Observable, Observer +from java.util import ArrayList, Collections, HashMap, Observable, Observer from org.python.tests import (Coercions, HiddenSuper, InterfaceCombination, Invisible, Matryoshka, OnlySubclassable, OtherSubVisible, SomePyMethods, SubVisible, Visible, VisibleOverride) from org.python.tests import VisibilityResults as Results @@ -142,6 +142,14 @@ """Bug #452947 - Class of innerclass inst <> innerclass""" self.assertEquals(id(Matryoshka.Outermost), id(Matryoshka.makeOutermost().__class__)) + def test_super_methods_merged(self): + '''Checks that all signatures on a class' methods are found, not just the first for a name + + Bug #628315''' + synchList = Collections.synchronizedList(ArrayList()) + synchList.add("a string") + self.assertEquals("a string", synchList.remove(0)) + class JavaClassTest(unittest.TestCase): def test_class_methods_visible(self): self.assertFalse(HashMap.isInterface(), Modified: trunk/jython/Lib/test/test_traceback_jy.py =================================================================== --- trunk/jython/Lib/test/test_traceback_jy.py 2009-01-11 14:49:43 UTC (rev 5919) +++ trunk/jython/Lib/test/test_traceback_jy.py 2009-01-11 22:26:38 UTC (rev 5920) @@ -50,8 +50,17 @@ # http://bugs.jython.org/issue437809 traceback.extract_stack() + def test_except_around_raising_call(self): + """[ #452526 ] traceback lineno is the except line""" + from test import except_in_raising_code + try: + except_in_raising_code.foo() + except NameError: + tb = sys.exc_info()[2] + self.assertEquals(6, tb.tb_next.tb_lineno) + else: + self.fail("Should've raised a NameError") - try: raise Exception('foo') except Exception: Deleted: trunk/jython/bugtests/classes/test395j1.java =================================================================== --- trunk/jython/bugtests/classes/test395j1.java 2009-01-11 14:49:43 UTC (rev 5919) +++ trunk/jython/bugtests/classes/test395j1.java 2009-01-11 22:26:38 UTC (rev 5920) @@ -1,4 +0,0 @@ -public class test395j1 { - protected test395j1() { - } -} Deleted: trunk/jython/bugtests/classes/test395j2.java =================================================================== --- trunk/jython/bugtests/classes/test395j2.java 2009-01-11 14:49:43 UTC (rev 5919) +++ trunk/jython/bugtests/classes/test395j2.java 2009-01-11 22:26:38 UTC (rev 5920) @@ -1,5 +0,0 @@ -public class test395j2 extends test395j1 { - private test395j2() { - super(); - } -} Deleted: trunk/jython/bugtests/classes/test396j.java =================================================================== --- trunk/jython/bugtests/classes/test396j.java 2009-01-11 14:49:43 UTC (rev 5919) +++ trunk/jython/bugtests/classes/test396j.java 2009-01-11 22:26:38 UTC (rev 5920) @@ -1,8 +0,0 @@ -public abstract class test396j { - public test396j() { - abstractMethod(); - } - - public abstract void abstractMethod(); - -} Deleted: trunk/jython/bugtests/test320.py =================================================================== --- trunk/jython/bugtests/test320.py 2009-01-11 14:49:43 UTC (rev 5919) +++ trunk/jython/bugtests/test320.py 2009-01-11 22:26:38 UTC (rev 5920) @@ -1,28 +0,0 @@ -""" -[ #452526 ] traceback lineno is the except line -""" - -import support -import sys, traceback - -def test(): - print noname - -def foo(): - try: - - - test() - - - except ValueError: - print "shouldn't happen." - -try: - foo() -except: - tb = sys.exc_info()[2] - #print tb.tb_lineno - #traceback.print_tb(tb) - assert tb.tb_next.tb_lineno == 15 - Deleted: trunk/jython/bugtests/test366.py =================================================================== --- trunk/jython/bugtests/test366.py 2009-01-11 14:49:43 UTC (rev 5919) +++ trunk/jython/bugtests/test366.py 2009-01-11 22:26:38 UTC (rev 5920) @@ -1,17 +0,0 @@ -""" -[ 610576 ] Impl of abstract method not found -""" - -import support - -support.compileJava("test366i.java"); -support.compileJava("test366j.java"); - -import test366i, test366j - -class MyCls(test366j, test366i): - def __init__(self): - self.foo(); - -MyCls() - Deleted: trunk/jython/bugtests/test373.py =================================================================== --- trunk/jython/bugtests/test373.py 2009-01-11 14:49:43 UTC (rev 5919) +++ trunk/jython/bugtests/test373.py 2009-01-11 22:26:38 UTC (rev 5920) @@ -1,14 +0,0 @@ -""" -Test for bug [608628] long(java.math.BigInteger) does not work. -""" - -import support - -# local name bugtests/test381.py -ns = '10000000000' -import java -ns2 = str(long(java.math.BigInteger(ns))) -assert ns == ns2, ns2 - - - Deleted: trunk/jython/bugtests/test391.py =================================================================== --- trunk/jython/bugtests/test391.py 2009-01-11 14:49:43 UTC (rev 5919) +++ trunk/jython/bugtests/test391.py 2009-01-11 22:26:38 UTC (rev 5920) @@ -1,14 +0,0 @@ -''' -Checks that all methods on a class are found, not just the first interface that has a method of the same name. - -Reported in bug 628315. -''' -import support -import java - -synchList = java.util.Collections.synchronizedList(java.util.ArrayList()) -synchList.add("a string") - -if not synchList.remove(0) == 'a string': - raise support.TestError, "'a string' should've been returned by the call to remove. The object version of remove was probably called instead of the int version" - Deleted: trunk/jython/bugtests/test395.py =================================================================== --- trunk/jython/bugtests/test395.py 2009-01-11 14:49:43 UTC (rev 5919) +++ trunk/jython/bugtests/test395.py 2009-01-11 22:26:38 UTC (rev 5920) @@ -1,20 +0,0 @@ -""" -Classes with greater a protected constructor can be subclassed and instantiated -Tests bug #649582 -""" - -import support - -support.compileJava("classes/test395j1.java") -support.compileJava("classes/test395j2.java") - -import test395j2 - -class Subclass(test395j2): - def __init__(self): - test395j2.__init__(self) - -try: - Subclass() -except AttributeError: - raise support.TestWarning('Unable to access protected superclass constructor') Deleted: trunk/jython/bugtests/test396.py =================================================================== --- trunk/jython/bugtests/test396.py 2009-01-11 14:49:43 UTC (rev 5919) +++ trunk/jython/bugtests/test396.py 2009-01-11 22:26:38 UTC (rev 5920) @@ -1,19 +0,0 @@ -''' -Checks for bug 663592. Used to be that if an abstract Java class called an -abstract method implemented by a Python subclass in its constructor, an -AttributeError would be thrown. -''' -import support - -support.compileJava("classes/test396j.java") - -import test396j - -class Subclass(test396j): - def __init__(self): - test396j.__init__(self) - - def abstractMethod(self): - pass - -x = Subclass() Modified: trunk/jython/src/org/python/compiler/ProxyMaker.java =================================================================== --- trunk/jython/src/org/python/compiler/ProxyMaker.java 2009-01-11 14:49:43 UTC (rev 5919) +++ trunk/jython/src/org/python/compiler/ProxyMaker.java 2009-01-11 22:26:38 UTC (rev 5920) @@ -703,9 +703,7 @@ addMethods(superclass, seenmethods); for (Class<?> iface : interfaces) { if (iface.isAssignableFrom(superclass)) { - Py.writeWarning("compiler", - "discarding redundant interface: "+ - iface.getName()); + Py.writeWarning("compiler", "discarding redundant interface: " + iface.getName()); continue; } classfile.addInterface(mapClass(iface)); Modified: trunk/jython/src/org/python/core/PyLong.java =================================================================== --- trunk/jython/src/org/python/core/PyLong.java 2009-01-11 14:49:43 UTC (rev 5919) +++ trunk/jython/src/org/python/core/PyLong.java 2009-01-11 22:26:38 UTC (rev 5920) @@ -18,7 +18,7 @@ public class PyLong extends PyObject { public static final PyType TYPE = PyType.fromClass(PyLong.class); - + public static final BigInteger minLong = BigInteger.valueOf(Long.MIN_VALUE); public static final BigInteger maxLong = BigInteger.valueOf(Long.MAX_VALUE); public static final BigInteger maxULong = @@ -35,6 +35,9 @@ ArgParser ap = new ArgParser("long", args, keywords, new String[] {"x", "base"}, 0); PyObject x = ap.getPyObject(0, null); + if (x != null && x.getJavaProxy() instanceof BigInteger) { + return new PyLong((BigInteger)x.getJavaProxy()); + } int base = ap.getInt(1, -909); if (x == null) { @@ -74,7 +77,7 @@ return new PyLongDerived(subtype, ((PyLong)tmp).value); } } - + public PyLong(PyType subType, BigInteger v) { super(subType); value = v; @@ -264,10 +267,10 @@ return adaptToCoerceTuple(long___coerce_ex__(other)); } - /** + /** * Coercion logic for long. Implemented as a final method to avoid - * invocation of virtual methods from the exposed coerce. - */ + * invocation of virtual methods from the exposed coerce. + */ final Object long___coerce_ex__(PyObject other) { if (other instanceof PyLong) return other; @@ -535,7 +538,7 @@ public PyObject __pow__(PyObject right, PyObject modulo) { return long___pow__(right, modulo); } - + @ExposedMethod(type = MethodType.BINARY, defaults = {"null"}, doc = BuiltinDocs.long___pow___doc) final PyObject long___pow__(PyObject right, PyObject modulo) { if (!canCoerce(right)) @@ -744,7 +747,7 @@ public PyObject __abs__() { return long___abs__(); } - + @ExposedMethod(doc = BuiltinDocs.long___abs___doc) final PyObject long___abs__() { return Py.newLong(value.abs()); @@ -830,7 +833,7 @@ public PyString __str__() { return Py.newString(value.toString()); } - + public PyUnicode __unicode__() { return new PyUnicode(value.toString()); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-01-12 01:59:10
|
Revision: 5923 http://jython.svn.sourceforge.net/jython/?rev=5923&view=rev Author: pjenvey Date: 2009-01-12 01:31:59 +0000 (Mon, 12 Jan 2009) Log Message: ----------- bump copyright year Modified Paths: -------------- trunk/jython/LICENSE.txt trunk/jython/src/org/python/core/PySystemState.java Modified: trunk/jython/LICENSE.txt =================================================================== --- trunk/jython/LICENSE.txt 2009-01-12 01:21:45 UTC (rev 5922) +++ trunk/jython/LICENSE.txt 2009-01-12 01:31:59 UTC (rev 5923) @@ -53,7 +53,7 @@ Jython 2.0, 2.1 License ================================ -Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Jython Developers +Copyright (c) 2000-2009 Jython Developers. All rights reserved. Redistribution and use in source and binary forms, with or without Modified: trunk/jython/src/org/python/core/PySystemState.java =================================================================== --- trunk/jython/src/org/python/core/PySystemState.java 2009-01-12 01:21:45 UTC (rev 5922) +++ trunk/jython/src/org/python/core/PySystemState.java 2009-01-12 01:31:59 UTC (rev 5923) @@ -55,14 +55,14 @@ */ public static final PyObject copyright = Py.newString( - "Copyright (c) 2000-2008, Jython Developers\n" + + "Copyright (c) 2000-2009 Jython Developers.\n" + "All rights reserved.\n\n" + "Copyright (c) 2000 BeOpen.com.\n" + "All Rights Reserved.\n\n"+ - "Copyright (c) 2000 The Apache Software Foundation. All rights\n" + - "reserved.\n\n" + + "Copyright (c) 2000 The Apache Software Foundation.\n" + + "All rights reserved.\n\n" + "Copyright (c) 1995-2000 Corporation for National Research "+ "Initiatives.\n" + @@ -70,7 +70,7 @@ "Copyright (c) 1991-1995 Stichting Mathematisch Centrum, " + "Amsterdam.\n" + - "All Rights Reserved.\n\n"); + "All Rights Reserved."); private static Map<String,String> builtinNames; public static PyTuple builtin_module_names = null; @@ -1103,4 +1103,4 @@ builtins = PySystemState.getDefaultBuiltins(); warnoptions = PySystemState.warnoptions; } -} \ No newline at end of file +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-01-14 23:56:26
|
Revision: 5931 http://jython.svn.sourceforge.net/jython/?rev=5931&view=rev Author: fwierzbicki Date: 2009-01-14 23:56:22 +0000 (Wed, 14 Jan 2009) Log Message: ----------- DEDENT problem with comments in single mode fixed. Modified Paths: -------------- trunk/jython/Lib/test/test_codeop_jy.py trunk/jython/src/org/python/antlr/PythonTokenSource.java Modified: trunk/jython/Lib/test/test_codeop_jy.py =================================================================== --- trunk/jython/Lib/test/test_codeop_jy.py 2009-01-14 14:09:58 UTC (rev 5930) +++ trunk/jython/Lib/test/test_codeop_jy.py 2009-01-14 23:56:22 UTC (rev 5931) @@ -102,6 +102,8 @@ # Failed for Jython 2.5a2. See http://bugs.jython.org/issue1116. av("@a.b.c\ndef f():\n pass") + av("def f():\n pass\n#foo") + def test_incomplete(self): ai = self.assertIncomplete Modified: trunk/jython/src/org/python/antlr/PythonTokenSource.java =================================================================== --- trunk/jython/src/org/python/antlr/PythonTokenSource.java 2009-01-14 14:09:58 UTC (rev 5930) +++ trunk/jython/src/org/python/antlr/PythonTokenSource.java 2009-01-14 23:56:22 UTC (rev 5931) @@ -195,7 +195,7 @@ t = stream.LT(1); stream.consume(); - enqueueHiddens(t); + List<Token> commentedNewlines = enqueueHiddens(t); // compute cpos as the char pos of next non-WS token in line int cpos = t.getCharPositionInLine(); // column dictates indent/dedent @@ -230,6 +230,9 @@ for(int i=1;i<newlines.length();i++) { generateNewline(newline); } + for (Token c : commentedNewlines) { + generateNewline(c); + } } if (t.getType() != PythonLexer.LEADING_WS) { // discard WS @@ -246,7 +249,8 @@ tokens.addElement(t); } - private void enqueueHiddens(Token t) { + private List<Token> enqueueHiddens(Token t) { + List<Token> newlines = new ArrayList<Token>(); if (inSingle && t.getType() == Token.EOF) { if (stream.size() > lastTokenAddedIndex + 1) { Token hidden = stream.get(lastTokenAddedIndex + 1); @@ -254,7 +258,7 @@ String text = hidden.getText(); int i = text.indexOf("\n"); while(i != -1) { - generateNewline(hidden); + newlines.add(hidden); i = text.indexOf("\n", i + 1); } } @@ -265,6 +269,7 @@ tokens.addAll(hiddenTokens); } lastTokenAddedIndex = t.getTokenIndex(); + return newlines; } private void handleIndents(int cpos, CommonToken t) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2009-01-16 08:11:53
|
Revision: 5935 http://jython.svn.sourceforge.net/jython/?rev=5935&view=rev Author: cgroves Date: 2009-01-16 08:11:48 +0000 (Fri, 16 Jan 2009) Log Message: ----------- underlying_class is the class of instances of this type, so PyJavaType shouldn't store the class it's wrapping in it. Instead, it should use the general javaProxy field on PyObject and leave underlying_class up to its bases. Without this, Python classes can't subclass a Java interface and a Python class subclassing a Java class or interface. Modified Paths: -------------- trunk/jython/Lib/test/test_java_subclasses.py trunk/jython/src/org/python/core/PyArray.java trunk/jython/src/org/python/core/PyJavaType.java trunk/jython/src/org/python/core/PyType.java Modified: trunk/jython/Lib/test/test_java_subclasses.py =================================================================== --- trunk/jython/Lib/test/test_java_subclasses.py 2009-01-15 03:36:09 UTC (rev 5934) +++ trunk/jython/Lib/test/test_java_subclasses.py 2009-01-16 08:11:48 UTC (rev 5935) @@ -5,8 +5,8 @@ from test import test_support -from java.lang import (Boolean, Class, ClassLoader, Integer, Object, Runnable, String, Thread, - ThreadGroup) +from java.lang import (Boolean, Class, ClassLoader, Comparable,Integer, Object, Runnable, String, + Thread, ThreadGroup) from java.util import Date, Hashtable, Vector from java.awt import Color, Component, Dimension, Rectangle @@ -29,6 +29,22 @@ self.fail("Shouldn't be callable with a no args") self.assertRaises(TypeError, Callbacker.callNoArg, PyBadCallback()) + def test_inheriting_from_python_and_java_interface(self): + calls = [] + class Runner(Runnable): + def run(self): + calls.append("Runner.run") + + class ComparableRunner(Comparable, Runner): + def compareTo(self, other): + calls.append("ComparableRunner.compareTo") + return 0 + + c = ComparableRunner() + c.compareTo(None) + c.run() + self.assertEquals(calls, ["ComparableRunner.compareTo", "Runner.run"]) + class TableModelTest(unittest.TestCase): def test_class_coercion(self): '''Python type instances coerce to a corresponding Java wrapper type in Object.getClass''' @@ -89,6 +105,17 @@ except TypeError: pass + class PyDim(Dimension): + pass + class PyDimRun(PyDim, Runnable): + pass + try: + class PyDimRunCol(PyDimRun, Color): + pass + self.fail("Shouldn't be able to subclass more than one concrete java class") + except TypeError: + pass + def test_multilevel_override(self): class SubDate(Date): def toString(self): Modified: trunk/jython/src/org/python/core/PyArray.java =================================================================== --- trunk/jython/src/org/python/core/PyArray.java 2009-01-15 03:36:09 UTC (rev 5934) +++ trunk/jython/src/org/python/core/PyArray.java 2009-01-16 08:11:48 UTC (rev 5935) @@ -96,7 +96,7 @@ typecode = obj.toString(); type = char2class(typecode.charAt(0)); } else if (obj instanceof PyJavaType) { - type = ((PyJavaType)obj).underlying_class; + type = ((PyJavaType)obj).getProxyType(); typecode = type.getName(); } else { throw Py.TypeError("array() argument 1 must be char, not " + obj.getType().fastGetName()); Modified: trunk/jython/src/org/python/core/PyJavaType.java =================================================================== --- trunk/jython/src/org/python/core/PyJavaType.java 2009-01-15 03:36:09 UTC (rev 5934) +++ trunk/jython/src/org/python/core/PyJavaType.java 2009-01-16 08:11:48 UTC (rev 5935) @@ -37,7 +37,7 @@ @Override public Class<?> getProxyType() { - return PyObject.class.isAssignableFrom(underlying_class) ? null : underlying_class; + return (Class<?>)javaProxy; } // Java types are ok with things being added and removed from their dicts as long as there isn't @@ -57,34 +57,35 @@ } @Override - protected void init() { - name = underlying_class.getName(); + protected void init(Class<?> forClass) { + name = forClass.getName(); // Strip the java fully qualified class name from Py classes in core if (name.startsWith("org.python.core.Py")) { name = name.substring("org.python.core.Py".length()).toLowerCase(); } dict = new PyStringMap(); - Class<?> baseClass = underlying_class.getSuperclass(); - if (PyObject.class.isAssignableFrom(underlying_class)) { + Class<?> baseClass = forClass.getSuperclass(); + if (PyObject.class.isAssignableFrom(forClass)) { // Non-exposed subclasses of PyObject use a simple linear mro to PyObject that ignores // their interfaces + underlying_class = forClass; computeLinearMro(baseClass); } else { - javaProxy = underlying_class; + javaProxy = forClass; objtype = PyType.fromClass(Class.class); // Wrapped Java types fill in their mro first using their base class and then all of // their interfaces. if (baseClass == null) { base = PyType.fromClass(PyObject.class); - } else if(underlying_class == Class.class) { + } else if (javaProxy == Class.class) { base = PyType.fromClass(PyType.class); } else { base = PyType.fromClass(baseClass); } - bases = new PyObject[1 + underlying_class.getInterfaces().length]; + bases = new PyObject[1 + forClass.getInterfaces().length]; bases[0] = base; for (int i = 1; i < bases.length; i++) { - bases[i] = PyType.fromClass(underlying_class.getInterfaces()[i - 1]); + bases[i] = PyType.fromClass(forClass.getInterfaces()[i - 1]); } Set<PyObject> seen = Generic.set(); List<PyObject> mros = Generic.list(); @@ -101,9 +102,9 @@ // PyReflected* can't call or access anything from non-public classes that aren't in // org.python.core - if (!Modifier.isPublic(underlying_class.getModifiers()) && + if (!Modifier.isPublic(forClass.getModifiers()) && !name.startsWith("org.python.core") && Options.respectJavaAccessibility) { - handleSuperMethodArgCollisions(); + handleSuperMethodArgCollisions(forClass); return; } @@ -113,9 +114,9 @@ Method[] methods; if (Options.respectJavaAccessibility) { // returns just the public methods - methods = underlying_class.getMethods(); + methods = forClass.getMethods(); } else { - methods = underlying_class.getDeclaredMethods(); + methods = forClass.getDeclaredMethods(); for (Method method : methods) { method.setAccessible(true); } @@ -205,9 +206,9 @@ Field[] fields; if (Options.respectJavaAccessibility) { // returns just the public fields - fields = underlying_class.getFields(); + fields = forClass.getFields(); } else { - fields = underlying_class.getDeclaredFields(); + fields = forClass.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); } @@ -281,11 +282,11 @@ Constructor<?>[] constructors; // No matter the security manager, trying to set the constructor on class to accessible // blows up - if (Options.respectJavaAccessibility || Class.class == underlying_class) { + if (Options.respectJavaAccessibility || Class.class == forClass) { // returns just the public constructors - constructors = underlying_class.getConstructors(); + constructors = forClass.getConstructors(); } else { - constructors = underlying_class.getDeclaredConstructors(); + constructors = forClass.getDeclaredConstructors(); for (Constructor<?> ctr : constructors) { ctr.setAccessible(true); } @@ -293,8 +294,8 @@ for (Constructor<?> ctr : constructors) { reflctr.addConstructor(ctr); } - if (PyObject.class.isAssignableFrom(underlying_class)) { - PyObject new_ = new PyNewWrapper(underlying_class, "__new__", -1, -1) { + if (PyObject.class.isAssignableFrom(forClass)) { + PyObject new_ = new PyNewWrapper(forClass, "__new__", -1, -1) { public PyObject new_impl(boolean init, PyType subtype, @@ -307,10 +308,10 @@ } else { dict.__setitem__("__init__", reflctr); } - for (Class<?> inner : underlying_class.getClasses()) { + for (Class<?> inner : forClass.getClasses()) { // Only add the class if there isn't something else with that name and it came from this // class - if (inner.getDeclaringClass() == underlying_class && + if (inner.getDeclaringClass() == forClass && dict.__finditem__(inner.getSimpleName()) == null) { // If this class is currently being loaded, any exposed types it contains won't have // set their builder in PyType yet, so add them to BOOTSTRAP_TYPES so they're @@ -323,16 +324,15 @@ } } for (Map.Entry<Class<?>, PyBuiltinMethod[]> entry : getCollectionProxies().entrySet()) { - if (entry.getKey() == underlying_class) { + if (entry.getKey() == forClass) { for (PyBuiltinMethod meth : entry.getValue()) { dict.__setitem__(meth.info.getName(), new PyMethodDescr(this, meth)); } } } - if (ClassDictInit.class.isAssignableFrom(underlying_class) - && underlying_class != ClassDictInit.class) { + if (ClassDictInit.class.isAssignableFrom(forClass) && forClass != ClassDictInit.class) { try { - Method m = underlying_class.getMethod("classDictInit", PyObject.class); + Method m = forClass.getMethod("classDictInit", PyObject.class); m.invoke(null, dict); // allow the class to override its name after it is loaded PyObject nameSpecified = dict.__finditem__("__name__"); @@ -344,10 +344,10 @@ } } if (baseClass != Object.class) { - has_set = getDescrMethod(underlying_class, "__set__", OO) != null - || getDescrMethod(underlying_class, "_doset", OO) != null; - has_delete = getDescrMethod(underlying_class, "__delete__", PyObject.class) != null - || getDescrMethod(underlying_class, "_dodel", PyObject.class) != null; + has_set = getDescrMethod(forClass, "__set__", OO) != null + || getDescrMethod(forClass, "_doset", OO) != null; + has_delete = getDescrMethod(forClass, "__delete__", PyObject.class) != null + || getDescrMethod(forClass, "_dodel", PyObject.class) != null; } else { // Pass __eq__ and __repr__ through to subclasses of Object PyBuiltinCallable equals = new PyBuiltinMethodNarrow("__eq__", 1, 1) { @@ -391,8 +391,8 @@ * drawback of failing when running in a security environment that didn't allow setting * accessibility, so this method replaced it. */ - private void handleSuperMethodArgCollisions() { - for (Class iface : underlying_class.getInterfaces()) { + private void handleSuperMethodArgCollisions(Class<?> forClass) { + for (Class<?> iface : forClass.getInterfaces()) { for (Method meth : iface.getMethods()) { if (!Modifier.isPublic(meth.getDeclaringClass().getModifiers())) { // Ignore methods from non-public interfaces as they're similarly bugged Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2009-01-15 03:36:09 UTC (rev 5934) +++ trunk/jython/src/org/python/core/PyType.java 2009-01-16 08:11:48 UTC (rev 5935) @@ -45,7 +45,10 @@ /** __flags__, the type's options. */ private long tp_flags; - /** The underlying java class or null. */ + /** + * The Java Class instances of this type will be represented as, or null if it's determined by a + * base type. + */ protected Class<?> underlying_class; /** Whether it's a builtin type. */ @@ -202,12 +205,12 @@ PyType newtype; if (new_.for_type == metatype || metatype == PyType.fromClass(Class.class)) { newtype = new PyType(); // XXX set metatype - if(proxyClass != null) { - newtype.underlying_class = proxyClass; - } } else { newtype = new PyTypeDerived(metatype); } + if (proxyClass != null) { + newtype.javaProxy = proxyClass; + } if (dict instanceof PyStringMap) { dict = ((PyStringMap)dict).copy(); } else { @@ -340,10 +343,11 @@ } /** - * Called on builtin types after underlying_class has been set on them. Should fill in dict, - * name, mro, base and bases from the class. + * Called on builtin types for a particular class. Should fill in dict, name, mro, base and + * bases from the class. */ - protected void init() { + protected void init(Class<?> forClass) { + underlying_class = forClass; if (underlying_class == PyObject.class) { mro = new PyType[] {this}; } else { @@ -950,7 +954,7 @@ } // The types in Py.BOOTSTRAP_TYPES are initialized before their builders are assigned, // so do the work of addFromClass & fillFromClass after the fact - fromClass(builder.getTypeClass()).init(); + fromClass(builder.getTypeClass()).init(builder.getTypeClass()); } } @@ -985,9 +989,8 @@ } class_to_type.put(c, newtype); - newtype.underlying_class = c; newtype.builtin = true; - newtype.init(); + newtype.init(c); return newtype; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2009-01-16 09:17:43
|
Revision: 5936 http://jython.svn.sourceforge.net/jython/?rev=5936&view=rev Author: cgroves Date: 2009-01-16 09:17:19 +0000 (Fri, 16 Jan 2009) Log Message: ----------- Check for parent beans of the same name to fill in missing getters or setters when adding beans for a type. Fixes bug #1132. Thanks to garyh for tracking it down and supplying a test case. Modified Paths: -------------- trunk/jython/Lib/test/test_java_visibility.py trunk/jython/src/org/python/core/PyJavaType.java Added Paths: ----------- trunk/jython/tests/java/org/python/tests/Child.java trunk/jython/tests/java/org/python/tests/Parent.java Modified: trunk/jython/Lib/test/test_java_visibility.py =================================================================== --- trunk/jython/Lib/test/test_java_visibility.py 2009-01-16 08:11:48 UTC (rev 5935) +++ trunk/jython/Lib/test/test_java_visibility.py 2009-01-16 09:17:19 UTC (rev 5936) @@ -187,6 +187,15 @@ self.assertEquals("OtherSubVisible[]", c.takeArray([OtherSubVisible()])) self.assertEquals("SubVisible[]", c.takeArray([SubVisible()])) + def test_iterable_coercion(self): + def simple_gen(): + yield 1 + yield 2 + yield 3 + self.assertEquals(6, Coercions.takeIterable(simple_gen())) + self.assertEquals(True, Coercions.takeBoolIterable(simple_gen())) + + def test_class_coercion(self): c = Coercions() from java.util import Hashtable, HashMap Modified: trunk/jython/src/org/python/core/PyJavaType.java =================================================================== --- trunk/jython/src/org/python/core/PyJavaType.java 2009-01-16 08:11:48 UTC (rev 5935) +++ trunk/jython/src/org/python/core/PyJavaType.java 2009-01-16 09:17:19 UTC (rev 5936) @@ -269,8 +269,31 @@ prop.field = ((PyReflectedField)prev).field; } } + + // If one of our superclasses has something defined for this name, check if its a bean + // property, and if so, try to fill in any gaps in our property from there + PyObject superForName = lookup(prop.__name__); + if (superForName instanceof PyBeanProperty) { + PyBeanProperty superProp = ((PyBeanProperty)superForName); + // If it has a set method and we don't, take it regardless. If the types don't line + // up, it'll be rejected below + if (prop.setMethod == null) { + prop.setMethod = superProp.setMethod; + } else if (superProp.myType == prop.setMethod.getParameterTypes()[0]) { + // Otherwise, we must not have a get method. Only take a get method if the type + // on it agrees with the set method we already have. The bean on this type + // overrides a conflicting one o the parent + prop.getMethod = superProp.getMethod; + prop.myType = superProp.myType; + } + + if (prop.field == null) { + // If the parent bean is hiding a static field, we need it as well. + prop.field = superProp.field; + } + } // If the return types on the set and get methods for a property don't agree, the get - // get method takes precedence + // method takes precedence if (prop.getMethod != null && prop.setMethod != null && prop.myType != prop.setMethod.getParameterTypes()[0]) { prop.setMethod = null; Added: trunk/jython/tests/java/org/python/tests/Child.java =================================================================== --- trunk/jython/tests/java/org/python/tests/Child.java (rev 0) +++ trunk/jython/tests/java/org/python/tests/Child.java 2009-01-16 09:17:19 UTC (rev 5936) @@ -0,0 +1,12 @@ +package org.python.tests; + +public class Child extends Parent { + + public void setId(int newId) { + this.id = newId; + } + + public String getValue() { + return value; + } +} Added: trunk/jython/tests/java/org/python/tests/Parent.java =================================================================== --- trunk/jython/tests/java/org/python/tests/Parent.java (rev 0) +++ trunk/jython/tests/java/org/python/tests/Parent.java 2009-01-16 09:17:19 UTC (rev 5936) @@ -0,0 +1,16 @@ +package org.python.tests; + +public class Parent { + + protected String value = "blah"; + + protected int id = 7; + + public int getId() { + return id; + } + + public void setValue(String value) { + this.value = value; + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-01-17 18:13:00
|
Revision: 5937 http://jython.svn.sourceforge.net/jython/?rev=5937&view=rev Author: zyasoft Date: 2009-01-17 18:12:57 +0000 (Sat, 17 Jan 2009) Log Message: ----------- Fixes #1227 where for cStringIO.StringIO, orderings of method calls like write-seek-write did not properly work. This also addresses a problem for PyAMF support. Thanks go to Nick Joyce for the patch! Modified Paths: -------------- trunk/jython/Lib/test/test_StringIO_jy.py trunk/jython/src/org/python/modules/cStringIO.java Modified: trunk/jython/Lib/test/test_StringIO_jy.py =================================================================== --- trunk/jython/Lib/test/test_StringIO_jy.py 2009-01-16 09:17:19 UTC (rev 5936) +++ trunk/jython/Lib/test/test_StringIO_jy.py 2009-01-17 18:12:57 UTC (rev 5937) @@ -12,9 +12,17 @@ self.assertEqual(u'foo', cStringIO.StringIO(u'foo').read()) self.assertEqual('foo', cStringIO.StringIO(u'foo').read()) +class TestWrite(unittest.TestCase): + def test_write_seek_write(self): + f = cStringIO.StringIO() + f.write('hello') + f.seek(2) + f.write('hi') + self.assertEquals(f.getvalue(), 'hehio') def test_main(): test_support.run_unittest(TestUnicodeInput) + test_support.run_unittest(TestWrite) if __name__ == '__main__': test_main() Modified: trunk/jython/src/org/python/modules/cStringIO.java =================================================================== --- trunk/jython/src/org/python/modules/cStringIO.java 2009-01-16 09:17:19 UTC (rev 5936) +++ trunk/jython/src/org/python/modules/cStringIO.java 2009-01-17 18:12:57 UTC (rev 5937) @@ -325,9 +325,40 @@ public synchronized void write(String s) { _complain_ifclosed(); - buf.setLength(pos); - int newpos = pos + s.length(); - buf.replace(pos, newpos, s); + + int spos = pos; + int slen = buf.length(); + + if (spos == slen) { + buf.append(s); + buf.setLength(slen + s.length()); + pos = spos + s.length(); + + return; + } + + if (spos > slen) { + int l = spos - slen; + char[] bytes = new char[l]; + + for (int i = 0; i < l - 1; i++) + bytes[i] = '\0'; + + buf.append(bytes); + slen = spos; + } + + int newpos = spos + s.length(); + + if (newpos > slen) { + buf.replace(spos, slen - spos, s); + buf.append(s.substring(slen)); + slen = newpos; + } else { + buf.replace(spos, spos + s.length(), s); + } + + buf.setLength(slen); pos = newpos; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2009-01-17 18:42:18
|
Revision: 5939 http://jython.svn.sourceforge.net/jython/?rev=5939&view=rev Author: cgroves Date: 2009-01-17 18:42:14 +0000 (Sat, 17 Jan 2009) Log Message: ----------- Shoulda gone out with r5607. Modified Paths: -------------- trunk/jython/Lib/test/test_builtin_jy.py trunk/jython/src/org/python/modules/Setup.java Modified: trunk/jython/Lib/test/test_builtin_jy.py =================================================================== --- trunk/jython/Lib/test/test_builtin_jy.py 2009-01-17 18:31:06 UTC (rev 5938) +++ trunk/jython/Lib/test/test_builtin_jy.py 2009-01-17 18:42:14 UTC (rev 5939) @@ -162,7 +162,7 @@ def test_names(self): for name in sys.builtin_module_names: - if name != '_jython' and name not in ('time', '_random', 'array', '_collections', '_ast'): + if name not in ('time', '_random', 'array', '_collections', '_ast'): module = __import__(name) self.assertEqual(name, module.__name__) Modified: trunk/jython/src/org/python/modules/Setup.java =================================================================== --- trunk/jython/src/org/python/modules/Setup.java 2009-01-17 18:31:06 UTC (rev 5938) +++ trunk/jython/src/org/python/modules/Setup.java 2009-01-17 18:42:14 UTC (rev 5939) @@ -41,7 +41,6 @@ "_codecs", "imp", "ucnhash", - "_jython", "_new:org.python.modules._newmodule", "_weakref:org.python.modules._weakref.WeakrefModule", "errno", This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2009-01-17 20:35:14
|
Revision: 5940 http://jython.svn.sourceforge.net/jython/?rev=5940&view=rev Author: cgroves Date: 2009-01-17 20:35:09 +0000 (Sat, 17 Jan 2009) Log Message: ----------- Add some tests for overriding attribute lookup and adding mapping methods to wrapped Java types. Modified Paths: -------------- trunk/jython/Lib/test/test_java_integration.py trunk/jython/src/org/python/core/PyBuiltinMethodNarrow.java trunk/jython/src/org/python/core/PyJavaType.java trunk/jython/src/org/python/core/PyType.java Added Paths: ----------- trunk/jython/tests/java/org/python/tests/CustomizableMapHolder.java Modified: trunk/jython/Lib/test/test_java_integration.py =================================================================== --- trunk/jython/Lib/test/test_java_integration.py 2009-01-17 18:42:14 UTC (rev 5939) +++ trunk/jython/Lib/test/test_java_integration.py 2009-01-17 20:35:09 UTC (rev 5940) @@ -1,3 +1,4 @@ +import operator import os import unittest import subprocess @@ -17,7 +18,7 @@ from javax.swing.tree import TreePath from org.python.core.util import FileUtil -from org.python.tests import BeanImplementation, Listenable +from org.python.tests import BeanImplementation, Child, Listenable, CustomizableMapHolder class InstantiationTest(unittest.TestCase): def test_cant_instantiate_abstract(self): @@ -62,7 +63,16 @@ self.assertEquals("name", bself.getName()) SubBean() + def test_inheriting_half_bean(self): + c = Child() + self.assertEquals("blah", c.value) + c.value = "bleh" + self.assertEquals("bleh", c.value) + self.assertEquals(7, c.id) + c.id = 16 + self.assertEquals(16, c.id) + class SysIntegrationTest(unittest.TestCase): def setUp(self): self.orig_stdout = sys.stdout @@ -338,6 +348,31 @@ "-J-Djava.security.manager", "-J-Djava.security.policy=%s" % policy, script]), 0) +class JavaWrapperCustomizationTest(unittest.TestCase): + def tearDown(self): + CustomizableMapHolder.clearAdditions() + + def test_adding_item_access(self): + m = CustomizableMapHolder() + self.assertRaises(TypeError, operator.getitem, m, "initial") + CustomizableMapHolder.addGetitem() + self.assertEquals(m.held["initial"], m["initial"]) + # dict would throw a KeyError here, but Map returns null for a missing key + self.assertEquals(None, m["nonexistent"]) + self.assertRaises(TypeError, operator.setitem, m, "initial") + CustomizableMapHolder.addSetitem() + m["initial"] = 12 + self.assertEquals(12, m["initial"]) + + def test_adding_attributes(self): + m = CustomizableMapHolder() + self.assertRaises(AttributeError, getattr, m, "initial") + CustomizableMapHolder.addGetattribute() + self.assertEquals(7, m.held["initial"], "Existing fields should still be accessible") + self.assertEquals(7, m.initial) + self.assertEquals(None, m.nonexistent, "Nonexistent fields should be passed on to the Map") + + def test_main(): test_support.run_unittest(InstantiationTest, BeanTest, @@ -351,7 +386,8 @@ BigNumberTest, JavaStringTest, JavaDelegationTest, - SecurityManagerTest) + SecurityManagerTest, + JavaWrapperCustomizationTest) if __name__ == "__main__": test_main() Modified: trunk/jython/src/org/python/core/PyBuiltinMethodNarrow.java =================================================================== --- trunk/jython/src/org/python/core/PyBuiltinMethodNarrow.java 2009-01-17 18:42:14 UTC (rev 5939) +++ trunk/jython/src/org/python/core/PyBuiltinMethodNarrow.java 2009-01-17 20:35:09 UTC (rev 5940) @@ -1,7 +1,25 @@ package org.python.core; public abstract class PyBuiltinMethodNarrow extends PyBuiltinMethod { + /** + * Creates a method for the given name that takes no arguments. + */ + protected PyBuiltinMethodNarrow(String name) { + this(name, 0); + } + /** + * Creates a method for the <code>name<code> that takes exactly <code>numArgs</code> arguments. + */ + protected PyBuiltinMethodNarrow(String name, int numArgs) { + this(name, numArgs, numArgs); + } + + + /** + * Creates a method for the given name that takes at least <code>minArgs</code> and at most + * <code>maxArgs</code> arguments. + */ protected PyBuiltinMethodNarrow(String name, int minArgs, int maxArgs) { super(null, new DefaultInfo(name, minArgs, maxArgs)); } Modified: trunk/jython/src/org/python/core/PyJavaType.java =================================================================== --- trunk/jython/src/org/python/core/PyJavaType.java 2009-01-17 18:42:14 UTC (rev 5939) +++ trunk/jython/src/org/python/core/PyJavaType.java 2009-01-17 20:35:09 UTC (rev 5940) @@ -349,7 +349,7 @@ for (Map.Entry<Class<?>, PyBuiltinMethod[]> entry : getCollectionProxies().entrySet()) { if (entry.getKey() == forClass) { for (PyBuiltinMethod meth : entry.getValue()) { - dict.__setitem__(meth.info.getName(), new PyMethodDescr(this, meth)); + addMethod(meth); } } } @@ -373,29 +373,26 @@ || getDescrMethod(forClass, "_dodel", PyObject.class) != null; } else { // Pass __eq__ and __repr__ through to subclasses of Object - PyBuiltinCallable equals = new PyBuiltinMethodNarrow("__eq__", 1, 1) { + addMethod(new PyBuiltinMethodNarrow("__eq__", 1) { @Override public PyObject __call__(PyObject o) { Object proxy = self.getJavaProxy(); Object oAsJava = o.__tojava__(proxy.getClass()); return proxy.equals(oAsJava) ? Py.True : Py.False; } - }; - dict.__setitem__("__eq__", new PyMethodDescr(this, equals)); - PyBuiltinCallable hash = new PyBuiltinMethodNarrow("__hash__", 0, 0) { + }); + addMethod(new PyBuiltinMethodNarrow("__hash__") { @Override public PyObject __call__() { return Py.newInteger(self.getJavaProxy().hashCode()); } - }; - dict.__setitem__("__hash__", new PyMethodDescr(this, hash)); - PyBuiltinCallable repr = new PyBuiltinMethodNarrow("__repr__", 0, 0) { + }); + addMethod(new PyBuiltinMethodNarrow("__repr__") { @Override public PyObject __call__() { return Py.newString(self.getJavaProxy().toString()); } - }; - dict.__setitem__("__repr__", new PyMethodDescr(this, repr)); + }); } } @@ -522,8 +519,8 @@ } private static class ListMethod extends PyBuiltinMethodNarrow { - protected ListMethod(String name, int minArgs, int maxArgs) { - super(name, minArgs, maxArgs); + protected ListMethod(String name, int numArgs) { + super(name, numArgs); } protected List<Object> asList(){ @@ -532,8 +529,8 @@ } private static class MapMethod extends PyBuiltinMethodNarrow { - protected MapMethod(String name, int minArgs, int maxArgs) { - super(name, minArgs, maxArgs); + protected MapMethod(String name, int numArgs) { + super(name, numArgs); } protected Map<Object, Object> asMap(){ @@ -545,21 +542,21 @@ if (collectionProxies == null) { collectionProxies = Generic.map(); - PyBuiltinMethodNarrow iterableProxy = new PyBuiltinMethodNarrow("__iter__", 0, 0) { + PyBuiltinMethodNarrow iterableProxy = new PyBuiltinMethodNarrow("__iter__") { public PyObject __call__() { return new IteratorIter(((Iterable)self.getJavaProxy())); } }; collectionProxies.put(Iterable.class, new PyBuiltinMethod[] {iterableProxy}); - PyBuiltinMethodNarrow lenProxy = new PyBuiltinMethodNarrow("__len__", 0, 0) { + PyBuiltinMethodNarrow lenProxy = new PyBuiltinMethodNarrow("__len__") { @Override public PyObject __call__() { return Py.newInteger(((Collection<?>)self.getJavaProxy()).size()); } }; - PyBuiltinMethodNarrow containsProxy = new PyBuiltinMethodNarrow("__contains__", 1, 1) { + PyBuiltinMethodNarrow containsProxy = new PyBuiltinMethodNarrow("__contains__") { @Override public PyObject __call__(PyObject obj) { Object other = obj.__tojava__(Object.class); @@ -570,14 +567,14 @@ collectionProxies.put(Collection.class, new PyBuiltinMethod[] {lenProxy, containsProxy}); - PyBuiltinMethodNarrow iteratorProxy = new PyBuiltinMethodNarrow("__iter__", 0, 0) { + PyBuiltinMethodNarrow iteratorProxy = new PyBuiltinMethodNarrow("__iter__") { public PyObject __call__() { return new IteratorIter(((Iterator)self.getJavaProxy())); } }; collectionProxies.put(Iterator.class, new PyBuiltinMethod[] {iteratorProxy}); - PyBuiltinMethodNarrow enumerationProxy = new PyBuiltinMethodNarrow("__iter__", 0, 0) { + PyBuiltinMethodNarrow enumerationProxy = new PyBuiltinMethodNarrow("__iter__") { public PyObject __call__() { return new EnumerationIter(((Enumeration)self.getJavaProxy())); } @@ -585,38 +582,38 @@ collectionProxies.put(Enumeration.class, new PyBuiltinMethod[] {enumerationProxy}); // Map doesn't extend Collection, so it needs its own version of len, iter and contains - PyBuiltinMethodNarrow mapLenProxy = new MapMethod("__len__", 0, 0) { + PyBuiltinMethodNarrow mapLenProxy = new MapMethod("__len__", 0) { @Override public PyObject __call__() { return Py.java2py(asMap().size()); } }; - PyBuiltinMethodNarrow mapIterProxy = new MapMethod("__iter__", 0, 0) { + PyBuiltinMethodNarrow mapIterProxy = new MapMethod("__iter__", 0) { @Override public PyObject __call__() { return new IteratorIter(asMap().keySet()); } }; - PyBuiltinMethodNarrow mapContainsProxy = new MapMethod("__contains__", 1, 1) { + PyBuiltinMethodNarrow mapContainsProxy = new MapMethod("__contains__", 1) { public PyObject __call__(PyObject obj) { Object other = obj.__tojava__(Object.class); return asMap().containsKey(other) ? Py.True : Py.False; } }; - PyBuiltinMethodNarrow mapGetProxy = new MapMethod("__getitem__", 1, 1) { + PyBuiltinMethodNarrow mapGetProxy = new MapMethod("__getitem__", 1) { @Override public PyObject __call__(PyObject key) { return Py.java2py(asMap().get(Py.tojava(key, Object.class))); } }; - PyBuiltinMethodNarrow mapPutProxy = new MapMethod("__setitem__", 2, 2) { + PyBuiltinMethodNarrow mapPutProxy = new MapMethod("__setitem__", 2) { @Override public PyObject __call__(PyObject key, PyObject value) { return Py.java2py(asMap().put(Py.tojava(key, Object.class), Py.tojava(value, Object.class))); } }; - PyBuiltinMethodNarrow mapRemoveProxy = new MapMethod("__delitem__", 1, 1) { + PyBuiltinMethodNarrow mapRemoveProxy = new MapMethod("__delitem__", 1) { @Override public PyObject __call__(PyObject key) { return Py.java2py(asMap().remove(Py.tojava(key, Object.class))); @@ -629,20 +626,20 @@ mapPutProxy, mapRemoveProxy}); - PyBuiltinMethodNarrow listGetProxy = new ListMethod("__getitem__", 1, 1) { + PyBuiltinMethodNarrow listGetProxy = new ListMethod("__getitem__", 1) { @Override public PyObject __call__(PyObject key) { return new ListIndexDelegate(asList()).checkIdxAndGetItem(key); } }; - PyBuiltinMethodNarrow listSetProxy = new ListMethod("__setitem__", 2, 2) { + PyBuiltinMethodNarrow listSetProxy = new ListMethod("__setitem__", 2) { @Override public PyObject __call__(PyObject key, PyObject value) { new ListIndexDelegate(asList()).checkIdxAndSetItem(key, value); return Py.None; } }; - PyBuiltinMethodNarrow listRemoveProxy = new ListMethod("__delitem__", 1, 1) { + PyBuiltinMethodNarrow listRemoveProxy = new ListMethod("__delitem__", 1) { @Override public PyObject __call__(PyObject key) { new ListIndexDelegate(asList()).checkIdxAndDelItem(key); Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2009-01-17 18:42:14 UTC (rev 5939) +++ trunk/jython/src/org/python/core/PyType.java 2009-01-17 20:35:09 UTC (rev 5940) @@ -1067,6 +1067,15 @@ type___setattr__(name, value); } + /** + * Adds the given method to this type's dict under its name in its descriptor. If there's an + * existing item in the dict, it's replaced. + */ + public void addMethod(PyBuiltinMethod meth) { + PyMethodDescr pmd = meth.makeDescriptor(this); + dict.__setitem__(pmd.getName(), pmd); + } + protected void checkSetattr() { if (builtin) { throw Py.TypeError(String.format("can't set attributes of built-in/extension type " Added: trunk/jython/tests/java/org/python/tests/CustomizableMapHolder.java =================================================================== --- trunk/jython/tests/java/org/python/tests/CustomizableMapHolder.java (rev 0) +++ trunk/jython/tests/java/org/python/tests/CustomizableMapHolder.java 2009-01-17 20:35:09 UTC (rev 5940) @@ -0,0 +1,75 @@ +package org.python.tests; + +import java.util.Map; + +import org.python.core.Py; +import org.python.core.PyBuiltinMethod; +import org.python.core.PyBuiltinMethodNarrow; +import org.python.core.PyException; +import org.python.core.PyObject; +import org.python.core.PyType; +import org.python.util.Generic; + + +public class CustomizableMapHolder { + + public Map<String, Integer> held = Generic.map(); + + { + held.put("initial", 7); + } + + public static void clearAdditions() { + PyObject dict = PyType.fromClass(CustomizableMapHolder.class).fastGetDict(); + for (String name : new String[] {"__getitem__", "__setitem__", "__getattribute__"}) { + if (dict.__finditem__(name) != null) { + dict.__delitem__(name); + } + } + } + + public static void addGetitem() { + PyBuiltinMethod meth = new PyBuiltinMethodNarrow("__getitem__", 1) { + @Override + public PyObject __call__(PyObject arg) { + CustomizableMapHolder inst = Py.tojava(self, CustomizableMapHolder.class); + String key = Py.tojava(arg, String.class); + return Py.java2py(inst.held.get(key)); + } + }; + PyType.fromClass(CustomizableMapHolder.class).addMethod(meth); + } + + public static void addSetitem() { + PyBuiltinMethod meth = new PyBuiltinMethodNarrow("__setitem__", 2) { + @Override + public PyObject __call__(PyObject arg1, PyObject arg2) { + CustomizableMapHolder inst = Py.tojava(self, CustomizableMapHolder.class); + String key = Py.tojava(arg1, String.class); + Integer val = Py.tojava(arg2, Integer.class); + inst.held.put(key, val); + return Py.None; + } + }; + PyType.fromClass(CustomizableMapHolder.class).addMethod(meth); + } + + public static void addGetattribute() { + final PyObject objectGetattribute = PyObject.TYPE.__getattr__("__getattribute__"); + PyBuiltinMethod meth = new PyBuiltinMethodNarrow("__getattribute__", 1) { + @Override + public PyObject __call__(PyObject name) { + try { + return objectGetattribute.__call__(self, name); + } catch (PyException pye) { + if (!Py.matchException(pye, Py.AttributeError)) { + throw pye; + } + } + CustomizableMapHolder inst = Py.tojava(self, CustomizableMapHolder.class); + return Py.java2py(inst.held.get(Py.tojava(name, String.class))); + } + }; + PyType.fromClass(CustomizableMapHolder.class).addMethod(meth); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2009-01-17 20:56:34
|
Revision: 5941 http://jython.svn.sourceforge.net/jython/?rev=5941&view=rev Author: cgroves Date: 2009-01-17 20:56:28 +0000 (Sat, 17 Jan 2009) Log Message: ----------- Implement Iterable in PyIterator. This means generators are usable in foreach loops. The implementation could be moved up to PyObject, but I feel like implementing the interface says that the object will be iterable, and not all PyObjects provide an __iter__. Modified Paths: -------------- trunk/jython/Lib/test/test_java_visibility.py trunk/jython/src/org/python/core/BytecodeLoader.java trunk/jython/src/org/python/core/PyIterator.java trunk/jython/src/org/python/core/PyList.java trunk/jython/src/org/python/core/PyObject.java trunk/jython/src/org/python/core/PySequence.java trunk/jython/src/org/python/core/codecs.java trunk/jython/src/org/python/core/imp.java trunk/jython/tests/java/org/python/tests/Coercions.java Added Paths: ----------- trunk/jython/src/org/python/core/WrappedIterIterator.java Modified: trunk/jython/Lib/test/test_java_visibility.py =================================================================== --- trunk/jython/Lib/test/test_java_visibility.py 2009-01-17 20:35:09 UTC (rev 5940) +++ trunk/jython/Lib/test/test_java_visibility.py 2009-01-17 20:56:28 UTC (rev 5941) @@ -193,9 +193,7 @@ yield 2 yield 3 self.assertEquals(6, Coercions.takeIterable(simple_gen())) - self.assertEquals(True, Coercions.takeBoolIterable(simple_gen())) - def test_class_coercion(self): c = Coercions() from java.util import Hashtable, HashMap Modified: trunk/jython/src/org/python/core/BytecodeLoader.java =================================================================== --- trunk/jython/src/org/python/core/BytecodeLoader.java 2009-01-17 20:35:09 UTC (rev 5940) +++ trunk/jython/src/org/python/core/BytecodeLoader.java 2009-01-17 20:56:28 UTC (rev 5941) @@ -8,7 +8,7 @@ import org.python.util.Generic; /** - * Utility class for loading of compiled python modules and java classes defined in python modules. + * Utility class for loading compiled python modules and java classes defined in python modules. */ public class BytecodeLoader { Modified: trunk/jython/src/org/python/core/PyIterator.java =================================================================== --- trunk/jython/src/org/python/core/PyIterator.java 2009-01-17 20:35:09 UTC (rev 5940) +++ trunk/jython/src/org/python/core/PyIterator.java 2009-01-17 20:56:28 UTC (rev 5941) @@ -1,6 +1,8 @@ // Copyright 2000 Finn Bock package org.python.core; +import java.util.Iterator; + /** * An abstract helper class useful when implementing an iterator object. This implementation supply * a correct __iter__() and a next() method based on the __iternext__() implementation. The @@ -9,7 +11,7 @@ * If the implementation raises a StopIteration exception, it should be stored in stopException so * the correct exception can be thrown to preserve the line numbers in the traceback. */ -public abstract class PyIterator extends PyObject { +public abstract class PyIterator extends PyObject implements Iterable<Object> { protected PyException stopException; @@ -51,4 +53,13 @@ } return ret; } + + public Iterator<Object> iterator() { + return new WrappedIterIterator<Object>(this) { + @Override + public Object next() { + return getNext().__tojava__(Object.class); + } + }; + } } Modified: trunk/jython/src/org/python/core/PyList.java =================================================================== --- trunk/jython/src/org/python/core/PyList.java 2009-01-17 20:35:09 UTC (rev 5940) +++ trunk/jython/src/org/python/core/PyList.java 2009-01-17 20:56:28 UTC (rev 5941) @@ -3,13 +3,13 @@ import java.util.Collection; import java.util.Iterator; -import java.util.LinkedList; import java.util.List; import org.python.expose.ExposedMethod; import org.python.expose.ExposedNew; import org.python.expose.ExposedType; import org.python.expose.MethodType; +import org.python.util.Generic; /** * A builtin python list. @@ -51,7 +51,7 @@ } private static List<PyObject> listify(Iterator<PyObject> iter) { - final List<PyObject> list = new LinkedList<PyObject>(); + List<PyObject> list = Generic.list(); while (iter.hasNext()) { list.add(iter.next()); } Modified: trunk/jython/src/org/python/core/PyObject.java =================================================================== --- trunk/jython/src/org/python/core/PyObject.java 2009-01-17 20:35:09 UTC (rev 5940) +++ trunk/jython/src/org/python/core/PyObject.java 2009-01-17 20:56:28 UTC (rev 5941) @@ -7,8 +7,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.NoSuchElementException; - import org.python.expose.ExposedDelete; import org.python.expose.ExposedGet; import org.python.expose.ExposedMethod; @@ -765,37 +763,11 @@ */ public Iterable<PyObject> asIterable() { return new Iterable<PyObject>() { - public Iterator<PyObject> iterator() { - return new Iterator<PyObject>() { - - private PyObject iter = __iter__(); - - private PyObject next; - - private boolean checkedForNext; - - public boolean hasNext() { - if (!checkedForNext) { - next = iter.__iternext__(); - checkedForNext = true; - } - return next != null; - } - + return new WrappedIterIterator<PyObject>(__iter__()) { public PyObject next() { - if (!hasNext()) { - throw new NoSuchElementException("End of the line, bub"); - } - PyObject toReturn = next; - checkedForNext = false; - next = null; - return toReturn; + return getNext(); } - - public void remove() { - throw new UnsupportedOperationException("Can't remove from a Python iterator"); - } }; } }; @@ -1894,8 +1866,8 @@ where1 = where[0]; PyObject impl2 = t2.lookup_where(right, where); where2 = where[0]; - if (impl2 != null && impl1 != null && where1 != where2 && - (t2.isSubType(t1) && !Py.isSubClass(where1, where2) + if (impl2 != null && impl1 != null && where1 != where2 && + (t2.isSubType(t1) && !Py.isSubClass(where1, where2) && !Py.isSubClass(t1, where2) || isStrUnicodeSpecialCase(t1, t2, op))) { PyObject tmp = o1; Modified: trunk/jython/src/org/python/core/PySequence.java =================================================================== --- trunk/jython/src/org/python/core/PySequence.java 2009-01-17 20:35:09 UTC (rev 5940) +++ trunk/jython/src/org/python/core/PySequence.java 2009-01-17 20:56:28 UTC (rev 5941) @@ -14,9 +14,6 @@ */ public abstract class PySequence extends PyObject { - /** - * This constructor is used by PyJavaClass.init() - */ public PySequence() {} public int gListAllocatedStatus = -1; Added: trunk/jython/src/org/python/core/WrappedIterIterator.java =================================================================== --- trunk/jython/src/org/python/core/WrappedIterIterator.java (rev 0) +++ trunk/jython/src/org/python/core/WrappedIterIterator.java 2009-01-17 20:56:28 UTC (rev 5941) @@ -0,0 +1,48 @@ +package org.python.core; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +/** + * Exposes a Python iter as a Java Iterator. + */ +public abstract class WrappedIterIterator<E> implements Iterator<E> { + + private final PyObject iter; + + private PyObject next; + + private boolean checkedForNext; + + public WrappedIterIterator(PyObject iter) { + this.iter = iter; + } + + public boolean hasNext() { + if (!checkedForNext) { + next = iter.__iternext__(); + checkedForNext = true; + } + return next != null; + } + + /** + * Subclasses must implement this to turn the type returned by the iter to the type expected by + * Java. + */ + public abstract E next(); + + public PyObject getNext() { + if (!hasNext()) { + throw new NoSuchElementException("End of the line, bub"); + } + PyObject toReturn = next; + checkedForNext = false; + next = null; + return toReturn; + } + + public void remove() { + throw new UnsupportedOperationException("Can't remove from a Python iterator"); + } +} Modified: trunk/jython/src/org/python/core/codecs.java =================================================================== --- trunk/jython/src/org/python/core/codecs.java 2009-01-17 20:35:09 UTC (rev 5940) +++ trunk/jython/src/org/python/core/codecs.java 2009-01-17 20:56:28 UTC (rev 5941) @@ -245,14 +245,14 @@ if (Py.isInstance(exc, Py.UnicodeDecodeError)) { PyObject object = exc.__getattr__("object"); if (!Py.isInstance(object, PyString.TYPE) || Py.isInstance(object, PyUnicode.TYPE)) { - throw Py.TypeError("object attribute must be str"); + throw Py.TypeError("object attribute must be str"); } PyObject end = exc.__getattr__("end"); return new PyTuple(new PyUnicode(Py_UNICODE_REPLACEMENT_CHARACTER), end); } else if (Py.isInstance(exc, Py.UnicodeEncodeError)) { PyObject object = exc.__getattr__("object"); if (!Py.isInstance(object, PyUnicode.TYPE)) { - throw Py.TypeError("object attribute must be unicode"); + throw Py.TypeError("object attribute must be unicode"); } PyObject end = exc.__getattr__("end"); return new PyTuple(Py.java2py("?"), end); @@ -388,9 +388,9 @@ XMLCHARREFREPLACE, BACKSLASHREPLACE }; - for (int i = 0; i < builtinErrorHandlers.length; i++) { - register_error(builtinErrorHandlers[i], Py.newJavaFunc(codecs.class, - builtinErrorHandlers[i] + "_errors")); + for (String builtinErrorHandler : builtinErrorHandlers) { + register_error(builtinErrorHandler, Py.newJavaFunc(codecs.class, + builtinErrorHandler + "_errors")); } import_encodings(); } @@ -414,9 +414,9 @@ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1 , - + }; - + private static boolean SPECIAL(char c, boolean encodeO, boolean encodeWS){ return (c>127 || utf7_special[(c)] == 1) || (encodeWS && (utf7_special[(c)] == 2)) || @@ -447,7 +447,6 @@ int bitsInCharsleft = 0; long charsleft = 0; boolean surrogate = false; - char highOrderSurrogate = 0; StringBuilder unicode = new StringBuilder(e); while (s < e) { // restart: @@ -470,7 +469,6 @@ surrogate = false; } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { surrogate = true; - highOrderSurrogate = outCh; } else { unicode.append(outCh); } @@ -537,7 +535,6 @@ "code pairs are not supported"); } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { surrogate = true; - highOrderSurrogate = outCh; } else { unicode.append(outCh); } @@ -631,7 +628,7 @@ bitsleft -= 6; } /* If the next character is special then we dont' need to terminate - the shift sequence. If the next character is not a BASE64 character + the shift sequence. If the next character is not a BASE64 character or '-' then the shift sequence will be terminated implicitly and we don't have to insert a '-'. */ @@ -1280,7 +1277,7 @@ this.start = start; this.stop = stop; this.step = step; - + // this bounds checking is necessary to convert between use of code units elsewhere, and codepoints here // it would be nice if it were unnecessary! int count = getCodePointCount(s); @@ -1290,7 +1287,7 @@ else if (stop >= count) { this.stop = count; } - + for (int i = 0; i < start; i++) { nextCodePoint(); } @@ -1303,7 +1300,7 @@ private static int getCodePointCount(String s) { return s.codePointCount(0, s.length()); } - + public boolean hasNext() { return current < stop; } Modified: trunk/jython/src/org/python/core/imp.java =================================================================== --- trunk/jython/src/org/python/core/imp.java 2009-01-17 20:35:09 UTC (rev 5940) +++ trunk/jython/src/org/python/core/imp.java 2009-01-17 20:56:28 UTC (rev 5941) @@ -9,6 +9,7 @@ import java.io.InputStream; import java.util.concurrent.locks.ReentrantLock; +import org.python.compiler.Module; import org.python.core.util.FileUtil; /** @@ -189,9 +190,7 @@ } } - public static byte[] compileSource(String name, - InputStream fp, - String filename) { + public static byte[] compileSource(String name, InputStream fp, String filename) { ByteArrayOutputStream ofp = new ByteArrayOutputStream(); try { if(filename == null) { @@ -203,13 +202,7 @@ } finally { fp.close(); } - org.python.compiler.Module.compile(node, - ofp, - name + "$py", - filename, - true, - false, - null); + Module.compile(node, ofp, name + "$py", filename, true, false, null); return ofp.toByteArray(); } catch(Throwable t) { throw ParserFacade.fixParseError(null, t, filename); Modified: trunk/jython/tests/java/org/python/tests/Coercions.java =================================================================== --- trunk/jython/tests/java/org/python/tests/Coercions.java 2009-01-17 20:35:09 UTC (rev 5940) +++ trunk/jython/tests/java/org/python/tests/Coercions.java 2009-01-17 20:56:28 UTC (rev 5941) @@ -83,4 +83,21 @@ public static String take(byte bt) { return "take with byte arg: " + bt; } + + public static int takeIterable(Iterable<Integer> it) { + int sum = 0; + for (Integer integer : it) { + sum += integer; + } + return sum; + } + + public static boolean takeBoolIterable(Iterable<Boolean> it) { + for (Boolean integer : it) { + if (!integer) { + return false; + } + } + return true; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2009-01-17 22:48:48
|
Revision: 5942 http://jython.svn.sourceforge.net/jython/?rev=5942&view=rev Author: cgroves Date: 2009-01-17 22:36:27 +0000 (Sat, 17 Jan 2009) Log Message: ----------- Resurrect some PyJavaClass code to fix bug #1235. Modified Paths: -------------- trunk/jython/Lib/test/test_java_integration.py trunk/jython/src/org/python/core/Py.java trunk/jython/src/org/python/core/PyJavaType.java trunk/jython/src/org/python/util/Generic.java Modified: trunk/jython/Lib/test/test_java_integration.py =================================================================== --- trunk/jython/Lib/test/test_java_integration.py 2009-01-17 20:56:28 UTC (rev 5941) +++ trunk/jython/Lib/test/test_java_integration.py 2009-01-17 22:36:27 UTC (rev 5942) @@ -72,6 +72,12 @@ c.id = 16 self.assertEquals(16, c.id) + def test_awt_hack(self): + # We ignore several deprecated methods in java.awt.* in favor of bean properties that were + # addded in Java 1.1. This tests that one of those bean properties is visible. + c = Container() + c.size = 400, 300 + self.assertEquals(Dimension(400, 300), c.size) class SysIntegrationTest(unittest.TestCase): def setUp(self): Modified: trunk/jython/src/org/python/core/Py.java =================================================================== --- trunk/jython/src/org/python/core/Py.java 2009-01-17 20:56:28 UTC (rev 5941) +++ trunk/jython/src/org/python/core/Py.java 2009-01-17 22:36:27 UTC (rev 5942) @@ -84,13 +84,10 @@ public static long TPFLAGS_BASETYPE = 1L << 10; /** Builtin types that are used to setup PyObject. */ - static final Set<Class<?>> BOOTSTRAP_TYPES = Generic.set(); - static { - BOOTSTRAP_TYPES.add(PyObject.class); - BOOTSTRAP_TYPES.add(PyType.class); - BOOTSTRAP_TYPES.add(PyBuiltinCallable.class); - BOOTSTRAP_TYPES.add(PyDataDescr.class); - } + static final Set<Class<?>> BOOTSTRAP_TYPES = Generic.set(PyObject.class, + PyType.class, + PyBuiltinCallable.class, + PyDataDescr.class); /** A unique object to indicate no conversion is possible in __tojava__ methods **/ Modified: trunk/jython/src/org/python/core/PyJavaType.java =================================================================== --- trunk/jython/src/org/python/core/PyJavaType.java 2009-01-17 20:56:28 UTC (rev 5941) +++ trunk/jython/src/org/python/core/PyJavaType.java 2009-01-17 22:36:27 UTC (rev 5942) @@ -23,6 +23,16 @@ private final static Class<?>[] OO = {PyObject.class, PyObject.class}; + /** Deprecated methods in java.awt.* that have bean property equivalents we prefer. */ + private final static Set<String> BAD_AWT_METHODS = Generic.set("layout", + "insets", + "size", + "minimumSize", + "preferredSize", + "maximumSize", + "bounds", + "enable"); + private static Map<Class<?>, PyBuiltinMethod[]> collectionProxies; public static PyObject wrapJavaObject(Object o) { @@ -121,11 +131,22 @@ method.setAccessible(true); } } + + boolean isInAwt = name.startsWith("java.awt.") && name.indexOf('.', 9) == -1; for (Method meth : methods) { if (!declaredOnMember(baseClass, meth) || ignore(meth)) { continue; } + String methname = meth.getName(); + + // Special case a few troublesome methods in java.awt.*. These methods are all + // deprecated and interfere too badly with bean properties to be tolerated. This is + // totally a hack but a lot of code that uses java.awt will break without it. + if (isInAwt && BAD_AWT_METHODS.contains(methname)) { + continue; + } + String nmethname = normalize(methname); PyReflectedFunction reflfunc = (PyReflectedFunction)dict.__finditem__(nmethname); if (reflfunc == null) { Modified: trunk/jython/src/org/python/util/Generic.java =================================================================== --- trunk/jython/src/org/python/util/Generic.java 2009-01-17 20:56:28 UTC (rev 5941) +++ trunk/jython/src/org/python/util/Generic.java 2009-01-17 22:36:27 UTC (rev 5942) @@ -45,4 +45,16 @@ public static <T> Set<T> set() { return new HashSet<T>(); } + + /** + * Makes a Set using the generic type inferred from whatever this is being assigned to filled + * with the items in <code>contents</code>. + */ + public static <T, U extends T> Set<T> set(U...contents) { + Set<T> s = new HashSet<T>(contents.length); + for (U u : contents) { + s.add(u); + } + return s; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |