Commits

Armin Rigo committed 3c269e6 Merge

merge heads

Comments (0)

Files changed (6)

lib-python/2.7/test/test_zipfile.py

 from unittest import skipUnless
 
 from test.test_support import TESTFN, TESTFN_UNICODE, TESTFN_ENCODING, \
-                              run_unittest, findfile, unlink
+                              run_unittest, findfile, unlink, rmtree
 try:
     TESTFN_UNICODE.encode(TESTFN_ENCODING)
 except (UnicodeError, TypeError):
         produces the expected result."""
         with zipfile.ZipFile(TESTFN2, "w") as zipfp:
             zipfp.write(TESTFN)
-            self.assertEqual(zipfp.read(TESTFN), open(TESTFN).read())
+            with open(TESTFN,'r') as fid:
+                self.assertEqual(zipfp.read(TESTFN), fid.read())
 
     @skipUnless(zlib, "requires zlib")
     def test_per_file_compression(self):
                 self.assertEqual(writtenfile, correctfile)
 
                 # make sure correct data is in correct file
-                self.assertEqual(fdata, open(writtenfile, "rb").read())
+                with open(writtenfile, "rb") as fid:
+                    self.assertEqual(fdata, fid.read())
                 os.remove(writtenfile)
 
         # remove the test file subdirectories
-        shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
+        rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
 
     def test_extract_all(self):
         with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
             zipfp.extractall()
             for fpath, fdata in SMALL_TEST_DATA:
                 outfile = os.path.join(os.getcwd(), fpath)
-
-                self.assertEqual(fdata, open(outfile, "rb").read())
+                
+                with open(outfile, "rb") as fid:
+                    self.assertEqual(fdata, fid.read())
                 os.remove(outfile)
 
         # remove the test file subdirectories
-        shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
+        rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
 
     def check_file(self, filename, content):
         self.assertTrue(os.path.isfile(filename))
                 self.assertEqual(writtenfile, correctfile,
                                  msg="extract %r" % arcname)
             self.check_file(correctfile, content)
-            shutil.rmtree('target')
+            rmtree('target')
 
             with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
                 zipfp.extractall(targetpath)
             self.check_file(correctfile, content)
-            shutil.rmtree('target')
+            rmtree('target')
 
             correctfile = os.path.join(os.getcwd(), *fixedname.split('/'))
 
                 self.assertEqual(writtenfile, correctfile,
                                  msg="extract %r" % arcname)
             self.check_file(correctfile, content)
-            shutil.rmtree(fixedname.split('/')[0])
+            rmtree(fixedname.split('/')[0])
 
             with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
                 zipfp.extractall()
             self.check_file(correctfile, content)
-            shutil.rmtree(fixedname.split('/')[0])
+            rmtree(fixedname.split('/')[0])
 
             os.remove(TESTFN2)
 
     def tearDown(self):
         unlink(TESTFN)
         unlink(TESTFN2)
+        if os.path.exists(TESTFN):
+            os.remove(TESTFN)
 
 
 class TestZip64InSmallFiles(unittest.TestCase):
 
 
 class PyZipFileTests(unittest.TestCase):
+    def teardown(self):
+        if os.path.exists(TESTFN):
+            os.remove(TESTFN)
+        if os.path.exists(TESTFN2):
+            os.remove(TESTFN2)
+
     def test_write_pyfile(self):
         with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
             fn = __file__
             self.assertNotIn('mod2.txt', names)
 
         finally:
-            shutil.rmtree(TESTFN2)
+            rmtree(TESTFN2)
 
     def test_write_non_pyfile(self):
+        if os.path.exists(TESTFN):
+            os.remove(TESTFN)
         with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
-            open(TESTFN, 'w').write('most definitely not a python file')
+            with open(TESTFN, 'w') as fid:
+                fid.write('most definitely not a python file')
             self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
             os.remove(TESTFN)
 
         self.assertRaises(RuntimeError, zipf.open, "foo.txt")
         self.assertRaises(RuntimeError, zipf.testzip)
         self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
-        open(TESTFN, 'w').write('zipfile test data')
-        self.assertRaises(RuntimeError, zipf.write, TESTFN)
+        with open(TESTFN, 'w') as fid:
+            fid.write('zipfile test data')
+            self.assertRaises(RuntimeError, zipf.write, TESTFN)
 
     def test_bad_constructor_mode(self):
         """Check that bad modes passed to ZipFile constructor are caught."""
             pass
         try:
             zipf = zipfile.ZipFile(TESTFN, mode="r")
+            zipf.close()
         except zipfile.BadZipfile:
             self.fail("Unable to create empty ZIP file in 'w' mode")
 
             pass
         try:
             zipf = zipfile.ZipFile(TESTFN, mode="r")
+            zipf.close()
         except:
             self.fail("Unable to create empty ZIP file in 'a' mode")
 
     def tearDown(self):
         unlink(TESTFN)
         unlink(TESTFN2)
+        if os.path.exists(TESTFN):
+            os.remove(TESTFN)
 
 
 class DecryptionTests(unittest.TestCase):
 
     def test_bad_password(self):
         self.zip.setpassword("perl")
-        self.assertRaises(RuntimeError, self.zip.read, "test.txt")
+        try:
+            self.assertRaises(RuntimeError, self.zip.read, "test.txt")
+        finally:
+            self.zip.close()
         self.zip2.setpassword("perl")
-        self.assertRaises(RuntimeError, self.zip2.read, "zero")
+        try:
+            self.assertRaises(RuntimeError, self.zip2.read, "zero")
+        finally:    
+            self.zip2.close()
 
     @skipUnless(zlib, "requires zlib")
     def test_good_password(self):
         self.zip.setpassword("python")
-        self.assertEqual(self.zip.read("test.txt"), self.plain)
+        try:
+            self.assertEqual(self.zip.read("test.txt"), self.plain)
+        finally:
+            self.zip.close()
         self.zip2.setpassword("12345")
-        self.assertEqual(self.zip2.read("zero"), self.plain2)
+        try:
+            self.assertEqual(self.zip2.read("zero"), self.plain2)
+        finally:
+            self.zip2.close()
 
 
 class TestsWithRandomBinaryFiles(unittest.TestCase):
             fp.write(self.data)
 
     def tearDown(self):
-        unlink(TESTFN)
-        unlink(TESTFN2)
+        if os.path.exists(TESTFN):
+            os.remove(TESTFN)
+        if os.path.exists(TESTFN2):
+            os.remove(TESTFN2)
 
     def make_test_archive(self, f, compression):
         # Create the ZIP archive
         # Verify that (when the ZipFile is in control of creating file objects)
         # multiple open() calls can be made without interfering with each other.
         with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
-            zopen1 = zipf.open('ones')
-            zopen2 = zipf.open('ones')
-            data1 = zopen1.read(500)
-            data2 = zopen2.read(500)
-            data1 += zopen1.read(500)
-            data2 += zopen2.read(500)
+            with zipf.open('ones') as zopen1, zipf.open('ones') as zopen2:
+                data1 = zopen1.read(500)
+                data2 = zopen2.read(500)
+                data1 += zopen1.read(500)
+                data2 += zopen2.read(500)
             self.assertEqual(data1, data2)
 
     def test_different_file(self):
 
     def test_store_dir(self):
         os.mkdir(os.path.join(TESTFN2, "x"))
-        zipf = zipfile.ZipFile(TESTFN, "w")
-        zipf.write(os.path.join(TESTFN2, "x"), "x")
-        self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
+        with zipfile.ZipFile(TESTFN, "w") as zipf:
+            zipf.write(os.path.join(TESTFN2, "x"), "x")
+            self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
 
     def tearDown(self):
-        shutil.rmtree(TESTFN2)
+        rmtree(TESTFN2)
         if os.path.exists(TESTFN):
-            unlink(TESTFN)
+            os.remove(TESTFN)
 
 
 class UniversalNewlineTests(unittest.TestCase):
         for n, s in enumerate(self.seps):
             self.arcdata[s] = s.join(self.line_gen) + s
             self.arcfiles[s] = '%s-%d' % (TESTFN, n)
-            open(self.arcfiles[s], "wb").write(self.arcdata[s])
+            with open(self.arcfiles[s], "wb") as fid:
+                fid.write(self.arcdata[s])
 
     def make_test_archive(self, f, compression):
         # Create the ZIP archive
         # Read the ZIP archive
         with zipfile.ZipFile(f, "r") as zipfp:
             for sep, fn in self.arcfiles.items():
-                for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
-                    self.assertEqual(zipline, line + '\n')
+                with zipfp.open(fn, "rU") as fid:
+                    for line, zipline in zip(self.line_gen, fid):
+                        self.assertEqual(zipline, line + '\n')
 
     def test_read_stored(self):
         for f in (TESTFN2, TemporaryFile(), StringIO()):

lib_pypy/_ctypes/function.py

                 raise ValueError(
                     "native COM method call without 'this' parameter"
                     )
-            thisvalue = args.pop(0)
+            thisvalue = args[0]
             thisarg = cast(thisvalue, POINTER(POINTER(c_void_p)))
             keepalives, newargs, argtypes, outargs, errcheckargs = (
-                        self._convert_args(argtypes, args, kwargs))
-            args.insert(0, thisvalue)
+                self._convert_args(argtypes, args[1:], kwargs))
             newargs.insert(0, thisvalue.value)
             argtypes.insert(0, c_void_p)
         else:
             thisarg = None
             keepalives, newargs, argtypes, outargs, errcheckargs = (
-                        self._convert_args(argtypes, args, kwargs))
+                self._convert_args(argtypes, args, kwargs))
 
         funcptr = self._getfuncptr(argtypes, self._restype_, thisarg)
         result = self._call_funcptr(funcptr, *newargs)

pypy/module/test_lib_pypy/cffi_tests/test_function.py

         return self._value
 
 lib_m = 'm'
+has_sinf = True
 if sys.platform == 'win32':
     #there is a small chance this fails on Mingw via environ $CC
     import distutils.ccompiler
     if distutils.ccompiler.get_default_compiler() == 'msvc':
         lib_m = 'msvcrt'
+        has_sinf = False
 
 class TestFunction(object):
     Backend = CTypesBackend
         assert x == math.sin(1.23)
 
     def test_sinf(self):
+        if not has_sinf:
+            py.test.skip("sinf not available")
         ffi = FFI(backend=self.Backend())
         ffi.cdef("""
             float sinf(float x);

pypy/module/test_lib_pypy/test_sqlite3.py

         return 42
     con.set_authorizer(authorizer_cb)
     with pytest.raises(_sqlite3.OperationalError) as e:
-        con.execute('select 42')
-    assert str(e.value) == 'authorizer malfunction'
+        con.execute('select 123')
+    major, minor, micro = _sqlite3.sqlite_version.split('.')[:3]
+    if (int(major), int(minor), int(micro)) >= (3, 6, 14):
+        assert str(e.value) == 'authorizer malfunction'
+    else:
+        assert str(e.value) == \
+            ("illegal return value (1) from the authorization function - "
+             "should be SQLITE_OK, SQLITE_IGNORE, or SQLITE_DENY")
 
 
 def test_issue1573(con):

rpython/rlib/rsocket.py

                 address_to_fill=None):
     # port_or_service is a string, not an int (but try str(port_number)).
     assert port_or_service is None or isinstance(port_or_service, str)
+    if _c._MACOSX:
+        if port_or_service is None or port_or_service == '0':
+            port_or_service = '00'
     hints = lltype.malloc(_c.addrinfo, flavor='raw', zero=True)
     rffi.setintfield(hints, 'c_ai_family',   family)
     rffi.setintfield(hints, 'c_ai_socktype', socktype)

rpython/rlib/test/test_rsocket.py

             found = True
     assert found, lst
 
+def test_getaddrinfo_osx_crash():
+    # see CPython issue17269
+    for port in [None, '0', '00']:
+        getaddrinfo('localhost', port, 0, 0, 0, AI_NUMERICSERV)
+
 def test_connect_ex():
     s = RSocket()
     err = s.connect_ex(INETAddress('0.0.0.0', 0))   # should not work