提交 46ef1c33 authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Flake8 for gof/cutils.py

上级 9d7c48d9
...@@ -14,18 +14,21 @@ if os.path.exists(os.path.join(config.compiledir, 'cutils_ext.so')): ...@@ -14,18 +14,21 @@ if os.path.exists(os.path.join(config.compiledir, 'cutils_ext.so')):
def compile_cutils_code(): def compile_cutils_code():
types = ['npy_' + t for t in ['int8', 'int16', 'int32', 'int64', 'int128', types = ['npy_' + t for t in ['int8', 'int16', 'int32', 'int64', 'int128',
'int256', 'uint8', 'uint16', 'uint32', 'uint64', 'uint128', 'uint256', 'int256', 'uint8', 'uint16', 'uint32',
'float16', 'float32', 'float64', 'float80', 'float96', 'float128', 'uint64', 'uint128', 'uint256',
'float256']] 'float16', 'float32', 'float64',
'float80', 'float96', 'float128',
'float256']]
complex_types = ['npy_' + t for t in ['complex32', 'complex64', complex_types = ['npy_' + t for t in ['complex32', 'complex64',
'complex128', 'complex160', 'complex192', 'complex512']] 'complex128', 'complex160',
'complex192', 'complex512']]
inplace_map_template = """ inplace_map_template = """
#if defined(%(typen)s) #if defined(%(typen)s)
static void %(type)s_inplace_add(PyArrayMapIterObject *mit, PyArrayIterObject *it, int inc_or_set) static void %(type)s_inplace_add(PyArrayMapIterObject *mit,
PyArrayIterObject *it, int inc_or_set)
{ {
int index = mit->size; int index = mit->size;
while (index--) { while (index--) {
...@@ -38,46 +41,52 @@ def compile_cutils_code(): ...@@ -38,46 +41,52 @@ def compile_cutils_code():
#endif #endif
""" """
floatadd = "((%(type)s*)mit->dataptr)[0] = inc_or_set * ((%(type)s*)mit->dataptr)[0] + ((%(type)s*)it->dataptr)[0];" floatadd = ("((%(type)s*)mit->dataptr)[0] = inc_or_set * "
"((%(type)s*)mit->dataptr)[0] + ((%(type)s*)it->dataptr)[0];")
complexadd = """ complexadd = """
((%(type)s*)mit->dataptr)[0].real = inc_or_set * ((%(type)s*)mit->dataptr)[0].real + ((%(type)s*)it->dataptr)[0].real; ((%(type)s*)mit->dataptr)[0].real = inc_or_set *
((%(type)s*)mit->dataptr)[0].imag = inc_or_set * ((%(type)s*)mit->dataptr)[0].imag + ((%(type)s*)it->dataptr)[0].imag; ((%(type)s*)mit->dataptr)[0].real + ((%(type)s*)it->dataptr)[0].real;
((%(type)s*)mit->dataptr)[0].imag = inc_or_set *
((%(type)s*)mit->dataptr)[0].imag + ((%(type)s*)it->dataptr)[0].imag;
""" """
fns = ''.join([inplace_map_template % {'type': t, 'typen': t.upper(), fns = ''.join([inplace_map_template % {'type': t, 'typen': t.upper(),
'op': floatadd % {'type': t}} 'op': floatadd % {'type': t}}
for t in types] + for t in types] +
[inplace_map_template % {'type': t, 'typen': t.upper(), [inplace_map_template % {'type': t, 'typen': t.upper(),
'op': complexadd % {'type': t}} 'op': complexadd % {'type': t}}
for t in complex_types]) for t in complex_types])
def gen_binop(type, typen):
return """
#if defined(%(typen)s)
%(type)s_inplace_add,
#endif
""" % dict(type=type, typen=typen)
fn_array = ("static inplace_map_binop addition_funcs[] = {" + fn_array = ("static inplace_map_binop addition_funcs[] = {" +
''.join([""" ''.join([gen_binop(type=t, typen=t.upper())
#if defined(%(typen)s) for t in types + complex_types]) + "NULL};\n")
%(type)s_inplace_add,
#endif def gen_num(typen):
""" % {'type': t, 'typen': t.upper()} return """
for t in types + complex_types]) + #if defined(%(typen)s)
"""NULL}; %(typen)s,
""") #endif
""" % dict(type=type, typen=typen)
type_number_array = ("static int type_numbers[] = {" + type_number_array = ("static int type_numbers[] = {" +
''.join([""" ''.join([gen_num(typen=t.upper())
#if defined(%(typen)s) for t in types + complex_types]) + "-1000};")
%(typen)s,
#endif
""" % {'type': t, 'typen': t.upper()}
for t in types + complex_types]) +
"-1000};")
code = (""" code = ("""
#if NPY_API_VERSION >= 0x00000008 #if NPY_API_VERSION >= 0x00000008
typedef void (*inplace_map_binop)(PyArrayMapIterObject *, PyArrayIterObject *, int inc_or_set); typedef void (*inplace_map_binop)(PyArrayMapIterObject *,
""" + fns + fn_array + type_number_array + PyArrayIterObject *, int inc_or_set);
""" + fns + fn_array + type_number_array + """
"""
static int static int
map_increment(PyArrayMapIterObject *mit, PyObject *op, inplace_map_binop add_inplace, int inc_or_set) map_increment(PyArrayMapIterObject *mit, PyObject *op,
inplace_map_binop add_inplace, int inc_or_set)
{ {
PyArrayObject *arr = NULL; PyArrayObject *arr = NULL;
PyArrayIterObject *it; PyArrayIterObject *it;
...@@ -129,7 +138,8 @@ inplace_increment(PyObject *dummy, PyObject *args) ...@@ -129,7 +138,8 @@ inplace_increment(PyObject *dummy, PyObject *args)
return NULL; return NULL;
} }
if (!PyArray_Check(arg_a)) { if (!PyArray_Check(arg_a)) {
PyErr_SetString(PyExc_ValueError, "needs an ndarray as first argument"); PyErr_SetString(PyExc_ValueError,
"needs an ndarray as first argument");
return NULL; return NULL;
} }
...@@ -285,7 +295,7 @@ try: ...@@ -285,7 +295,7 @@ try:
open(os.path.join(location, '__init__.py'), 'w').close() open(os.path.join(location, '__init__.py'), 'w').close()
try: try:
from cutils_ext.cutils_ext import * from cutils_ext.cutils_ext import * # noqa
except ImportError: except ImportError:
get_lock() get_lock()
# Ensure no-one else is currently modifying the content of the compilation # Ensure no-one else is currently modifying the content of the compilation
...@@ -296,11 +306,11 @@ try: ...@@ -296,11 +306,11 @@ try:
# We must retry to import it as some other process could # We must retry to import it as some other process could
# have been compiling it between the first failed import # have been compiling it between the first failed import
# and when we receive the lock # and when we receive the lock
from cutils_ext.cutils_ext import * from cutils_ext.cutils_ext import * # noqa
except ImportError: except ImportError:
compile_cutils() compile_cutils()
from cutils_ext.cutils_ext import * from cutils_ext.cutils_ext import * # noqa
finally: finally:
# Release lock on compilation directory. # Release lock on compilation directory.
......
...@@ -268,7 +268,6 @@ whitelist_flake8 = [ ...@@ -268,7 +268,6 @@ whitelist_flake8 = [
"sparse/sandbox/truedot.py", "sparse/sandbox/truedot.py",
"sparse/sandbox/sp.py", "sparse/sandbox/sp.py",
"gof/destroyhandler.py", "gof/destroyhandler.py",
"gof/cutils.py",
"gof/compiledir.py", "gof/compiledir.py",
"gof/unify.py", "gof/unify.py",
"gof/lazylinker_c.py", "gof/lazylinker_c.py",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论