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

Use fstrings in Alloc.c_code

上级 28fc9acb
...@@ -1494,36 +1494,30 @@ class Alloc(COp): ...@@ -1494,36 +1494,30 @@ class Alloc(COp):
# Initialize shape # Initialize shape
for i, shp_i in enumerate(inp[1:]): for i, shp_i in enumerate(inp[1:]):
code += """ code += f"""
shape[%(i)s] = ((dtype_%(shp_i)s*) PyArray_DATA(%(shp_i)s))[0]; shape[{i}] = ((dtype_{shp_i}*) PyArray_DATA({shp_i}))[0];
""" % dict( """
i=i, shp_i=shp_i
)
code += """ code += f"""
int need_new_out = (NULL == %(zz)s); int need_new_out = (NULL == {zz});
for (int i = 0; i < %(ndim)s; i++) for (int i = 0; i < {ndim}; i++)
need_new_out = (need_new_out need_new_out = (need_new_out || (PyArray_DIMS({zz})[i] != shape[i]));
|| (PyArray_DIMS(%(zz)s)[i] != shape[i]));
if (need_new_out) if (need_new_out)
{ {{
Py_XDECREF(%(zz)s); Py_XDECREF({zz});
%(zz)s = (PyArrayObject*) PyArray_SimpleNew(%(ndim)s, {zz} = (PyArrayObject*) PyArray_SimpleNew({ndim}, shape, PyArray_TYPE((PyArrayObject*) py_{vv}));
shape, PyArray_TYPE((PyArrayObject*) py_%(vv)s)); if (!{zz})
if (!%(zz)s) {{
{
PyErr_SetString(PyExc_MemoryError, "alloc failed"); PyErr_SetString(PyExc_MemoryError, "alloc failed");
%(fail)s {fail}
} }}
} }}
// This function takes care of broadcasting // This function takes care of broadcasting
if (PyArray_CopyInto(%(zz)s, %(vv)s) == -1) if (PyArray_CopyInto({zz}, {vv}) == -1)
%(fail)s {fail}
""" % dict( """
vv=vv, ndim=ndim, zz=zz, fail=fail
)
return code return code
...@@ -1568,7 +1562,7 @@ class Alloc(COp): ...@@ -1568,7 +1562,7 @@ class Alloc(COp):
for idx, axis in enumerate(axis_kept): for idx, axis in enumerate(axis_kept):
new_order[axis] = idx new_order[axis] = idx
gx = gx.dimshuffle(new_order) gx = gx.dimshuffle(new_order)
# Dimshuffle to add back the broadcasted dims # Dimshuffle to add back the broadcasted dims
# The *elements* of the output are not connected to # The *elements* of the output are not connected to
# the inputs that specify the shape. If you grow the # the inputs that specify the shape. If you grow the
# shape by epsilon, the existing elements do not # shape by epsilon, the existing elements do not
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论