def run_accessibility_script(self, script, error=AttributeError): fn = test_support.findfile(script) # Check expected error in current environment self.assertRaises(error, execfile, fn) # Prepare to break the rules self.command.append("-J-Dpython.cachedir.skip=true") self.command.append("-J-Dpython.security.respectJavaAccessibility=false") if test_support.get_java_version() >= (9,): # See all the cases for which we have forgotten --add-opens self.command.append("-J--illegal-access=warn") # Open the packages used in the scripts self.add_opens("java.desktop", "java.awt.geom") for package in ("lang", "util", "nio", "nio.charset"): self.add_opens("java.base", "java." + package) if test_support.get_java_version() >= (12,): self.add_opens("java.base", "java.lang.constant") self.command.append(fn) self.assertEquals(subprocess.call(self.command), 0)
def test_strftime_japanese_locale(self): # Verifies fix of http://bugs.jython.org/issue2301 - produces # UTF-8 encoded output per what CPython does, rather than Unicode. # We will revisit in Jython 3.x! self.get_installed_locales("ja_JP.UTF-8") if test_support.get_java_version() < (10, ): expected = "'\\xe6\\x97\\xa5 3 29 14:55:13 2015'" else: # From Java 10 onwards, Japanese formatting more correctly includes # 月, the kanji character for month expected = "'\\xe6\\x97\\xa5 3\\xe6\\x9c\\x88 29 14:55:13 2015'" self.assertEqual( subprocess.check_output([ sys.executable, "-J-Duser.country=JP", "-J-Duser.language=ja", "-c", "import time; print repr(time.strftime('%c', (2015, 3, 29, 14, 55, 13, 6, 88, 0)))" ]), expected + os.linesep)
def test_strftime_japanese_locale(self): # Verifies fix of http://bugs.jython.org/issue2301 - produces # UTF-8 encoded output per what CPython does, rather than Unicode. # We will revisit in Jython 3.x! self.get_installed_locales("ja_JP.UTF-8") if test_support.get_java_version() < (10,): expected = "'\\xe6\\x97\\xa5 3 29 14:55:13 2015'" else: # From Java 10 onwards, Japanese formatting more correctly includes # 月, the kanji character for month expected = "'\\xe6\\x97\\xa5 3\\xe6\\x9c\\x88 29 14:55:13 2015'" self.assertEqual( subprocess.check_output( [sys.executable, "-J-Duser.country=JP", "-J-Duser.language=ja", "-c", "import time; print repr(time.strftime('%c', (2015, 3, 29, 14, 55, 13, 6, 88, 0)))"]), expected + os.linesep)
def run_accessibility_script(self, script, error=AttributeError): fn = test_support.findfile(script) # Check expected error in current environment self.assertRaises(error, execfile, fn) # Prepare to break the rules self.command.append("-J-Dpython.cachedir.skip=true") self.command.append("-J-Dpython.security.respectJavaAccessibility=false") if test_support.get_java_version() >= (9,): # See all the cases for which we have forgotten --add-opens self.command.append("-J--illegal-access=warn") # Open the packages used in the scripts self.add_opens("java.desktop", "java.awt.geom") for package in ("lang", "util", "nio", "nio.charset"): self.add_opens("java.base", "java." + package) self.command.append(fn) self.assertEquals(subprocess.call(self.command), 0)
# Technically, the weak reference breaks and is restored after # gc-run finishes. However wa() blocks until the referent is # restored or the deletion is confirmed. except Exception: pass self.assertEqual(comments, []) self.assertEqual(resurrect, []) while comments == [] or resurrect == []: self.assertEqual(str(wa()), '<a>') self.assertEqual(wc(), None) self.assertEqual(str(wa()), '<a>') self.assertEqual(wc(), None) @unittest.skipUnless(test_support.is_jython and test_support.get_java_version() < (9, ), "Test is specific to Java versions <9") # From Java 9 onwards we get ugly warnings. # See discussion in http://bugs.jython.org/issue2656 class GCTests_Jy_TraverseByReflection(unittest.TestCase): @classmethod def setUpClass(cls): #Jython-specific block: try: cls.savedJythonGCFlags = gc.getJythonGCFlags() gc.removeJythonGCFlags( gc.DONT_TRAVERSE_BY_REFLECTION) # i.e. enable ... gc.addJythonGCFlags(gc.SUPPRESS_TRAVERSE_BY_REFLECTION_WARNING) gc.setMonitorGlobal(True) except Exception: pass
# test_codecencodings_tw.py # Codec encoding tests for ROC encodings. # from test import test_support from test import test_multibytecodec_support import unittest import sys # Codecs re-synchronise faster after illegal byte in Java 8+ than in Java 7 to # update 60 (and in CPython 3.3+ faster than in CPython 2.7-3.2). Either is # correct, but we need to know which one to expect. RESYNC_FASTER = False # True for CPython 3.3 and later if sys.platform.startswith('java'): if test_support.get_java_version() > (1, 7, 0, 60): RESYNC_FASTER = True class Test_Big5(test_multibytecodec_support.TestBase, unittest.TestCase): encoding = 'big5' tstring = test_multibytecodec_support.load_teststring('big5') if RESYNC_FASTER: # Version from CPython 3.6 where \0x80\0x80 is two invalid sequences. # Java 8 agrees with this interpretation. codectests = ( # invalid bytes (b"abc\x80\x80\xc1\xc4", "strict", None), (b"abc\xc8", "strict", None), (b"abc\x80\x80\xc1\xc4", "replace", u"abc\ufffd\ufffd\u8b10"), (b"abc\x80\x80\xc1\xc4\xc8", "replace", u"abc\ufffd\ufffd\u8b10\ufffd"),
# its result right away, due to thread-safe implementation. # Technically, the weak reference breaks and is restored after # gc-run finishes. However wa() blocks until the referent is # restored or the deletion is confirmed. except Exception: pass self.assertEqual(comments, []) self.assertEqual(resurrect, []) while comments == [] or resurrect == []: self.assertEqual(str(wa()), '<a>') self.assertEqual(wc(), None) self.assertEqual(str(wa()), '<a>') self.assertEqual(wc(), None) @unittest.skipUnless(test_support.is_jython and test_support.get_java_version() < (9,), "Test is specific to Java versions <9") # From Java 9 onwards we get ugly warnings. # See discussion in http://bugs.jython.org/issue2656 class GCTests_Jy_TraverseByReflection(unittest.TestCase): @classmethod def setUpClass(cls): #Jython-specific block: try: cls.savedJythonGCFlags = gc.getJythonGCFlags() gc.removeJythonGCFlags(gc.DONT_TRAVERSE_BY_REFLECTION) # i.e. enable ... gc.addJythonGCFlags(gc.SUPPRESS_TRAVERSE_BY_REFLECTION_WARNING) gc.setMonitorGlobal(True) except Exception: pass
class DictTest(unittest.TestCase): _class = None def _make_dict(self, pydict): return pydict if not self._class else self._class(pydict) def test_constructor(self): # calling built-in types without argument must return empty self.assertEqual(self._make_dict(dict()), {}) self.assertIsNot(self._make_dict(dict()), {}) def test_literal_constructor(self): # check literal constructor for different sized dicts # (to exercise the BUILD_MAP oparg). for n in (0, 1, 6, 256, 400): items = [(''.join(random.sample(string.letters, 8)), i) for i in range(n)] random.shuffle(items) formatted_items = ('{!r}: {:d}'.format(k, v) for k, v in items) dictliteral = '{' + ', '.join(formatted_items) + '}' self.assertEqual(self._make_dict(eval(dictliteral)), dict(items)) def test_bool(self): self.assertIs(not self._make_dict({}), True) self.assertTrue(self._make_dict({1: 2})) self.assertIs(bool(self._make_dict({})), False) self.assertIs(bool(self._make_dict({1: 2})), True) def test_keys(self): d = self._make_dict({}) self.assertEqual(d.keys(), []) d = {'a': 1, 'b': 2} k = d.keys() self.assertTrue(d.has_key('a')) self.assertTrue(d.has_key('b')) self.assertRaises(TypeError, d.keys, None) def test_values(self): d = self._make_dict({}) self.assertEqual(d.values(), []) d = self._make_dict({1: 2}) self.assertEqual(d.values(), [2]) self.assertRaises(TypeError, d.values, None) def test_items(self): d = self._make_dict({}) self.assertEqual(d.items(), []) d = self._make_dict({1: 2}) self.assertEqual(d.items(), [(1, 2)]) self.assertRaises(TypeError, d.items, None) def test_has_key(self): d = self._make_dict({}) self.assertFalse(d.has_key('a')) d = self._make_dict({'a': 1, 'b': 2}) k = d.keys() k.sort() self.assertEqual(k, ['a', 'b']) self.assertRaises(TypeError, d.has_key) def test_contains(self): d = self._make_dict({}) self.assertNotIn('a', d) self.assertFalse('a' in d) self.assertTrue('a' not in d) d = self._make_dict({'a': 1, 'b': 2}) self.assertIn('a', d) self.assertIn('b', d) self.assertNotIn('c', d) self.assertRaises(TypeError, d.__contains__) def test_len(self): d = self._make_dict({}) self.assertEqual(len(d), 0) d = self._make_dict({'a': 1, 'b': 2}) self.assertEqual(len(d), 2) def test_getitem(self): d = self._make_dict({'a': 1, 'b': 2}) self.assertEqual(d['a'], 1) self.assertEqual(d['b'], 2) d['c'] = 3 d['a'] = 4 self.assertEqual(d['c'], 3) self.assertEqual(d['a'], 4) del d['b'] self.assertEqual(d, {'a': 4, 'c': 3}) self.assertRaises(TypeError, d.__getitem__) class BadEq(object): def __eq__(self, other): raise Exc() def __hash__(self): return 24 d = self._make_dict({}) d[BadEq()] = 42 self.assertRaises(KeyError, d.__getitem__, 23) class Exc(Exception): pass class BadHash(object): fail = False def __hash__(self): if self.fail: raise Exc() else: return 42 x = BadHash() d[x] = 42 x.fail = True self.assertRaises(Exc, d.__getitem__, x) def test_clear(self): d = self._make_dict({1: 1, 2: 2, 3: 3}) d.clear() self.assertEqual(d, {}) self.assertRaises(TypeError, d.clear, None) def test_update(self): d = self._make_dict({}) d.update({1: 100}) d.update({2: 20}) d.update({1: 1, 2: 2, 3: 3}) self.assertEqual(d, {1: 1, 2: 2, 3: 3}) d.update() self.assertEqual(d, {1: 1, 2: 2, 3: 3}) self.assertRaises((TypeError, AttributeError), d.update, None) class SimpleUserDict: def __init__(self): self.d = {1: 1, 2: 2, 3: 3} def keys(self): return self.d.keys() def __getitem__(self, i): return self.d[i] d.clear() d.update(SimpleUserDict()) self.assertEqual(d, {1: 1, 2: 2, 3: 3}) class Exc(Exception): pass d.clear() class FailingUserDict: def keys(self): raise Exc self.assertRaises(Exc, d.update, FailingUserDict()) class FailingUserDict: def keys(self): class BogonIter: def __init__(self): self.i = 1 def __iter__(self): return self def next(self): if self.i: self.i = 0 return 'a' raise Exc return BogonIter() def __getitem__(self, key): return key self.assertRaises(Exc, d.update, FailingUserDict()) class FailingUserDict: def keys(self): class BogonIter: def __init__(self): self.i = ord('a') def __iter__(self): return self def next(self): if self.i <= ord('z'): rtn = chr(self.i) self.i += 1 return rtn raise StopIteration return BogonIter() def __getitem__(self, key): raise Exc self.assertRaises(Exc, d.update, FailingUserDict()) class badseq(object): def __iter__(self): return self def next(self): raise Exc() d = self._make_dict({}) self.assertRaises(Exc, d.update, badseq()) self.assertRaises(ValueError, d.update, [(1, 2, 3)]) def test_fromkeys(self): self.assertEqual(dict.fromkeys('abc'), { 'a': None, 'b': None, 'c': None }) d = self._make_dict({}) self.assertIsNot(d.fromkeys('abc'), d) self.assertEqual(d.fromkeys('abc'), {'a': None, 'b': None, 'c': None}) self.assertEqual(d.fromkeys((4, 5), 0), {4: 0, 5: 0}) self.assertEqual(d.fromkeys([]), {}) def g(): yield 1 self.assertEqual(d.fromkeys(g()), {1: None}) self.assertRaises(TypeError, {}.fromkeys, 3) class dictlike(dict): pass self.assertEqual(dictlike.fromkeys('a'), {'a': None}) self.assertEqual(dictlike().fromkeys('a'), {'a': None}) self.assertIsInstance(dictlike.fromkeys('a'), dictlike) self.assertIsInstance(dictlike().fromkeys('a'), dictlike) class mydict(dict): def __new__(cls): return UserDict.UserDict() ud = mydict.fromkeys('ab') self.assertEqual(ud, {'a': None, 'b': None}) self.assertIsInstance(ud, UserDict.UserDict) self.assertRaises(TypeError, dict.fromkeys) class Exc(Exception): pass class baddict1(dict): def __init__(self): raise Exc() self.assertRaises(Exc, baddict1.fromkeys, [1]) class BadSeq(object): def __iter__(self): return self def next(self): raise Exc() self.assertRaises(Exc, dict.fromkeys, BadSeq()) class baddict2(dict): def __setitem__(self, key, value): raise Exc() self.assertRaises(Exc, baddict2.fromkeys, [1]) # test fast path for dictionary inputs d = dict(zip(range(6), range(6))) self.assertEqual(dict.fromkeys(d, 0), dict(zip(range(6), [0] * 6))) def test_copy(self): d = self._make_dict({1: 1, 2: 2, 3: 3}) self.assertEqual(d.copy(), {1: 1, 2: 2, 3: 3}) self.assertEqual(self._make_dict({}).copy(), {}) self.assertRaises(TypeError, d.copy, None) def test_get(self): d = self._make_dict({}) self.assertIs(d.get('c'), None) self.assertEqual(d.get('c', 3), 3) d = self._make_dict({'a': 1, 'b': 2}) self.assertIs(d.get('c'), None) self.assertEqual(d.get('c', 3), 3) self.assertEqual(d.get('a'), 1) self.assertEqual(d.get('a', 3), 1) self.assertRaises(TypeError, d.get) self.assertRaises(TypeError, d.get, None, None, None) def test_setdefault(self): # dict.setdefault() d = self._make_dict({}) self.assertIs(d.setdefault('key0'), None) d.setdefault('key0', []) self.assertIs(d.setdefault('key0'), None) d.setdefault('key', []).append(3) self.assertEqual(d['key'][0], 3) d.setdefault('key', []).append(4) self.assertEqual(len(d['key']), 2) self.assertRaises(TypeError, d.setdefault) class Exc(Exception): pass class BadHash(object): fail = False def __hash__(self): if self.fail: raise Exc() else: return 42 x = BadHash() d[x] = 42 x.fail = True self.assertRaises(Exc, d.setdefault, x, []) @unittest.skipIf(test_support.is_jython and test_support.get_java_version() >= (9, ), "Fails on Java 9+. See b.j.o. issue 2711.") def test_setdefault_atomic(self): # Issue #13521: setdefault() calls __hash__ and __eq__ only once. class Hashed(object): def __init__(self): self.hash_count = 0 self.eq_count = 0 def __hash__(self): self.hash_count += 1 return 42 def __eq__(self, other): self.eq_count += 1 return id(self) == id(other) hashed1 = Hashed() y = self._make_dict({hashed1: 5}) if isinstance(y, Map) and not isinstance(y, ConcurrentMap): raise unittest.SkipTest( "java.util.Map objects that do not implement ConcurrentMap have no concurrency guarantees" ) # given that there are potentially multiple copies of the # above dict in self._make_dict, record the hash_count so it # can be subtracted out setup_hash_count = hashed1.hash_count hashed2 = Hashed() y.setdefault(hashed2, []) self.assertEqual(hashed1.hash_count, setup_hash_count) self.assertEqual(hashed2.hash_count, 1) self.assertEqual(hashed1.eq_count + hashed2.eq_count, 1) def test_popitem(self): # dict.popitem() for copymode in -1, +1: # -1: b has same structure as a # +1: b is a.copy() for log2size in range(12): size = 2**log2size a = {} b = {} for i in range(size): a[repr(i)] = i if copymode < 0: b[repr(i)] = i if copymode > 0: b = a.copy() for i in range(size): ka, va = ta = a.popitem() self.assertEqual(va, int(ka)) kb, vb = tb = b.popitem() self.assertEqual(vb, int(kb)) self.assertFalse(copymode < 0 and ta != tb) self.assertFalse(a) self.assertFalse(b) d = self._make_dict({}) self.assertRaises(KeyError, d.popitem) def test_pop(self): # Tests for pop with specified key d = self._make_dict({}) k, v = 'abc', 'def' d[k] = v self.assertRaises(KeyError, d.pop, 'ghi') self.assertEqual(d.pop(k), v) self.assertEqual(len(d), 0) self.assertRaises(KeyError, d.pop, k) # verify longs/ints get same value when key > 32 bits # (for 64-bit archs). See SF bug #689659. x = 4503599627370496L y = 4503599627370496 h = self._make_dict({x: 'anything', y: 'something else'}) self.assertEqual(h[x], h[y]) self.assertEqual(d.pop(k, v), v) d[k] = v self.assertEqual(d.pop(k, 1), v) self.assertRaises(TypeError, d.pop) class Exc(Exception): pass class BadHash(object): fail = False def __hash__(self): if self.fail: raise Exc() else: return 42 x = BadHash() d[x] = 42 x.fail = True self.assertRaises(Exc, d.pop, x) @unittest.skipIf(test_support.is_jython, "Weakly consistent iteration is compatible with mutation") def test_mutatingiteration(self): # changing dict size during iteration d = self._make_dict({}) d[1] = 1 with self.assertRaises(RuntimeError): for i in d: d[i + 1] = 1 def test_repr(self): d = self._make_dict({}) self.assertEqual(repr(d), '{}') d[1] = 2 self.assertEqual(repr(d), '{1: 2}') d = self._make_dict({}) d[1] = d self.assertEqual(repr(d), '{1: {...}}') class Exc(Exception): pass class BadRepr(object): def __repr__(self): raise Exc() d = self._make_dict({1: BadRepr()}) self.assertRaises(Exc, repr, d) def test_le(self): self.assertFalse(self._make_dict({}) < {}) self.assertFalse(self._make_dict({1: 2}) < {1L: 2L}) class Exc(Exception): pass class BadCmp(object): def __eq__(self, other): raise Exc() def __hash__(self): return 42 d1 = self._make_dict({BadCmp(): 1}) d2 = self._make_dict({1: 1}) with self.assertRaises(Exc): d1 < d2 def test_missing(self): # Make sure dict doesn't have a __missing__ method self.assertFalse(hasattr(dict, "__missing__")) self.assertFalse(hasattr(self._make_dict({}), "__missing__")) # Test several cases: # (D) subclass defines __missing__ method returning a value # (E) subclass defines __missing__ method raising RuntimeError # (F) subclass sets __missing__ instance variable (no effect) # (G) subclass doesn't define __missing__ at a all class D(dict): def __missing__(self, key): return 42 d = D({1: 2, 3: 4}) self.assertEqual(d[1], 2) self.assertEqual(d[3], 4) self.assertNotIn(2, d) self.assertNotIn(2, d.keys()) self.assertEqual(d[2], 42) class E(dict): def __missing__(self, key): raise RuntimeError(key) e = E() with self.assertRaises(RuntimeError) as c: e[42] self.assertEqual(c.exception.args, (42, )) class F(dict): def __init__(self): # An instance variable __missing__ should have no effect self.__missing__ = lambda key: None f = F() with self.assertRaises(KeyError) as c: f[42] self.assertEqual(c.exception.args, (42, )) class G(dict): pass g = G() with self.assertRaises(KeyError) as c: g[42] self.assertEqual(c.exception.args, (42, )) def test_tuple_keyerror(self): # SF #1576657 d = self._make_dict({}) with self.assertRaises(KeyError) as c: d[(1, )] self.assertEqual(c.exception.args, ((1, ), )) def test_bad_key(self): # Dictionary lookups should fail if __cmp__() raises an exception. class CustomException(Exception): pass class BadDictKey: def __hash__(self): return hash(self.__class__) def __cmp__(self, other): if isinstance(other, self.__class__): raise CustomException return other d = self._make_dict({}) x1 = BadDictKey() x2 = BadDictKey() d[x1] = 1 for stmt in [ 'd[x2] = 2', 'z = d[x2]', 'x2 in d', 'd.has_key(x2)', 'd.get(x2)', 'd.setdefault(x2, 42)', 'd.pop(x2)', 'd.update({x2: 2})' ]: with self.assertRaises(CustomException): exec stmt in locals() def test_resize1(self): # Dict resizing bug, found by Jack Jansen in 2.2 CVS development. # This version got an assert failure in debug build, infinite loop in # release build. Unfortunately, provoking this kind of stuff requires # a mix of inserts and deletes hitting exactly the right hash codes in # exactly the right order, and I can't think of a randomized approach # that would be *likely* to hit a failing case in reasonable time. d = self._make_dict({}) for i in range(5): d[i] = i for i in range(5): del d[i] for i in range(5, 9): # i==8 was the problem d[i] = i def test_resize2(self): # Another dict resizing bug (SF bug #1456209). # This caused Segmentation faults or Illegal instructions. class X(object): def __hash__(self): return 5 def __eq__(self, other): if resizing: d.clear() return False d = self._make_dict({}) resizing = False d[X()] = 1 d[X()] = 2 d[X()] = 3 d[X()] = 4 d[X()] = 5 # now trigger a resize resizing = True d[9] = 6 def test_empty_presized_dict_in_freelist(self): # Bug #3537: if an empty but presized dict with a size larger # than 7 was in the freelist, it triggered an assertion failure with self.assertRaises(ZeroDivisionError): d = self._make_dict({ 'a': 1 // 0, 'b': None, 'c': None, 'd': None, 'e': None, 'f': None, 'g': None, 'h': None }) d = {} def test_container_iterator(self): # Bug #3680: tp_traverse was not implemented for dictiter objects class C(object): pass iterators = (dict.iteritems, dict.itervalues, dict.iterkeys) for i in iterators: obj = C() ref = weakref.ref(obj) container = {obj: 1} obj.x = i(container) del obj, container gc.collect() self.assertIs(ref(), None, "Cycle was not collected") def _not_tracked(self, t): # Nested containers can take several collections to untrack gc.collect() gc.collect() self.assertFalse(gc.is_tracked(t), t) def _tracked(self, t): self.assertTrue(gc.is_tracked(t), t) gc.collect() gc.collect() self.assertTrue(gc.is_tracked(t), t) @test_support.cpython_only def test_track_literals(self): # Test GC-optimization of dict literals x, y, z, w = 1.5, "a", (1, None), [] self._not_tracked({}) self._not_tracked({x: (), y: x, z: 1}) self._not_tracked({1: "a", "b": 2}) self._not_tracked({1: 2, (None, True, False, ()): int}) self._not_tracked({1: object()}) # Dicts with mutable elements are always tracked, even if those # elements are not tracked right now. self._tracked({1: []}) self._tracked({1: ([], )}) self._tracked({1: {}}) self._tracked({1: set()}) @test_support.cpython_only def test_track_dynamic(self): # Test GC-optimization of dynamically-created dicts class MyObject(object): pass x, y, z, w, o = 1.5, "a", (1, object()), [], MyObject() d = dict() self._not_tracked(d) d[1] = "a" self._not_tracked(d) d[y] = 2 self._not_tracked(d) d[z] = 3 self._not_tracked(d) self._not_tracked(d.copy()) d[4] = w self._tracked(d) self._tracked(d.copy()) d[4] = None self._not_tracked(d) self._not_tracked(d.copy()) # dd isn't tracked right now, but it may mutate and therefore d # which contains it must be tracked. d = dict() dd = dict() d[1] = dd self._not_tracked(dd) self._tracked(d) dd[1] = d self._tracked(dd) d = dict.fromkeys([x, y, z]) self._not_tracked(d) dd = dict() dd.update(d) self._not_tracked(dd) d = dict.fromkeys([x, y, z, o]) self._tracked(d) dd = dict() dd.update(d) self._tracked(dd) d = dict(x=x, y=y, z=z) self._not_tracked(d) d = dict(x=x, y=y, z=z, w=w) self._tracked(d) d = dict() d.update(x=x, y=y, z=z) self._not_tracked(d) d.update(w=w) self._tracked(d) d = dict([(x, y), (z, 1)]) self._not_tracked(d) d = dict([(x, y), (z, w)]) self._tracked(d) d = dict() d.update([(x, y), (z, 1)]) self._not_tracked(d) d.update([(x, y), (z, w)]) self._tracked(d) @test_support.cpython_only def test_track_subtypes(self): # Dict subtypes are always tracked class MyDict(dict): pass self._tracked(MyDict()) def test_list_equality(self): class A(dict): pass for dtype in (A, dict): self.assertEquals([dtype()], [dict()])
class SSLSocketConnectTest(unittest.TestCase): def setUp(self): self.httpd, self.server_thread = start_server() self.httpd.socket = ssl.wrap_socket( self.httpd.socket, certfile=ONLYCERT, server_side=True, keyfile=ONLYKEY, ) self.address = self.httpd.server_name, self.httpd.server_port def tearDown(self): self.httpd.shutdown() self.server_thread.join() def do_nonblocking_connection(self, results, index): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setblocking(0) connect_errno = 0 connect_attempt = 0 sock = ssl.wrap_socket(sock, certfile=CERTFILE, do_handshake_on_connect=True) while connect_errno != errno.EISCONN and connect_attempt < 10000: connect_attempt += 1 connect_errno = sock.connect_ex(self.address) results[index].append(connect_errno) time.sleep(0.01) sock.close() def do_workout(self, num_threads=10): connect_results = [] connect_threads = [] for i in xrange(num_threads): connect_results.append([]) connect_threads.append( threading.Thread(target=self.do_nonblocking_connection, name="socket-workout-%s" % i, args=(connect_results, i))) for thread in connect_threads: thread.start() for thread in connect_threads: thread.join() return connect_results @unittest.skipIf( test_support.is_jython and test_support.get_java_version() >= (9, ), # FIXME "Fails on Java 9+. See b.j.o. issue #2710") def test_connect_ex_workout(self): """Verify connect_ex states go through EINPROGRESS?, EALREADY*, EISCONN""" # Tests fix for http://bugs.jython.org/issue2428; based in part on the # code showing failure that was submitted with that bug for result in self.do_workout(): if len(result) == 0: self.fail("A socket-workout thread failed to run") self.assertIn(result[0], {errno.EINPROGRESS, errno.EISCONN}) self.assertEqual(result[-1], errno.EISCONN) for code in result[1:-1]: self.assertEqual(code, errno.EALREADY)
class ImpTestCase(unittest.TestCase): def test_imp_find_module_builtins(self): self.assertEqual(imp.find_module('sys'), (None, 'sys', ('', '', 6))) self.assertEqual(imp.find_module('__builtin__'), (None, '__builtin__', ('', '', 6))) def test_imp_is_builtin(self): self.assertTrue( all(imp.is_builtin(mod) for mod in ['sys', '__builtin__'])) self.assertFalse(imp.is_builtin('os')) def test_load_compiled(self): compiled = os.__file__ if compiled.endswith('.py'): compiled = compiled[:-3] + COMPILED_SUFFIX os.__doc__ = 'foo' self.assertEqual(os, imp.load_compiled("os", compiled)) self.assertFalse(os.__doc__ == 'foo') with open(compiled, 'rb') as fp: os.__doc__ = 'foo' self.assertEqual(os, imp.load_compiled("os", compiled, fp)) self.assertFalse(os.__doc__ == 'foo') 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') @unittest.skipIf(test_support.get_java_version() >= (9, ), "Fails on Java 9+. See b.j.o. issue #2362") # FIXME # Probably related to Java modules: ensure also works outside java.base def test_import_star(self): self.assertEquals( 0, subprocess.call([ sys.executable, test_support.findfile("import_star_from_java.py") ])) def test_selfreferential_classes(self): from org.python.tests.inbred import Metis from org.python.tests.inbred import Zeus self.assertEquals(Metis, Zeus.Athena.__bases__[0]) self.assertEquals(Zeus, Metis.__bases__[0]) def test_sys_modules_deletion(self): self.assertRaises(ZeroDivisionError, __import__, 'test.module_deleter') #XXX: this is probably a good test to push upstream to CPython. if hasattr(os, "symlink"): def test_symlinks(self): # Ensure imports work over symlinks. Did not work in Jython from # 2.1 to 2.5.0, fixed in 2.5.1 See # http://bugs.jython.org/issue645615. sym = test_support.TESTFN + "1" try: os.mkdir(test_support.TESTFN) init = os.path.join(test_support.TESTFN, "__init__.py") with open(init, 'w') as fp: fp.write("test = 'imported'") os.symlink(test_support.TESTFN, sym) module = os.path.basename(sym) module_obj = __import__(module) self.assertEquals(module_obj.test, 'imported') finally: shutil.rmtree(test_support.TESTFN) test_support.unlink(sym) def test_issue1811(self): # Previously this blew out the stack from test.issue1811 import foo self.assertTrue(foo.issue1811.foo is foo) def test_issue1952(self): # CPython 2.x ignores non-dict's in second arg to __import__ # The following threw an exception in Jython previously. __import__("os", [], level=-1)
class PyclasspathImporterTestCase(unittest.TestCase): RESOURCE_DATA = "Always look\non the bright side\r\nof life." def setUp(self): self.orig_context = Thread.currentThread().contextClassLoader self.temp_dir = tempfile.mkdtemp() self.modules = sys.modules.keys() def tearDown(self): Thread.currentThread().contextClassLoader = self.orig_context for module in sys.modules.keys(): if module not in self.modules: del sys.modules[module] try: shutil.rmtree(self.temp_dir) except OSError: # On Windows at least we cannot delete the open JAR pass def prepareJar(self, orig_jar): # Create a new copy of the checked-in test jar orig_jar = test_support.findfile(orig_jar) jar = os.path.join(self.temp_dir, os.path.basename(orig_jar)) shutil.copy(orig_jar, jar) return jar def compileToJar(self, jar, compile_path=''): # Add a compiled version of prefer_compiled.py to the jar source = 'prefer_compiled.py' code = os.path.join(self.temp_dir, source) with open(code, 'w') as fp: fp.write('compiled = True') # Compile that file py_compile.compile(code) # Now add the compiled file to the jar compiled = source.replace('.py', '$py.class') with zipfile.ZipFile(jar, 'a') as zip: zip.write(os.path.join(self.temp_dir, compiled), os.path.join(compile_path, 'jar_pkg', compiled)) return compiled def addResourceToJar(self, jar, package='jar_pkg'): name = 'testdata.dat' with zipfile.ZipFile(jar, 'a') as zip: zip.writestr(package + '/' + name, self.RESOURCE_DATA) return name def checkImports(self, prefix, compiled): import flat_in_jar self.assertEquals(flat_in_jar.value, 7) import jar_pkg self.assertEquals(prefix + '/jar_pkg/__init__.py', jar_pkg.__file__) from jar_pkg import prefer_compiled self.assertEquals(prefix + '/jar_pkg/' + compiled, 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): jar = self.prepareJar('classimport.jar') compiled = self.compileToJar(jar) Thread.currentThread( ).contextClassLoader = test_support.make_jar_classloader(jar) self.checkImports('__pyclasspath__', compiled) def test_path_in_pyclasspath(self): jar = self.prepareJar('classimport_Lib.jar') compiled = self.compileToJar(jar, 'Lib') Thread.currentThread( ).contextClassLoader = test_support.make_jar_classloader(jar) with test_support.DirsOnSysPath(): sys.path = ['__pyclasspath__/Lib'] self.checkImports('__pyclasspath__/Lib', compiled) def test_loader_is_package(self): jar = self.prepareJar('classimport.jar') Thread.currentThread( ).contextClassLoader = test_support.make_jar_classloader(jar) mod_name = 'flat_in_jar' loader = pkgutil.get_loader(mod_name) self.assertFalse(loader.is_package(mod_name)) self.assertTrue(loader.is_package('jar_pkg')) self.assertFalse(loader.is_package('jar_pkg.prefer_compiled')) @unittest.skipIf(test_support.is_jython_posix, "FIXME: failing on Linux issue #2422") @unittest.skipIf(test_support.get_java_version() >= (9, ), "Fails on Java 9+. See b.j.o. issue #2362") # FIXME # Probably related to Java modules: ensure also works outside java.base def test_loader_get_code(self): # Execute Python code out of the JAR jar = self.prepareJar('classimport.jar') Thread.currentThread( ).contextClassLoader = test_support.make_jar_classloader(jar) loader = pkgutil.get_loader('jar_pkg') space = {'value': None, 'compiled': None} # flat_in_jar contains the assignment value = 7 code = loader.get_code('flat_in_jar') exec code in space self.assertEquals(space['value'], 7) # jar_pkg.prefer_compiled contains the assignment compiled = False code = loader.get_code('jar_pkg.prefer_compiled') exec code in space self.assertEquals(space['compiled'], False) # Compile a new one containing the assignment compiled = True self.compileToJar(jar) code = loader.get_code('jar_pkg.prefer_compiled') exec code in space self.assertEquals(space['compiled'], True) def test_pkgutil_get_data(self): # Test loader.get_data used via pkgutil jar = self.prepareJar('classimport.jar') name = self.addResourceToJar(jar) Thread.currentThread( ).contextClassLoader = test_support.make_jar_classloader(jar) data = pkgutil.get_data('jar_pkg', name) self.assertIsInstance(data, bytes) self.assertEqual(data, self.RESOURCE_DATA) def test_loader_get_data(self): # Test loader.get_data used via pkgutil.get_loader jar = self.prepareJar('classimport.jar') name = self.addResourceToJar(jar) Thread.currentThread( ).contextClassLoader = test_support.make_jar_classloader(jar) loader = pkgutil.get_loader('jar_pkg') # path is a resource path (not file system path using os.path.sep) path = 'jar_pkg/' + name data = loader.get_data(path) self.assertIsInstance(data, bytes) self.assertEqual(data, self.RESOURCE_DATA) def test_importer_get_data(self): # Test loader.get_data used via pkgutil.get_importer jar = self.prepareJar('classimport.jar') name = self.addResourceToJar(jar) Thread.currentThread( ).contextClassLoader = test_support.make_jar_classloader(jar) importer = pkgutil.get_importer('__pyclasspath__/') # path is a resource path (may be file system path using os.path.sep) path = os.path.join('jar_pkg', name) data = importer.get_data(path) self.assertIsInstance(data, bytes) self.assertEqual(data, self.RESOURCE_DATA) # Check works a second time (stream use internal to implementation) data = importer.get_data(path) self.assertEqual(data, self.RESOURCE_DATA) def test_importer_get_source(self): # Test loader.get_source used via pkgutil.get_importer jar = self.prepareJar('classimport.jar') Thread.currentThread( ).contextClassLoader = test_support.make_jar_classloader(jar) importer = pkgutil.get_importer('__pyclasspath__/') # In package mod = 'jar_pkg.prefer_compiled' source = importer.get_source(mod) self.assertIsInstance(source, bytes) self.assertEqual(source, 'compiled = False\n')