--- src/pycurl.c.old	Tue Sep  9 10:40:34 2008
+++ src/pycurl.c	Tue Apr 28 20:22:53 2009
@@ -745,69 +745,86 @@
     memset(self->error, 0, sizeof(self->error));
 
     return self;
 }
 
-
-/* constructor - this is a module-level function returning a new instance */
-static CurlObject *
-do_curl_new(PyObject *dummy)
+/* initializer - used to intialize curl easy handles for use with pycurl */
+static int
+util_curl_init(CurlObject *self)
 {
-    CurlObject *self = NULL;
     int res;
     char *s = NULL;
 
-    UNUSED(dummy);
-
-    /* Allocate python curl object */
-    self = util_curl_new();
-    if (self == NULL)
-        return NULL;
-
-    /* Initialize curl handle */
-    self->handle = curl_easy_init();
-    if (self->handle == NULL)
-        goto error;
-
     /* Set curl error buffer and zero it */
     res = curl_easy_setopt(self->handle, CURLOPT_ERRORBUFFER, self->error);
-    if (res != CURLE_OK)
-        goto error;
+    if (res != CURLE_OK) {
+        return (-1);
+    }
     memset(self->error, 0, sizeof(self->error));
 
     /* Set backreference */
     res = curl_easy_setopt(self->handle, CURLOPT_PRIVATE, (char *) self);
-    if (res != CURLE_OK)
-        goto error;
+    if (res != CURLE_OK) {
+        return (-1);
+    }
 
     /* Enable NOPROGRESS by default, i.e. no progress output */
     res = curl_easy_setopt(self->handle, CURLOPT_NOPROGRESS, (long)1);
-    if (res != CURLE_OK)
-        goto error;
+    if (res != CURLE_OK) {
+        return (-1);
+    }
 
     /* Disable VERBOSE by default, i.e. no verbose output */
     res = curl_easy_setopt(self->handle, CURLOPT_VERBOSE, (long)0);
-    if (res != CURLE_OK)
-        goto error;
+    if (res != CURLE_OK) {
+        return (-1);
+    }
 
     /* Set FTP_ACCOUNT to NULL by default */
     res = curl_easy_setopt(self->handle, CURLOPT_FTP_ACCOUNT, NULL);
-    if (res != CURLE_OK)
-        goto error;
+    if (res != CURLE_OK) {
+        return (-1);
+    }
 
     /* Set default USERAGENT */
     s = (char *) malloc(7 + strlen(LIBCURL_VERSION) + 1);
-    if (s == NULL)
-        goto error;
+    if (s == NULL) {
+        return (-1);
+    }
     strcpy(s, "PycURL/"); strcpy(s+7, LIBCURL_VERSION);
     res = curl_easy_setopt(self->handle, CURLOPT_USERAGENT, (char *) s);
     if (res != CURLE_OK) {
         free(s);
-        goto error;
+        return (-1);
     }
     self->options[ OPT_INDEX(CURLOPT_USERAGENT) ] = s; s = NULL;
 
+    return (0);
+}
+
+/* constructor - this is a module-level function returning a new instance */
+static CurlObject *
+do_curl_new(PyObject *dummy)
+{
+    CurlObject *self = NULL;
+    int res;
+
+    UNUSED(dummy);
+
+    /* Allocate python curl object */
+    self = util_curl_new();
+    if (self == NULL)
+        return NULL;
+
+    /* Initialize curl handle */
+    self->handle = curl_easy_init();
+    if (self->handle == NULL)
+        goto error;
+
+    res = util_curl_init(self);
+    if (res < 0)
+            goto error;
     /* Success - return new object */
     return self;
 
 error:
     Py_DECREF(self);    /* this also closes self->handle */
@@ -1423,10 +1440,11 @@
 
 static PyObject*
 do_curl_reset(CurlObject *self)
 {
     unsigned int i;
+    int res;
 
     curl_easy_reset(self->handle);
 
     /* Decref callbacks and file handles */
     util_curl_xdecref(self, 4 | 8, self->handle);
@@ -1450,11 +1468,21 @@
             free(self->options[i]);
             self->options[i] = NULL;
         }
     }
 
+    res = util_curl_init(self);
+    if (res < 0)
+            goto error;
+
+    Py_INCREF(Py_None);
     return Py_None;
+
+error:
+    Py_DECREF(self);    /* this also closes self->handle */
+    PyErr_SetString(ErrorObject, "resetting curl failed");
+    return NULL;
 }
 
 /* --------------- unsetopt/setopt/getinfo --------------- */
 
 static PyObject *

