summaryrefslogtreecommitdiff
path: root/libs/python/src
diff options
context:
space:
mode:
authorDongHun Kwak <dh0128.kwak@samsung.com>2017-09-13 11:24:46 +0900
committerDongHun Kwak <dh0128.kwak@samsung.com>2017-09-13 11:25:39 +0900
commit4fadd968fa12130524c8380f33fcfe25d4de79e5 (patch)
treefd26a490cd15388d42fc6652b3c5c13012e7f93e /libs/python/src
parentb5c87084afaef42b2d058f68091be31988a6a874 (diff)
downloadboost-4fadd968fa12130524c8380f33fcfe25d4de79e5.tar.gz
boost-4fadd968fa12130524c8380f33fcfe25d4de79e5.tar.bz2
boost-4fadd968fa12130524c8380f33fcfe25d4de79e5.zip
Imported Upstream version 1.65.0upstream/1.65.0
Change-Id: Icf8400b375482cb11bcf77440a6934ba360d6ba4 Signed-off-by: DongHun Kwak <dh0128.kwak@samsung.com>
Diffstat (limited to 'libs/python/src')
-rw-r--r--libs/python/src/SConscript3
-rw-r--r--libs/python/src/exec.cpp28
-rw-r--r--libs/python/src/numeric.cpp325
-rwxr-xr-x[-rw-r--r--]libs/python/src/numpy/dtype.cpp40
-rw-r--r--libs/python/src/numpy/ndarray.cpp28
5 files changed, 62 insertions, 362 deletions
diff --git a/libs/python/src/SConscript b/libs/python/src/SConscript
index 49ab73b808..6d2e5694b9 100644
--- a/libs/python/src/SConscript
+++ b/libs/python/src/SConscript
@@ -16,8 +16,7 @@ env1.AppendUnique(CPPDEFINES = ['BOOST_PYTHON_SOURCE'])
env1.BoostLibrary(
'python',
- ['numeric.cpp',
- 'list.cpp',
+ ['list.cpp',
'long.cpp',
'dict.cpp',
'tuple.cpp',
diff --git a/libs/python/src/exec.cpp b/libs/python/src/exec.cpp
index fa2860e40e..603a6f013f 100644
--- a/libs/python/src/exec.cpp
+++ b/libs/python/src/exec.cpp
@@ -16,6 +16,11 @@ namespace python
object BOOST_PYTHON_DECL eval(str string, object global, object local)
{
+ return eval(python::extract<char const *>(string));
+}
+
+object BOOST_PYTHON_DECL eval(char const *string, object global, object local)
+{
// Set suitable default values for global and local dicts.
if (global.is_none())
{
@@ -26,7 +31,7 @@ object BOOST_PYTHON_DECL eval(str string, object global, object local)
}
if (local.is_none()) local = global;
// should be 'char const *' but older python versions don't use 'const' yet.
- char *s = python::extract<char *>(string);
+ char *s = const_cast<char *>(string);
PyObject* result = PyRun_String(s, Py_eval_input, global.ptr(), local.ptr());
if (!result) throw_error_already_set();
return object(detail::new_reference(result));
@@ -34,6 +39,11 @@ object BOOST_PYTHON_DECL eval(str string, object global, object local)
object BOOST_PYTHON_DECL exec(str string, object global, object local)
{
+ return exec(python::extract<char const *>(string));
+}
+
+object BOOST_PYTHON_DECL exec(char const *string, object global, object local)
+{
// Set suitable default values for global and local dicts.
if (global.is_none())
{
@@ -44,7 +54,7 @@ object BOOST_PYTHON_DECL exec(str string, object global, object local)
}
if (local.is_none()) local = global;
// should be 'char const *' but older python versions don't use 'const' yet.
- char *s = python::extract<char *>(string);
+ char *s = const_cast<char *>(string);
PyObject* result = PyRun_String(s, Py_file_input, global.ptr(), local.ptr());
if (!result) throw_error_already_set();
return object(detail::new_reference(result));
@@ -52,6 +62,11 @@ object BOOST_PYTHON_DECL exec(str string, object global, object local)
object BOOST_PYTHON_DECL exec_statement(str string, object global, object local)
{
+ return exec_statement(python::extract<char const *>(string), global, local);
+}
+
+object BOOST_PYTHON_DECL exec_statement(char const *string, object global, object local)
+{
// Set suitable default values for global and local dicts.
if (global.is_none())
{
@@ -62,7 +77,7 @@ object BOOST_PYTHON_DECL exec_statement(str string, object global, object local)
}
if (local.is_none()) local = global;
// should be 'char const *' but older python versions don't use 'const' yet.
- char *s = python::extract<char *>(string);
+ char *s = const_cast<char *>(string);
PyObject* result = PyRun_String(s, Py_single_input, global.ptr(), local.ptr());
if (!result) throw_error_already_set();
return object(detail::new_reference(result));
@@ -73,6 +88,11 @@ object BOOST_PYTHON_DECL exec_statement(str string, object global, object local)
// used during execution.
object BOOST_PYTHON_DECL exec_file(str filename, object global, object local)
{
+ return exec_file(python::extract<char const *>(filename), global, local);
+}
+
+object BOOST_PYTHON_DECL exec_file(char const *filename, object global, object local)
+{
// Set suitable default values for global and local dicts.
if (global.is_none())
{
@@ -83,7 +103,7 @@ object BOOST_PYTHON_DECL exec_file(str filename, object global, object local)
}
if (local.is_none()) local = global;
// should be 'char const *' but older python versions don't use 'const' yet.
- char *f = python::extract<char *>(filename);
+ char *f = const_cast<char *>(filename);
// Let python open the file to avoid potential binary incompatibilities.
#if PY_VERSION_HEX >= 0x03040000
FILE *fs = _Py_fopen(f, "r");
diff --git a/libs/python/src/numeric.cpp b/libs/python/src/numeric.cpp
deleted file mode 100644
index c8a5f071d9..0000000000
--- a/libs/python/src/numeric.cpp
+++ /dev/null
@@ -1,325 +0,0 @@
-// Copyright David Abrahams 2002.
-// Distributed under the Boost Software License, Version 1.0. (See
-// accompanying file LICENSE_1_0.txt or copy at
-// http://www.boost.org/LICENSE_1_0.txt)
-
-#include <boost/python/numeric.hpp>
-#include <boost/python/handle.hpp>
-#include <boost/python/cast.hpp>
-#include <boost/python/tuple.hpp>
-#include <boost/python/detail/raw_pyobject.hpp>
-#include <boost/python/extract.hpp>
-
-namespace boost { namespace python { namespace numeric {
-
-namespace
-{
- enum state_t { failed = -1, unknown, succeeded };
- state_t state = unknown;
- std::string module_name;
- std::string type_name;
-
- handle<> array_module;
- handle<> array_type;
- handle<> array_function;
-
- void throw_load_failure()
- {
- PyErr_Format(
- PyExc_ImportError
- , "No module named '%s' or its type '%s' did not follow the NumPy protocol"
- , module_name.c_str(), type_name.c_str());
- throw_error_already_set();
-
- }
-
- bool load(bool throw_on_error)
- {
- if (!state)
- {
- if (module_name.size() == 0)
- {
- module_name = "numarray";
- type_name = "NDArray";
- if (load(false))
- return true;
- module_name = "Numeric";
- type_name = "ArrayType";
- }
-
- state = failed;
- PyObject* module = ::PyImport_Import(object(module_name).ptr());
- if (module)
- {
- PyObject* type = ::PyObject_GetAttrString(module, const_cast<char*>(type_name.c_str()));
-
- if (type && PyType_Check(type))
- {
- array_type = handle<>(type);
- PyObject* function = ::PyObject_GetAttrString(module, const_cast<char*>("array"));
-
- if (function && PyCallable_Check(function))
- {
- array_function = handle<>(function);
- state = succeeded;
- }
- }
- }
- }
-
- if (state == succeeded)
- return true;
-
- if (throw_on_error)
- throw_load_failure();
-
- PyErr_Clear();
- return false;
- }
-
- object demand_array_function()
- {
- load(true);
- return object(array_function);
- }
-}
-
-void array::set_module_and_type(char const* package_name, char const* type_attribute_name)
-{
- state = unknown;
- module_name = package_name ? package_name : "" ;
- type_name = type_attribute_name ? type_attribute_name : "" ;
-}
-
-std::string array::get_module_name()
-{
- load(false);
- return module_name;
-}
-
-namespace aux
-{
- bool array_object_manager_traits::check(PyObject* obj)
- {
- if (!load(false))
- return false;
- return ::PyObject_IsInstance(obj, array_type.get());
- }
-
- python::detail::new_non_null_reference
- array_object_manager_traits::adopt(PyObject* obj)
- {
- load(true);
- return detail::new_non_null_reference(
- pytype_check(downcast<PyTypeObject>(array_type.get()), obj));
- }
-
- PyTypeObject const* array_object_manager_traits::get_pytype()
- {
- load(false);
- if(!array_type) return 0;
- return downcast<PyTypeObject>(array_type.get());
- }
-
-# define BOOST_PYTHON_AS_OBJECT(z, n, _) object(x##n)
-# define BOOST_PP_LOCAL_MACRO(n) \
- array_base::array_base(BOOST_PP_ENUM_PARAMS(n, object const& x)) \
- : object(demand_array_function()(BOOST_PP_ENUM_PARAMS(n, x))) \
- {}
-# define BOOST_PP_LOCAL_LIMITS (1, 6)
-# include BOOST_PP_LOCAL_ITERATE()
-# undef BOOST_PYTHON_AS_OBJECT
-
- array_base::array_base(BOOST_PP_ENUM_PARAMS(7, object const& x))
- : object(demand_array_function()(BOOST_PP_ENUM_PARAMS(7, x)))
- {}
-
- object array_base::argmax(long axis)
- {
- return attr("argmax")(axis);
- }
-
- object array_base::argmin(long axis)
- {
- return attr("argmin")(axis);
- }
-
- object array_base::argsort(long axis)
- {
- return attr("argsort")(axis);
- }
-
- object array_base::astype(object const& type)
- {
- return attr("astype")(type);
- }
-
- void array_base::byteswap()
- {
- attr("byteswap")();
- }
-
- object array_base::copy() const
- {
- return attr("copy")();
- }
-
- object array_base::diagonal(long offset, long axis1, long axis2) const
- {
- return attr("diagonal")(offset, axis1, axis2);
- }
-
- void array_base::info() const
- {
- attr("info")();
- }
-
- bool array_base::is_c_array() const
- {
- return extract<bool>(attr("is_c_array")());
- }
-
- bool array_base::isbyteswapped() const
- {
- return extract<bool>(attr("isbyteswapped")());
- }
-
- array array_base::new_(object type) const
- {
- return extract<array>(attr("new")(type))();
- }
-
- void array_base::sort()
- {
- attr("sort")();
- }
-
- object array_base::trace(long offset, long axis1, long axis2) const
- {
- return attr("trace")(offset, axis1, axis2);
- }
-
- object array_base::type() const
- {
- return attr("type")();
- }
-
- char array_base::typecode() const
- {
- return extract<char>(attr("typecode")());
- }
-
- object array_base::factory(
- object const& sequence
- , object const& typecode
- , bool copy
- , bool savespace
- , object type
- , object shape
- )
- {
- return attr("factory")(sequence, typecode, copy, savespace, type, shape);
- }
-
- object array_base::getflat() const
- {
- return attr("getflat")();
- }
-
- long array_base::getrank() const
- {
- return extract<long>(attr("getrank")());
- }
-
- object array_base::getshape() const
- {
- return attr("getshape")();
- }
-
- bool array_base::isaligned() const
- {
- return extract<bool>(attr("isaligned")());
- }
-
- bool array_base::iscontiguous() const
- {
- return extract<bool>(attr("iscontiguous")());
- }
-
- long array_base::itemsize() const
- {
- return extract<long>(attr("itemsize")());
- }
-
- long array_base::nelements() const
- {
- return extract<long>(attr("nelements")());
- }
-
- object array_base::nonzero() const
- {
- return attr("nonzero")();
- }
-
- void array_base::put(object const& indices, object const& values)
- {
- attr("put")(indices, values);
- }
-
- void array_base::ravel()
- {
- attr("ravel")();
- }
-
- object array_base::repeat(object const& repeats, long axis)
- {
- return attr("repeat")(repeats, axis);
- }
-
- void array_base::resize(object const& shape)
- {
- attr("resize")(shape);
- }
-
- void array_base::setflat(object const& flat)
- {
- attr("setflat")(flat);
- }
-
- void array_base::setshape(object const& shape)
- {
- attr("setshape")(shape);
- }
-
- void array_base::swapaxes(long axis1, long axis2)
- {
- attr("swapaxes")(axis1, axis2);
- }
-
- object array_base::take(object const& sequence, long axis) const
- {
- return attr("take")(sequence, axis);
- }
-
- void array_base::tofile(object const& file) const
- {
- attr("tofile")(file);
- }
-
- str array_base::tostring() const
- {
- return str(attr("tostring")());
- }
-
- void array_base::transpose(object const& axes)
- {
- attr("transpose")(axes);
- }
-
- object array_base::view() const
- {
- return attr("view")();
- }
-}
-
-}}} // namespace boost::python::numeric
diff --git a/libs/python/src/numpy/dtype.cpp b/libs/python/src/numpy/dtype.cpp
index 13904ddde3..88a20a27b5 100644..100755
--- a/libs/python/src/numpy/dtype.cpp
+++ b/libs/python/src/numpy/dtype.cpp
@@ -11,29 +11,33 @@
#include <boost/python/numpy/internal.hpp>
#define DTYPE_FROM_CODE(code) \
- dtype(python::detail::new_reference(reinterpret_cast<PyObject*>(PyArray_DescrFromType(code))))
+ dtype(python::detail::new_reference(reinterpret_cast<PyObject*>(PyArray_DescrFromType(code))))
#define BUILTIN_INT_DTYPE(bits) \
- template <> struct builtin_int_dtype< bits, false > { \
- static dtype get() { return DTYPE_FROM_CODE(NPY_INT ## bits); } \
- }; \
- template <> struct builtin_int_dtype< bits, true > { \
- static dtype get() { return DTYPE_FROM_CODE(NPY_UINT ## bits); } \
- }; \
- template dtype get_int_dtype< bits, false >(); \
- template dtype get_int_dtype< bits, true >()
+ template <> struct builtin_int_dtype<bits, false> \
+ { \
+ static dtype get() { return DTYPE_FROM_CODE(NPY_INT ## bits);} \
+ }; \
+ template <> struct builtin_int_dtype<bits, true> \
+ { \
+ static dtype get() { return DTYPE_FROM_CODE(NPY_UINT ## bits);} \
+ }; \
+ template BOOST_NUMPY_DECL dtype get_int_dtype<bits, false>(); \
+ template BOOST_NUMPY_DECL dtype get_int_dtype<bits, true>()
#define BUILTIN_FLOAT_DTYPE(bits) \
- template <> struct builtin_float_dtype< bits > { \
- static dtype get() { return DTYPE_FROM_CODE(NPY_FLOAT ## bits); } \
- }; \
- template dtype get_float_dtype< bits >()
+ template <> struct builtin_float_dtype<bits> \
+ { \
+ static dtype get() { return DTYPE_FROM_CODE(NPY_FLOAT ## bits);} \
+ }; \
+ template BOOST_NUMPY_DECL dtype get_float_dtype<bits>()
#define BUILTIN_COMPLEX_DTYPE(bits) \
- template <> struct builtin_complex_dtype< bits > { \
- static dtype get() { return DTYPE_FROM_CODE(NPY_COMPLEX ## bits); } \
- }; \
- template dtype get_complex_dtype< bits >()
+ template <> struct builtin_complex_dtype<bits> \
+ { \
+ static dtype get() { return DTYPE_FROM_CODE(NPY_COMPLEX ## bits);} \
+ }; \
+ template BOOST_NUMPY_DECL dtype get_complex_dtype<bits>()
namespace boost { namespace python { namespace converter {
NUMPY_OBJECT_MANAGER_TRAITS_IMPL(PyArrayDescr_Type, numpy::dtype)
@@ -58,7 +62,9 @@ BUILTIN_INT_DTYPE(8);
BUILTIN_INT_DTYPE(16);
BUILTIN_INT_DTYPE(32);
BUILTIN_INT_DTYPE(64);
+#ifdef NPY_FLOAT16
BUILTIN_FLOAT_DTYPE(16);
+#endif
BUILTIN_FLOAT_DTYPE(32);
BUILTIN_FLOAT_DTYPE(64);
BUILTIN_COMPLEX_DTYPE(64);
diff --git a/libs/python/src/numpy/ndarray.cpp b/libs/python/src/numpy/ndarray.cpp
index 710e3d4993..8ae67b89ed 100644
--- a/libs/python/src/numpy/ndarray.cpp
+++ b/libs/python/src/numpy/ndarray.cpp
@@ -22,20 +22,20 @@ namespace detail
ndarray::bitflag numpy_to_bitflag(int const f)
{
ndarray::bitflag r = ndarray::NONE;
- if (f & NPY_C_CONTIGUOUS) r = (r | ndarray::C_CONTIGUOUS);
- if (f & NPY_F_CONTIGUOUS) r = (r | ndarray::F_CONTIGUOUS);
- if (f & NPY_ALIGNED) r = (r | ndarray::ALIGNED);
- if (f & NPY_WRITEABLE) r = (r | ndarray::WRITEABLE);
+ if (f & NPY_ARRAY_C_CONTIGUOUS) r = (r | ndarray::C_CONTIGUOUS);
+ if (f & NPY_ARRAY_F_CONTIGUOUS) r = (r | ndarray::F_CONTIGUOUS);
+ if (f & NPY_ARRAY_ALIGNED) r = (r | ndarray::ALIGNED);
+ if (f & NPY_ARRAY_WRITEABLE) r = (r | ndarray::WRITEABLE);
return r;
}
int bitflag_to_numpy(ndarray::bitflag f)
{
int r = 0;
- if (f & ndarray::C_CONTIGUOUS) r |= NPY_C_CONTIGUOUS;
- if (f & ndarray::F_CONTIGUOUS) r |= NPY_F_CONTIGUOUS;
- if (f & ndarray::ALIGNED) r |= NPY_ALIGNED;
- if (f & ndarray::WRITEABLE) r |= NPY_WRITEABLE;
+ if (f & ndarray::C_CONTIGUOUS) r |= NPY_ARRAY_C_CONTIGUOUS;
+ if (f & ndarray::F_CONTIGUOUS) r |= NPY_ARRAY_F_CONTIGUOUS;
+ if (f & ndarray::ALIGNED) r |= NPY_ARRAY_ALIGNED;
+ if (f & ndarray::WRITEABLE) r |= NPY_ARRAY_WRITEABLE;
return r;
}
@@ -119,10 +119,10 @@ ndarray from_data_impl(void * data,
}
int itemsize = dt.get_itemsize();
int flags = 0;
- if (writeable) flags |= NPY_WRITEABLE;
- if (is_c_contiguous(shape, strides, itemsize)) flags |= NPY_C_CONTIGUOUS;
- if (is_f_contiguous(shape, strides, itemsize)) flags |= NPY_F_CONTIGUOUS;
- if (is_aligned(strides, itemsize)) flags |= NPY_ALIGNED;
+ if (writeable) flags |= NPY_ARRAY_WRITEABLE;
+ if (is_c_contiguous(shape, strides, itemsize)) flags |= NPY_ARRAY_C_CONTIGUOUS;
+ if (is_f_contiguous(shape, strides, itemsize)) flags |= NPY_ARRAY_F_CONTIGUOUS;
+ if (is_aligned(strides, itemsize)) flags |= NPY_ARRAY_ALIGNED;
ndarray r(python::detail::new_reference
(PyArray_NewFromDescr(&PyArray_Type,
incref_dtype(dt),
@@ -243,13 +243,13 @@ ndarray empty(int nd, Py_intptr_t const * shape, dtype const & dt)
ndarray array(python::object const & obj)
{
return ndarray(python::detail::new_reference
- (PyArray_FromAny(obj.ptr(), NULL, 0, 0, NPY_ENSUREARRAY, NULL)));
+ (PyArray_FromAny(obj.ptr(), NULL, 0, 0, NPY_ARRAY_ENSUREARRAY, NULL)));
}
ndarray array(python::object const & obj, dtype const & dt)
{
return ndarray(python::detail::new_reference
- (PyArray_FromAny(obj.ptr(), detail::incref_dtype(dt), 0, 0, NPY_ENSUREARRAY, NULL)));
+ (PyArray_FromAny(obj.ptr(), detail::incref_dtype(dt), 0, 0, NPY_ARRAY_ENSUREARRAY, NULL)));
}
ndarray from_object(python::object const & obj, dtype const & dt, int nd_min, int nd_max, ndarray::bitflag flags)