From: <cg...@us...> - 2006-11-18 19:47:28
|
Revision: 2973 http://svn.sourceforge.net/jython/?rev=2973&view=rev Author: cgroves Date: 2006-11-18 11:47:26 -0800 (Sat, 18 Nov 2006) Log Message: ----------- Merged 2830 to HEAD from trunk Modified Paths: -------------- branches/2.3/Lib/javaos.py branches/2.3/Lib/test/regrtest.py branches/2.3/Lib/test/test_descr.py branches/2.3/Tools/jythonc/ImportName.py branches/2.3/build.xml branches/2.3/registry branches/2.3/src/org/python/compiler/CodeCompiler.java branches/2.3/src/org/python/compiler/Module.java branches/2.3/src/org/python/compiler/ProxyMaker.java branches/2.3/src/org/python/compiler/ScopeInfo.java branches/2.3/src/org/python/compiler/ScopesCompiler.java branches/2.3/src/org/python/core/CachedJarsPackageManager.java branches/2.3/src/org/python/core/Py.java branches/2.3/src/org/python/core/PyBaseString.java branches/2.3/src/org/python/core/PyBooleanDerived.java branches/2.3/src/org/python/core/PyClassMethodDerived.java branches/2.3/src/org/python/core/PyComplexDerived.java branches/2.3/src/org/python/core/PyDictionary.java branches/2.3/src/org/python/core/PyDictionaryDerived.java branches/2.3/src/org/python/core/PyFileDerived.java branches/2.3/src/org/python/core/PyFloatDerived.java branches/2.3/src/org/python/core/PyFrame.java branches/2.3/src/org/python/core/PyIntegerDerived.java branches/2.3/src/org/python/core/PyJavaClass.java branches/2.3/src/org/python/core/PyListDerived.java branches/2.3/src/org/python/core/PyLongDerived.java branches/2.3/src/org/python/core/PyMethod.java branches/2.3/src/org/python/core/PyObject.java branches/2.3/src/org/python/core/PyObjectDerived.java branches/2.3/src/org/python/core/PyPropertyDerived.java branches/2.3/src/org/python/core/PyString.java branches/2.3/src/org/python/core/PyStringDerived.java branches/2.3/src/org/python/core/PySuperDerived.java branches/2.3/src/org/python/core/PySystemState.java branches/2.3/src/org/python/core/PyTableCode.java branches/2.3/src/org/python/core/PyTuple.java branches/2.3/src/org/python/core/PyTupleDerived.java branches/2.3/src/org/python/core/PyTypeDerived.java branches/2.3/src/org/python/core/PyUnicode.java branches/2.3/src/org/python/core/PyUnicodeDerived.java branches/2.3/src/org/python/core/ZipFileImporter.java branches/2.3/src/org/python/core/__builtin__.java branches/2.3/src/org/python/core/imp.java branches/2.3/src/org/python/modules/cPickle.java branches/2.3/src/org/python/modules/imp.java branches/2.3/src/org/python/modules/time/PyTimeTuple.java branches/2.3/src/org/python/modules/time/PyTimeTupleSetup.java branches/2.3/src/org/python/util/PyServlet.java branches/2.3/src/org/python/util/jython.java branches/2.3/src/templates/float.derived branches/2.3/src/templates/list.expose branches/2.3/src/templates/long.derived branches/2.3/src/templates/object.derived branches/2.3/src/templates/str.expose branches/2.3/src/templates/timetuple.expose branches/2.3/src/templates/tuple.expose branches/2.3/src/templates/unicode.expose Added Paths: ----------- branches/2.3/Lib/test/test_cpickle.py branches/2.3/Lib/test/test_minidom.py branches/2.3/Lib/test/test_sax.py branches/2.3/Lib/test/test_scope.py branches/2.3/Lib/xml/ branches/2.3/Lib/xml/FtCore.py branches/2.3/Lib/xml/Uri.py branches/2.3/Lib/xml/__init__.py branches/2.3/Lib/xml/dom/ branches/2.3/Lib/xml/dom/MessageSource.py branches/2.3/Lib/xml/dom/NodeFilter.py branches/2.3/Lib/xml/dom/__init__.py branches/2.3/Lib/xml/dom/domreg.py branches/2.3/Lib/xml/dom/minicompat.py branches/2.3/Lib/xml/dom/minidom.py branches/2.3/Lib/xml/dom/pulldom.py branches/2.3/Lib/xml/dom/xmlbuilder.py branches/2.3/Lib/xml/sax/ branches/2.3/Lib/xml/sax/__init__.py branches/2.3/Lib/xml/sax/_exceptions.py branches/2.3/Lib/xml/sax/drivers2/ branches/2.3/Lib/xml/sax/drivers2/__init__.py branches/2.3/Lib/xml/sax/drivers2/drv_javasax.py branches/2.3/Lib/xml/sax/handler.py branches/2.3/Lib/xml/sax/saxlib.py branches/2.3/Lib/xml/sax/saxutils.py branches/2.3/Lib/xml/sax/xmlreader.py branches/2.3/src/org/python/core/FilelikeInputStream.java branches/2.3/src/org/python/core/JavaImportHelper.java branches/2.3/src/templates/basestring.expose Removed Paths: ------------- branches/2.3/Lib/xml/FtCore.py branches/2.3/Lib/xml/Uri.py branches/2.3/Lib/xml/__init__.py branches/2.3/Lib/xml/dom/ branches/2.3/Lib/xml/dom/MessageSource.py branches/2.3/Lib/xml/dom/NodeFilter.py branches/2.3/Lib/xml/dom/__init__.py branches/2.3/Lib/xml/dom/domreg.py branches/2.3/Lib/xml/dom/minicompat.py branches/2.3/Lib/xml/dom/minidom.py branches/2.3/Lib/xml/dom/pulldom.py branches/2.3/Lib/xml/dom/xmlbuilder.py branches/2.3/Lib/xml/sax/ branches/2.3/Lib/xml/sax/__init__.py branches/2.3/Lib/xml/sax/_exceptions.py branches/2.3/Lib/xml/sax/drivers2/ branches/2.3/Lib/xml/sax/drivers2/__init__.py branches/2.3/Lib/xml/sax/drivers2/drv_javasax.py branches/2.3/Lib/xml/sax/handler.py branches/2.3/Lib/xml/sax/saxlib.py branches/2.3/Lib/xml/sax/saxutils.py branches/2.3/Lib/xml/sax/xmlreader.py Modified: branches/2.3/Lib/javaos.py =================================================================== --- branches/2.3/Lib/javaos.py 2006-11-16 07:49:15 UTC (rev 2972) +++ branches/2.3/Lib/javaos.py 2006-11-18 19:47:26 UTC (rev 2973) @@ -49,22 +49,31 @@ def __init__(self, results): if len(results) != 10: - raise TypeError("stat_result() takes an at least 10-sequence") + raise TypeError("stat_result() takes an a 10-sequence") for (name, index) in stat_result._stat_members: self.__dict__[name] = results[index] + def __getitem__(self, i): if i < 0 or i > 9: raise IndexError(i) return getattr(self, stat_result._stat_members[i][0]) + def __setitem__(self, x, value): raise TypeError("object doesn't support item assignment") + def __setattr__(self, name, value): if name in [x[0] for x in stat_result._stat_members]: raise TypeError(name) raise AttributeError("readonly attribute") + def __len__(self): return 10 + def __cmp__(self, other): + if not isinstance(other, stat_result): + return 1 + return cmp(self.__dict__, other.__dict__) + error = OSError name = 'java' # discriminate based on JDK version? Modified: branches/2.3/Lib/test/regrtest.py =================================================================== --- branches/2.3/Lib/test/regrtest.py 2006-11-16 07:49:15 UTC (rev 2972) +++ branches/2.3/Lib/test/regrtest.py 2006-11-18 19:47:26 UTC (rev 2973) @@ -372,7 +372,7 @@ sys.path = saved_sys_path - test = test_spec +# test = test_spec if ok > 0: good.append(test) @@ -407,27 +407,14 @@ if verbose: print "CAUTION: stdout isn't compared in verbose mode: a test" print "that passes in verbose mode may fail without it." + if skipped and not quiet: + print count(len(skipped), "test"), "skipped:" + printlist(skipped) + printsurprises(_Expected(_skips), skipped, 'skip') if bad: print count(len(bad), "test"), "failed:" printlist(bad) - if skipped and not quiet: - print count(len(skipped), "test"), "skipped:" - printlist(skipped) - - e = _ExpectedSkips() - plat = sys.platform - if e.isvalid(): - surprise = _Set(skipped) - e.getexpected() - if surprise: - print count(len(surprise), "skip"), \ - "unexpected on", plat + ":" - printlist(surprise) - else: - print "Those skips are all expected on", plat + "." - else: - print "Ask someone to teach regrtest.py about which tests are" - print "expected to get skipped on", plat + "." - + printsurprises(_Expected(_failures), bad, 'fail') ## if single: ## alltests = findtests(testdir, stdtests, nottests) ## for i in range(len(alltests)): @@ -644,6 +631,18 @@ if len(line) > indent: print line +def printsurprises(e, actual, name): + plat = sys.platform + if e.isvalid(): + surprise = _Set(actual) - e.getexpected() + if surprise: + print count(len(surprise), name), \ + "unexpected on", plat + ":" + printlist(surprise) + else: + print "Ask someone to teach regrtest.py about which tests are" + print "expected to %s on %s." % (name, plat) + class _Set: def __init__(self, seq=[]): data = self.data = {} @@ -672,7 +671,7 @@ data.sort() return data -_expectations = { +_skips = { 'win32': """ test_al @@ -983,18 +982,104 @@ test_winreg test_winsound """, + 'java': + """ + test_al + test_asynchat + test_audioop + test_bsddb + test_capi + test_cd + test_cl + test_cmath + test_commands + test_crypt + test_curses + test_dbm + test_dl + test_email + test_email_codecs + test_fcntl + test_fork1 + test_gc + test_gdbm + test_gettext + test_gl + test_grp + test_hotshot + test_imageop + test_imgfile + test_linuxaudiodev + test_locale + test_longexp + test_mmap + test_nis + test_openpty + test_parser + test_poll + test_pty + test_pwd + test_pyexpat + test_regex + test_rgbimg + test_rotor + test_select + test_signal + test_socket_ssl + test_socketserver + test_strop + test_sunaudiodev + test_sundry + test_symtable + test_timing + test_ucn + test_unicode_file + test_unicodedata + test_wave + test_winreg + test_winsound + """ } -class _ExpectedSkips: - def __init__(self): +_failures = { + 'java': + ''' + test_atexit + test_class + test_cpickle + test_descr + test_descrtut + test_doctest2 + test_frozen + test_long_future + test_marshal + test_new + test_pickle + test_pkgimport + test_profilehooks + test_pyclbr + test_sre + test_threaded_import + test_trace + test_uu + test_weakref + test_zlib + ''', +} + +class _Expected: + def __init__(self, expect_dict): self.valid = 0 - if _expectations.has_key(sys.platform): - s = _expectations[sys.platform] + platform = sys.platform + if platform[:4] == 'java': + platform = 'java' + if expect_dict.has_key(platform): + s = expect_dict[platform] self.expected = _Set(s.split()) self.valid = 1 def isvalid(self): - "Return true iff _ExpectedSkips knows about the current platform." + "Return true iff _Expected knows about the current platform." return self.valid def getexpected(self): Copied: branches/2.3/Lib/test/test_cpickle.py (from rev 2972, trunk/jython/Lib/test/test_cpickle.py) =================================================================== --- branches/2.3/Lib/test/test_cpickle.py (rev 0) +++ branches/2.3/Lib/test/test_cpickle.py 2006-11-18 19:47:26 UTC (rev 2973) @@ -0,0 +1,81 @@ +import cPickle +import test_support +import unittest +from cStringIO import StringIO +from pickletester import AbstractPickleTests, AbstractPickleModuleTests + +class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests): + + def setUp(self): + self.dumps = cPickle.dumps + self.loads = cPickle.loads + + error = cPickle.BadPickleGet + module = cPickle + +class cPicklePicklerTests(AbstractPickleTests): + + def dumps(self, arg, bin=0): + f = StringIO() + p = cPickle.Pickler(f, bin) + p.dump(arg) + f.seek(0) + return f.read() + + def loads(self, buf): + f = StringIO(buf) + p = cPickle.Unpickler(f) + return p.load() + + error = cPickle.BadPickleGet + +class cPickleListPicklerTests(AbstractPickleTests): + + def dumps(self, arg, bin=0): + p = cPickle.Pickler(bin) + p.dump(arg) + return p.getvalue() + + def loads(self, *args): + f = StringIO(args[0]) + p = cPickle.Unpickler(f) + return p.load() + + error = cPickle.BadPickleGet + +class cPickleFastPicklerTests(AbstractPickleTests): + + def dumps(self, arg, bin=0): + f = StringIO() + p = cPickle.Pickler(f, bin) + p.fast = 1 + p.dump(arg) + f.seek(0) + return f.read() + + def loads(self, *args): + f = StringIO(args[0]) + p = cPickle.Unpickler(f) + return p.load() + + error = cPickle.BadPickleGet + + def test_nonrecursive_deep(self): + a = [] + for i in range(100): + a = [a] + b = self.loads(self.dumps(a)) + self.assertEqual(a, b) + +def test_main(): + loader = unittest.TestLoader() + suite = unittest.TestSuite() + suite.addTest(loader.loadTestsFromTestCase(cPickleTests)) + suite.addTest(loader.loadTestsFromTestCase(cPicklePicklerTests)) + #Jython doesn't support list based picklers + #suite.addTest(loader.loadTestsFromTestCase(cPickleListPicklerTests)) + suite.addTest(loader.loadTestsFromTestCase(cPickleFastPicklerTests)) + test_support.run_suite(suite) + +if __name__ == "__main__": + test_main() Modified: branches/2.3/Lib/test/test_descr.py =================================================================== --- branches/2.3/Lib/test/test_descr.py 2006-11-16 07:49:15 UTC (rev 2972) +++ branches/2.3/Lib/test/test_descr.py 2006-11-18 19:47:26 UTC (rev 2973) @@ -3927,7 +3927,7 @@ testrmul, testipow, test_mutable_bases, - test_mutable_bases_with_failing_mro, + #test_mutable_bases_with_failing_mro, test_mutable_bases_catch_mro_conflict, mutable_names, subclass_right_op, Copied: branches/2.3/Lib/test/test_minidom.py (from rev 2972, trunk/jython/Lib/test/test_minidom.py) =================================================================== --- branches/2.3/Lib/test/test_minidom.py (rev 0) +++ branches/2.3/Lib/test/test_minidom.py 2006-11-18 19:47:26 UTC (rev 2973) @@ -0,0 +1,648 @@ +# test for xml.dom.minidom + +from xml.dom.minidom import parse, Node, Document, parseString +from xml.dom import HierarchyRequestErr + +import os +import sys +import traceback +from test_support import verbose + +if __name__ == "__main__": + base = sys.argv[0] +else: + base = __file__ +tstfile = os.path.join(os.path.dirname(base), "test"+os.extsep+"xml") +del base + +def confirm(test, testname = "Test"): + if not test: + print "Failed " + testname + raise Exception + +Node._debug = 1 + +def testParseFromFile(): + from StringIO import StringIO + dom = parse(StringIO(open(tstfile).read())) + dom.unlink() + confirm(isinstance(dom,Document)) + +def testGetElementsByTagName(): + dom = parse(tstfile) + confirm(dom.getElementsByTagName("LI") == \ + dom.documentElement.getElementsByTagName("LI")) + dom.unlink() + +def testInsertBefore(): + dom = parseString("<doc><foo/></doc>") + root = dom.documentElement + elem = root.childNodes[0] + nelem = dom.createElement("element") + root.insertBefore(nelem, elem) + confirm(len(root.childNodes) == 2 + and root.childNodes.length == 2 + and root.childNodes[0] is nelem + and root.childNodes.item(0) is nelem + and root.childNodes[1] is elem + and root.childNodes.item(1) is elem + and root.firstChild is nelem + and root.lastChild is elem + and root.toxml() == "<doc><element/><foo/></doc>" + , "testInsertBefore -- node properly placed in tree") + nelem = dom.createElement("element") + root.insertBefore(nelem, None) + confirm(len(root.childNodes) == 3 + and root.childNodes.length == 3 + and root.childNodes[1] is elem + and root.childNodes.item(1) is elem + and root.childNodes[2] is nelem + and root.childNodes.item(2) is nelem + and root.lastChild is nelem + and nelem.previousSibling is elem + and root.toxml() == "<doc><element/><foo/><element/></doc>" + , "testInsertBefore -- node properly placed in tree") + nelem2 = dom.createElement("bar") + root.insertBefore(nelem2, nelem) + confirm(len(root.childNodes) == 4 + and root.childNodes.length == 4 + and root.childNodes[2] is nelem2 + and root.childNodes.item(2) is nelem2 + and root.childNodes[3] is nelem + and root.childNodes.item(3) is nelem + and nelem2.nextSibling is nelem + and nelem.previousSibling is nelem2 + and root.toxml() == "<doc><element/><foo/><bar/><element/></doc>" + , "testInsertBefore -- node properly placed in tree") + dom.unlink() + +def _create_fragment_test_nodes(): + dom = parseString("<doc/>") + orig = dom.createTextNode("original") + c1 = dom.createTextNode("foo") + c2 = dom.createTextNode("bar") + c3 = dom.createTextNode("bat") + dom.documentElement.appendChild(orig) + frag = dom.createDocumentFragment() + frag.appendChild(c1) + frag.appendChild(c2) + frag.appendChild(c3) + return dom, orig, c1, c2, c3, frag + +def testInsertBeforeFragment(): + dom, orig, c1, c2, c3, frag = _create_fragment_test_nodes() + dom.documentElement.insertBefore(frag, None) + confirm(tuple(dom.documentElement.childNodes) == (orig, c1, c2, c3), + "insertBefore(<fragment>, None)") + frag.unlink() + dom.unlink() + # + dom, orig, c1, c2, c3, frag = _create_fragment_test_nodes() + dom.documentElement.insertBefore(frag, orig) + confirm(tuple(dom.documentElement.childNodes) == (c1, c2, c3, orig), + "insertBefore(<fragment>, orig)") + frag.unlink() + dom.unlink() + +def testAppendChild(): + dom = parse(tstfile) + dom.documentElement.appendChild(dom.createComment(u"Hello")) + confirm(dom.documentElement.childNodes[-1].nodeName == "#comment") + confirm(dom.documentElement.childNodes[-1].data == "Hello") + dom.unlink() + +def testAppendChildFragment(): + dom, orig, c1, c2, c3, frag = _create_fragment_test_nodes() + dom.documentElement.appendChild(frag) + confirm(tuple(dom.documentElement.childNodes) == (orig, c1, c2, c3), + "appendChild(<fragment>)") + frag.unlink() + dom.unlink() + +def testReplaceChildFragment(): + dom, orig, c1, c2, c3, frag = _create_fragment_test_nodes() + dom.documentElement.replaceChild(frag, orig) + orig.unlink() + confirm(tuple(dom.documentElement.childNodes) == (c1, c2, c3), + "replaceChild(<fragment>)") + frag.unlink() + dom.unlink() + +def testLegalChildren(): + dom = Document() + elem = dom.createElement('element') + text = dom.createTextNode('text') + + try: dom.appendChild(text) + except HierarchyRequestErr: pass + else: + print "dom.appendChild didn't raise HierarchyRequestErr" + + dom.appendChild(elem) + try: dom.insertBefore(text, elem) + except HierarchyRequestErr: pass + else: + print "dom.appendChild didn't raise HierarchyRequestErr" + + try: dom.replaceChild(text, elem) + except HierarchyRequestErr: pass + else: + print "dom.appendChild didn't raise HierarchyRequestErr" + + nodemap = elem.attributes + try: nodemap.setNamedItem(text) + except HierarchyRequestErr: pass + else: + print "NamedNodeMap.setNamedItem didn't raise HierarchyRequestErr" + + try: nodemap.setNamedItemNS(text) + except HierarchyRequestErr: pass + else: + print "NamedNodeMap.setNamedItemNS didn't raise HierarchyRequestErr" + + elem.appendChild(text) + dom.unlink() + +def testNamedNodeMapSetItem(): + dom = Document() + elem = dom.createElement('element') + attrs = elem.attributes + attrs["foo"] = "bar" + a = attrs.item(0) + confirm(a.ownerDocument is dom, + "NamedNodeMap.__setitem__() sets ownerDocument") + confirm(a.ownerElement is elem, + "NamedNodeMap.__setitem__() sets ownerElement") + confirm(a.value == "bar", + "NamedNodeMap.__setitem__() sets value") + confirm(a.nodeValue == "bar", + "NamedNodeMap.__setitem__() sets nodeValue") + elem.unlink() + dom.unlink() + +def testNonZero(): + dom = parse(tstfile) + confirm(dom)# should not be zero + dom.appendChild(dom.createComment("foo")) + confirm(not dom.childNodes[-1].childNodes) + dom.unlink() + +def testUnlink(): + dom = parse(tstfile) + dom.unlink() + +def testElement(): + dom = Document() + dom.appendChild(dom.createElement("abc")) + confirm(dom.documentElement) + dom.unlink() + +def testAAA(): + dom = parseString("<abc/>") + el = dom.documentElement + el.setAttribute("spam", "jam2") + confirm(el.toxml() == '<abc spam="jam2"/>', "testAAA") + a = el.getAttributeNode("spam") + confirm(a.ownerDocument is dom, + "setAttribute() sets ownerDocument") + confirm(a.ownerElement is dom.documentElement, + "setAttribute() sets ownerElement") + dom.unlink() + +def testAAB(): + dom = parseString("<abc/>") + el = dom.documentElement + el.setAttribute("spam", "jam") + el.setAttribute("spam", "jam2") + confirm(el.toxml() == '<abc spam="jam2"/>', "testAAB") + dom.unlink() + +def testAddAttr(): + dom = Document() + child = dom.appendChild(dom.createElement("abc")) + + child.setAttribute("def", "ghi") + confirm(child.getAttribute("def") == "ghi") + confirm(child.attributes["def"].value == "ghi") + + child.setAttribute("jkl", "mno") + confirm(child.getAttribute("jkl") == "mno") + confirm(child.attributes["jkl"].value == "mno") + + confirm(len(child.attributes) == 2) + + child.setAttribute("def", "newval") + confirm(child.getAttribute("def") == "newval") + confirm(child.attributes["def"].value == "newval") + + confirm(len(child.attributes) == 2) + dom.unlink() + +def testDeleteAttr(): + dom = Document() + child = dom.appendChild(dom.createElement("abc")) + + confirm(len(child.attributes) == 0) + child.setAttribute("def", "ghi") + confirm(len(child.attributes) == 1) + del child.attributes["def"] + confirm(len(child.attributes) == 0) + dom.unlink() + +def testRemoveAttr(): + dom = Document() + child = dom.appendChild(dom.createElement("abc")) + + child.setAttribute("def", "ghi") + confirm(len(child.attributes) == 1) + child.removeAttribute("def") + confirm(len(child.attributes) == 0) + + dom.unlink() + +def testRemoveAttrNS(): + dom = Document() + child = dom.appendChild( + dom.createElementNS("http://www.python.org", "python:abc")) + child.setAttributeNS("http://www.w3.org", "xmlns:python", + "http://www.python.org") + child.setAttributeNS("http://www.python.org", "python:abcattr", "foo") + confirm(len(child.attributes) == 2) + child.removeAttributeNS("http://www.python.org", "abcattr") + confirm(len(child.attributes) == 1) + + dom.unlink() + +def testRemoveAttributeNode(): + dom = Document() + child = dom.appendChild(dom.createElement("foo")) + child.setAttribute("spam", "jam") + confirm(len(child.attributes) == 1) + node = child.getAttributeNode("spam") + child.removeAttributeNode(node) + confirm(len(child.attributes) == 0) + + dom.unlink() + +def testChangeAttr(): + dom = parseString("<abc/>") + el = dom.documentElement + el.setAttribute("spam", "jam") + confirm(len(el.attributes) == 1) + el.setAttribute("spam", "bam") + confirm(len(el.attributes) == 1) + el.attributes["spam"] = "ham" + confirm(len(el.attributes) == 1) + el.setAttribute("spam2", "bam") + confirm(len(el.attributes) == 2) + el.attributes[ "spam2"] = "bam2" + confirm(len(el.attributes) == 2) + dom.unlink() + +def testGetAttrList(): + pass + +def testGetAttrValues(): pass + +def testGetAttrLength(): pass + +def testGetAttribute(): pass + +def testGetAttributeNS(): pass + +def testGetAttributeNode(): pass + +def testGetElementsByTagNameNS(): + d="""<foo xmlns:minidom="http://pyxml.sf.net/minidom"> + <minidom:myelem/> + </foo>""" + dom = parseString(d) + elem = dom.getElementsByTagNameNS("http://pyxml.sf.net/minidom","myelem") + confirm(len(elem) == 1) + dom.unlink() + +def testGetEmptyNodeListFromElementsByTagNameNS(): pass + +def testElementReprAndStr(): + dom = Document() + el = dom.appendChild(dom.createElement("abc")) + string1 = repr(el) + string2 = str(el) + confirm(string1 == string2) + dom.unlink() + +# commented out until Fredrick's fix is checked in +def _testElementReprAndStrUnicode(): + dom = Document() + el = dom.appendChild(dom.createElement(u"abc")) + string1 = repr(el) + string2 = str(el) + confirm(string1 == string2) + dom.unlink() + +# commented out until Fredrick's fix is checked in +def _testElementReprAndStrUnicodeNS(): + dom = Document() + el = dom.appendChild( + dom.createElementNS(u"http://www.slashdot.org", u"slash:abc")) + string1 = repr(el) + string2 = str(el) + confirm(string1 == string2) + confirm(string1.find("slash:abc") != -1) + dom.unlink() + +def testAttributeRepr(): + dom = Document() + el = dom.appendChild(dom.createElement(u"abc")) + node = el.setAttribute("abc", "def") + confirm(str(node) == repr(node)) + dom.unlink() + +def testTextNodeRepr(): pass + +def testWriteXML(): + str = '<?xml version="1.0" ?>\n<a b="c"/>' + dom = parseString(str) + domstr = dom.toxml() + dom.unlink() + confirm(str == domstr) + +def testProcessingInstruction(): pass + +def testProcessingInstructionRepr(): pass + +def testTextRepr(): pass + +def testWriteText(): pass + +def testDocumentElement(): pass + +def testTooManyDocumentElements(): + doc = parseString("<doc/>") + elem = doc.createElement("extra") + try: + doc.appendChild(elem) + except HierarchyRequestErr: + pass + else: + print "Failed to catch expected exception when" \ + " adding extra document element." + elem.unlink() + doc.unlink() + +def testCreateElementNS(): pass + +def testCreateAttributeNS(): pass + +def testParse(): pass + +def testParseString(): pass + +def testComment(): pass + +def testAttrListItem(): pass + +def testAttrListItems(): pass + +def testAttrListItemNS(): pass + +def testAttrListKeys(): pass + +def testAttrListKeysNS(): pass + +def testAttrListValues(): pass + +def testAttrListLength(): pass + +def testAttrList__getitem__(): pass + +def testAttrList__setitem__(): pass + +def testSetAttrValueandNodeValue(): pass + +def testParseElement(): pass + +def testParseAttributes(): pass + +def testParseElementNamespaces(): pass + +def testParseAttributeNamespaces(): pass + +def testParseProcessingInstructions(): pass + +def testChildNodes(): pass + +def testFirstChild(): pass + +def testHasChildNodes(): pass + +def testCloneElementShallow(): + dom, clone = _setupCloneElement(0) + confirm(len(clone.childNodes) == 0 + and clone.childNodes.length == 0 + and clone.parentNode is None + and clone.toxml() == '<doc attr="value"/>' + , "testCloneElementShallow") + dom.unlink() + +def testCloneElementDeep(): + dom, clone = _setupCloneElement(1) + confirm(len(clone.childNodes) == 1 + and clone.childNodes.length == 1 + and clone.parentNode is None + and clone.toxml() == '<doc attr="value"><foo/></doc>' + , "testCloneElementDeep") + dom.unlink() + +def _setupCloneElement(deep): + dom = parseString("<doc attr='value'><foo/></doc>") + root = dom.documentElement + clone = root.cloneNode(deep) + _testCloneElementCopiesAttributes( + root, clone, "testCloneElement" + (deep and "Deep" or "Shallow")) + # mutilate the original so shared data is detected + root.tagName = root.nodeName = "MODIFIED" + root.setAttribute("attr", "NEW VALUE") + root.setAttribute("added", "VALUE") + return dom, clone + +def _testCloneElementCopiesAttributes(e1, e2, test): + attrs1 = e1.attributes + attrs2 = e2.attributes + keys1 = attrs1.keys() + keys2 = attrs2.keys() + keys1.sort() + keys2.sort() + confirm(keys1 == keys2, "clone of element has same attribute keys") + for i in range(len(keys1)): + a1 = attrs1.item(i) + a2 = attrs2.item(i) + confirm(a1 is not a2 + and a1.value == a2.value + and a1.nodeValue == a2.nodeValue + and a1.namespaceURI == a2.namespaceURI + and a1.localName == a2.localName + , "clone of attribute node has proper attribute values") + confirm(a2.ownerElement is e2, + "clone of attribute node correctly owned") + + +def testCloneDocumentShallow(): pass + +def testCloneDocumentDeep(): pass + +def testCloneAttributeShallow(): pass + +def testCloneAttributeDeep(): pass + +def testClonePIShallow(): pass + +def testClonePIDeep(): pass + +def testNormalize(): + doc = parseString("<doc/>") + root = doc.documentElement + root.appendChild(doc.createTextNode("first")) + root.appendChild(doc.createTextNode("second")) + confirm(len(root.childNodes) == 2 + and root.childNodes.length == 2, "testNormalize -- preparation") + doc.normalize() + confirm(len(root.childNodes) == 1 + and root.childNodes.length == 1 + and root.firstChild is root.lastChild + and root.firstChild.data == "firstsecond" + , "testNormalize -- result") + doc.unlink() + + doc = parseString("<doc/>") + root = doc.documentElement + root.appendChild(doc.createTextNode("")) + doc.normalize() + confirm(len(root.childNodes) == 0 + and root.childNodes.length == 0, + "testNormalize -- single empty node removed") + doc.unlink() + +def testSiblings(): + doc = parseString("<doc><?pi?>text?<elm/></doc>") + root = doc.documentElement + (pi, text, elm) = root.childNodes + + confirm(pi.nextSibling is text and + pi.previousSibling is None and + text.nextSibling is elm and + text.previousSibling is pi and + elm.nextSibling is None and + elm.previousSibling is text, "testSiblings") + + doc.unlink() + +def testParents(): + doc = parseString("<doc><elm1><elm2/><elm2><elm3/></elm2></elm1></doc>") + root = doc.documentElement + elm1 = root.childNodes[0] + (elm2a, elm2b) = elm1.childNodes + elm3 = elm2b.childNodes[0] + + confirm(root.parentNode is doc and + elm1.parentNode is root and + elm2a.parentNode is elm1 and + elm2b.parentNode is elm1 and + elm3.parentNode is elm2b, "testParents") + + doc.unlink() + +def testNodeListItem(): + doc = parseString("<doc><e/><e/></doc>") + children = doc.childNodes + docelem = children[0] + confirm(children[0] is children.item(0) + and children.item(1) is None + and docelem.childNodes.item(0) is docelem.childNodes[0] + and docelem.childNodes.item(1) is docelem.childNodes[1] + and docelem.childNodes.item(0).childNodes.item(0) is None, + "test NodeList.item()") + doc.unlink() + +def testSAX2DOM(): + from xml.dom import pulldom + + sax2dom = pulldom.SAX2DOM() + sax2dom.startDocument() + sax2dom.startElement("doc", {}) + sax2dom.characters("text") + sax2dom.startElement("subelm", {}) + sax2dom.characters("text") + sax2dom.endElement("subelm") + sax2dom.characters("text") + sax2dom.endElement("doc") + sax2dom.endDocument() + + doc = sax2dom.document + root = doc.documentElement + (text1, elm1, text2) = root.childNodes + text3 = elm1.childNodes[0] + + confirm(text1.previousSibling is None and + text1.nextSibling is elm1 and + elm1.previousSibling is text1 and + elm1.nextSibling is text2 and + text2.previousSibling is elm1 and + text2.nextSibling is None and + text3.previousSibling is None and + text3.nextSibling is None, "testSAX2DOM - siblings") + + confirm(root.parentNode is doc and + text1.parentNode is root and + elm1.parentNode is root and + text2.parentNode is root and + text3.parentNode is elm1, "testSAX2DOM - parents") + + doc.unlink() + +# --- MAIN PROGRAM + +names = globals().keys() +names.sort() + +failed = [] + +try: + Node.allnodes +except AttributeError: + # We don't actually have the minidom from the standard library, + # but are picking up the PyXML version from site-packages. + def check_allnodes(): + pass +else: + def check_allnodes(): + confirm(len(Node.allnodes) == 0, + "assertion: len(Node.allnodes) == 0") + if len(Node.allnodes): + print "Garbage left over:" + if verbose: + print Node.allnodes.items()[0:10] + else: + # Don't print specific nodes if repeatable results + # are needed + print len(Node.allnodes) + Node.allnodes = {} + +for name in names: + if name.startswith("test"): + func = globals()[name] + try: + func() + check_allnodes() + except: + failed.append(name) + print "Test Failed: ", name + sys.stdout.flush() + traceback.print_exception(*sys.exc_info()) + print `sys.exc_info()[1]` + Node.allnodes = {} + +if failed: + print "\n\n\n**** Check for failures in these tests:" + for name in failed: + print " " + name Copied: branches/2.3/Lib/test/test_sax.py (from rev 2972, trunk/jython/Lib/test/test_sax.py) =================================================================== --- branches/2.3/Lib/test/test_sax.py (rev 0) +++ branches/2.3/Lib/test/test_sax.py 2006-11-18 19:47:26 UTC (rev 2973) @@ -0,0 +1,713 @@ +# -*- coding: iso-8859-1 -*- +# regression test for SAX 2.0 +# $Id: test_sax.py,v 1.13 2004/03/20 07:46:04 fdrake Exp $ + +from xml.sax import handler, make_parser, ContentHandler, \ + SAXException, SAXReaderNotAvailable, SAXParseException +try: + make_parser() +except SAXReaderNotAvailable: + # don't try to test this module if we cannot create a parser + raise ImportError("no XML parsers available") +from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \ + XMLFilterBase, Location +from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl +from cStringIO import StringIO +from test.test_support import verbose, TestFailed, findfile + +# ===== Utilities + +tests = 0 +failures = [] + +def confirm(outcome, name): + global tests + + tests = tests + 1 + if outcome: + if verbose: + print "Passed", name + else: + print "Failed", name + failures.append(name) + +def test_make_parser2(): + try: + # Creating parsers several times in a row should succeed. + # Testing this because there have been failures of this kind + # before. + from xml.sax import make_parser + p = make_parser() + from xml.sax import make_parser + p = make_parser() + from xml.sax import make_parser + p = make_parser() + from xml.sax import make_parser + p = make_parser() + from xml.sax import make_parser + p = make_parser() + from xml.sax import make_parser + p = make_parser() + except: + return 0 + else: + return p + + +# =========================================================================== +# +# saxutils tests +# +# =========================================================================== + +# ===== escape + +def test_escape_basic(): + return escape("Donald Duck & Co") == "Donald Duck & Co" + +def test_escape_all(): + return escape("<Donald Duck & Co>") == "<Donald Duck & Co>" + +def test_escape_extra(): + return escape("Hei p\xE5 deg", {"\xE5" : "å"}) == "Hei på deg" + +# ===== unescape + +def test_unescape_basic(): + return unescape("Donald Duck & Co") == "Donald Duck & Co" + +def test_unescape_all(): + return unescape("<Donald Duck & Co>") == "<Donald Duck & Co>" + +def test_unescape_extra(): + return unescape("Hei p\xE5 deg", {"\xE5" : "å"}) == "Hei på deg" + +def test_unescape_amp_extra(): + return unescape("&foo;", {"&foo;": "splat"}) == "&foo;" + +# ===== quoteattr + +def test_quoteattr_basic(): + return quoteattr("Donald Duck & Co") == '"Donald Duck & Co"' + +def test_single_quoteattr(): + return (quoteattr('Includes "double" quotes') + == '\'Includes "double" quotes\'') + +def test_double_quoteattr(): + return (quoteattr("Includes 'single' quotes") + == "\"Includes 'single' quotes\"") + +def test_single_double_quoteattr(): + return (quoteattr("Includes 'single' and \"double\" quotes") + == "\"Includes 'single' and "double" quotes\"") + +# ===== make_parser + +def test_make_parser(): + try: + # Creating a parser should succeed - it should fall back + # to the expatreader + p = make_parser(['xml.parsers.no_such_parser']) + except: + return 0 + else: + return p + + +# ===== XMLGenerator + +start = '<?xml version="1.0" encoding="iso-8859-1"?>\n' + +def test_xmlgen_basic(): + result = StringIO() + gen = XMLGenerator(result) + gen.startDocument() + gen.startElement("doc", {}) + gen.endElement("doc") + gen.endDocument() + + return result.getvalue() == start + "<doc></doc>" + +def test_xmlgen_content(): + result = StringIO() + gen = XMLGenerator(result) + + gen.startDocument() + gen.startElement("doc", {}) + gen.characters("huhei") + gen.endElement("doc") + gen.endDocument() + + return result.getvalue() == start + "<doc>huhei</doc>" + +def test_xmlgen_escaped_content(): + result = StringIO() + gen = XMLGenerator(result) + + gen.startDocument() + gen.startElement("doc", {}) + gen.characters(unicode("\xa0\\u3042", "unicode-escape")) + gen.endElement("doc") + gen.endDocument() + + return result.getvalue() == start + "<doc>\xa0あ</doc>" + +def test_xmlgen_escaped_attr(): + result = StringIO() + gen = XMLGenerator(result) + + gen.startDocument() + gen.startElement("doc", {"x": unicode("\\u3042", "unicode-escape")}) + gen.endElement("doc") + gen.endDocument() + + return result.getvalue() == start + '<doc x="あ"></doc>' + +def test_xmlgen_pi(): + result = StringIO() + gen = XMLGenerator(result) + + gen.startDocument() + gen.processingInstruction("test", "data") + gen.startElement("doc", {}) + gen.endElement("doc") + gen.endDocument() + + return result.getvalue() == start + "<?test data?><doc></doc>" + +def test_xmlgen_content_escape(): + result = StringIO() + gen = XMLGenerator(result) + + gen.startDocument() + gen.startElement("doc", {}) + gen.characters("<huhei&") + gen.endElement("doc") + gen.endDocument() + + return result.getvalue() == start + "<doc><huhei&</doc>" + +def test_xmlgen_attr_escape(): + result = StringIO() + gen = XMLGenerator(result) + + gen.startDocument() + gen.startElement("doc", {"a": '"'}) + gen.startElement("e", {"a": "'"}) + gen.endElement("e") + gen.startElement("e", {"a": "'\""}) + gen.endElement("e") + gen.endElement("doc") + gen.endDocument() + + return result.getvalue() == start \ + + "<doc a='\"'><e a=\"'\"></e><e a=\"'"\"></e></doc>" + +def test_xmlgen_attr_escape_manydouble(): + result = StringIO() + gen = XMLGenerator(result) + + gen.startDocument() + gen.startElement("doc", {"a": '"\'"'}) + gen.endElement("doc") + gen.endDocument() + + return result.getvalue() == start + "<doc a='\"'\"'></doc>" + +def test_xmlgen_attr_escape_manysingle(): + result = StringIO() + gen = XMLGenerator(result) + + gen.startDocument() + gen.startElement("doc", {"a": "'\"'"}) + gen.endElement("doc") + gen.endDocument() + + return result.getvalue() == start + '<doc a="\'"\'"></doc>' + +def test_xmlgen_ignorable(): + result = StringIO() + gen = XMLGenerator(result) + + gen.startDocument() + gen.startElement("doc", {}) + gen.ignorableWhitespace(" ") + gen.endElement("doc") + gen.endDocument() + + return result.getvalue() == start + "<doc> </doc>" + +ns_uri = "http://www.python.org/xml-ns/saxtest/" + +def test_xmlgen_ns(): + result = StringIO() + gen = XMLGenerator(result) + + gen.startDocument() + gen.startPrefixMapping("ns1", ns_uri) + gen.startElementNS((ns_uri, "doc"), "ns1:doc", {}) + # add an unqualified name + gen.startElementNS((None, "udoc"), None, {}) + gen.endElementNS((None, "udoc"), None) + gen.endElementNS((ns_uri, "doc"), "ns1:doc") + gen.endPrefixMapping("ns1") + gen.endDocument() + + return result.getvalue() == start + \ + ('<ns1:doc xmlns:ns1="%s"><udoc></udoc></ns1:doc>' % + ns_uri) + +# ===== XMLFilterBase + +def test_filter_basic(): + result = StringIO() + gen = XMLGenerator(result) + filter = XMLFilterBase() + filter.setContentHandler(gen) + + filter.startDocument() + filter.startElement("doc", {}) + filter.characters("content") + filter.ignorableWhitespace(" ") + filter.endElement("doc") + filter.endDocument() + + return result.getvalue() == start + "<doc>content </doc>" + +# =========================================================================== +# +# expatreader tests +# +# =========================================================================== + +# ===== XMLReader support + +def test_expat_file(): + parser = make_parser() + result = StringIO() + xmlgen = XMLGenerator(result) + + parser.setContentHandler(xmlgen) + parser.parse(open(findfile("test.xml"))) + + return result.getvalue() == xml_test_out + +# ===== DTDHandler support + +class TestDTDHandler: + + def __init__(self): + self._notations = [] + self._entities = [] + + def notationDecl(self, name, publicId, systemId): + self._notations.append((name, publicId, systemId)) + + def unparsedEntityDecl(self, name, publicId, systemId, ndata): + self._entities.append((name, publicId, systemId, ndata)) + +def test_expat_dtdhandler(): + parser = make_parser() + handler = TestDTDHandler() + parser.setDTDHandler(handler) + + parser.parse(StringIO('''<!DOCTYPE doc [ + <!ENTITY img SYSTEM "expat.gif" NDATA GIF> + <!NOTATION GIF PUBLIC "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN"> +]> +<doc></doc>''')) + if len(handler._entities) != 1 or len(handler._entities[0]) != 4: + return 0 + name, pubId, sysId, ndata = handler._entities[0] + if name != 'img' or not pubId is None or not sysId.endswith('expat.gif') or ndata != 'GIF': + return 0 + return handler._notations == [("GIF", "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN", None)] + +# ===== EntityResolver support + +class TestEntityResolver: + + def resolveEntity(self, publicId, systemId): + inpsrc = InputSource() + inpsrc.setByteStream(StringIO("<entity/>")) + return inpsrc + +def test_expat_entityresolver(): + parser = make_parser() + parser.setEntityResolver(TestEntityResolver()) + result = StringIO() + parser.setContentHandler(XMLGenerator(result)) + + parser.parse(StringIO('''<!DOCTYPE doc [ + <!ENTITY test SYSTEM "whatever"> +]> +<doc>&test;</doc>''')) + return result.getvalue() == start + "<doc><entity></entity></doc>" + +# ===== Attributes support + +class AttrGatherer(ContentHandler): + + def startElement(self, name, attrs): + self._attrs = attrs + + def startElementNS(self, name, qname, attrs): + self._attrs = attrs + +def test_expat_attrs_empty(): + parser = make_parser() + gather = AttrGatherer() + parser.setContentHandler(gather) + + parser.parse(StringIO("<doc/>")) + + return verify_empty_attrs(gather._attrs) + +def test_expat_attrs_wattr(): + parser = make_parser() + gather = AttrGatherer() + parser.setContentHandler(gather) + + parser.parse(StringIO("<doc attr='val'/>")) + + return verify_attrs_wattr(gather._attrs) + +def test_expat_nsattrs_empty(): + parser = make_parser() + parser.setFeature(handler.feature_namespaces, 1) + gather = AttrGatherer() + parser.setContentHandler(gather) + + parser.parse(StringIO("<doc/>")) + + return verify_empty_nsattrs(gather._attrs) + +def test_expat_nsattrs_wattr(): + parser = make_parser() + parser.setFeature(handler.feature_namespaces, 1) + gather = AttrGatherer() + parser.setContentHandler(gather) + + parser.parse(StringIO("<doc xmlns:ns='%s' ns:attr='val'/>" % ns_uri)) + + attrs = gather._attrs + + return attrs.getLength() == 1 and \ + attrs.getNames() == [(ns_uri, "attr")] and \ + attrs.getQNames() == ["ns:attr"] and \ + len(attrs) == 1 and \ + attrs.has_key((ns_uri, "attr")) and \ + attrs.keys() == [(ns_uri, "attr")] and \ + attrs.get((ns_uri, "attr")) == "val" and \ + attrs.get((ns_uri, "attr"), 25) == "val" and \ + attrs.items() == [((ns_uri, "attr"), "val")] and \ + attrs.values() == ["val"] and \ + attrs.getValue((ns_uri, "attr")) == "val" and \ + attrs[(ns_uri, "attr")] == "val" + +# ===== InputSource support + +xml_test_out = open(findfile("test.xml.out")).read() + +def test_expat_inpsource_filename(): + parser = make_parser() + result = StringIO() + xmlgen = XMLGenerator(result) + + parser.setContentHandler(xmlgen) + parser.parse(findfile("test.xml")) + + return result.getvalue() == xml_test_out + +def test_expat_inpsource_sysid(): + parser = make_parser() + result = StringIO() + xmlgen = XMLGenerator(result) + + parser.setContentHandler(xmlgen) + parser.parse(InputSource(findfile("test.xml"))) + + return result.getvalue() == xml_test_out + +def test_expat_inpsource_stream(): + parser = make_parser() + result = StringIO() + xmlgen = XMLGenerator(result) + + parser.setContentHandler(xmlgen) + inpsrc = InputSource() + inpsrc.setByteStream(open(findfile("test.xml"))) + parser.parse(inpsrc) + + return result.getvalue() == xml_test_out + +# ===== Locator support + +class LocatorTest(XMLGenerator): + def __init__(self, out=None, encoding="iso-8859-1"): + XMLGenerator.__init__(self, out, encoding) + self.location = None + + def endDocument(self): + XMLGenerator.endDocument(self) + self.location = Location(self._locator) + +def test_expat_locator_noinfo(): + result = StringIO() + xmlgen = LocatorTest(result) + parser = make_parser() + parser.setContentHandler(xmlgen) + + parser.parse(StringIO("<doc></doc>")) + + return xmlgen.location.getSystemId() is None and \ + xmlgen.location.getPublicId() is None and \ + xmlgen.location.getLineNumber() == 1 + +def test_expat_locator_withinfo(): + result = StringIO() + xmlgen = LocatorTest(result) + parser = make_parser() + parser.setContentHandler(xmlgen) + parser.parse(findfile("test.xml")) + + return xmlgen.location.getSystemId().endswith(findfile("test.xml")) and \ + xmlgen.location.getPublicId() is None + + +# =========================================================================== +# +# error reporting +# +# =========================================================================== + +def test_expat_incomplete(): + parser = make_parser() + parser.setContentHandler(ContentHandler()) # do nothing + try: + parser.parse(StringIO("<foo>")) + except SAXParseException: + return 1 # ok, error found + else: + return 0 + +def test_sax_location_str(): + # pass various values from a locator to the SAXParseException to + # make sure that the __str__() doesn't fall apart when None is + # passed instead of an integer line and column number + # + # use "normal" values for the locator: + str(Location(DummyLocator(1, 1))) + # use None for the line number: + str(Location(DummyLocator(None, 1))) + # use None for the column number: + str(Location(DummyLocator(1, None))) + # use None for both: + str(Location(DummyLocator(None, None))) + return 1 + +def test_sax_parse_exception_str(): + # pass various values from a locator to the SAXParseException to + # make sure that the __str__() doesn't fall apart when None is + # passed instead of an integer line and column number + # + # use "normal" values for the locator: + str(SAXParseException("message", None, + DummyLocator(1, 1))) + # use None for the line number: + str(SAXParseException("message", None, + DummyLocator(None, 1))) + # use None for the column number: + str(SAXParseException("message", None, + DummyLocator(1, None))) + # use None for both: + str(SAXParseException("message", None, + DummyLocator(None, None))) + return 1 + +class DummyLocator: + def __init__(self, lineno, colno): + self._lineno = lineno + self._colno = colno + + def getPublicId(self): + return "pubid" + + def getSystemId(self): + return "sysid" + + def getLineNumber(self): + return self._lineno + + def getColumnNumber(self): + return self._colno + +# =========================================================================== +# +# xmlreader tests +# +# =========================================================================== + +# ===== AttributesImpl + +def verify_empty_attrs(attrs): + try: + attrs.getValue("attr") + gvk = 0 + except KeyError: + gvk = 1 + + try: + attrs.getValueByQName("attr") + gvqk = 0 + except KeyError: + gvqk = 1 + + try: + attrs.getNameByQName("attr") + gnqk = 0 + except KeyError: + gnqk = 1 + + try: + attrs.getQNameByName("attr") + gqnk = 0 + except KeyError: + gqnk = 1 + + try: + attrs["attr"] + gik = 0 + except KeyError: + gik = 1 + + return attrs.getLength() == 0 and \ + attrs.getNames() == [] and \ + attrs.getQNames() == [] and \ + len(attrs) == 0 and \ + not attrs.has_key("attr") and \ + attrs.keys() == [] and \ + attrs.get("attrs") is None and \ + attrs.get("attrs", 25) == 25 and \ + attrs.items() == [] and \ + attrs.values() == [] and \ + gvk and gvqk and gnqk and gik and gqnk + +def verify_attrs_wattr(attrs): + return attrs.getLength() == 1 and \ + attrs.getNames() == ["attr"] and \ + attrs.getQNames() == ["attr"] and \ + len(attrs) == 1 and \ + attrs.has_key("attr") and \ + attrs.keys() == ["attr"] and \ + attrs.get("attr") == "val" and \ + attrs.get("attr", 25) == "val" and \ + attrs.items() == [("attr", "val")] and \ + attrs.values() == ["val"] and \ + attrs.getValue("attr") == "val" and \ + attrs.getValueByQName("attr") == "val" and \ + attrs.getNameByQName("attr") == "attr" and \ + attrs["attr"] == "val" and \ + attrs.getQNameByName("attr") == "attr" + +def test_attrs_empty(): + return verify_empty_attrs(AttributesImpl({})) + +def test_attrs_wattr(): + return verify_attrs_wattr(AttributesImpl({"attr" : "val"})) + +# ===== AttributesImpl + +def verify_empty_nsattrs(attrs): + try: + attrs.getValue((ns_uri, "attr")) + gvk = 0 + except KeyError: + gvk = 1 + + try: + attrs.getValueByQName("ns:attr") + gvqk = 0 + except KeyError: + gvqk = 1 + + try: + attrs.getNameByQName("ns:attr") + gnqk = 0 + except KeyError: + gnqk = 1 + + try: + attrs.getQNameByName((ns_uri, "attr")) + gqnk = 0 + except KeyError: + gqnk = 1 + + try: + attrs[(ns_uri, "attr")] + gik = 0 + except KeyError: + gik = 1 + + return attrs.getLength() == 0 and \ + attrs.getNames() == [] and \ + attrs.getQNames() == [] and \ + len(attrs) == 0 and \ + not attrs.has_key((ns_uri, "attr")) and \ + attrs.keys() == [] and \ + attrs.get((ns_uri, "attr")) is None and \ + attrs.get((ns_uri, "attr"), 25) == 25 and \ + attrs.items() == [] and \ + attrs.values() == [] and \ + gvk and gvqk and gnqk and gik and gqnk + +def test_nsattrs_empty(): + return verify_empty_nsattrs(AttributesNSImpl({}, {})) + +def test_nsattrs_wattr(): + attrs = AttributesNSImpl({(ns_uri, "attr") : "val"}, + {(ns_uri, "attr") : "ns:attr"}) + + return attrs.getLength() == 1 and \ + attrs.getNames() == [(ns_uri, "attr")] and \ + attrs.getQNames() == ["ns:attr"] and \ + len(attrs) == 1 and \ + attrs.has_key((ns_uri, "attr")) and \ + attrs.keys() == [(ns_uri, "attr")] and \ + attrs.get((ns_uri, "attr")) == "val" and \ + attrs.get((ns_uri, "attr"), 25) == "val" and \ + attrs.items() == [((ns_uri, "attr"), "val")] and \ + attrs.values() == ["val"] and \ + attrs.getValue((ns_uri, "attr")) == "val" and \ + attrs.getValueByQName("ns:attr") == "val" and \ + attrs.getNameByQName("ns:attr") == (ns_uri, "attr") and \ + attrs[(ns_uri, "attr")] == "val" and \ + attrs.getQNameByName((ns_uri, "attr")) == "ns:attr" + + +# ===== Main program + +def make_test_output(): + parser = make_parser() + result = StringIO() + xmlgen = XMLGenerator(result) + + parser.setContentHandler(xmlgen) + parser.parse(findfile("test.xml")) + + outf = open(findfile("test.xml.out"), "w") + outf.write(result.getvalue()) + outf.close() + +items = locals().items() +items.sort() +for (name, value) in items: + if name[:5] == "test_": + confirm(value(), name) + +if verbose: + print "%d tests, %d failures" % (tests, len(failures)) +if failures: + raise TestFailed("%d of %d tests failed: %s" + % (len(failures), tests, ", ".join(failures))) Copied: branches/2.3/Lib/test/test_scope.py (from rev 2972, trunk/jython/Lib/test/test_scope.py) =================================================================== --- branches/2.3/Lib/test/test_scope.py (rev 0) +++ branches/2.3/Lib/test/test_scope.py 2006-11-18 19:47:26 UTC (rev 2973) @@ -0,0 +1,530 @@ +from test_support import verify, TestFailed, check_syntax + +import warnings +warnings.filterwarnings("ignore", r"import \*", SyntaxWarning, "<string>") + +print "1. simple nesting" + +def make_adder(x): + def adder(y): + return x + y + return adder + +inc = make_adder(1) +plus10 = make_adder(10) + +verify(inc(1) == 2) +verify(plus10(-2) == 8) + +print "2. extra nesting" + +def make_adder2(x): + def extra(): # check freevars passing through non-use scopes + def adder(y): + return x + y + return adder + return extra() + +inc = make_adder2(1) +plus10 = make_adder2(10) + +verify(inc(1) == 2) +verify(plus10(-2) == 8) + +print "3. simple nesting + rebinding" + +def make_adder3(x): + def adder(y): + return x + y + x = x + 1 # check tracking of assignment to x in defining scope + return adder + +inc = make_adder3(0) +plus10 = make_adder3(9) + +verify(inc(1) == 2) +verify(plus10(-2) == 8) + +print "4. nesting with global but no free" + +def make_adder4(): # XXX add exta level of indirection + def nest(): + def nest(): + def adder(y): + return global_x + y # check that plain old globals work + return adder + return nest() + return nest() + +global_x = 1 +adder = make_adder4() +verify(adder(1) == 2) + +global_x = 10 +verify(adder(-2) == 8) + +print "5. nesting through class" + +def make_adder5(x): + class Adder: + def __call__(self, y): + return x + y + return Adder() + +inc = make_adder5(1) +plus10 = make_adder5(10) + +verify(inc(1) == 2) +verify(plus10(-2) == 8) + +print "6. nesting plus free ref to global" + +def make_adder6(x): + global global_nest_x + def adder(y): + return global_nest_x + y + global_nest_x = x + return adder + +inc = make_adder6(1) +plus10 = make_adder6(10) + +verify(inc(1) == 11) # there's only one global +verify(plus10(-2) == 8) + +print "7. nearest enclosing scope" + +def f(x): + def g(y): + x = 42 # check that this masks binding in f() + def h(z): + return x + z + return h + return g(2) + +test_func = f(10) +verify(test_func(5) == 47) + +print "8. mixed freevars and cellvars" + +def identity(x): + return x + +def f(x, y, z): + def g(a, b, c): + a = a + x # 3 + def h(): + # z * (4 + 9) + # 3 * 13 + return identity(z * (b + y)) + y = c + z # 9 + return h + return g + +g = f(1, 2, 3) +h = g(2, 4, 6) +verify(h() == 39) + +print "9. free variable in method" + +def test(): + method_and_var = "var" + class Test: + def method_and_var(self): + return "method" + def test(self): + return method_and_var + def actual_global(self): + return str("global") + def str(self): + return str(self) + return Test() + +t = test() +verify(t.test() == "var") +verify(t.method_and_var() == "method") +verify(t.actual_global() == "global") + +method_and_var = "var" +class Test: + # this class is not nested, so the rules are different + def method_and_var(self): + return "method" + def test(self): + return method_and_var + def actual_global(self): + return str("global") + def str(self): + return str(self) + +t = Test() +verify(t.test() == "var") +verify(t.method_and_var() == "method") +verify(t.actual_global() == "global") + +print "10. recursion" + +def f(x): + def fact(n): + if n == 0: + return 1 + else: + return n * fact(n - 1) + if x >= 0: + return fact(x) + else: + raise ValueError, "x must be >= 0" + +verify(f(6) == 720) + + +print "11. unoptimized namespaces" + +check_syntax("""\ +def unoptimized_clash1(strip): + def f(s): + from string import * + return strip(s) # ambiguity: free or local + return f +""") + +check_syntax("""\ +def unoptimized_clash2(): + from string import * + def f(s): + return strip(s) # ambiguity: global or local + return f +""") + +check_syntax("""\ +def unoptimized_clash2(): + from string import * + def g(): + def f(s): + return strip(s) # ambiguity: global or local + return f +""") + +# XXX could allow this for exec with const argument, but what's the point +check_syntax("""\ +def error(y): + exec "a = 1" + def f(x): + return x + y + return f +""") + +check_syntax("""\ +def f(x): + def g(): + return x + del x # can't del name +""") + +check_syntax("""\ +def f(): + def g(): + from string import * + return strip # global or local? +""") + +# and verify a few cases that should work + +exec """ +def noproblem1(): + from string import * + f = lambda x:x + +def noproblem2(): + from string import * + def f(x): + return x + 1 + +def noproblem3(): + from string import * + def f(x): + global y + y = x +""" + +print "12. lambdas" + +f1 = lambda x: lambda y: x + y +inc = f1(1) +plus10 = f1(10) +verify(inc(1) == 2) +verify(plus10(5) == ... [truncated message content] |