Skip to content

Commit c786670

Browse files
committed
respond to review comments; clarify integer return codes
1 parent ec0c8e6 commit c786670

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

stringdtype/stringdtype/src/dtype.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,19 @@ stringdtype_setitem(StringDTypeObject *NPY_UNUSED(descr), PyObject *obj,
124124
ssfree((ss *)dataptr);
125125

126126
// copies contents of val into item_val->buf
127-
if (ssnewlen(val, length, (ss *)dataptr) == -1) {
128-
PyErr_SetString(PyExc_MemoryError, "ssnewlen failed");
127+
int res = ssnewlen(val, length, (ss *)dataptr);
128+
// val_obj must stay alive until here to ensure *val* doesn't get
129+
// deallocated
130+
Py_DECREF(val_obj);
131+
if (res == -1) {
132+
PyErr_NoMemory();
129133
return -1;
130134
}
135+
else if (res == -2) {
136+
// this should never happen
137+
assert(0);
138+
}
131139

132-
// val_obj must stay alive until here to ensure *val* doesn't get
133-
// deallocated
134140
Py_DECREF(val_obj);
135141
return 0;
136142
}
@@ -156,10 +162,6 @@ stringdtype_getitem(StringDTypeObject *NPY_UNUSED(descr), char **dataptr)
156162
PyObject *res = PyObject_CallFunctionObjArgs((PyObject *)StringScalar_Type,
157163
val_obj, NULL);
158164

159-
if (res == NULL) {
160-
return NULL;
161-
}
162-
163165
Py_DECREF(val_obj);
164166

165167
return res;

stringdtype/stringdtype/src/static_string.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ int
44
ssnewlen(const char *init, size_t len, ss *to_init)
55
{
66
if ((to_init->buf != NULL) || (to_init->len != 0)) {
7-
return -1;
7+
return -2;
88
}
99

1010
// one extra byte for null terminator
@@ -47,7 +47,7 @@ int
4747
ssnewemptylen(size_t num_bytes, ss *out)
4848
{
4949
if (out->len != 0 || out->buf != NULL) {
50-
return -1;
50+
return -2;
5151
}
5252

5353
char *buf = (char *)malloc(sizeof(char) * (num_bytes + 1));

stringdtype/stringdtype/src/static_string.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,27 @@ typedef struct ss {
1010
} ss;
1111

1212
// Allocates a new buffer for *to_init*, filling with the copied contents of
13-
// *init* and sets *to_init->len* to *len*.
13+
// *init* and sets *to_init->len* to *len*. Returns -1 if malloc fails and -2
14+
// if *to_init* is not empty. Returns 0 on success.
1415
int
1516
ssnewlen(const char *init, size_t len, ss *to_init);
1617

17-
// Frees the internal string buffer held by *str*. If str->buf is NULL, does
18-
// nothing.
18+
// Sets len to 0 and if str->buf is not already NULL, frees it and sets it to
19+
// NULL. Cannot fail.
1920
void
2021
ssfree(ss *str);
2122

2223
// copies the contents out *in* into *out*. Allocates a new string buffer for
23-
// *out*, checks that *out->buf* is NULL and *out->len* is zero and errors
24-
// otherwise. Also errors if malloc fails.
24+
// *out*. Returns -1 if malloc fails and -2 if *out* is not empty. Returns 0 on
25+
// success.
2526
int
2627
ssdup(ss *in, ss *out);
2728

2829
// Allocates a new string buffer for *out* with enough capacity to store
2930
// *num_bytes* of text. The actual allocation is num_bytes + 1 bytes, to
3031
// account for the null terminator. Does not do any initialization, the caller
31-
// must initialize and null-terminate the string buffer. Errors if *out*
32-
// already contains data or if malloc fails.
32+
// must initialize and null-terminate the string buffer. Returns -1 if malloc
33+
// fails and -2 if *out* is not empty. Returns 0 on success.
3334
int
3435
ssnewemptylen(size_t num_bytes, ss *out);
3536

0 commit comments

Comments
 (0)