Commit 0d4fbcba authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Merge branch 'python' into 'develop'

Python bindings

See merge request mmg/tnl-dev!2
parents df30f370 fe668686
Loading
Loading
Loading
Loading
+29 −1
Original line number Diff line number Diff line
find_package( PythonInterp 3 )
find_package( PythonLibs 3 )

set( PYTHON_SITE_PACKAGES_DIR lib/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}/site-packages )

if( PYTHONINTERP_FOUND )
   CONFIGURE_FILE( "__init__.py.in" "${PROJECT_BUILD_PATH}/Python/__init__.py" )
   INSTALL( FILES ${PROJECT_BUILD_PATH}/Python/__init__.py
                  LogParser.py
            DESTINATION lib/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}/site-packages/TNL )
            DESTINATION ${PYTHON_SITE_PACKAGES_DIR}/TNL )
endif()

if( PYTHONLIBS_FOUND )
   # download and build pybind11 at configure time
   configure_file(pybind11.cmake.in ${CMAKE_BINARY_DIR}/pybind11-download/CMakeLists.txt)
   execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
      RESULT_VARIABLE result
      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/pybind11-download )
   if(result)
      message(FATAL_ERROR "CMake step for pybind11 failed: ${result}")
   endif()
   execute_process(COMMAND ${CMAKE_COMMAND} --build .
      RESULT_VARIABLE result
      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/pybind11-download )
   if(result)
      message(FATAL_ERROR "Build step for pybind11 failed: ${result}")
   endif()

   # add the pybind11 subdirectory to provide the pybind11_add_module macro
   add_subdirectory(${CMAKE_BINARY_DIR}/pybind11-src ${CMAKE_BINARY_DIR}/pybind11-build)

   # add the subdirectory with our bindings
   add_subdirectory(pytnl)
else()
   message( "The Python.h header file was not found, Python bindings will not be builg." )
endif()
+12 −0
Original line number Diff line number Diff line
cmake_minimum_required(VERSION 2.8.2)

project(pybind11-download)
include(ExternalProject)

ExternalProject_Add(pybind11
  GIT_REPOSITORY    https://github.com/pybind/pybind11.git
  GIT_TAG           master
  SOURCE_DIR        "${CMAKE_BINARY_DIR}/pybind11-src"
  BINARY_DIR        "${CMAKE_BINARY_DIR}/pybind11-build"
  CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} -DPYBIND11_TEST=FALSE
)
+13 −0
Original line number Diff line number Diff line
add_subdirectory( tnl )

set( headers
         exceptions.h
         RawIterator.h
         tnl_conversions.h
         tnl_indexing.h
         tnl_str_conversion.h
         tnl_tuple_conversion.h
         typedefs.h
)

install( FILES ${headers} DESTINATION "pytnl" )
+51 −0
Original line number Diff line number Diff line
#pragma once

#include <iterator>

template< typename DataType >
class RawIterator : public std::iterator<std::random_access_iterator_tag,
                                           DataType,
                                           ptrdiff_t,
                                           DataType*,
                                           DataType&>
{
protected:
    DataType*               m_ptr;

public:
    RawIterator( DataType* ptr = nullptr ) { m_ptr = ptr; }
    RawIterator( const RawIterator<DataType> & rawIterator ) = default;
    ~RawIterator(){}

    RawIterator<DataType>&  operator=( const RawIterator<DataType> & rawIterator ) = default;
    RawIterator<DataType>&  operator=( DataType* ptr ) { m_ptr = ptr; return (*this); }

    operator                bool() const
    {
        if(m_ptr)
            return true;
        else
            return false;
    }

    bool                    operator==( const RawIterator<DataType> & rawIterator ) const { return ( m_ptr == rawIterator.getConstPtr() ); }
    bool                    operator!=( const RawIterator<DataType> & rawIterator ) const { return ( m_ptr != rawIterator.getConstPtr() ); }

    RawIterator<DataType>&  operator+=( const ptrdiff_t & movement ){ m_ptr += movement; return (*this); }
    RawIterator<DataType>&  operator-=( const ptrdiff_t & movement ){ m_ptr -= movement; return (*this); }
    RawIterator<DataType>&  operator++() { ++m_ptr; return (*this); }
    RawIterator<DataType>&  operator--() { --m_ptr; return (*this); }
    RawIterator<DataType>   operator++( int ) { auto temp(*this); ++m_ptr; return temp; }
    RawIterator<DataType>   operator--( int ) { auto temp(*this); --m_ptr; return temp; }
    RawIterator<DataType>   operator+( const ptrdiff_t & movement ) { auto oldPtr = m_ptr; m_ptr+=movement; auto temp(*this); m_ptr = oldPtr; return temp; }
    RawIterator<DataType>   operator-( const ptrdiff_t & movement ) { auto oldPtr = m_ptr; m_ptr-=movement; auto temp(*this); m_ptr = oldPtr; return temp; }

    ptrdiff_t               operator-( const RawIterator<DataType>& rawIterator ) { return std::distance(rawIterator.getPtr(), this->getPtr()); }

    DataType&               operator*() { return *m_ptr; }
    const DataType&         operator*() const { return *m_ptr; }
    DataType*               operator->() { return m_ptr; }

    DataType*               getPtr() const { return m_ptr; }
    const DataType*         getConstPtr() const { return m_ptr; }
};
+35 −0
Original line number Diff line number Diff line
#pragma once

#include <stdexcept>

#include <pybind11/pybind11.h>
namespace py = pybind11;

#include <TNL/Assert.h>

struct NotImplementedError
   : public std::runtime_error
{
   NotImplementedError( const char* msg )
   : std::runtime_error( msg )
   {}
};

static void register_exceptions( py::module & m )
{
    py::register_exception_translator(
        [](std::exception_ptr p) {
            try {
                if (p) std::rethrow_exception(p);
            }
            // translate exceptions used in the bindings
            catch (const NotImplementedError & e) {
                PyErr_SetString(PyExc_NotImplementedError, e.what());
            }
            // translate TNL::Assert::AssertionError
            catch (const TNL::Assert::AssertionError & e) {
                PyErr_SetString(PyExc_AssertionError, e.what());
            }
        }
    );
}
Loading