Skip to content

Commit 81372de

Browse files
authored
Merge pull request #20 from ngoldbaum/refcnt-fix
fix reference counting issues in `asciidtype` and `metadatadtype`
2 parents a47a865 + 6155d14 commit 81372de

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

asciidtype/asciidtype/src/dtype.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,20 @@ new_asciidtype_instance(long size)
5555
}
5656

5757
/*
58-
* This is used to determine the correct dtype to return when operations mix
59-
* dtypes (I think?). For now just return the first one.
58+
* This is used to determine the correct dtype to return when dealing
59+
* with a mix of different dtypes (for example when creating an array
60+
* from a list of scalars). Always return the dtype with the biggest
61+
* size.
6062
*/
6163
static ASCIIDTypeObject *
6264
common_instance(ASCIIDTypeObject *dtype1, ASCIIDTypeObject *dtype2)
6365
{
64-
if (!PyObject_RichCompareBool((PyObject *)dtype1, (PyObject *)dtype2,
65-
Py_EQ)) {
66-
PyErr_SetString(
67-
PyExc_RuntimeError,
68-
"common_instance called on unequal ASCIIDType instances");
69-
return NULL;
66+
if (dtype1->size >= dtype2->size) {
67+
Py_INCREF(dtype1);
68+
return dtype1;
7069
}
71-
return dtype1;
70+
Py_INCREF(dtype2);
71+
return dtype2;
7272
}
7373

7474
static PyArray_DTypeMeta *

asciidtype/tests/test_asciidtype.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ def test_creation_with_explicit_dtype():
2424
)
2525

2626

27+
def test_creation_from_scalar():
28+
data = [
29+
ASCIIScalar("hello", ASCIIDType(6)),
30+
ASCIIScalar("array", ASCIIDType(7)),
31+
]
32+
arr = np.array(data)
33+
assert repr(arr) == ("array(['hello', 'array'], dtype=ASCIIDType(7))")
34+
35+
2736
def test_creation_truncation():
2837
inp = ["hello", "this", "is", "an", "array"]
2938

metadatadtype/metadatadtype/src/dtype.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ get_metadata(PyObject *scalar)
5252
}
5353

5454
PyObject *metadata = dtype->metadata;
55+
Py_DECREF(dtype);
5556
if (metadata == NULL) {
5657
return NULL;
5758
}
@@ -87,6 +88,7 @@ new_metadatadtype_instance(PyObject *metadata)
8788
static MetadataDTypeObject *
8889
common_instance(MetadataDTypeObject *dtype1, MetadataDTypeObject *dtype2)
8990
{
91+
Py_INCREF(dtype1);
9092
return dtype1;
9193
}
9294

0 commit comments

Comments
 (0)