MCPcopy Create free account
hub / github.com/numpy/numpy / arraydescr_new

Function arraydescr_new

numpy/core/src/multiarray/descriptor.c:2433–2543  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2431};
2432
2433static PyObject *
2434arraydescr_new(PyTypeObject *subtype,
2435 PyObject *args, PyObject *kwds)
2436{
2437 if (subtype != &PyArrayDescr_Type) {
2438 if (Py_TYPE(subtype) == &PyArrayDTypeMeta_Type &&
2439 (NPY_DT_SLOTS((PyArray_DTypeMeta *)subtype)) != NULL &&
2440 !NPY_DT_is_legacy((PyArray_DTypeMeta *)subtype) &&
2441 subtype->tp_new != PyArrayDescr_Type.tp_new) {
2442 /*
2443 * Appears to be a properly initialized user DType. Allocate
2444 * it and initialize the main part as best we can.
2445 * TODO: This should probably be a user function, and enforce
2446 * things like the `elsize` being correctly set.
2447 * TODO: This is EXPERIMENTAL API!
2448 */
2449 PyArray_DTypeMeta *DType = (PyArray_DTypeMeta *)subtype;
2450 PyArray_Descr *descr = (PyArray_Descr *)subtype->tp_alloc(subtype, 0);
2451 if (descr == 0) {
2452 PyErr_NoMemory();
2453 return NULL;
2454 }
2455 descr->f = &NPY_DT_SLOTS(DType)->f;
2456 Py_XINCREF(DType->scalar_type);
2457 descr->typeobj = DType->scalar_type;
2458 descr->type_num = DType->type_num;
2459 descr->flags = NPY_USE_GETITEM|NPY_USE_SETITEM;
2460 descr->byteorder = '|'; /* If DType uses it, let it override */
2461 descr->elsize = -1; /* Initialize to invalid value */
2462 descr->hash = -1;
2463 return (PyObject *)descr;
2464 }
2465 /* The DTypeMeta class should prevent this from happening. */
2466 PyErr_Format(PyExc_SystemError,
2467 "'%S' must not inherit np.dtype.__new__(). User DTypes should "
2468 "currently call `PyArrayDescr_Type.tp_new` from their new.",
2469 subtype);
2470 return NULL;
2471 }
2472
2473 PyObject *odescr, *metadata=NULL;
2474 PyArray_Descr *conv;
2475 npy_bool align = NPY_FALSE;
2476 npy_bool copy = NPY_FALSE;
2477 npy_bool copied = NPY_FALSE;
2478
2479 static char *kwlist[] = {"dtype", "align", "copy", "metadata", NULL};
2480
2481 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O&O&O!:dtype", kwlist,
2482 &odescr,
2483 PyArray_BoolConverter, &align,
2484 PyArray_BoolConverter, &copy,
2485 &PyDict_Type, &metadata)) {
2486 return NULL;
2487 }
2488
2489 conv = _convert_from_any(odescr, align);
2490 if (conv == NULL) {

Callers

nothing calls this directly

Calls 1

_convert_from_anyFunction · 0.85

Tested by

no test coverage detected