Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Borrow C strings instead of copying them #335

Merged
merged 7 commits into from
Feb 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ Config_get_system_config(void)
int
Config_contains(Config *self, PyObject *py_key) {
int err;
const char *c_value;
char *c_key;
const char *c_value, *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)
return -1;

err = git_config_get_string(&c_value, self->config, c_key);
free(c_key);
Py_DECREF(tkey);

if (err < 0) {
if (err == GIT_ENOTFOUND)
Expand All @@ -175,14 +175,15 @@ Config_getitem(Config *self, PyObject *py_key)
int64_t value_int;
int err, value_bool;
const char *value_str;
char *key;
PyObject* py_value;
const char *key;
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)
return NULL;

err = git_config_get_string(&value_str, self->config, key);
Py_CLEAR(tmp);
if (err < 0)
goto cleanup;

Expand All @@ -194,8 +195,6 @@ Config_getitem(Config *self, PyObject *py_key)
py_value = to_unicode(value_str, NULL, NULL);

cleanup:
free(key);

if (err < 0) {
if (err == GIT_ENOTFOUND) {
PyErr_SetObject(PyExc_KeyError, py_key);
Expand All @@ -212,9 +211,10 @@ int
Config_setitem(Config *self, PyObject *py_key, PyObject *py_value)
{
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)
return -1;

Expand All @@ -227,12 +227,12 @@ Config_setitem(Config *self, PyObject *py_key, PyObject *py_value)
err = git_config_set_int64(self->config, key,
(int64_t)PyLong_AsLong(py_value));
} 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);
free(value);
Py_DECREF(tvalue);
}

free(key);
Py_DECREF(tkey);
if (err < 0) {
Error_set(err);
return -1;
Expand Down
12 changes: 3 additions & 9 deletions src/index.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,18 +650,12 @@ IndexEntry_path__get__(IndexEntry *self)
int
IndexEntry_path__set__(IndexEntry *self, PyObject *py_path)
{
char *c_inner, *c_path;
char *c_path;

c_inner = py_str_to_c_str(py_path, NULL);
if (!c_inner)
c_path = py_str_to_c_str(py_path, NULL);
if (!c_path)
return -1;

c_path = strdup(c_inner);
if (!c_path) {
PyErr_NoMemory();
return -1;
}

free(self->entry.path);
self->entry.path = c_path;

Expand Down
36 changes: 20 additions & 16 deletions src/refspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,16 @@ PyDoc_STRVAR(Refspec_src_matches__doc__,
PyObject *
Refspec_src_matches(Refspec *self, PyObject *py_str)
{
char *str;
const char *str;
PyObject *tstr;
int res;

str = py_str_to_c_str(py_str, NULL);
str = py_str_borrow_c_str(&tstr, py_str, NULL);
if (!str)
return NULL;

res = git_refspec_src_matches(self->refspec, str);
free(str);
Py_DECREF(tstr);

if (res)
Py_RETURN_TRUE;
Expand All @@ -129,15 +130,16 @@ PyDoc_STRVAR(Refspec_dst_matches__doc__,
PyObject *
Refspec_dst_matches(Refspec *self, PyObject *py_str)
{
char *str;
const char *str;
PyObject *tstr;
int res;

str = py_str_to_c_str(py_str, NULL);
str = py_str_borrow_c_str(&tstr, py_str, NULL);
if (!str)
return NULL;

res = git_refspec_dst_matches(self->refspec, str);
free(str);
Py_DECREF(tstr);

if (res)
Py_RETURN_TRUE;
Expand All @@ -153,24 +155,25 @@ PyDoc_STRVAR(Refspec_transform__doc__,
PyObject *
Refspec_transform(Refspec *self, PyObject *py_str)
{
char *str, *trans;
const char *str;
char *trans;
int err, len, alen;
PyObject *py_trans;
PyObject *py_trans, *tstr;

str = py_str_to_c_str(py_str, NULL);
str = py_str_borrow_c_str(&tstr, py_str, NULL);
alen = len = strlen(str);

do {
alen *= alen;
trans = malloc(alen);
if (!trans) {
free(str);
Py_DECREF(tstr);
return PyErr_NoMemory();
}

err = git_refspec_transform(trans, alen, self->refspec, str);
} while(err == GIT_EBUFS);
free(str);
Py_DECREF(tstr);

if (err < 0) {
free(trans);
Expand All @@ -193,24 +196,25 @@ PyDoc_STRVAR(Refspec_rtransform__doc__,
PyObject *
Refspec_rtransform(Refspec *self, PyObject *py_str)
{
char *str, *trans;
const char *str;
char *trans;
int err, len, alen;
PyObject *py_trans;
PyObject *py_trans, *tstr;

str = py_str_to_c_str(py_str, NULL);
str = py_str_borrow_c_str(&tstr, py_str, NULL);
alen = len = strlen(str);

do {
alen *= alen;
trans = malloc(alen);
if (!trans) {
free(str);
Py_DECREF(tstr);
return PyErr_NoMemory();
}

err = git_refspec_rtransform(trans, alen, self->refspec, str);
} while(err == GIT_EBUFS);
free(str);
Py_DECREF(tstr);

if (err < 0) {
free(trans);
Expand Down
21 changes: 12 additions & 9 deletions src/remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,13 @@ int
Remote_name__set__(Remote *self, PyObject* py_name)
{
int err;
char* name;
const char* name;
PyObject *tname;

name = py_str_to_c_str(py_name, NULL);
name = py_str_borrow_c_str(&tname, py_name, NULL);
if (name != NULL) {
err = git_remote_rename(self->remote, name, NULL, NULL);
free(name);
Py_DECREF(tname);

if (err == GIT_OK)
return 0;
Expand Down Expand Up @@ -404,12 +405,13 @@ int
Remote_url__set__(Remote *self, PyObject* py_url)
{
int err;
char* url = NULL;
const char* url = NULL;
PyObject *turl;

url = py_str_to_c_str(py_url, NULL);
url = py_str_borrow_c_str(&turl, py_url, NULL);
if (url != NULL) {
err = git_remote_set_url(self->remote, url);
free(url);
Py_DECREF(turl);

if (err == GIT_OK)
return 0;
Expand Down Expand Up @@ -440,12 +442,13 @@ int
Remote_push_url__set__(Remote *self, PyObject* py_url)
{
int err;
char* url = NULL;
const char* url = NULL;
PyObject *turl;

url = py_str_to_c_str(py_url, NULL);
url = py_str_borrow_c_str(&turl, py_url, NULL);
if (url != NULL) {
err = git_remote_set_pushurl(self->remote, url);
free(url);
Py_DECREF(turl);

if (err == GIT_OK)
return 0;
Expand Down
25 changes: 14 additions & 11 deletions src/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,15 @@ int
Repository_head__set__(Repository *self, PyObject *py_refname)
{
int err;
char *refname;
const char *refname;
PyObject *trefname;

refname = py_str_to_c_str(py_refname, NULL);
refname = py_str_borrow_c_str(&trefname, py_refname, NULL);
if (refname == NULL)
return -1;

err = git_repository_set_head(self->repo, refname);
free(refname);
Py_DECREF(trefname);
if (err < 0) {
Error_set_str(err, refname);
return -1;
Expand Down Expand Up @@ -318,11 +319,12 @@ PyObject *
Repository_revparse_single(Repository *self, PyObject *py_spec)
{
git_object *c_obj;
char *c_spec;
const char *c_spec;
PyObject *tspec;
int err;

/* 1- Get the C revision spec */
c_spec = py_str_to_c_str(py_spec, NULL);
c_spec = py_str_borrow_c_str(&tspec, py_spec, NULL);
if (c_spec == NULL)
return NULL;

Expand All @@ -331,10 +333,10 @@ Repository_revparse_single(Repository *self, PyObject *py_spec)

if (err < 0) {
PyObject *err_obj = Error_set_str(err, c_spec);
free(c_spec);
Py_DECREF(tspec);
return err_obj;
}
free(c_spec);
Py_DECREF(tspec);

return wrap_object(c_obj, self);
}
Expand Down Expand Up @@ -782,7 +784,8 @@ Repository_create_commit(Repository *self, PyObject *args)
Signature *py_author, *py_committer;
PyObject *py_oid, *py_message, *py_parents, *py_parent;
PyObject *py_result = NULL;
char *message = NULL;
PyObject *tmessage;
const char *message = NULL;
char *update_ref = NULL;
char *encoding = NULL;
git_oid oid;
Expand All @@ -806,7 +809,7 @@ Repository_create_commit(Repository *self, PyObject *args)
if (len == 0)
goto out;

message = py_str_to_c_str(py_message, encoding);
message = py_str_borrow_c_str(&tmessage, py_message, encoding);
if (message == NULL)
goto out;

Expand Down Expand Up @@ -846,7 +849,7 @@ Repository_create_commit(Repository *self, PyObject *args)
py_result = git_oid_to_python(&oid);

out:
free(message);
Py_DECREF(tmessage);
git_tree_free(tree);
while (i > 0) {
i--;
Expand Down Expand Up @@ -1048,7 +1051,7 @@ Repository_lookup_reference(Repository *self, PyObject *py_name)
err = git_reference_lookup(&c_reference, self->repo, c_name);
if (err < 0) {
PyObject *err_obj = Error_set_str(err, c_name);
free(c_name);
free(c_name);
return err_obj;
}
free(c_name);
Expand Down
10 changes: 6 additions & 4 deletions src/signature.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ int
Signature_init(Signature *self, PyObject *args, PyObject *kwds)
{
char *keywords[] = {"name", "email", "time", "offset", "encoding", NULL};
PyObject *py_name;
char *name, *email, *encoding = "ascii";
PyObject *py_name, *tname;
char *email, *encoding = "ascii";
const char *name;
long long time = -1;
int offset = 0;
int err;
Expand All @@ -49,7 +50,7 @@ Signature_init(Signature *self, PyObject *args, PyObject *kwds)
&py_name, &email, &time, &offset, &encoding))
return -1;

name = py_str_to_c_str(py_name, encoding);
name = py_str_borrow_c_str(&tname, py_name, encoding);
if (name == NULL)
return -1;

Expand All @@ -58,7 +59,8 @@ Signature_init(Signature *self, PyObject *args, PyObject *kwds)
} else {
err = git_signature_new(&signature, name, email, time, offset);
}
free(name);
Py_DECREF(tname);

if (err < 0) {
Error_set(err);
return -1;
Expand Down
Loading