FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pygit2/src/utils.c at master · libgit2/pygit2 · GitHub
libgit2
pygit2
Repository navigation
Code
Issues
184
(184)
Pull requests
5
(5)
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
pygit2
/
src
/
utils.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
236 lines (201 loc) · 6.1 KB
Breadcrumbs
pygit2
/
src
/
utils.c
Copy path
File metadata and controls
236 lines (201 loc) · 6.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Copyright 2010-2026 The pygit2 contributors
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* as published by the Free Software Foundation.
*
* In addition to the permissions in the GNU General Public License,
* the authors give you unlimited permission to link the compiled
* version of this file into combinations with other programs,
* and to distribute those combinations without any restriction
* coming from the use of this file. (The General Public License
* restrictions do apply in other respects; for example, they cover
* modification of the file, and distribution when not linked into
* a combined executable.)
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#define
PY_SSIZE_T_CLEAN
#include
<Python.h>
#include
"error.h"
#include
"utils.h"
extern
PyTypeObject
ReferenceType
;
extern
PyTypeObject
TreeType
;
extern
PyTypeObject
CommitType
;
extern
PyTypeObject
BlobType
;
extern
PyTypeObject
TagType
;
/**
* Attempt to convert a C string to a Python string with the given encoding.
* If the conversion fails, return a fallback string.
*/
PyObject
*
to_unicode_safe
(
const
char
*
value
,
const
char
*
encoding
)
{
PyObject
*
py_str
;
if
(!
value
) {
py_str
=
PyUnicode_FromString
(
"None"
);
}
else
{
py_str
=
to_unicode
(
value
,
encoding
,
"replace"
);
if
(!
py_str
) {
assert
(
PyErr_Occurred
());
py_str
=
PyUnicode_FromString
(
"(error)"
);
PyErr_Clear
();
}
}
assert
(!
PyErr_Occurred
());
assert
(
py_str
);
return
py_str
;
}
char
*
pgit_borrow_fsdefault
(
PyObject
*
value
,
PyObject
*
*
tvalue
)
{
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
);
}
/**
* Return a pointer to the underlying C string in 'value'. The pointer is
* guaranteed by 'tvalue', decrease its refcount when done with the string.
*/
const
char
*
pgit_borrow_encoding
(
PyObject
*
value
,
const
char
*
encoding
,
const
char
*
errors
,
PyObject
*
*
tvalue
)
{
PyObject
*
py_value
=
NULL
;
PyObject
*
py_str
=
NULL
;
py_value
=
PyOS_FSPath
(
value
);
if
(
py_value
==
NULL
) {
Error_type_error
(
"unexpected %.200s"
,
value
);
return
NULL
;
}
// Get new PyBytes reference from value
if
(
PyUnicode_Check
(
py_value
)) {
// Text string
py_str
=
PyUnicode_AsEncodedString
(
py_value
,
encoding
?
encoding
:
"utf-8"
,
errors
?
errors
:
"strict"
);
Py_DECREF
(
py_value
);
if
(
py_str
==
NULL
)
return
NULL
;
}
else
if
(
PyBytes_Check
(
py_value
)) {
// Byte string
py_str
=
py_value
;
}
else
{
// Type error
Error_type_error
(
"unexpected %.200s"
,
value
);
Py_DECREF
(
py_value
);
return
NULL
;
}
// Borrow c string from the new PyBytes reference
char
*
c_str
=
PyBytes_AsString
(
py_str
);
if
(
c_str
==
NULL
) {
Py_DECREF
(
py_str
);
return
NULL
;
}
// Return the borrowed c string and the new PyBytes reference
*
tvalue
=
py_str
;
return
c_str
;
}
/**
* Return a borrowed c string with the representation of the given Unicode or
* Bytes object:
* - If value is Unicode return the UTF-8 representation
* - If value is Bytes return the raw sttring
* In both cases the returned string is owned by value and must not be
* modified, nor freed.
*/
const
char
*
pgit_borrow
(
PyObject
*
value
)
{
if
(
PyUnicode_Check
(
value
)) {
// Text string
return
PyUnicode_AsUTF8
(
value
);
}
else
if
(
PyBytes_Check
(
value
)) {
// Byte string
return
PyBytes_AsString
(
value
);
}
// Type error
Error_type_error
(
"unexpected %.200s"
,
value
);
return
NULL
;
}
char
*
pgit_strdup
(
PyObject
*
value
)
{
const
char
*
str
;
char
*
copy
;
size_t
len
;
if
(
PyUnicode_Check
(
value
)) {
str
=
PyUnicode_AsUTF8
(
value
);
if
(
str
==
NULL
)
return
NULL
;
len
=
strlen
(
str
);
}
else
{
Error_type_error
(
"unexpected %.200s"
,
value
);
return
NULL
;
}
copy
=
malloc
(
len
+
1
);
if
(
copy
==
NULL
) {
PyErr_NoMemory
();
return
NULL
;
}
memcpy
(
copy
,
str
,
len
+
1
);
return
copy
;
}
static
git_otype
py_type_to_git_type
(
PyTypeObject
*
py_type
)
{
if
(
py_type
==
&
CommitType
)
return
GIT_OBJECT_COMMIT
;
else
if
(
py_type
==
&
TreeType
)
return
GIT_OBJECT_TREE
;
else
if
(
py_type
==
&
BlobType
)
return
GIT_OBJECT_BLOB
;
else
if
(
py_type
==
&
TagType
)
return
GIT_OBJECT_TAG
;
PyErr_SetString
(
PyExc_ValueError
,
"invalid target type"
);
return
GIT_OBJECT_INVALID
;
/* -1 */
}
git_otype
py_object_to_otype
(
PyObject
*
py_type
)
{
long
value
;
if
(
py_type
==
Py_None
)
return
GIT_OBJECT_ANY
;
if
(
PyLong_Check
(
py_type
)) {
value
=
PyLong_AsLong
(
py_type
);
if
(
value
==
-1
&&
PyErr_Occurred
())
return
GIT_OBJECT_INVALID
;
/* TODO Check whether the value is a valid value */
return
(
git_otype
)
value
;
}
if
(
PyType_Check
(
py_type
))
return
py_type_to_git_type
((
PyTypeObject
*
)
py_type
);
PyErr_SetString
(
PyExc_ValueError
,
"invalid target type"
);
return
GIT_OBJECT_INVALID
;
/* -1 */
}
/**
* Convert an integer to a reference to an IntEnum or IntFlag in pygit2.enums.
*/
PyObject
*
pygit2_enum
(
PyObject
*
enum_type
,
int
value
)
{
if
(!
enum_type
) {
PyErr_SetString
(
PyExc_TypeError
,
"an enum has not been cached in _pygit2.cache_enums()"
);
return
NULL
;
}
PyObject
*
enum_instance
=
PyObject_CallFunction
(
enum_type
,
"(i)"
,
value
);
return
enum_instance
;
}
Back
|
FazBrowse Home
|
New Git URL