Skip to content

Commit a5fd24e

Browse files
committed
Move the scalar back to python
1 parent 352add8 commit a5fd24e

File tree

8 files changed

+24
-55
lines changed

8 files changed

+24
-55
lines changed

stringdtype/meson.build

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ 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',
3533
]
3634

3735
py.install_sources(
3836
[
3937
'stringdtype/__init__.py',
38+
'stringdtype/scalar.py'
4039
],
4140
subdir: 'stringdtype'
4241
)

stringdtype/stringdtype/__init__.py

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

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

78
__all__ = [
89
"StringDType",

stringdtype/stringdtype/scalar.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""A scalar type needed by the dtype machinery."""
2+
3+
4+
class StringScalar(str):
5+
pass

stringdtype/stringdtype/src/dtype.c

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

33
#include "casts.h"
4-
#include "scalar.h"
54
#include "static_string.h"
65

6+
PyTypeObject *StringScalar_Type = NULL;
7+
78
/*
89
* Internal helper to create new instances
910
*/
@@ -63,7 +64,7 @@ static PyArray_Descr *
6364
string_discover_descriptor_from_pyobject(PyArray_DTypeMeta *NPY_UNUSED(cls),
6465
PyObject *obj)
6566
{
66-
if (Py_TYPE(obj) != &StringScalar_Type) {
67+
if (Py_TYPE(obj) != StringScalar_Type) {
6768
PyErr_SetString(PyExc_TypeError,
6869
"Can only store StringScalar in a StringDType array.");
6970
return NULL;
@@ -83,7 +84,7 @@ get_value(PyObject *scalar)
8384
PyTypeObject *scalar_type = Py_TYPE(scalar);
8485
// FIXME: handle bytes too
8586
if ((scalar_type == &PyUnicode_Type) ||
86-
(scalar_type == &StringScalar_Type)) {
87+
(scalar_type == StringScalar_Type)) {
8788
// attempt to decode as UTF8
8889
ret_bytes = PyUnicode_AsUTF8String(scalar);
8990
if (ret_bytes == NULL) {
@@ -394,7 +395,7 @@ init_string_dtype(void)
394395
PyArrayMethod_Spec **casts = get_casts();
395396

396397
PyArrayDTypeMeta_Spec StringDType_DTypeSpec = {
397-
.typeobj = &StringScalar_Type,
398+
.typeobj = StringScalar_Type,
398399
.slots = StringDType_Slots,
399400
.casts = casts,
400401
};

stringdtype/stringdtype/src/dtype.h

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

2020
extern PyArray_DTypeMeta StringDType;
21+
extern PyTypeObject *StringScalar_Type;
2122

2223
StringDTypeObject *
2324
new_stringdtype_instance(void);

stringdtype/stringdtype/src/main.c

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

88
#include "dtype.h"
9-
#include "scalar.h"
109
#include "static_string.h"
1110
#include "umath.h"
1211

@@ -101,7 +100,16 @@ PyInit__main(void)
101100
return NULL;
102101
}
103102

104-
if (init_stringdtype_scalar() < 0) {
103+
PyObject *mod = PyImport_ImportModule("stringdtype");
104+
if (mod == NULL) {
105+
goto error;
106+
}
107+
108+
StringScalar_Type =
109+
(PyTypeObject *)PyObject_GetAttrString(mod, "StringScalar");
110+
Py_DECREF(mod);
111+
112+
if (StringScalar_Type == NULL) {
105113
goto error;
106114
}
107115

@@ -115,13 +123,6 @@ PyInit__main(void)
115123
goto error;
116124
}
117125

118-
Py_INCREF((PyObject *)&StringScalar_Type);
119-
if (PyModule_AddObject(m, "StringScalar", (PyObject *)&StringScalar_Type) <
120-
0) {
121-
Py_DECREF((PyObject *)&StringScalar_Type);
122-
goto error;
123-
}
124-
125126
if (init_ufuncs() < 0) {
126127
goto error;
127128
}

stringdtype/stringdtype/src/scalar.c

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

stringdtype/stringdtype/src/scalar.h

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

0 commit comments

Comments
 (0)