-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.c
More file actions
107 lines (96 loc) · 3.19 KB
/
Copy pathutils.c
File metadata and controls
107 lines (96 loc) · 3.19 KB
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
#include "utils.h"
int
gmp_parse_pyargs(const gmp_pyargs *fnargs, Py_ssize_t argidx[],
PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
if (nargs > fnargs->maxpos) {
PyErr_Format(PyExc_TypeError,
"%s() takes at most %zu positional arguments",
fnargs->fname, fnargs->maxpos);
return -1;
}
for (Py_ssize_t i = 0; i < nargs; i++) {
argidx[i] = i;
}
Py_ssize_t nkws = 0;
if (kwnames) {
nkws = PyTuple_Size(kwnames);
}
if (nkws > fnargs->maxpos) {
PyErr_Format(PyExc_TypeError,
"%s() takes at most %zu keyword arguments", fnargs->fname,
fnargs->maxargs);
return -1;
}
if (nkws + nargs < fnargs->minargs) {
PyErr_Format(PyExc_TypeError,
("%s() takes at least %zu positional or "
"keyword arguments"),
fnargs->fname, fnargs->minargs);
return -1;
}
for (Py_ssize_t i = 0; i < nkws; i++) {
const char *kwname = PyUnicode_AsUTF8AndSize(PyTuple_GetItem(kwnames,
i), NULL);
Py_ssize_t j = 0;
for (; j < fnargs->maxargs; j++) {
if (strcmp(kwname, fnargs->keywords[j]) == 0) {
if (j > fnargs->maxpos || nargs <= j) {
argidx[j] = (int)(nargs + i);
break;
}
else {
PyErr_Format(PyExc_TypeError,
("argument for %s() given by name "
"('%s') and position (%zu)"),
fnargs->fname, fnargs->keywords[j], j + 1);
return -1;
}
}
}
if (j == fnargs->maxargs) {
PyErr_Format(PyExc_TypeError,
"%s() got an unexpected keyword argument '%s'",
fnargs->fname, kwname);
return -1;
}
}
return 0;
}
/* copied from CPython internals */
PyObject *
gmp_PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode)
{
assert(PyUnicode_Check(unicode));
if (PyUnicode_IS_ASCII(unicode)) {
return Py_NewRef(unicode);
}
Py_ssize_t len = PyUnicode_GetLength(unicode);
PyObject *result = PyUnicode_New(len, 127);
if (result == NULL) {
return NULL; /* LCOV_EXCL_LINE */
}
Py_UCS1 *out = PyUnicode_1BYTE_DATA(result);
int kind = (int)PyUnicode_KIND(unicode); /* oracle/graalpython#580 */
const void *data = PyUnicode_DATA(unicode);
for (Py_ssize_t i = 0; i < len; ++i) {
Py_UCS4 ch = PyUnicode_READ(kind, data, i);
if (ch < 127) {
out[i] = (Py_UCS1)ch;
}
else if (Py_UNICODE_ISSPACE(ch)) {
out[i] = ' ';
}
else {
int decimal = Py_UNICODE_TODECIMAL(ch);
if (decimal < 0) {
out[i] = '?';
out[i + 1] = '\0';
break;
}
assert(decimal < 127);
out[i] = '0' + (Py_UCS1)decimal;
}
}
return result;
}