| 432 | } |
| 433 | |
| 434 | static int |
| 435 | fortran_setattr(PyFortranObject *fp, char *name, PyObject *v) |
| 436 | { |
| 437 | int i, j, flag; |
| 438 | PyArrayObject *arr = NULL; |
| 439 | for (i = 0, j = 1; i < fp->len && (j = strcmp(name, fp->defs[i].name)); |
| 440 | i++) |
| 441 | ; |
| 442 | if (j == 0) { |
| 443 | if (fp->defs[i].rank == -1) { |
| 444 | PyErr_SetString(PyExc_AttributeError, |
| 445 | "over-writing fortran routine"); |
| 446 | return -1; |
| 447 | } |
| 448 | if (fp->defs[i].func != NULL) { /* is allocatable array */ |
| 449 | npy_intp dims[F2PY_MAX_DIMS]; |
| 450 | int k; |
| 451 | save_def = &fp->defs[i]; |
| 452 | if (v != Py_None) { /* set new value (reallocate if needed -- |
| 453 | see f2py generated code for more |
| 454 | details ) */ |
| 455 | for (k = 0; k < fp->defs[i].rank; k++) dims[k] = -1; |
| 456 | if ((arr = array_from_pyobj(fp->defs[i].type, dims, |
| 457 | fp->defs[i].rank, F2PY_INTENT_IN, |
| 458 | v)) == NULL) |
| 459 | return -1; |
| 460 | (*(fp->defs[i].func))(&fp->defs[i].rank, PyArray_DIMS(arr), |
| 461 | set_data, &flag); |
| 462 | } |
| 463 | else { /* deallocate */ |
| 464 | for (k = 0; k < fp->defs[i].rank; k++) dims[k] = 0; |
| 465 | (*(fp->defs[i].func))(&fp->defs[i].rank, dims, set_data, |
| 466 | &flag); |
| 467 | for (k = 0; k < fp->defs[i].rank; k++) dims[k] = -1; |
| 468 | } |
| 469 | memcpy(fp->defs[i].dims.d, dims, |
| 470 | fp->defs[i].rank * sizeof(npy_intp)); |
| 471 | } |
| 472 | else { /* not allocatable array */ |
| 473 | if ((arr = array_from_pyobj(fp->defs[i].type, fp->defs[i].dims.d, |
| 474 | fp->defs[i].rank, F2PY_INTENT_IN, |
| 475 | v)) == NULL) |
| 476 | return -1; |
| 477 | } |
| 478 | if (fp->defs[i].data != |
| 479 | NULL) { /* copy Python object to Fortran array */ |
| 480 | npy_intp s = PyArray_MultiplyList(fp->defs[i].dims.d, |
| 481 | PyArray_NDIM(arr)); |
| 482 | if (s == -1) |
| 483 | s = PyArray_MultiplyList(PyArray_DIMS(arr), PyArray_NDIM(arr)); |
| 484 | if (s < 0 || (memcpy(fp->defs[i].data, PyArray_DATA(arr), |
| 485 | s * PyArray_ITEMSIZE(arr))) == NULL) { |
| 486 | if ((PyObject *)arr != v) { |
| 487 | Py_DECREF(arr); |
| 488 | } |
| 489 | return -1; |
| 490 | } |
| 491 | if ((PyObject *)arr != v) { |
nothing calls this directly
no test coverage detected