Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8a9259b

Browse files
Reset arraymodule.c.
1 parent e425908 commit 8a9259b

2 files changed

Lines changed: 42 additions & 104 deletions

File tree

Modules/arraymodule.c

Lines changed: 32 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -241,40 +241,29 @@ u_getitem(arrayobject *ap, Py_ssize_t i)
241241
static int
242242
u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
243243
{
244-
#if USE_UNICODE_WCHAR_CACHE
245-
Py_UNICODE *p;
246-
Py_ssize_t len;
247-
248-
if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
244+
PyObject *u;
245+
if (!PyArg_Parse(v, "U;array item must be unicode character", &u)) {
249246
return -1;
250-
if (len != 1) {
247+
}
248+
249+
Py_ssize_t len = PyUnicode_AsWideChar(u, NULL, 0);
250+
if (len != 2) {
251251
PyErr_SetString(PyExc_TypeError,
252252
"array item must be unicode character");
253253
return -1;
254254
}
255-
if (i >= 0)
256-
((Py_UNICODE *)ap->ob_item)[i] = p[0];
257-
return 0;
258-
#else
259-
PyObject *u;
260-
Py_ssize_t len;
261255

262-
if (!PyArg_Parse(v, "U;array item must be unicode character", &u))
263-
return -1;
264-
len = PyUnicode_AsWideChar(u, NULL, 0);
265-
if (len != 2) {
266-
if (len >= 0) {
267-
PyErr_SetString(PyExc_TypeError,
268-
"array item must be unicode character");
269-
}
270-
return -1;
256+
wchar_t w;
257+
len = PyUnicode_AsWideChar(u, &w, 1);
258+
assert(len == 1);
259+
260+
if (i >= 0) {
261+
((wchar_t *)ap->ob_item)[i] = w;
271262
}
272-
if (i >= 0)
273-
PyUnicode_AsWideChar(u, &((wchar_t *)ap->ob_item)[i], 1);
274263
return 0;
275-
#endif /* USE_UNICODE_WCHAR_CACHE */
276264
}
277265

266+
278267
static PyObject *
279268
h_getitem(arrayobject *ap, Py_ssize_t i)
280269
{
@@ -1667,7 +1656,7 @@ array_array_tobytes_impl(arrayobject *self)
16671656
/*[clinic input]
16681657
array.array.fromunicode
16691658
1670-
arg: unicode
1659+
ustr: unicode
16711660
/
16721661
16731662
Extends this array with data from the unicode string ustr.
@@ -1678,41 +1667,28 @@ some other type.
16781667
[clinic start generated code]*/
16791668

16801669
static PyObject *
1681-
array_array_fromunicode_impl(arrayobject *self, PyObject *arg)
1682-
/*[clinic end generated code: output=b691324745f39114 input=bc9c8ea0d901b328]*/
1670+
array_array_fromunicode_impl(arrayobject *self, PyObject *ustr)
1671+
/*[clinic end generated code: output=24359f5e001a7f2b input=025db1fdade7a4ce]*/
16831672
{
1684-
Py_ssize_t len;
1685-
1686-
char typecode = self->ob_descr->typecode;
1687-
if (typecode != 'u') {
1673+
if (self->ob_descr->typecode != 'u') {
16881674
PyErr_SetString(PyExc_ValueError,
16891675
"fromunicode() may only be called on "
16901676
"unicode type arrays");
16911677
return NULL;
16921678
}
16931679

1694-
#if USE_UNICODE_WCHAR_CACHE
1695-
_Py_COMP_DIAG_PUSH
1696-
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
1697-
len = PyUnicode_GetSize(arg);
1698-
if (len < 0) {
1699-
return NULL;
1700-
}
1701-
_Py_COMP_DIAG_POP
1702-
#else /* USE_UNICODE_WCHAR_CACHE */
1703-
len = PyUnicode_AsWideChar(arg, NULL, 0);
1704-
if (len < 0) {
1705-
return NULL;
1706-
}
1707-
assert(len > 0);
1708-
len--;
1709-
#endif /* USE_UNICODE_WCHAR_CACHE */
1710-
1711-
if (len > 0) {
1680+
Py_ssize_t ustr_length = PyUnicode_AsWideChar(ustr, NULL, 0);
1681+
assert(ustr_length > 0);
1682+
if (ustr_length > 1) {
1683+
ustr_length--; /* trim trailing NUL character */
17121684
Py_ssize_t old_size = Py_SIZE(self);
1713-
if (array_resize(self, old_size + len) == -1)
1685+
if (array_resize(self, old_size + ustr_length) == -1) {
17141686
return NULL;
1715-
PyUnicode_AsWideChar(arg, (wchar_t *)self->ob_item + old_size, len);
1687+
}
1688+
1689+
// must not fail
1690+
PyUnicode_AsWideChar(
1691+
ustr, ((wchar_t *)self->ob_item) + old_size, ustr_length);
17161692
}
17171693

17181694
Py_RETURN_NONE;
@@ -2697,58 +2673,20 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
26972673
}
26982674
else if (initial != NULL && PyUnicode_Check(initial)) {
26992675
Py_ssize_t n;
2700-
#if USE_UNICODE_WCHAR_CACHE
2701-
_Py_COMP_DIAG_PUSH
2702-
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
2703-
Py_UNICODE *ustr;
2704-
2705-
ustr = PyUnicode_AsUnicode(initial);
2676+
wchar_t *ustr = PyUnicode_AsWideCharString(initial, &n);
27062677
if (ustr == NULL) {
2707-
PyErr_NoMemory();
27082678
Py_DECREF(a);
27092679
return NULL;
27102680
}
27112681

2712-
n = PyUnicode_GET_DATA_SIZE(initial);
2713-
if (n > 0) {
2714-
arrayobject *self = (arrayobject *)a;
2715-
char *item = self->ob_item;
2716-
item = (char *)PyMem_Realloc(item, n);
2717-
if (item == NULL) {
2718-
PyErr_NoMemory();
2719-
Py_DECREF(a);
2720-
return NULL;
2721-
}
2722-
self->ob_item = item;
2723-
Py_SET_SIZE(self, n / sizeof(Py_UNICODE));
2724-
memcpy(item, ustr, n);
2725-
self->allocated = Py_SIZE(self);
2726-
}
2727-
_Py_COMP_DIAG_POP
2728-
#else /* USE_UNICODE_WCHAR_CACHE */
2729-
n = PyUnicode_AsWideChar(initial, NULL, 0);
2730-
if (n < 0) {
2731-
PyErr_NoMemory();
2732-
Py_DECREF(a);
2733-
return NULL;
2734-
}
2735-
assert(n > 0);
2736-
n--;
27372682
if (n > 0) {
27382683
arrayobject *self = (arrayobject *)a;
2739-
char *item = self->ob_item;
2740-
item = (char *)PyMem_Realloc(item, n * sizeof(wchar_t));
2741-
if (item == NULL) {
2742-
PyErr_NoMemory();
2743-
Py_DECREF(a);
2744-
return NULL;
2745-
}
2746-
self->ob_item = item;
2684+
// self->ob_item may be NULL but it is safe.
2685+
PyMem_Free(self->ob_item);
2686+
self->ob_item = (char *)ustr;
27472687
Py_SET_SIZE(self, n);
2748-
PyUnicode_AsWideChar(initial, (wchar_t*)item, n);
2749-
self->allocated = Py_SIZE(self);
2688+
self->allocated = n;
27502689
}
2751-
#endif /* USE_UNICODE_WCHAR_CACHE */
27522690
}
27532691
else if (initial != NULL && array_Check(initial) && len > 0) {
27542692
arrayobject *self = (arrayobject *)a;

Modules/clinic/arraymodule.c.h

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)