Browse Source

python: rename Python struct format helper to avoid clashes.

next
Vladimír Vondruš 3 years ago
parent
commit
173a01f1b6
  1. 26
      src/Corrade/Containers/StridedArrayViewPythonBindings.h
  2. 2
      src/python/corrade/containers.cpp
  3. 4
      src/python/corrade/test/test_stridedarrayview.cpp

26
src/Corrade/Containers/StridedArrayViewPythonBindings.h

@ -34,26 +34,26 @@ namespace Implementation {
/* For maintainability please keep in the same order as /* For maintainability please keep in the same order as
https://docs.python.org/3/library/struct.html#format-characters */ https://docs.python.org/3/library/struct.html#format-characters */
template<class T> constexpr const char* formatString() { template<class T> constexpr const char* pythonFormatString() {
static_assert(sizeof(T) == 0, "format string unknown for this type, supply it explicitly"); static_assert(sizeof(T) == 0, "format string unknown for this type, supply it explicitly");
return {}; return {};
} }
/* Representing bytes as unsigned. Not using 'c' because then it behaves /* Representing bytes as unsigned. Not using 'c' because then it behaves
differently from bytes/bytearray, where you can do `a[0] = ord('A')`. */ differently from bytes/bytearray, where you can do `a[0] = ord('A')`. */
template<> constexpr const char* formatString<char>() { return "B"; } template<> constexpr const char* pythonFormatString<char>() { return "B"; }
template<> constexpr const char* formatString<std::int8_t>() { return "b"; } template<> constexpr const char* pythonFormatString<std::int8_t>() { return "b"; }
template<> constexpr const char* formatString<std::uint8_t>() { return "B"; } template<> constexpr const char* pythonFormatString<std::uint8_t>() { return "B"; }
template<> constexpr const char* formatString<std::int16_t>() { return "h"; } template<> constexpr const char* pythonFormatString<std::int16_t>() { return "h"; }
template<> constexpr const char* formatString<std::uint16_t>() { return "H"; } template<> constexpr const char* pythonFormatString<std::uint16_t>() { return "H"; }
template<> constexpr const char* formatString<std::int32_t>() { return "i"; } template<> constexpr const char* pythonFormatString<std::int32_t>() { return "i"; }
template<> constexpr const char* formatString<std::uint32_t>() { return "I"; } template<> constexpr const char* pythonFormatString<std::uint32_t>() { return "I"; }
/* *not* l / L, that's 4 bytes in Python */ /* *not* l / L, that's 4 bytes in Python */
template<> constexpr const char* formatString<std::int64_t>() { return "q"; } template<> constexpr const char* pythonFormatString<std::int64_t>() { return "q"; }
template<> constexpr const char* formatString<std::uint64_t>() { return "Q"; } template<> constexpr const char* pythonFormatString<std::uint64_t>() { return "Q"; }
/** @todo how to represent std::size_t? conflicts with uint32_t/uint64_t above */ /** @todo how to represent std::size_t? conflicts with uint32_t/uint64_t above */
/** @todo half? take from Magnum? */ /** @todo half? take from Magnum? */
template<> constexpr const char* formatString<float>() { return "f"; } template<> constexpr const char* pythonFormatString<float>() { return "f"; }
template<> constexpr const char* formatString<double>() { return "d"; } template<> constexpr const char* pythonFormatString<double>() { return "d"; }
template<class T, class U> struct PyStridedArrayViewSetItem; template<class T, class U> struct PyStridedArrayViewSetItem;
template<class U> struct PyStridedArrayViewSetItem<const char, U> { template<class U> struct PyStridedArrayViewSetItem<const char, U> {
@ -77,7 +77,7 @@ template<unsigned dimensions, class T> class PyStridedArrayView: public StridedA
format, choosing bytes for safety. */ format, choosing bytes for safety. */
/*implicit*/ PyStridedArrayView(): format{"B"}, getitem{} {} /*implicit*/ PyStridedArrayView(): format{"B"}, getitem{} {}
template<class U> explicit PyStridedArrayView(const StridedArrayView<dimensions, U>& view): PyStridedArrayView{view, Implementation::formatString<typename std::decay<U>::type>(), sizeof(U)} {} template<class U> explicit PyStridedArrayView(const StridedArrayView<dimensions, U>& view): PyStridedArrayView{view, Implementation::pythonFormatString<typename std::decay<U>::type>(), sizeof(U)} {}
template<class U> explicit PyStridedArrayView(const StridedArrayView<dimensions, U>& view, const char* format, std::size_t itemsize): PyStridedArrayView<dimensions, T>{ template<class U> explicit PyStridedArrayView(const StridedArrayView<dimensions, U>& view, const char* format, std::size_t itemsize): PyStridedArrayView<dimensions, T>{
arrayCast<T>(view), arrayCast<T>(view),

2
src/python/corrade/containers.cpp

@ -77,7 +77,7 @@ template<class T> bool arrayViewBufferProtocol(T& self, Py_buffer& buffer, int f
buffer.buf = const_cast<typename std::decay<typename T::Type>::type*>(self.data()); buffer.buf = const_cast<typename std::decay<typename T::Type>::type*>(self.data());
buffer.readonly = std::is_const<typename T::Type>::value; buffer.readonly = std::is_const<typename T::Type>::value;
if((flags & PyBUF_FORMAT) == PyBUF_FORMAT) if((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
buffer.format = const_cast<char*>(Containers::Implementation::formatString<typename std::decay<typename T::Type>::type>()); buffer.format = const_cast<char*>(Containers::Implementation::pythonFormatString<typename std::decay<typename T::Type>::type>());
if(flags != PyBUF_SIMPLE) { if(flags != PyBUF_SIMPLE) {
/* The view is immutable (can't change its size after it has been /* The view is immutable (can't change its size after it has been
constructed), so referencing the size directly is okay */ constructed), so referencing the size directly is okay */

4
src/python/corrade/test/test_stridedarrayview.cpp

@ -32,10 +32,10 @@
#include "Corrade/Containers/StridedArrayViewPythonBindings.h" #include "Corrade/Containers/StridedArrayViewPythonBindings.h"
namespace Corrade { namespace Containers { namespace Implementation { namespace Corrade { namespace Containers { namespace Implementation {
template<> constexpr const char* formatString<std::array<double, 3>>() { template<> constexpr const char* pythonFormatString<std::array<double, 3>>() {
return "ddd"; return "ddd";
} }
template<> constexpr const char* formatString<std::pair<std::uint64_t, float>>() { template<> constexpr const char* pythonFormatString<std::pair<std::uint64_t, float>>() {
return "Qf"; return "Qf";
} }
}}} }}}

Loading…
Cancel
Save