Commit 0e6322e4 authored by Stefan Behnel's avatar Stefan Behnel

Clean up Pythran module: normalise whitespace, remove dead code, etc.

parent 3ddd89a7
from .PyrexTypes import BufferType, CType, CTypedefType, CStructOrUnionType from __future__ import absolute_import
from .PyrexTypes import CType, CTypedefType, CStructOrUnionType
_pythran_var_prefix = "__pythran__"
# Pythran/Numpy specific operations # Pythran/Numpy specific operations
def has_np_pythran(env): def has_np_pythran(env):
while not env is None: while env is not None:
if hasattr(env, "directives") and env.directives.get('np_pythran', False): directives = getattr(env, 'directives', None)
if directives and env.directives.get('np_pythran', False):
return True return True
env = env.outer_scope env = env.outer_scope
def is_pythran_supported_dtype(type_): def is_pythran_supported_dtype(type_):
if isinstance(type_, CTypedefType): if isinstance(type_, CTypedefType):
return is_pythran_supported_type(type_.typedef_base_type) return is_pythran_supported_type(type_.typedef_base_type)
return type_.is_numeric return type_.is_numeric
def pythran_type(Ty,ptype="ndarray"):
def pythran_type(Ty, ptype="ndarray"):
if Ty.is_buffer: if Ty.is_buffer:
ndim,dtype = Ty.ndim, Ty.dtype ndim,dtype = Ty.ndim, Ty.dtype
if isinstance(dtype, CStructOrUnionType): if isinstance(dtype, CStructOrUnionType):
...@@ -23,29 +29,31 @@ def pythran_type(Ty,ptype="ndarray"): ...@@ -23,29 +29,31 @@ def pythran_type(Ty,ptype="ndarray"):
elif isinstance(dtype, CTypedefType): elif isinstance(dtype, CTypedefType):
ctype = dtype.typedef_cname ctype = dtype.typedef_cname
else: else:
raise ValueError("unsupported type %s!" % str(dtype)) raise ValueError("unsupported type %s!" % dtype)
return "pythonic::types::%s<%s,%d>" % (ptype,ctype, ndim) return "pythonic::types::%s<%s,%d>" % (ptype,ctype, ndim)
from .PyrexTypes import PythranExpr
if Ty.is_pythran_expr: if Ty.is_pythran_expr:
return Ty.pythran_type return Ty.pythran_type
#if Ty.is_none: #if Ty.is_none:
# return "decltype(pythonic::__builtin__::None)" # return "decltype(pythonic::__builtin__::None)"
if Ty.is_numeric: if Ty.is_numeric:
return Ty.sign_and_name() return Ty.sign_and_name()
raise ValueError("unsupported pythran type %s (%s)" % (str(Ty), str(type(Ty)))) raise ValueError("unsupported pythran type %s (%s)" % (Ty, type(Ty)))
return None
def type_remove_ref(ty): def type_remove_ref(ty):
return "typename std::remove_reference<%s>::type" % ty return "typename std::remove_reference<%s>::type" % ty
def pythran_binop_type(op, tA, tB): def pythran_binop_type(op, tA, tB):
return "decltype(std::declval<%s>() %s std::declval<%s>())" % \ return "decltype(std::declval<%s>() %s std::declval<%s>())" % (
(pythran_type(tA), op, pythran_type(tB)) pythran_type(tA), op, pythran_type(tB))
def pythran_unaryop_type(op, type_): def pythran_unaryop_type(op, type_):
return "decltype(%sstd::declval<%s>())" % ( return "decltype(%sstd::declval<%s>())" % (
op, pythran_type(type_)) op, pythran_type(type_))
def pythran_indexing_type(type_, indices): def pythran_indexing_type(type_, indices):
def index_code(idx): def index_code(idx):
if idx.is_slice: if idx.is_slice:
...@@ -55,15 +63,18 @@ def pythran_indexing_type(type_, indices): ...@@ -55,15 +63,18 @@ def pythran_indexing_type(type_, indices):
else: else:
func = "slice" func = "slice"
n = 3 n = 3
return "pythonic::types::%s(%s)" % (func,",".join(["0"]*n)) return "pythonic::types::%s(%s)" % (
func, ",".join(["0"]*n))
elif idx.type.is_int: elif idx.type.is_int:
return "std::declval<%s>()" % idx.type.sign_and_name() return "std::declval<%s>()" % idx.type.sign_and_name()
elif idx.type.is_pythran_expr: elif idx.type.is_pythran_expr:
return "std::declval<%s>()" % idx.type.pythran_type return "std::declval<%s>()" % idx.type.pythran_type
raise ValueError("unsupported indice type %s!" % idx.type) raise ValueError("unsupported indexing type %s!" % idx.type)
indexing = ",".join(index_code(idx) for idx in indices) indexing = ",".join(index_code(idx) for idx in indices)
return type_remove_ref("decltype(std::declval<%s>()(%s))" % (pythran_type(type_), indexing)) return type_remove_ref("decltype(std::declval<%s>()(%s))" % (pythran_type(type_), indexing))
def pythran_indexing_code(indices): def pythran_indexing_code(indices):
def index_code(idx): def index_code(idx):
if idx.is_slice: if idx.is_slice:
...@@ -73,18 +84,22 @@ def pythran_indexing_code(indices): ...@@ -73,18 +84,22 @@ def pythran_indexing_code(indices):
values = values[:2] values = values[:2]
else: else:
func = "slice" func = "slice"
return "pythonic::types::%s(%s)" % (func,",".join((v.pythran_result() for v in values))) return "pythonic::types::%s(%s)" % (
func, ",".join((v.pythran_result() for v in values)))
elif idx.type.is_int: elif idx.type.is_int:
return to_pythran(idx) return to_pythran(idx)
elif idx.type.is_pythran_expr: elif idx.type.is_pythran_expr:
return idx.pythran_result() return idx.pythran_result()
raise ValueError("unsupported indice type %s!" % str(idx.type)) raise ValueError("unsupported indexing type %s" % idx.type)
return ",".join(index_code(idx) for idx in indices) return ",".join(index_code(idx) for idx in indices)
def pythran_func_type(func, args): def pythran_func_type(func, args):
args = ",".join(("std::declval<%s>()" % pythran_type(a.type) for a in args)) args = ",".join(("std::declval<%s>()" % pythran_type(a.type) for a in args))
return "decltype(pythonic::numpy::functor::%s{}(%s))" % (func, args) return "decltype(pythonic::numpy::functor::%s{}(%s))" % (func, args)
def to_pythran(op, ptype=None): def to_pythran(op, ptype=None):
op_type = op.type op_type = op.type
if op_type.is_int: if op_type.is_int:
...@@ -96,11 +111,10 @@ def to_pythran(op, ptype=None): ...@@ -96,11 +111,10 @@ def to_pythran(op, ptype=None):
return "pythonic::__builtin__::None" return "pythonic::__builtin__::None"
if ptype is None: if ptype is None:
ptype = pythran_type(op_type) ptype = pythran_type(op_type)
assert(op.type.is_pyobject)
assert op.type.is_pyobject
return "from_python<%s>(%s)" % (ptype, op.py_result()) return "from_python<%s>(%s)" % (ptype, op.py_result())
def from_pythran():
return "to_python"
def is_type(type_, types): def is_type(type_, types):
for attr in types: for attr in types:
...@@ -108,25 +122,31 @@ def is_type(type_, types): ...@@ -108,25 +122,31 @@ def is_type(type_, types):
return True return True
return False return False
def is_pythran_supported_node_or_none(node): def is_pythran_supported_node_or_none(node):
return node.is_none or is_pythran_supported_type(node.type) return node.is_none or is_pythran_supported_type(node.type)
def is_pythran_supported_type(type_): def is_pythran_supported_type(type_):
pythran_supported = ( pythran_supported = (
"is_pythran_expr", "is_int", "is_numeric", "is_float", "is_none", "is_complex") "is_pythran_expr", "is_int", "is_numeric", "is_float", "is_none", "is_complex")
return is_type(type_, pythran_supported) or is_pythran_expr(type_) return is_type(type_, pythran_supported) or is_pythran_expr(type_)
def is_pythran_supported_operation_type(type_): def is_pythran_supported_operation_type(type_):
pythran_supported = ( pythran_supported = (
"is_pythran_expr", "is_int", "is_numeric", "is_float", "is_complex") "is_pythran_expr", "is_int", "is_numeric", "is_float", "is_complex")
return is_type(type_,pythran_supported) or is_pythran_expr(type_) return is_type(type_,pythran_supported) or is_pythran_expr(type_)
def is_pythran_expr(type_): def is_pythran_expr(type_):
return type_.is_pythran_expr return type_.is_pythran_expr
def is_pythran_buffer(type_): def is_pythran_buffer(type_):
return type_.is_numpy_buffer and is_pythran_supported_dtype(type_.dtype) and \ return (type_.is_numpy_buffer and is_pythran_supported_dtype(type_.dtype) and
type_.mode in ("c","strided") and not type_.cast type_.mode in ("c", "strided") and not type_.cast)
def include_pythran_generic(env): def include_pythran_generic(env):
# Generic files # Generic files
...@@ -134,19 +154,11 @@ def include_pythran_generic(env): ...@@ -134,19 +154,11 @@ def include_pythran_generic(env):
env.add_include_file("pythonic/python/core.hpp") env.add_include_file("pythonic/python/core.hpp")
env.add_include_file("pythonic/types/bool.hpp") env.add_include_file("pythonic/types/bool.hpp")
env.add_include_file("pythonic/types/ndarray.hpp") env.add_include_file("pythonic/types/ndarray.hpp")
env.add_include_file("<new>") # for placement new env.add_include_file("<new>") # for placement new
for i in (8,16,32,64): for i in (8, 16, 32, 64):
env.add_include_file("pythonic/types/uint%d.hpp" % i) env.add_include_file("pythonic/types/uint%d.hpp" % i)
env.add_include_file("pythonic/types/int%d.hpp" % i) env.add_include_file("pythonic/types/int%d.hpp" % i)
for t in ("float", "float32", "float64", "set", "slice", "tuple", "int", for t in ("float", "float32", "float64", "set", "slice", "tuple", "int",
"long", "complex", "complex64", "complex128"): "long", "complex", "complex64", "complex128"):
env.add_include_file("pythonic/types/%s.hpp" % t) env.add_include_file("pythonic/types/%s.hpp" % t)
def include_pythran_type(env, type_):
pass
def type_is_numpy(type_):
if not hasattr(type_, "is_numpy"):
return False
return type_.is_numpy
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment