summaryrefslogtreecommitdiff
path: root/numpy/core/src
diff options
context:
space:
mode:
authorsasha <sasha@localhost>2006-01-18 19:05:28 +0000
committersasha <sasha@localhost>2006-01-18 19:05:28 +0000
commit968b9239c6b2401d6688bf86d55bbcc6a48cc3b7 (patch)
treed8145b8936548071e4fe820b3a07102fee22e583 /numpy/core/src
parent5aab814ef2fa34881a3d9914c6d056f445c31625 (diff)
downloadnumpy-968b9239c6b2401d6688bf86d55bbcc6a48cc3b7.tar.gz
intern bool_arrtype values
Diffstat (limited to 'numpy/core/src')
-rw-r--r--numpy/core/src/arrayobject.c10
-rw-r--r--numpy/core/src/multiarraymodule.c3
-rw-r--r--numpy/core/src/scalartypes.inc.src37
3 files changed, 47 insertions, 3 deletions
diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c
index afa9dbc95..0207570da 100644
--- a/numpy/core/src/arrayobject.c
+++ b/numpy/core/src/arrayobject.c
@@ -810,7 +810,15 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base)
int swap;
type_num = descr->type_num;
- if ((type_num == PyArray_OBJECT)) {
+ /* XXX: Extra check is needed to allow this function to
+ XXX: be called to initialize bool values. It would
+ XXX: be better not to use this function in init. */
+ if (type_num == PyArray_BOOL && PyArrayScalar_True) {
+ int i = *(Bool*)data;
+ Py_INCREF(PyArrayScalar_BoolValues[i]);
+ return PyArrayScalar_BoolValues[i];
+ }
+ else if (type_num == PyArray_OBJECT) {
Py_INCREF(*((PyObject **)data));
return *((PyObject **)data);
}
diff --git a/numpy/core/src/multiarraymodule.c b/numpy/core/src/multiarraymodule.c
index 09092274d..866b531de 100644
--- a/numpy/core/src/multiarraymodule.c
+++ b/numpy/core/src/multiarraymodule.c
@@ -5480,6 +5480,9 @@ DL_EXPORT(void) initmultiarray(void) {
if (PyType_Ready(&PyArrayDescr_Type) < 0)
return;
+ /* NB: this cannot be called before types are initialized */
+ if (initialize_bool_values() < 0) goto err;
+
c_api = PyCObject_FromVoidPtr((void *)PyArray_API, NULL);
if (PyErr_Occurred()) goto err;
PyDict_SetItemString(d, "_ARRAY_API", c_api);
diff --git a/numpy/core/src/scalartypes.inc.src b/numpy/core/src/scalartypes.inc.src
index 8fed7cb4a..6321706fe 100644
--- a/numpy/core/src/scalartypes.inc.src
+++ b/numpy/core/src/scalartypes.inc.src
@@ -1,6 +1,9 @@
/* -*- c -*- */
static int PyArrayScalar_Offset[PyArray_NTYPES+1];
+static PyObject *PyArrayScalar_BoolValues[2];
+static PyObject *PyArrayScalar_True;
+static PyObject *PyArrayScalar_False;
#define _SOFFSET_(obj, type_num) ((char *)(obj) + PyArrayScalar_Offset[(type_num)])
@@ -1628,8 +1631,20 @@ bool_arrtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
PyObject *arr;
if (!PyArg_ParseTuple(args, "O", &obj)) return NULL;
-
+ if (obj == Py_False) {
+ Py_INCREF(PyArrayScalar_False);
+ return PyArrayScalar_False;
+ }
+ if (obj == Py_True) {
+ Py_INCREF(PyArrayScalar_True);
+ return PyArrayScalar_True;
+ }
arr = PyArray_FROM_OTF(obj, PyArray_BOOL, FORCECAST);
+ if (arr && 0 == PyArray_NDIM(arr)) {
+ int i = *(Bool*)PyArray_DATA(arr);
+ Py_INCREF(PyArrayScalar_BoolValues[i]);
+ return PyArrayScalar_BoolValues[i];
+ }
return PyArray_Return((PyArrayObject *)arr);
}
@@ -2035,7 +2050,7 @@ static PyNumberMethods longdoubletype_as_number;
static PyNumberMethods clongdoubletype_as_number;
-static void
+static void
initialize_numeric_types(void)
{
PyGenericArrType_Type.tp_dealloc = (destructor)gentype_dealloc;
@@ -2122,6 +2137,24 @@ ComplexFloating, Flexible, Character#
/**end repeat**/
}
+int
+initialize_bool_values(void)
+{
+ PyArrayScalar_False = PyArray_Return((PyArrayObject *)\
+ PyArray_FROM_OTF(Py_False, PyArray_BOOL, FORCECAST));
+ if (PyArrayScalar_False == NULL)
+ return -1;
+ PyArrayScalar_True = PyArray_Return((PyArrayObject *)\
+ PyArray_FROM_OTF(Py_True, PyArray_BOOL, FORCECAST));
+ if (PyArrayScalar_True == NULL) {
+ Py_DECREF(PyArrayScalar_False);
+ return -1;
+ }
+ PyArrayScalar_BoolValues[0] = PyArrayScalar_False;
+ PyArrayScalar_BoolValues[1] = PyArrayScalar_True;
+ return 0;
+}
+
/* the order of this table is important */
static PyTypeObject *typeobjects[] = {