summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarkus F.X.J. Oberhumer <markus@oberhumer.com>2001-12-01 14:29:57 +0000
committerMarkus F.X.J. Oberhumer <markus@oberhumer.com>2001-12-01 14:29:57 +0000
commit348dc9194e148e337a6cd653f4642f4f22cb28d1 (patch)
treebcc3ddf14ae11f9720f98467af43aa7bd5f49750
parentebe90b3d5231b2abb9829944fbddf0964b4fae81 (diff)
downloadpycurl-348dc9194e148e337a6cd653f4642f4f22cb28d1.tar.gz
do_init(): Improved error handling - don't leak memory, and always
set an exception error object.
-rw-r--r--src/curl.c62
1 files changed, 29 insertions, 33 deletions
diff --git a/src/curl.c b/src/curl.c
index f8c632f..fb46cc8 100644
--- a/src/curl.c
+++ b/src/curl.c
@@ -741,50 +741,20 @@ statichere PyTypeObject Curl_Type = {
static CurlObject *
do_init(PyObject *arg)
{
- CURL *curlhandle;
CurlObject *self;
int res;
- /* Initialize curl */
- curlhandle = curl_easy_init();
- if (curlhandle == NULL) {
- return NULL;
- }
-
/* Allocate python curl object */
#if (PY_VERSION_HEX < 0x01060000)
self = (CurlObject *) PyObject_NEW(CurlObject, &Curl_Type);
#else
self = (CurlObject *) PyObject_New(CurlObject, &Curl_Type);
#endif
- if (self == NULL) {
- curl_easy_cleanup(curlhandle);
- return NULL;
- }
-
- /* Set error buffer */
- res = curl_easy_setopt(curlhandle, CURLOPT_ERRORBUFFER, self->error);
- if (res != 0) {
- curl_easy_cleanup(curlhandle);
+ if (self == NULL)
return NULL;
- }
- memset(self->error, 0, sizeof(char) * CURL_ERROR_SIZE);
- /* Set NOPROGRESS to 1 by default */
- res = curl_easy_setopt(curlhandle, CURLOPT_NOPROGRESS, 1);
- if (res != 0) {
- curl_easy_cleanup(curlhandle);
- return NULL;
- }
- /* Set VERBOSE to 0 by default */
- res = curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 0);
- if (res != 0) {
- curl_easy_cleanup(curlhandle);
- return NULL;
- }
-
- /* Setup python curl object initial values and return object */
- self->handle = curlhandle;
+ /* Setup python curl object initial values */
+ self->handle = NULL;
self->url = NULL;
self->httpheader = NULL;
self->quote = NULL;
@@ -798,7 +768,33 @@ do_init(PyObject *arg)
self->pro_cb = NULL;
self->pwd_cb = NULL;
+ /* Initialize curl */
+ self->handle = curl_easy_init();
+ if (self->handle == NULL)
+ goto error;
+
+ /* Set error buffer */
+ res = curl_easy_setopt(self->handle, CURLOPT_ERRORBUFFER, self->error);
+ if (res != 0)
+ goto error;
+ memset(self->error, 0, sizeof(char) * CURL_ERROR_SIZE);
+
+ /* Set NOPROGRESS to 1 by default */
+ res = curl_easy_setopt(self->handle, CURLOPT_NOPROGRESS, 1);
+ if (res != 0)
+ goto error;
+ /* Set VERBOSE to 0 by default */
+ res = curl_easy_setopt(self->handle, CURLOPT_VERBOSE, 0);
+ if (res != 0)
+ goto error;
+
+ /* Success - return new object */
return self;
+
+error:
+ Py_DECREF(self); /* this also closes self->handle */
+ PyErr_SetString(ErrorObject, "initializing curl failed");
+ return NULL;
}