Skip to content

Commit 352add8

Browse files
committed
Implement StringScalar in C
1 parent fae5b53 commit 352add8

File tree

9 files changed

+58
-35
lines changed

9 files changed

+58
-35
lines changed

stringdtype/meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ srcs = [
3030
'stringdtype/src/static_string.h',
3131
'stringdtype/src/umath.c',
3232
'stringdtype/src/umath.h',
33+
'stringdtype/src/scalar.h',
34+
'stringdtype/src/scalar.c',
3335
]
3436

3537
py.install_sources(
3638
[
3739
'stringdtype/__init__.py',
38-
'stringdtype/scalar.py'
3940
],
4041
subdir: 'stringdtype'
4142
)

stringdtype/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ per-file-ignores = {"__init__.py" = ["F401"]}
3131

3232
[tool.meson-python.args]
3333
dist = []
34-
setup = ["-Ddebug=true", "-Doptimization=0"]
34+
setup = ["-Ddebug=true", "-Doptimization=2"]
3535
compile = []
3636
install = []

stringdtype/stringdtype/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
33
"""
44

5-
from .scalar import StringScalar # isort: skip
6-
from ._main import StringDType, _memory_usage
5+
from ._main import StringDType, StringScalar, _memory_usage
76

87
__all__ = [
98
"StringDType",

stringdtype/stringdtype/scalar.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

stringdtype/stringdtype/src/dtype.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#include "dtype.h"
22

33
#include "casts.h"
4+
#include "scalar.h"
45
#include "static_string.h"
56

6-
PyTypeObject *StringScalar_Type = NULL;
7-
87
/*
98
* Internal helper to create new instances
109
*/
@@ -64,7 +63,7 @@ static PyArray_Descr *
6463
string_discover_descriptor_from_pyobject(PyArray_DTypeMeta *NPY_UNUSED(cls),
6564
PyObject *obj)
6665
{
67-
if (Py_TYPE(obj) != StringScalar_Type) {
66+
if (Py_TYPE(obj) != &StringScalar_Type) {
6867
PyErr_SetString(PyExc_TypeError,
6968
"Can only store StringScalar in a StringDType array.");
7069
return NULL;
@@ -84,7 +83,7 @@ get_value(PyObject *scalar)
8483
PyTypeObject *scalar_type = Py_TYPE(scalar);
8584
// FIXME: handle bytes too
8685
if ((scalar_type == &PyUnicode_Type) ||
87-
(scalar_type == StringScalar_Type)) {
86+
(scalar_type == &StringScalar_Type)) {
8887
// attempt to decode as UTF8
8988
ret_bytes = PyUnicode_AsUTF8String(scalar);
9089
if (ret_bytes == NULL) {
@@ -160,12 +159,7 @@ stringdtype_getitem(StringDTypeObject *NPY_UNUSED(descr), char **dataptr)
160159
return NULL;
161160
}
162161

163-
PyObject *res = PyObject_CallFunctionObjArgs((PyObject *)StringScalar_Type,
164-
val_obj, NULL);
165-
166-
Py_DECREF(val_obj);
167-
168-
return res;
162+
return val_obj;
169163
}
170164

171165
// PyArray_NonzeroFunc
@@ -400,7 +394,7 @@ init_string_dtype(void)
400394
PyArrayMethod_Spec **casts = get_casts();
401395

402396
PyArrayDTypeMeta_Spec StringDType_DTypeSpec = {
403-
.typeobj = StringScalar_Type,
397+
.typeobj = &StringScalar_Type,
404398
.slots = StringDType_Slots,
405399
.casts = casts,
406400
};

stringdtype/stringdtype/src/dtype.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ typedef struct {
1818
} StringDTypeObject;
1919

2020
extern PyArray_DTypeMeta StringDType;
21-
extern PyTypeObject *StringScalar_Type;
2221

2322
StringDTypeObject *
2423
new_stringdtype_instance(void);

stringdtype/stringdtype/src/main.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "numpy/experimental_dtype_api.h"
77

88
#include "dtype.h"
9+
#include "scalar.h"
910
#include "static_string.h"
1011
#include "umath.h"
1112

@@ -100,23 +101,24 @@ PyInit__main(void)
100101
return NULL;
101102
}
102103

103-
PyObject *mod = PyImport_ImportModule("stringdtype");
104-
if (mod == NULL) {
104+
if (init_stringdtype_scalar() < 0) {
105105
goto error;
106106
}
107-
StringScalar_Type =
108-
(PyTypeObject *)PyObject_GetAttrString(mod, "StringScalar");
109-
Py_DECREF(mod);
110107

111-
if (StringScalar_Type == NULL) {
108+
if (init_string_dtype() < 0) {
112109
goto error;
113110
}
114111

115-
if (init_string_dtype() < 0) {
112+
Py_INCREF((PyObject *)&StringDType);
113+
if (PyModule_AddObject(m, "StringDType", (PyObject *)&StringDType) < 0) {
114+
Py_DECREF((PyObject *)&StringDType);
116115
goto error;
117116
}
118117

119-
if (PyModule_AddObject(m, "StringDType", (PyObject *)&StringDType) < 0) {
118+
Py_INCREF((PyObject *)&StringScalar_Type);
119+
if (PyModule_AddObject(m, "StringScalar", (PyObject *)&StringScalar_Type) <
120+
0) {
121+
Py_DECREF((PyObject *)&StringScalar_Type);
120122
goto error;
121123
}
122124

stringdtype/stringdtype/src/scalar.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#define PY_ARRAY_UNIQUE_SYMBOL STRINGDTYPE_ARRAY_API
2+
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
3+
#define NO_IMPORT_ARRAY
4+
#include "numpy/arrayobject.h"
5+
#include "numpy/experimental_dtype_api.h"
6+
#include "numpy/ndarraytypes.h"
7+
8+
#include "scalar.h"
9+
10+
PyTypeObject StringScalar_Type = {
11+
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "stringdtype.StringScalar",
12+
.tp_basicsize = sizeof(StringScalarObject),
13+
};
14+
15+
int
16+
init_stringdtype_scalar(void)
17+
{
18+
StringScalar_Type.tp_base = &PyUnicode_Type;
19+
if (PyType_Ready(&StringScalar_Type) < 0) {
20+
return -1;
21+
}
22+
23+
return 0;
24+
}

stringdtype/stringdtype/src/scalar.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef _STRINGDTYPE_SCALAR_H
2+
#define _STRINGDTYPE_SCALAR_H
3+
4+
#include <Python.h>
5+
6+
extern PyTypeObject StringScalar_Type;
7+
8+
int
9+
init_stringdtype_scalar(void);
10+
11+
typedef struct {
12+
PyUnicodeObject str;
13+
} StringScalarObject;
14+
15+
#endif /* _STRINGDTYPE_SCALAR_H */

0 commit comments

Comments
 (0)