@@ -345,7 +345,7 @@ stArangeFunc g_ArangeFuncLUT[ATOP_LAST];
345345
346346
347347// set to 0 to disable
348- stSettings g_Settings = { 1 , 0 , 0 , 0 , 0 };
348+ stSettings g_Settings = { 1 , 0 , 0 , 0 , 0 , 0 , 0 };
349349
350350// Macro used just before call a ufunc
351351#define LEDGER_START () g_Settings.LedgerEnabled = 0 ; int64_t ledgerStartTime = __rdtsc();
@@ -1555,7 +1555,6 @@ PyObject* newinit(PyObject* self, PyObject* args, PyObject* kwargs) {
15551555
15561556 // Allocate an array of 10
15571557 PyArrayObject* pTemp= AllocateNumpyArray (1 , dims, srcdtype);
1558- // PyArray_Descr* pSrcDtype = PyArray_DescrFromType(srcdtype);
15591558 PyArray_Descr* pSrcDtype = PyArray_DESCR (pTemp);
15601559
15611560 if (pSrcDtype) {
@@ -1662,9 +1661,10 @@ PyObject* newinit(PyObject* self, PyObject* args, PyObject* kwargs) {
16621661 // }
16631662 // }
16641663 }
1665- // Py_DECREF(pSrcDtype);
1666-
1667- // Py_DECREF(arr);
1664+ // Py_DECREF(pSrcDtype);
1665+ // Py_DECREF(pTemp);
1666+ //
1667+
16681668 }
16691669 RETURN_NONE;
16701670 }
@@ -1675,6 +1675,94 @@ PyObject* newinit(PyObject* self, PyObject* args, PyObject* kwargs) {
16751675 RETURN_NONE;
16761676}
16771677
1678+ // ---------------------------------------------------------
1679+ // GetItem hook
1680+ //
1681+ extern " C"
1682+ PyObject * GetItemHook (PyObject* aValues, PyObject* aIndex) {
1683+
1684+ // Quick check for an array or we bail
1685+ if (PyType_IsSubtype (aIndex->ob_type , &PyArray_Type)) {
1686+ int32_t numpyValuesType = PyArray_TYPE ((PyArrayObject*)aValues);
1687+ int32_t numpyIndexType = PyArray_TYPE ((PyArrayObject*)aIndex);
1688+
1689+ PyObject* result = NULL ;
1690+ if (numpyIndexType == NPY_BOOL) {
1691+ // special path for boolean
1692+ result = BooleanIndexInternal ((PyArrayObject*)aValues, (PyArrayObject*)aIndex);
1693+ if (result) {
1694+ return result;
1695+ }
1696+ // clear error since punting
1697+ PyErr_Clear ();
1698+
1699+ } else
1700+ // TODO: improve this to handle strings/unicode/datetime/other
1701+ if (numpyIndexType <= NPY_LONGDOUBLE) {
1702+ result = getitem (aValues, aIndex);
1703+ if (result) {
1704+ return result;
1705+ }
1706+ PyErr_Clear ();
1707+ }
1708+ }
1709+ return g_Settings.NumpyGetItem (aValues, aIndex);
1710+ }
1711+
1712+ // ---------------------------------------------------------
1713+ // Call to get hook
1714+ extern " C"
1715+ PyObject * hook_enable (PyObject * self, PyObject * args) {
1716+
1717+ if (g_Settings.NumpyGetItem == NULL ) {
1718+ npy_intp dims[1 ] = { 10 };
1719+
1720+ // Allocate an array of 10 bools
1721+ PyArrayObject* pTemp = AllocateNumpyArray (1 , dims, 0 );
1722+ if (pTemp) {
1723+ struct _typeobject * pNumpyType = ((PyObject*)pTemp)->ob_type ;
1724+
1725+ // Not hooked yet
1726+ // PyNumberMethods* numbermethods = pNumpyType->tp_as_number;
1727+ // richcmpfunc comparefunc = pNumpyType->tp_richcompare;
1728+ // __setitem__
1729+ // objobjargproc PyMappingMethods.mp_ass_subscript
1730+
1731+ // __getitem__
1732+ // Reroute hook
1733+ g_Settings.NumpyGetItem = pNumpyType->tp_as_mapping ->mp_subscript ;
1734+ pNumpyType->tp_as_mapping ->mp_subscript = GetItemHook;
1735+
1736+ Py_DECREF (pTemp);
1737+ RETURN_TRUE;
1738+ }
1739+ }
1740+ RETURN_FALSE;
1741+ }
1742+
1743+ // ---------------------------------------------------------
1744+ // Call to remove previous hook
1745+ extern " C"
1746+ PyObject * hook_disable (PyObject * self, PyObject * args) {
1747+ if (g_Settings.NumpyGetItem != NULL ) {
1748+ npy_intp dims[1 ] = { 10 };
1749+
1750+ // Allocate an array of 10 bools
1751+ PyArrayObject* pTemp = AllocateNumpyArray (1 , dims, 0 );
1752+ if (pTemp) {
1753+ struct _typeobject * pNumpyType = ((PyObject*)pTemp)->ob_type ;
1754+ // __getitem__
1755+ // Put hook back
1756+ pNumpyType->tp_as_mapping ->mp_subscript = g_Settings.NumpyGetItem ;
1757+ g_Settings.NumpyGetItem = NULL ;
1758+ Py_DECREF (pTemp);
1759+ RETURN_TRUE;
1760+ }
1761+ }
1762+ RETURN_FALSE;
1763+ }
1764+
1765+
16781766extern " C"
16791767PyObject * atop_enable (PyObject * self, PyObject * args) {
16801768 g_Settings.AtopEnabled = TRUE ;
0 commit comments