Skip to content

Commit 31aea66

Browse files
committed
Remove usage of deprecated Py_FileSystemDefaultEncoding
1 parent ea97961 commit 31aea66

File tree

9 files changed

+75
-52
lines changed

9 files changed

+75
-52
lines changed

src/odb.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ Odb_init(Odb *self, PyObject *args, PyObject *kwds)
6565

6666
int err;
6767
if (py_path) {
68-
char *path = pgit_encode_fsdefault(py_path);
68+
PyObject *tvalue;
69+
char *path = pgit_borrow_fsdefault(py_path, &tvalue);
6970
if (path == NULL)
7071
return -1;
7172
err = git_odb_open(&self->odb, path);
72-
free(path);
73-
} else {
73+
Py_DECREF(tvalue);
74+
}
75+
else {
7476
err = git_odb_new(&self->odb);
7577
}
7678

@@ -139,12 +141,13 @@ PyDoc_STRVAR(Odb_add_disk_alternate__doc__,
139141
PyObject *
140142
Odb_add_disk_alternate(Odb *self, PyObject *py_path)
141143
{
142-
char *path = pgit_encode_fsdefault(py_path);
144+
PyObject *tvalue;
145+
char *path = pgit_borrow_fsdefault(py_path, &tvalue);
143146
if (path == NULL)
144147
return NULL;
145148

146149
int err = git_odb_add_disk_alternate(self->odb, path);
147-
free(path);
150+
Py_DECREF(tvalue);
148151
if (err)
149152
return Error_set(err);
150153

src/odb_backend.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,12 +595,13 @@ OdbBackendPack_init(OdbBackendPack *self, PyObject *args, PyObject *kwds)
595595
if (!PyArg_ParseTuple(args, "O", &py_path))
596596
return -1;
597597

598-
char *path = pgit_encode_fsdefault(py_path);
598+
PyObject *tvalue;
599+
char *path = pgit_borrow_fsdefault(py_path, &tvalue);
599600
if (path == NULL)
600601
return -1;
601602

602603
int err = git_odb_backend_pack(&self->super.odb_backend, path);
603-
free(path);
604+
Py_DECREF(tvalue);
604605
if (err) {
605606
Error_set(err);
606607
return -1;
@@ -688,13 +689,14 @@ OdbBackendLoose_init(OdbBackendLoose *self, PyObject *args, PyObject *kwds)
688689
&do_fsync, &dir_mode, &file_mode))
689690
return -1;
690691

691-
char *path = pgit_encode_fsdefault(py_path);
692+
PyObject *tvalue;
693+
char *path = pgit_borrow_fsdefault(py_path, &tvalue);
692694
if (path == NULL)
693695
return -1;
694696

695697
int err = git_odb_backend_loose(&self->super.odb_backend, path, compression_level,
696698
do_fsync, dir_mode, file_mode);
697-
free(path);
699+
Py_DECREF(tvalue);
698700
if (err) {
699701
Error_set(err);
700702
return -1;

src/options.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,17 @@ option(PyObject *self, PyObject *args)
267267
return NULL;
268268

269269
/* py_file and py_dir are only valid if they are strings */
270+
PyObject *tvalue_file = NULL;
270271
if (PyUnicode_Check(py_file) || PyBytes_Check(py_file))
271-
file_path = pgit_encode_fsdefault(py_file);
272+
file_path = pgit_borrow_fsdefault(py_file, &tvalue_file);
272273

274+
PyObject *tvalue_dir = NULL;
273275
if (PyUnicode_Check(py_dir) || PyBytes_Check(py_dir))
274-
dir_path = pgit_encode_fsdefault(py_dir);
276+
dir_path = pgit_borrow_fsdefault(py_dir, &tvalue_dir);
275277

276278
err = git_libgit2_opts(GIT_OPT_SET_SSL_CERT_LOCATIONS, file_path, dir_path);
277-
free(file_path);
278-
free(dir_path);
279+
Py_XDECREF(tvalue_file);
280+
Py_XDECREF(tvalue_dir);
279281

280282
if (err)
281283
return Error_set(err);

src/reference.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,15 @@ Reference_rename(Reference *self, PyObject *py_name)
218218
CHECK_REFERENCE(self);
219219

220220
// Get the C name
221-
char *c_name = pgit_encode_fsdefault(py_name);
221+
PyObject *tvalue;
222+
char *c_name = pgit_borrow_fsdefault(py_name, &tvalue);
222223
if (c_name == NULL)
223224
return NULL;
224225

225226
// Rename
226227
git_reference *new_reference;
227228
int err = git_reference_rename(&new_reference, self->reference, c_name, 0, NULL);
228-
free(c_name);
229+
Py_DECREF(tvalue);
229230
if (err)
230231
return Error_set(err);
231232

@@ -366,12 +367,13 @@ Reference_set_target(Reference *self, PyObject *args, PyObject *kwds)
366367
}
367368

368369
/* Case 2: Symbolic */
369-
char *c_name = pgit_encode_fsdefault(py_target);
370+
PyObject *tvalue;
371+
char *c_name = pgit_borrow_fsdefault(py_target, &tvalue);
370372
if (c_name == NULL)
371373
return NULL;
372374

373375
err = git_reference_symbolic_set_target(&new_ref, self->reference, c_name, message);
374-
free(c_name);
376+
Py_DECREF(tvalue);
375377
if (err < 0)
376378
goto error;
377379

src/repository.c

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -879,17 +879,17 @@ PyDoc_STRVAR(Repository_create_blob_fromworkdir__doc__,
879879
"is raised.");
880880

881881
PyObject *
882-
Repository_create_blob_fromworkdir(Repository *self, PyObject *py_path)
882+
Repository_create_blob_fromworkdir(Repository *self, PyObject *value)
883883
{
884884
PyObject *tvalue;
885-
const char *path = pgit_borrow_encoding(py_path, Py_FileSystemDefaultEncoding,
886-
Py_FileSystemDefaultEncodeErrors, &tvalue);
885+
char *path = pgit_borrow_fsdefault(value, &tvalue);
887886
if (path == NULL)
888887
return NULL;
889888

890889
git_oid oid;
891890
int err = git_blob_create_fromworkdir(&oid, self->repo, path);
892891
Py_DECREF(tvalue);
892+
893893
if (err < 0)
894894
return Error_set(err);
895895

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

905905
PyObject *
906-
Repository_create_blob_fromdisk(Repository *self, PyObject *args)
906+
Repository_create_blob_fromdisk(Repository *self, PyObject *value)
907907
{
908-
git_oid oid;
909-
PyBytesObject *py_path = NULL;
910-
const char* path = NULL;
911-
int err;
912-
913-
if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, &py_path))
908+
PyObject *tvalue;
909+
char *path = pgit_borrow_fsdefault(value, &tvalue);
910+
if (path == NULL)
914911
return NULL;
915912

916-
if (py_path != NULL)
917-
path = PyBytes_AS_STRING(py_path);
913+
git_oid oid;
914+
int err = git_blob_create_fromdisk(&oid, self->repo, path);
915+
Py_DECREF(tvalue);
918916

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

@@ -1532,7 +1528,8 @@ PyObject *
15321528
Repository_lookup_reference(Repository *self, PyObject *py_name)
15331529
{
15341530
/* 1- Get the C name */
1535-
char *c_name = pgit_encode_fsdefault(py_name);
1531+
PyObject *tvalue;
1532+
char *c_name = pgit_borrow_fsdefault(py_name, &tvalue);
15361533
if (c_name == NULL)
15371534
return NULL;
15381535

@@ -1541,10 +1538,10 @@ Repository_lookup_reference(Repository *self, PyObject *py_name)
15411538
int err = git_reference_lookup(&c_reference, self->repo, c_name);
15421539
if (err) {
15431540
PyObject *err_obj = Error_set_str(err, c_name);
1544-
free(c_name);
1541+
Py_DECREF(tvalue);
15451542
return err_obj;
15461543
}
1547-
free(c_name);
1544+
Py_DECREF(tvalue);
15481545

15491546
/* 3- Make an instance of Reference and return it */
15501547
return wrap_reference(c_reference, self);
@@ -1559,7 +1556,8 @@ PyObject *
15591556
Repository_lookup_reference_dwim(Repository *self, PyObject *py_name)
15601557
{
15611558
/* 1- Get the C name */
1562-
char *c_name = pgit_encode_fsdefault(py_name);
1559+
PyObject *tvalue;
1560+
char *c_name = pgit_borrow_fsdefault(py_name, &tvalue);
15631561
if (c_name == NULL)
15641562
return NULL;
15651563

@@ -1568,10 +1566,10 @@ Repository_lookup_reference_dwim(Repository *self, PyObject *py_name)
15681566
int err = git_reference_dwim(&c_reference, self->repo, c_name);
15691567
if (err) {
15701568
PyObject *err_obj = Error_set_str(err, c_name);
1571-
free(c_name);
1569+
Py_DECREF(tvalue);
15721570
return err_obj;
15731571
}
1574-
free(c_name);
1572+
Py_DECREF(tvalue);
15751573

15761574
/* 3- Make an instance of Reference and return it */
15771575
return wrap_reference(c_reference, self);
@@ -1802,18 +1800,19 @@ PyDoc_STRVAR(Repository_status_file__doc__,
18021800
PyObject *
18031801
Repository_status_file(Repository *self, PyObject *value)
18041802
{
1805-
char *path = pgit_encode_fsdefault(value);
1803+
PyObject *tvalue;
1804+
char *path = pgit_borrow_fsdefault(value, &tvalue);
18061805
if (!path)
18071806
return NULL;
18081807

18091808
unsigned int status;
18101809
int err = git_status_file(&status, self->repo, path);
18111810
if (err) {
18121811
PyObject *err_obj = Error_set_str(err, path);
1813-
free(path);
1812+
Py_DECREF(tvalue);
18141813
return err_obj;
18151814
}
1816-
free(path);
1815+
Py_DECREF(tvalue);
18171816

18181817
return pygit2_enum(FileStatusEnum, (int) status);
18191818
}
@@ -2405,7 +2404,7 @@ Repository_listall_mergeheads(Repository *self, PyObject *args)
24052404
PyMethodDef Repository_methods[] = {
24062405
METHOD(Repository, create_blob, METH_VARARGS),
24072406
METHOD(Repository, create_blob_fromworkdir, METH_O),
2408-
METHOD(Repository, create_blob_fromdisk, METH_VARARGS),
2407+
METHOD(Repository, create_blob_fromdisk, METH_O),
24092408
METHOD(Repository, create_blob_fromiobase, METH_O),
24102409
METHOD(Repository, create_commit, METH_VARARGS),
24112410
METHOD(Repository, create_commit_string, METH_VARARGS),

src/tree.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ Tree_contains(Tree *self, PyObject *py_name)
6565
{
6666
if (Object__load((Object*)self) == NULL) { return -1; } // Lazy load
6767

68-
char *name = pgit_encode_fsdefault(py_name);
68+
PyObject *tvalue;
69+
char *name = pgit_borrow_fsdefault(py_name, &tvalue);
6970
if (name == NULL)
7071
return -1;
7172

7273
git_tree_entry *entry;
7374
int err = git_tree_entry_bypath(&entry, self->tree, name);
74-
free(name);
75+
Py_DECREF(tvalue);
7576

7677
if (err == GIT_ENOTFOUND) {
7778
return 0;
@@ -158,15 +159,16 @@ tree_getentry_by_index(const git_tree *tree, Repository *repo, PyObject *py_inde
158159
PyObject*
159160
tree_getentry_by_path(const git_tree *tree, Repository *repo, PyObject *py_path)
160161
{
161-
char *path = pgit_encode_fsdefault(py_path);
162+
PyObject *tvalue;
163+
char *path = pgit_borrow_fsdefault(py_path, &tvalue);
162164
if (path == NULL) {
163165
PyErr_SetString(PyExc_TypeError, "Value must be a path string");
164166
return NULL;
165167
}
166168

167169
git_tree_entry *entry;
168170
int err = git_tree_entry_bypath(&entry, tree, path);
169-
free(path);
171+
Py_DECREF(tvalue);
170172

171173
if (err == GIT_ENOTFOUND) {
172174
PyErr_SetObject(PyExc_KeyError, py_path);

src/treebuilder.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,13 @@ PyDoc_STRVAR(TreeBuilder_get__doc__,
106106
PyObject *
107107
TreeBuilder_get(TreeBuilder *self, PyObject *py_filename)
108108
{
109-
char *filename = pgit_encode_fsdefault(py_filename);
109+
PyObject *tvalue;
110+
char *filename = pgit_borrow_fsdefault(py_filename, &tvalue);
110111
if (filename == NULL)
111112
return NULL;
112113

113114
const git_tree_entry *entry_src = git_treebuilder_get(self->bld, filename);
114-
free(filename);
115+
Py_DECREF(tvalue);
115116
if (entry_src == NULL)
116117
Py_RETURN_NONE;
117118

@@ -133,12 +134,13 @@ PyDoc_STRVAR(TreeBuilder_remove__doc__,
133134
PyObject *
134135
TreeBuilder_remove(TreeBuilder *self, PyObject *py_filename)
135136
{
136-
char *filename = pgit_encode_fsdefault(py_filename);
137+
PyObject *tvalue;
138+
char *filename = pgit_borrow_fsdefault(py_filename, &tvalue);
137139
if (filename == NULL)
138140
return NULL;
139141

140142
int err = git_treebuilder_remove(self->bld, filename);
141-
free(filename);
143+
Py_DECREF(tvalue);
142144
if (err)
143145
return Error_set(err);
144146

src/utils.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,20 @@ pgit_encode(PyObject *value, const char *encoding)
8787
}
8888

8989
char*
90-
pgit_encode_fsdefault(PyObject *value)
90+
pgit_borrow_fsdefault(PyObject *value, PyObject **tvalue)
9191
{
92-
return pgit_encode(value, Py_FileSystemDefaultEncoding);
92+
PyObject *str = PyOS_FSPath(value);
93+
if (str == NULL) {
94+
return NULL;
95+
}
96+
97+
PyObject *bytes = PyUnicode_EncodeFSDefault(str);
98+
if (bytes == NULL) {
99+
return NULL;
100+
}
101+
102+
*tvalue = bytes;
103+
return PyBytes_AS_STRING(bytes);
93104
}
94105

95106
/**

src/utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ to_unicode_n(const char *value, size_t len, const char *encoding,
9090
const char* pgit_borrow(PyObject *value);
9191
const char* pgit_borrow_encoding(PyObject *value, const char *encoding, const char *errors, PyObject **tvalue);
9292
char* pgit_encode(PyObject *value, const char *encoding);
93-
char* pgit_encode_fsdefault(PyObject *value);
93+
char* pgit_borrow_fsdefault(PyObject *value, PyObject **tvalue);
9494

9595

9696
//PyObject * get_pylist_from_git_strarray(git_strarray *strarray);

0 commit comments

Comments
 (0)