config: borrow the string for lookup and setting

We don't need our own copy of the string, so use the new borrowing
mechanism to use python's underlying string for the key to get/set.
This commit is contained in:
Carlos Martín Nieto
2014-02-03 17:56:59 +01:00
parent 74d091d609
commit 1c74676ed4

View File

@@ -147,15 +147,15 @@ Config_get_system_config(void)
int int
Config_contains(Config *self, PyObject *py_key) { Config_contains(Config *self, PyObject *py_key) {
int err; int err;
const char *c_value; const char *c_value, *c_key;
char *c_key; PyObject *tkey;
c_key = py_str_to_c_str(py_key, NULL); c_key = py_str_borrow_c_str(&tkey, py_key, NULL);
if (c_key == NULL) if (c_key == NULL)
return -1; return -1;
err = git_config_get_string(&c_value, self->config, c_key); err = git_config_get_string(&c_value, self->config, c_key);
free(c_key); Py_DECREF(tkey);
if (err < 0) { if (err < 0) {
if (err == GIT_ENOTFOUND) if (err == GIT_ENOTFOUND)
@@ -175,14 +175,15 @@ Config_getitem(Config *self, PyObject *py_key)
int64_t value_int; int64_t value_int;
int err, value_bool; int err, value_bool;
const char *value_str; const char *value_str;
char *key; const char *key;
PyObject* py_value; PyObject* py_value, *tmp;
key = py_str_to_c_str(py_key, NULL); key = py_str_borrow_c_str(&tmp, py_key, NULL);
if (key == NULL) if (key == NULL)
return NULL; return NULL;
err = git_config_get_string(&value_str, self->config, key); err = git_config_get_string(&value_str, self->config, key);
Py_CLEAR(tmp);
if (err < 0) if (err < 0)
goto cleanup; goto cleanup;
@@ -194,8 +195,6 @@ Config_getitem(Config *self, PyObject *py_key)
py_value = to_unicode(value_str, NULL, NULL); py_value = to_unicode(value_str, NULL, NULL);
cleanup: cleanup:
free(key);
if (err < 0) { if (err < 0) {
if (err == GIT_ENOTFOUND) { if (err == GIT_ENOTFOUND) {
PyErr_SetObject(PyExc_KeyError, py_key); PyErr_SetObject(PyExc_KeyError, py_key);
@@ -212,9 +211,10 @@ int
Config_setitem(Config *self, PyObject *py_key, PyObject *py_value) Config_setitem(Config *self, PyObject *py_key, PyObject *py_value)
{ {
int err; int err;
char *key, *value; const char *key, *value;
PyObject *tkey, *tvalue;
key = py_str_to_c_str(py_key, NULL); key = py_str_borrow_c_str(&tkey, py_key, NULL);
if (key == NULL) if (key == NULL)
return -1; return -1;
@@ -227,12 +227,12 @@ Config_setitem(Config *self, PyObject *py_key, PyObject *py_value)
err = git_config_set_int64(self->config, key, err = git_config_set_int64(self->config, key,
(int64_t)PyLong_AsLong(py_value)); (int64_t)PyLong_AsLong(py_value));
} else { } else {
value = py_str_to_c_str(py_value, NULL); value = py_str_borrow_c_str(&tvalue, py_value, NULL);
err = git_config_set_string(self->config, key, value); err = git_config_set_string(self->config, key, value);
free(value); Py_DECREF(tvalue);
} }
free(key); Py_DECREF(tkey);
if (err < 0) { if (err < 0) {
Error_set(err); Error_set(err);
return -1; return -1;