Skip to content

Commit

Permalink
Remove usage of deprecated Py_FileSystemDefaultEncoding
Browse files Browse the repository at this point in the history
  • Loading branch information
jdavid committed Mar 30, 2024
1 parent ea97961 commit 31aea66
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 52 deletions.
13 changes: 8 additions & 5 deletions src/odb.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ Odb_init(Odb *self, PyObject *args, PyObject *kwds)

int err;
if (py_path) {
char *path = pgit_encode_fsdefault(py_path);
PyObject *tvalue;
char *path = pgit_borrow_fsdefault(py_path, &tvalue);
if (path == NULL)
return -1;
err = git_odb_open(&self->odb, path);
free(path);
} else {
Py_DECREF(tvalue);
}
else {
err = git_odb_new(&self->odb);
}

Expand Down Expand Up @@ -139,12 +141,13 @@ PyDoc_STRVAR(Odb_add_disk_alternate__doc__,
PyObject *
Odb_add_disk_alternate(Odb *self, PyObject *py_path)
{
char *path = pgit_encode_fsdefault(py_path);
PyObject *tvalue;
char *path = pgit_borrow_fsdefault(py_path, &tvalue);
if (path == NULL)
return NULL;

int err = git_odb_add_disk_alternate(self->odb, path);
free(path);
Py_DECREF(tvalue);
if (err)
return Error_set(err);

Expand Down
10 changes: 6 additions & 4 deletions src/odb_backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,12 +595,13 @@ OdbBackendPack_init(OdbBackendPack *self, PyObject *args, PyObject *kwds)
if (!PyArg_ParseTuple(args, "O", &py_path))
return -1;

char *path = pgit_encode_fsdefault(py_path);
PyObject *tvalue;
char *path = pgit_borrow_fsdefault(py_path, &tvalue);
if (path == NULL)
return -1;

int err = git_odb_backend_pack(&self->super.odb_backend, path);
free(path);
Py_DECREF(tvalue);
if (err) {
Error_set(err);
return -1;
Expand Down Expand Up @@ -688,13 +689,14 @@ OdbBackendLoose_init(OdbBackendLoose *self, PyObject *args, PyObject *kwds)
&do_fsync, &dir_mode, &file_mode))
return -1;

char *path = pgit_encode_fsdefault(py_path);
PyObject *tvalue;
char *path = pgit_borrow_fsdefault(py_path, &tvalue);
if (path == NULL)
return -1;

int err = git_odb_backend_loose(&self->super.odb_backend, path, compression_level,
do_fsync, dir_mode, file_mode);
free(path);
Py_DECREF(tvalue);
if (err) {
Error_set(err);
return -1;
Expand Down
10 changes: 6 additions & 4 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,17 @@ option(PyObject *self, PyObject *args)
return NULL;

/* py_file and py_dir are only valid if they are strings */
PyObject *tvalue_file = NULL;
if (PyUnicode_Check(py_file) || PyBytes_Check(py_file))
file_path = pgit_encode_fsdefault(py_file);
file_path = pgit_borrow_fsdefault(py_file, &tvalue_file);

PyObject *tvalue_dir = NULL;
if (PyUnicode_Check(py_dir) || PyBytes_Check(py_dir))
dir_path = pgit_encode_fsdefault(py_dir);
dir_path = pgit_borrow_fsdefault(py_dir, &tvalue_dir);

err = git_libgit2_opts(GIT_OPT_SET_SSL_CERT_LOCATIONS, file_path, dir_path);
free(file_path);
free(dir_path);
Py_XDECREF(tvalue_file);
Py_XDECREF(tvalue_dir);

if (err)
return Error_set(err);
Expand Down
10 changes: 6 additions & 4 deletions src/reference.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,15 @@ Reference_rename(Reference *self, PyObject *py_name)
CHECK_REFERENCE(self);

// Get the C name
char *c_name = pgit_encode_fsdefault(py_name);
PyObject *tvalue;
char *c_name = pgit_borrow_fsdefault(py_name, &tvalue);
if (c_name == NULL)
return NULL;

// Rename
git_reference *new_reference;
int err = git_reference_rename(&new_reference, self->reference, c_name, 0, NULL);
free(c_name);
Py_DECREF(tvalue);
if (err)
return Error_set(err);

Expand Down Expand Up @@ -366,12 +367,13 @@ Reference_set_target(Reference *self, PyObject *args, PyObject *kwds)
}

/* Case 2: Symbolic */
char *c_name = pgit_encode_fsdefault(py_target);
PyObject *tvalue;
char *c_name = pgit_borrow_fsdefault(py_target, &tvalue);
if (c_name == NULL)
return NULL;

err = git_reference_symbolic_set_target(&new_ref, self->reference, c_name, message);
free(c_name);
Py_DECREF(tvalue);
if (err < 0)
goto error;

Expand Down
47 changes: 23 additions & 24 deletions src/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -879,17 +879,17 @@ PyDoc_STRVAR(Repository_create_blob_fromworkdir__doc__,
"is raised.");

PyObject *
Repository_create_blob_fromworkdir(Repository *self, PyObject *py_path)
Repository_create_blob_fromworkdir(Repository *self, PyObject *value)
{
PyObject *tvalue;
const char *path = pgit_borrow_encoding(py_path, Py_FileSystemDefaultEncoding,
Py_FileSystemDefaultEncodeErrors, &tvalue);
char *path = pgit_borrow_fsdefault(value, &tvalue);
if (path == NULL)
return NULL;

git_oid oid;
int err = git_blob_create_fromworkdir(&oid, self->repo, path);
Py_DECREF(tvalue);

if (err < 0)
return Error_set(err);

Expand All @@ -903,21 +903,17 @@ PyDoc_STRVAR(Repository_create_blob_fromdisk__doc__,
"Create a new blob from a file anywhere (no working directory check).");

PyObject *
Repository_create_blob_fromdisk(Repository *self, PyObject *args)
Repository_create_blob_fromdisk(Repository *self, PyObject *value)
{
git_oid oid;
PyBytesObject *py_path = NULL;
const char* path = NULL;
int err;

if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, &py_path))
PyObject *tvalue;
char *path = pgit_borrow_fsdefault(value, &tvalue);
if (path == NULL)
return NULL;

if (py_path != NULL)
path = PyBytes_AS_STRING(py_path);
git_oid oid;
int err = git_blob_create_fromdisk(&oid, self->repo, path);
Py_DECREF(tvalue);

err = git_blob_create_fromdisk(&oid, self->repo, path);
Py_XDECREF(py_path);
if (err < 0)
return Error_set(err);

Expand Down Expand Up @@ -1532,7 +1528,8 @@ PyObject *
Repository_lookup_reference(Repository *self, PyObject *py_name)
{
/* 1- Get the C name */
char *c_name = pgit_encode_fsdefault(py_name);
PyObject *tvalue;
char *c_name = pgit_borrow_fsdefault(py_name, &tvalue);
if (c_name == NULL)
return NULL;

Expand All @@ -1541,10 +1538,10 @@ Repository_lookup_reference(Repository *self, PyObject *py_name)
int err = git_reference_lookup(&c_reference, self->repo, c_name);
if (err) {
PyObject *err_obj = Error_set_str(err, c_name);
free(c_name);
Py_DECREF(tvalue);
return err_obj;
}
free(c_name);
Py_DECREF(tvalue);

/* 3- Make an instance of Reference and return it */
return wrap_reference(c_reference, self);
Expand All @@ -1559,7 +1556,8 @@ PyObject *
Repository_lookup_reference_dwim(Repository *self, PyObject *py_name)
{
/* 1- Get the C name */
char *c_name = pgit_encode_fsdefault(py_name);
PyObject *tvalue;
char *c_name = pgit_borrow_fsdefault(py_name, &tvalue);
if (c_name == NULL)
return NULL;

Expand All @@ -1568,10 +1566,10 @@ Repository_lookup_reference_dwim(Repository *self, PyObject *py_name)
int err = git_reference_dwim(&c_reference, self->repo, c_name);
if (err) {
PyObject *err_obj = Error_set_str(err, c_name);
free(c_name);
Py_DECREF(tvalue);
return err_obj;
}
free(c_name);
Py_DECREF(tvalue);

/* 3- Make an instance of Reference and return it */
return wrap_reference(c_reference, self);
Expand Down Expand Up @@ -1802,18 +1800,19 @@ PyDoc_STRVAR(Repository_status_file__doc__,
PyObject *
Repository_status_file(Repository *self, PyObject *value)
{
char *path = pgit_encode_fsdefault(value);
PyObject *tvalue;
char *path = pgit_borrow_fsdefault(value, &tvalue);
if (!path)
return NULL;

unsigned int status;
int err = git_status_file(&status, self->repo, path);
if (err) {
PyObject *err_obj = Error_set_str(err, path);
free(path);
Py_DECREF(tvalue);
return err_obj;
}
free(path);
Py_DECREF(tvalue);

return pygit2_enum(FileStatusEnum, (int) status);
}
Expand Down Expand Up @@ -2405,7 +2404,7 @@ Repository_listall_mergeheads(Repository *self, PyObject *args)
PyMethodDef Repository_methods[] = {
METHOD(Repository, create_blob, METH_VARARGS),
METHOD(Repository, create_blob_fromworkdir, METH_O),
METHOD(Repository, create_blob_fromdisk, METH_VARARGS),
METHOD(Repository, create_blob_fromdisk, METH_O),
METHOD(Repository, create_blob_fromiobase, METH_O),
METHOD(Repository, create_commit, METH_VARARGS),
METHOD(Repository, create_commit_string, METH_VARARGS),
Expand Down
10 changes: 6 additions & 4 deletions src/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ Tree_contains(Tree *self, PyObject *py_name)
{
if (Object__load((Object*)self) == NULL) { return -1; } // Lazy load

char *name = pgit_encode_fsdefault(py_name);
PyObject *tvalue;
char *name = pgit_borrow_fsdefault(py_name, &tvalue);
if (name == NULL)
return -1;

git_tree_entry *entry;
int err = git_tree_entry_bypath(&entry, self->tree, name);
free(name);
Py_DECREF(tvalue);

if (err == GIT_ENOTFOUND) {
return 0;
Expand Down Expand Up @@ -158,15 +159,16 @@ tree_getentry_by_index(const git_tree *tree, Repository *repo, PyObject *py_inde
PyObject*
tree_getentry_by_path(const git_tree *tree, Repository *repo, PyObject *py_path)
{
char *path = pgit_encode_fsdefault(py_path);
PyObject *tvalue;
char *path = pgit_borrow_fsdefault(py_path, &tvalue);
if (path == NULL) {
PyErr_SetString(PyExc_TypeError, "Value must be a path string");
return NULL;
}

git_tree_entry *entry;
int err = git_tree_entry_bypath(&entry, tree, path);
free(path);
Py_DECREF(tvalue);

if (err == GIT_ENOTFOUND) {
PyErr_SetObject(PyExc_KeyError, py_path);
Expand Down
10 changes: 6 additions & 4 deletions src/treebuilder.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ PyDoc_STRVAR(TreeBuilder_get__doc__,
PyObject *
TreeBuilder_get(TreeBuilder *self, PyObject *py_filename)
{
char *filename = pgit_encode_fsdefault(py_filename);
PyObject *tvalue;
char *filename = pgit_borrow_fsdefault(py_filename, &tvalue);
if (filename == NULL)
return NULL;

const git_tree_entry *entry_src = git_treebuilder_get(self->bld, filename);
free(filename);
Py_DECREF(tvalue);
if (entry_src == NULL)
Py_RETURN_NONE;

Expand All @@ -133,12 +134,13 @@ PyDoc_STRVAR(TreeBuilder_remove__doc__,
PyObject *
TreeBuilder_remove(TreeBuilder *self, PyObject *py_filename)
{
char *filename = pgit_encode_fsdefault(py_filename);
PyObject *tvalue;
char *filename = pgit_borrow_fsdefault(py_filename, &tvalue);
if (filename == NULL)
return NULL;

int err = git_treebuilder_remove(self->bld, filename);
free(filename);
Py_DECREF(tvalue);
if (err)
return Error_set(err);

Expand Down
15 changes: 13 additions & 2 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,20 @@ pgit_encode(PyObject *value, const char *encoding)
}

char*
pgit_encode_fsdefault(PyObject *value)
pgit_borrow_fsdefault(PyObject *value, PyObject **tvalue)
{
return pgit_encode(value, Py_FileSystemDefaultEncoding);
PyObject *str = PyOS_FSPath(value);
if (str == NULL) {
return NULL;
}

PyObject *bytes = PyUnicode_EncodeFSDefault(str);
if (bytes == NULL) {
return NULL;
}

*tvalue = bytes;
return PyBytes_AS_STRING(bytes);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ to_unicode_n(const char *value, size_t len, const char *encoding,
const char* pgit_borrow(PyObject *value);
const char* pgit_borrow_encoding(PyObject *value, const char *encoding, const char *errors, PyObject **tvalue);
char* pgit_encode(PyObject *value, const char *encoding);
char* pgit_encode_fsdefault(PyObject *value);
char* pgit_borrow_fsdefault(PyObject *value, PyObject **tvalue);


//PyObject * get_pylist_from_git_strarray(git_strarray *strarray);
Expand Down

0 comments on commit 31aea66

Please sign in to comment.