Skip to content

Commit

Permalink
Revert "Revert "example: Add example for pybind11" (#444)"
Browse files Browse the repository at this point in the history
This reverts commit aae7c24.
  • Loading branch information
alexeagle authored Nov 20, 2024
1 parent aae7c24 commit 227ca90
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,5 @@ crate.from_cargo(
)

use_repo(crate, "crate_index")

bazel_dep(name = "pybind11_bazel", version = "2.12.0", dev_dependency = True)
19 changes: 19 additions & 0 deletions examples/pybind11/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@aspect_rules_py//py:defs.bzl", "py_library", "py_test")
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")

pybind_extension(
name = "basic",
srcs = ["basic.cpp"],
)

py_library(
name = "basic_lib",
data = [":basic"],
imports = ["."],
)

py_test(
name = "basic_test",
srcs = ["basic_test.py"],
deps = [":basic_lib"],
)
10 changes: 10 additions & 0 deletions examples/pybind11/basic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <pybind11/pybind11.h>

int add(int i, int j) {
return i + j;
}

PYBIND11_MODULE(basic, module) {
module.doc() = "A basic pybind11 extension";
module.def("add", &add, "A function that adds two numbers");
}
14 changes: 14 additions & 0 deletions examples/pybind11/basic_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import unittest

import basic


class TestBasic(unittest.TestCase):

def test_add(self):
self.assertEqual(basic.add(1, 2), 3)
self.assertEqual(basic.add(2, 2), 4)


if __name__ == "__main__":
unittest.main()

0 comments on commit 227ca90

Please sign in to comment.