Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Python buffer protocol. #238

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion uhal/python/src/common/cactus_pycohal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ PYBIND11_MODULE(_core, m)
;

// Wrap uhal::ValVector<uint32_t>
py::class_<uhal::ValVector<uint32_t>>(m, "ValVector_uint32")
py::class_<uhal::ValVector<uint32_t>>(m, "ValVector_uint32", py::buffer_protocol())
.def ( py::init<>() )
.def ( py::init< const uhal::ValVector<uint32_t>& >() )
.def ( "valid", static_cast< bool ( uhal::ValVector<uint32_t>::* ) () > ( &uhal::ValVector<uint32_t>::valid ) )
Expand All @@ -272,6 +272,13 @@ PYBIND11_MODULE(_core, m)
.def ( "__getitem__", &pycohal::ValVectorIndexingSuite<uint32_t>::getItem , pycohal::const_ref_return_policy )
.def ( "__getitem__", &pycohal::ValVectorIndexingSuite<uint32_t>::getSlice )
.def ( "__iter__", [](const uhal::ValVector<uint32_t>& v) { return py::make_iterator ( v.begin() , v.end() ); })
.def_buffer([](const uhal::ValVector<uint32_t> &v) -> py::buffer_info {
return py::buffer_info(
v.data(), /* Pointer to buffer */
v.size(), /* Buffer size */
true /* Read-only */
);
})
;

// Wrap uhal::Node
Expand Down
6 changes: 6 additions & 0 deletions uhal/uhal/include/uhal/ValMem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,12 @@ namespace uhal
*/
std::size_t size() const;

/**
Return the address of the underlying memory
@return the address of the underlying memory
*/
T* data() const;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the return type to const T* (to match the return type constness of the std::vector data() const method)


//! Clear the underlying memory and set Validity to false
void clear();

Expand Down
7 changes: 7 additions & 0 deletions uhal/uhal/src/common/ValMem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,13 @@ namespace uhal
}


template< typename T >
T* ValVector< T >::data() const
{
return mMembers->value.data();
Copy link
Collaborator

@tswilliams tswilliams Mar 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change this implementation to:

    if ( mMembers->valid )
    {
      return mMembers->value.data();
    }
    else
    {
      exception::NonValidatedMemory lExc;
      log ( lExc , "Access attempted on non-validated memory" );
      throw lExc;
    }

}


template< typename T >
void ValVector< T >::clear()
{
Expand Down