| File: | /tmp/pyrefcon/scipy/scipy/optimize/_lsap_module.c |
| Warning: | line 121, column 9 PyObject ownership leak with reference count of 1 |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* | |||
| 2 | Redistribution and use in source and binary forms, with or without | |||
| 3 | modification, are permitted provided that the following conditions | |||
| 4 | are met: | |||
| 5 | ||||
| 6 | 1. Redistributions of source code must retain the above copyright | |||
| 7 | notice, this list of conditions and the following disclaimer. | |||
| 8 | ||||
| 9 | 2. Redistributions in binary form must reproduce the above | |||
| 10 | copyright notice, this list of conditions and the following | |||
| 11 | disclaimer in the documentation and/or other materials provided | |||
| 12 | with the distribution. | |||
| 13 | ||||
| 14 | 3. Neither the name of the copyright holder nor the names of its | |||
| 15 | contributors may be used to endorse or promote products derived | |||
| 16 | from this software without specific prior written permission. | |||
| 17 | ||||
| 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |||
| 19 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |||
| 20 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |||
| 21 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |||
| 22 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||
| 23 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
| 24 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |||
| 25 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |||
| 26 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |||
| 27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |||
| 28 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |||
| 29 | */ | |||
| 30 | ||||
| 31 | #include "numpy/arrayobject.h" | |||
| 32 | #include "numpy/ndarraytypes.h" | |||
| 33 | #include "rectangular_lsap/rectangular_lsap.h" | |||
| 34 | ||||
| 35 | static PyObject* | |||
| 36 | calculate_assignment(PyObject* self, PyObject* args) | |||
| 37 | { | |||
| 38 | PyObject* a = NULL((void*)0); | |||
| 39 | PyObject* b = NULL((void*)0); | |||
| 40 | PyObject* result = NULL((void*)0); | |||
| 41 | PyObject* obj_cost = NULL((void*)0); | |||
| 42 | int maximize = 0; | |||
| 43 | if (!PyArg_ParseTuple(args, "Op", &obj_cost, &maximize)) | |||
| 44 | return NULL((void*)0); | |||
| 45 | ||||
| 46 | PyArrayObject* obj_cont = | |||
| 47 | (PyArrayObject*)PyArray_ContiguousFromAny(obj_cost, NPY_DOUBLE, 0, 0)(*(PyObject * (*)(PyObject *, PyArray_Descr *, int, int, int, PyObject *)) PyArray_API[69])(obj_cost, (*(PyArray_Descr * ( *)(int)) PyArray_API[45])(NPY_DOUBLE), 0, 0, ((0x0001 | (0x0100 | 0x0400))), ((void*)0)); | |||
| 48 | if (!obj_cont) { | |||
| 49 | return NULL((void*)0); | |||
| 50 | } | |||
| 51 | ||||
| 52 | if (PyArray_NDIM(obj_cont) != 2) { | |||
| 53 | PyErr_Format(PyExc_ValueError, | |||
| 54 | "expected a matrix (2-D array), got a %d array", | |||
| 55 | PyArray_NDIM(obj_cont)); | |||
| 56 | goto cleanup; | |||
| 57 | } | |||
| 58 | ||||
| 59 | double* cost_matrix = (double*)PyArray_DATA(obj_cont); | |||
| 60 | if (cost_matrix == NULL((void*)0)) { | |||
| 61 | PyErr_SetString(PyExc_TypeError, "invalid cost matrix object"); | |||
| 62 | goto cleanup; | |||
| 63 | } | |||
| 64 | ||||
| 65 | npy_intp num_rows = PyArray_DIM(obj_cont, 0); | |||
| 66 | npy_intp num_cols = PyArray_DIM(obj_cont, 1); | |||
| 67 | npy_intp dim[1] = { num_rows < num_cols ? num_rows : num_cols }; | |||
| 68 | a = PyArray_SimpleNew(1, dim, NPY_INT64)(*(PyObject * (*)(PyTypeObject *, int, npy_intp const *, int, npy_intp const *, void *, int, int, PyObject *)) PyArray_API [93])(&(*(PyTypeObject *)PyArray_API[2]), 1, dim, NPY_LONG , ((void*)0), ((void*)0), 0, 0, ((void*)0)); | |||
| 69 | if (!a) | |||
| 70 | goto cleanup; | |||
| 71 | ||||
| 72 | b = PyArray_SimpleNew(1, dim, NPY_INT64)(*(PyObject * (*)(PyTypeObject *, int, npy_intp const *, int, npy_intp const *, void *, int, int, PyObject *)) PyArray_API [93])(&(*(PyTypeObject *)PyArray_API[2]), 1, dim, NPY_LONG , ((void*)0), ((void*)0), 0, 0, ((void*)0)); | |||
| 73 | if (!b) | |||
| 74 | goto cleanup; | |||
| 75 | ||||
| 76 | int ret = solve_rectangular_linear_sum_assignment( | |||
| 77 | num_rows, num_cols, cost_matrix, maximize, | |||
| 78 | PyArray_DATA((PyArrayObject*)a), | |||
| 79 | PyArray_DATA((PyArrayObject*)b)); | |||
| 80 | if (ret == RECTANGULAR_LSAP_INFEASIBLE-1) { | |||
| 81 | PyErr_SetString(PyExc_ValueError, "cost matrix is infeasible"); | |||
| 82 | goto cleanup; | |||
| 83 | } | |||
| 84 | else if (ret == RECTANGULAR_LSAP_INVALID-2) { | |||
| 85 | PyErr_SetString(PyExc_ValueError, | |||
| 86 | "matrix contains invalid numeric entries"); | |||
| 87 | goto cleanup; | |||
| 88 | } | |||
| 89 | ||||
| 90 | result = Py_BuildValue("OO", a, b); | |||
| 91 | ||||
| 92 | cleanup: | |||
| 93 | Py_XDECREF((PyObject*)obj_cont)_Py_XDECREF(((PyObject*)((PyObject*)obj_cont))); | |||
| 94 | Py_XDECREF(a)_Py_XDECREF(((PyObject*)(a))); | |||
| 95 | Py_XDECREF(b)_Py_XDECREF(((PyObject*)(b))); | |||
| 96 | return result; | |||
| 97 | } | |||
| 98 | ||||
| 99 | static PyMethodDef lsap_module_methods[] = { | |||
| 100 | { "calculate_assignment", calculate_assignment, METH_VARARGS0x0001, | |||
| 101 | "Solves the rectangular linear sum assignment problem." }, | |||
| 102 | { NULL((void*)0), NULL((void*)0), 0, NULL((void*)0) } | |||
| 103 | }; | |||
| 104 | ||||
| 105 | static struct PyModuleDef moduledef = { | |||
| 106 | PyModuleDef_HEAD_INIT{ { 1, ((void*)0) }, ((void*)0), 0, ((void*)0), }, | |||
| 107 | "_lsap_module", | |||
| 108 | "Solves the rectangular linear sum assignment.", | |||
| 109 | -1, | |||
| 110 | lsap_module_methods, | |||
| 111 | NULL((void*)0), | |||
| 112 | NULL((void*)0), | |||
| 113 | NULL((void*)0), | |||
| 114 | NULL((void*)0), | |||
| 115 | }; | |||
| 116 | ||||
| 117 | PyObject* | |||
| 118 | PyInit__lsap_module(void) | |||
| 119 | { | |||
| 120 | PyObject* m; | |||
| 121 | m = PyModule_Create(&moduledef)PyModule_Create2(&moduledef, 1013); | |||
| ||||
| ||||
| 122 | import_array(){if (_import_array() < 0) {PyErr_Print(); PyErr_SetString( PyExc_ImportError, "numpy.core.multiarray failed to import"); return ((void*)0); } }; | |||
| 123 | return m; | |||
| 124 | } |
| 1 | #ifndef PyModule_Create2 |
| 2 | struct _object; |
| 3 | typedef struct _object PyObject; |
| 4 | PyObject* clang_analyzer_PyObject_New_Reference(); |
| 5 | PyObject* PyModule_Create2(PyModuleDef *def, int module_api_version) { |
| 6 | return clang_analyzer_PyObject_New_Reference(); |
| 7 | } |
| 8 | #else |
| 9 | #warning "API PyModule_Create2 is defined as a macro." |
| 10 | #endif |