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

Customization for wrapping class members #4

Open
cnyeh opened this issue Oct 23, 2024 · 0 comments
Open

Customization for wrapping class members #4

cnyeh opened this issue Oct 23, 2024 · 0 comments
Assignees

Comments

@cnyeh
Copy link

cnyeh commented Oct 23, 2024

Summary

Currently, both C2PY_IGNORE/C2PY_WRAP and match_names/reject_names don't seem to allow users to customize the wrapping of individual members within a class. It would be very helpful to have the ability to selectively wrap specific members of a C++ class when exposing it to Python.

For example, in the following MF class, bound to my_module:

// mf.cpp
#include <c2py/c2py.hpp>

struct MF {
  MF(int _nkpts, int _nsin):
    nkpts(_nkpts), nspin(_nspin) {}
    
  int nkpts;
  C2PY_IGNORE int nspin;
};
# CMakeLists.txt
cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
project(mpi_context VERSION 3.2.0 LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 20)

# Find Python, c2py and clair
find_package(Python COMPONENTS Interpreter Development NumPy)
find_package(c2py REQUIRED)
find_package(clair REQUIRED)

Python_add_library(my_module MODULE mf.cpp)

# Link to low level c2py and add the -fplugin option using a cmake target. 
target_link_libraries(my_module PRIVATE c2py::c2py clair::c2py_plugin)

I would like to expose only nkpts in Python, while keeping nspin hidden:

# text.py
import my_module

my_mf = my_module.MF(10, 2)

print(my_mf.nkpts)  # 10
print(my_mf.nspin)  # Current: 2
                    # Desired: AttributeError: 'my_module.MF' object has no attribute 'nspin'

Motivation

There are many cases where binding a class but selectively exposing only certain members to Python is desired. This is particularly useful when a member, such as nspin, is not a standard type, allowing users to avoid the need to write a customized py_converter for it.

@parcollet parcollet self-assigned this Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants