提交 81e7dff6 authored 作者: Ricardo Vieira's avatar Ricardo Vieira 提交者: Ricardo Vieira

Use fstrings in Alloc.c_code

上级 28fc9acb
......@@ -1494,36 +1494,30 @@ class Alloc(COp):
# Initialize shape
for i, shp_i in enumerate(inp[1:]):
code += """
shape[%(i)s] = ((dtype_%(shp_i)s*) PyArray_DATA(%(shp_i)s))[0];
""" % dict(
i=i, shp_i=shp_i
)
code += f"""
shape[{i}] = ((dtype_{shp_i}*) PyArray_DATA({shp_i}))[0];
"""
code += """
int need_new_out = (NULL == %(zz)s);
for (int i = 0; i < %(ndim)s; i++)
need_new_out = (need_new_out
|| (PyArray_DIMS(%(zz)s)[i] != shape[i]));
code += f"""
int need_new_out = (NULL == {zz});
for (int i = 0; i < {ndim}; i++)
need_new_out = (need_new_out || (PyArray_DIMS({zz})[i] != shape[i]));
if (need_new_out)
{
Py_XDECREF(%(zz)s);
%(zz)s = (PyArrayObject*) PyArray_SimpleNew(%(ndim)s,
shape, PyArray_TYPE((PyArrayObject*) py_%(vv)s));
if (!%(zz)s)
{
{{
Py_XDECREF({zz});
{zz} = (PyArrayObject*) PyArray_SimpleNew({ndim}, shape, PyArray_TYPE((PyArrayObject*) py_{vv}));
if (!{zz})
{{
PyErr_SetString(PyExc_MemoryError, "alloc failed");
%(fail)s
}
}
{fail}
}}
}}
// This function takes care of broadcasting
if (PyArray_CopyInto(%(zz)s, %(vv)s) == -1)
%(fail)s
""" % dict(
vv=vv, ndim=ndim, zz=zz, fail=fail
)
if (PyArray_CopyInto({zz}, {vv}) == -1)
{fail}
"""
return code
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论