From 00dd78bf1b316e398280f13fa266f117294dc846 Mon Sep 17 00:00:00 2001
From: Matthias Bartelmess <mba@fourplusone.de>
Date: Fri, 21 Oct 2016 06:06:40 +0300
Subject: [PATCH] Fix windows tests

---
 pygit2/errors.py        |  4 +++-
 pygit2/remote.py        | 21 +++++++++------------
 test/test_remote.py     |  6 ++++++
 test/test_repository.py | 12 +++++++++---
 4 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/pygit2/errors.py b/pygit2/errors.py
index 363ed42..57c155c 100644
--- a/pygit2/errors.py
+++ b/pygit2/errors.py
@@ -64,4 +64,6 @@ def check_error(err, io=False):
     raise GitError(message)
 
 # Indicate that we want libgit2 to pretend a function was not set
-Passthrough = Exception("The function asked for pass-through")
+class Passthrough(Exception):
+    def __init__(self):
+        super(Passthrough, self).__init__( "The function asked for pass-through")
diff --git a/pygit2/remote.py b/pygit2/remote.py
index 431b356..2ac9316 100644
--- a/pygit2/remote.py
+++ b/pygit2/remote.py
@@ -278,11 +278,9 @@ class RemoteCallbacks(object):
         try:
             ccred = get_credentials(credentials, url, username, allowed)
             cred_out[0] = ccred[0]
-
+        except Passthrough as e:
+            return C.GIT_PASSTHROUGH
         except Exception as e:
-            if e is Passthrough:
-                return C.GIT_PASSTHROUGH
-
             self._stored_exception = e
             return C.GIT_EUSER
 
@@ -308,15 +306,14 @@ class RemoteCallbacks(object):
             val = certificate_check(None, bool(valid), ffi.string(host))
             if not val:
                 return C.GIT_ECERTIFICATE
+        except Passthrough as e:
+            if is_ssh:
+                return 0
+            elif valid:
+                return 0
+            else:
+                return C.GIT_ECERTIFICATE
         except Exception as e:
-            if e is Passthrough:
-                if is_ssh:
-                    return 0
-                elif valid:
-                    return 0
-                else:
-                    return C.GIT_ECERTIFICATE
-
             self._stored_exception = e
             return C.GIT_EUSER
 
diff --git a/test/test_remote.py b/test/test_remote.py
index 489a6ae..5d76f06 100644
--- a/test/test_remote.py
+++ b/test/test_remote.py
@@ -33,6 +33,7 @@ import pygit2
 import sys
 from pygit2 import Oid
 from . import utils
+import gc
 
 try:
     import __pypy__
@@ -238,6 +239,11 @@ class PushTestCase(unittest.TestCase):
         self.remote = self.clone.create_remote('origin', self.origin.path)
 
     def tearDown(self):
+        self.origin = None
+        self.clone = None
+        self.remote = None
+        gc.collect()
+        
         self.origin_ctxtmgr.__exit__(None, None, None)
         self.clone_ctxtmgr.__exit__(None, None, None)
 
diff --git a/test/test_repository.py b/test/test_repository.py
index 5590ea2..2c59465 100644
--- a/test/test_repository.py
+++ b/test/test_repository.py
@@ -41,6 +41,12 @@ import sys
 
 import six
 
+if six.PY2:
+    from urllib import pathname2url
+    
+if six.PY3:
+    from urllib.request import pathname2url
+
 # Import from pygit2
 from pygit2 import GIT_OBJ_ANY, GIT_OBJ_BLOB, GIT_OBJ_COMMIT
 from pygit2 import init_repository, clone_repository, discover_repository
@@ -192,8 +198,8 @@ class RepositoryTest(utils.BareRepoTestCase):
 
     def test_hashfile(self):
         data = "bazbarfoo"
-        tempfile_path = tempfile.mkstemp()[1]
-        with open(tempfile_path, 'w') as fh:
+        handle, tempfile_path = tempfile.mkstemp()
+        with os.fdopen(handle, 'w') as fh:
             fh.write(data)
         hashed_sha1 = hashfile(tempfile_path)
         os.unlink(tempfile_path)
@@ -513,7 +519,7 @@ class CloneRepositoryTest(utils.NoRepoTestCase):
     def test_clone_repository_and_remote_callbacks(self):
         src_repo_relpath = "./test/data/testrepo.git/"
         repo_path = os.path.join(self._temp_dir, "clone-into")
-        url = 'file://' + os.path.realpath(src_repo_relpath)
+        url = 'file:' + pathname2url(os.path.realpath(src_repo_relpath))
 
         def create_repository(path, bare):
             return init_repository(path, bare)