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

Incompatible with nanobind 2.2 on Py3.12 #789

Closed
inducer opened this issue Oct 3, 2024 · 2 comments · Fixed by #791
Closed

Incompatible with nanobind 2.2 on Py3.12 #789

inducer opened this issue Oct 3, 2024 · 2 comments · Fixed by #791
Labels

Comments

@inducer
Copy link
Owner

inducer commented Oct 3, 2024

As pointed out by @matthiasdiener in #788.

Example:

Traceback (most recent call last):
  File "/home/andreas/src/pyopencl/examples/demo.py", line 29, in <module>
    knl = prg.sum  # Use this Kernel object for repeated calls
          ^^^^^^^
  File "/home/andreas/src/env-3.12/lib/python3.12/site-packages/pyopencl/__init__.py", line 443, in __getattr__
    knl = Kernel(self, attr)
          ^^^^^^^^^^^^^^^^^^
TypeError: __init__(): incompatible function arguments. The following argument types are supported:
    1. __init__(self, arg0: pyopencl._cl._Program, arg1: str, /) -> None

Invoked with types: pyopencl._cl.Kernel, pyopencl.Program, str

We'll probably need to avoid overriding __init__ the way we have. The easiest way to do this may be to just port the existing constructors to C++.

@inducer inducer added the bug label Oct 3, 2024
@matthiasdiener
Copy link
Contributor

matthiasdiener commented Oct 4, 2024

Unfortunately, a simple reproducer does not appear to show the same issue:

cmake_minimum_required(VERSION 3.15...3.27)
project(my_project) # Replace 'my_project' with the name of your project

if (CMAKE_VERSION VERSION_LESS 3.18)
  set(DEV_MODULE Development)
else()
  set(DEV_MODULE Development.Module)
endif()

find_package(Python 3.8 COMPONENTS Interpreter ${DEV_MODULE} REQUIRED)

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
    set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()

execute_process(
    COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
    OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE nanobind_ROOT)
find_package(nanobind CONFIG REQUIRED)

nanobind_add_module(my_ext my_ext.cpp)
#include <nanobind/nanobind.h>
#include <iostream>

struct MyType {
    MyType() {}};

NB_MODULE(my_ext, m) {
    nanobind::class_<MyType>(m, "MyType", nanobind::dynamic_attr())
        .def("__init__",
                [](MyType *t, int bar)
                {
                    std::cout << "C++ __init__ " << bar << std::endl;
                    new (t) MyType();
                });
}
from my_ext import MyType

def my_init(self, s:str):  # different arg type compared to C++
    print("python __init__", s)

MyType.__init__ = my_init

MyType(42)
MyType("foo")
$ python t.py
python __init__ 42
python __init__ foo

Edit: Turns out this is specific to Python 3.12; with that version, the reproducer above shows the issue.

@alexfikl

This comment was marked as outdated.

@inducer inducer mentioned this issue Oct 7, 2024
2 tasks
@inducer inducer changed the title Incompatible with nanobind 2.2 Incompatible with nanobind 2.2 on Py3.12 Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants