18 Commits

Author SHA1 Message Date
3335e0471b Return None instead of KeyError for failure of BpfMap::lookup for missing key 2025-11-24 23:51:08 +05:30
ffc5a9f15f Add .so and .pyd wildcards to setuptools package-data in pyproject.toml 2025-11-11 17:31:09 +05:30
503e6da987 Janitorial formatting 2025-11-11 17:28:30 +05:30
8585f170cb Remove debug prints from test, fix name of Pip workflow 2025-11-11 17:21:30 +05:30
0780615fc5 Config setuptools to get library location 2025-11-11 17:12:31 +05:30
c26617d64f Overhaul pip.yml 2025-11-11 16:57:30 +05:30
303fe4e6c8 Make wheels workflow test oneline 2025-11-11 16:35:26 +05:30
9c18f5967d Fix more 2025-11-11 16:28:15 +05:30
434f145f9b Add better test to wheels workflow 2025-11-11 16:20:51 +05:30
b3e9410e0d Fix wheels workflow 2025-11-11 10:51:13 +05:30
fd739e87c1 Don't run pytest in isolated mode in pip workflow 2025-11-11 10:37:28 +05:30
87c698e940 Print pylibbpf dir in test 2025-11-11 10:29:48 +05:30
298df7ede6 Update workflows to treat Python 3.12 as minimum version 2025-11-11 10:23:58 +05:30
b48f6a8a97 Bump minimum python version to 3.12 2025-11-11 10:22:06 +05:30
6e4dc0f5aa Bump setuptools version in setup.py to use new licensing format 2025-11-11 10:13:41 +05:30
a51bed14ca Fix LICENSE and metadata in pyproject.toml 2025-11-11 10:04:13 +05:30
3a85a6446e Add Python discovery to CMakeLists.txt 2025-11-11 10:03:48 +05:30
6bc378defd Remove unnecessary metadata, fix CMake flag in setup.py 2025-11-11 10:02:13 +05:30
8 changed files with 113 additions and 86 deletions

View File

@ -1,29 +1,38 @@
name: Pip
on:
workflow_dispatch:
pull_request:
push:
branches:
- master
- main
branches: [main, master]
pull_request:
branches: [main, master]
workflow_dispatch:
jobs:
build:
test:
name: Test on Python ${{ matrix.python-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
platform: [ubuntu-latest]
python-version: ["3.8", "3.12"]
runs-on: ${{ matrix.platform }}
python-version: ['3.12', '3.13']
steps:
- uses: actions/checkout@v5
- name: Checkout code
uses: actions/checkout@v5
with:
submodules: true
submodules: recursive
- uses: actions/setup-python@v6
- name: Verify submodules
run: |
echo "Checking submodule status:"
git submodule status
echo "Checking libbpf directory:"
ls -la libbpf/
echo "Checking libbpf/src:"
ls -la libbpf/src/ || echo "libbpf/src not found!"
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
@ -33,19 +42,72 @@ jobs:
sudo apt-get install -y \
libbpf-dev \
libelf-dev \
linux-headers-generic \
zlib1g-dev \
build-essential \
clang \
cmake \
ninja-build
ninja-build \
pkg-config \
git \
make
- name: Add requirements
run: python -m pip install --upgrade pip wheel setuptools
- name: Install Python build dependencies
run: |
python -m pip install --upgrade pip
pip install --upgrade "setuptools>=77.0.0" wheel
pip install cmake ninja pybind11
- name: Build and install
run: pip install --verbose .[test]
- name: Check build requirements
run: |
echo "Python version:"
python --version
echo "CMake version:"
cmake --version
echo "Ninja version:"
ninja --version
echo "Setuptools version:"
python -c "import setuptools; print(setuptools.__version__)"
- name: Test import
run: python -I -c "import pylibbpf; print('Import successful')"
- name: Build and install pylibbpf (verbose)
run: |
pip install -v -e . 2>&1 | tee build.log
continue-on-error: false
- name: Test
run: python -I -m pytest -v
- name: Check build output
run: |
echo "Build directory contents:"
find build -type f -name "*.so" 2>/dev/null || echo "No .so files found in build/"
echo ""
echo "Looking for pylibbpf extension:"
find . -name "pylibbpf*.so" -o -name "pylibbpf*.pyd"
echo ""
echo "Site-packages contents:"
python -c "import site; print(site.getsitepackages())"
ls -la $(python -c "import site; print(site.getsitepackages()[0])")/pylibbpf/ || echo "pylibbpf not in site-packages"
- name: Try importing extension directly
run: |
python -c "
import sys
print('Python path:', sys.path)
try:
from pylibbpf import pylibbpf
print('Successfully imported pylibbpf.pylibbpf')
print('pylibbpf.pylibbpf members:', dir(pylibbpf))
except ImportError as e:
print(f'Failed to import pylibbpf.pylibbpf: {e}')
"
- name: Verify extension loaded
run: |
python -c "import pylibbpf; print('Members:', dir(pylibbpf)); assert hasattr(pylibbpf, 'BpfObject'), 'BpfObject not found!'; print('✓ OK')"
- name: Install test dependencies
if: success()
run: |
pip install pytest pytest-cov
- name: Run tests
if: success()
run: |
python -I -m pytest -v -s --cov=pylibbpf --cov-report=term-missing

View File

@ -17,7 +17,7 @@ jobs:
steps:
- uses: actions/checkout@v5
with:
submodules: true
submodules: recursive
- name: Build SDist
run: pipx run build --sdist
@ -41,22 +41,24 @@ jobs:
steps:
- uses: actions/checkout@v5
with:
submodules: true
submodules: recursive
- name: Build wheels
uses: pypa/cibuildwheel@v3.2
env:
CIBW_PLATFORM: linux
CIBW_ARCHS_LINUX: ${{ matrix.arch }}
CIBW_BUILD: "cp38-* cp311-*"
CIBW_BUILD: "cp312-* cp313-*"
CIBW_SKIP: "*-musllinux*"
CIBW_TEST_COMMAND: "python -c 'import pylibbpf; print(f\"pylibbpf {pylibbpf.__version__} imported successfully\")'"
CIBW_TEST_COMMAND: "python -c 'import pylibbpf; assert hasattr(pylibbpf, \"BpfObject\"), \"BpfObject not found\"; print(f\"pylibbpf {pylibbpf.__version__} OK\")'"
CIBW_TEST_SKIP: "*-linux_aarch64"
CIBW_MANYLINUX_X86_64_IMAGE: manylinux_2_28
CIBW_BEFORE_ALL_LINUX: |
dnf install -y elfutils-libelf-devel zlib-devel
dnf install -y elfutils-libelf-devel zlib-devel make gcc gcc-c++ git
CIBW_BEFORE_BUILD: >
pip install --upgrade "setuptools>=77.0.0" wheel cmake ninja pybind11
- name: Verify clean directory
run: git diff --exit-code

View File

@ -5,6 +5,11 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
find_package(
Python
COMPONENTS Interpreter Development.Module
REQUIRED)
# pybind11
include_directories(${CMAKE_SOURCE_DIR}/src)
add_subdirectory(pybind11)

View File

@ -1,6 +1,5 @@
import ctypes
import logging
from typing import Dict, Type
from llvmlite import ir
@ -51,7 +50,7 @@ def _make_repr(struct_name: str, fields: list):
return __repr__
def convert_structs_to_ctypes(structs_sym_tab) -> Dict[str, Type[ctypes.Structure]]:
def convert_structs_to_ctypes(structs_sym_tab) -> dict[str, type[ctypes.Structure]]:
"""Convert PythonBPF's structs_sym_tab to ctypes.Structure classes."""
if not structs_sym_tab:
return {}

View File

@ -1,4 +1,4 @@
from typing import Callable, Optional
from collections.abc import Callable
class PerfEventArrayHelper:
@ -13,7 +13,7 @@ class PerfEventArrayHelper:
callback: Callable,
struct_name: str = "",
page_cnt: int = 8,
lost_callback: Optional[Callable] = None,
lost_callback: Callable | None = None,
):
"""Open perf buffer with auto-deserialization."""
from .pylibbpf import PerfEventArray

View File

@ -1,6 +1,6 @@
[build-system]
requires = [
"setuptools>=42",
"setuptools>=77",
"wheel",
"ninja",
"cmake>=4.0",
@ -17,8 +17,8 @@ authors = [
{ name = "varun-r-mallya", email = "varunrmallya@gmail.com" }
]
readme = "README.md"
license = { file = "LICENSE" }
requires-python = ">=3.8"
license = "Apache-2.0"
requires-python = ">=3.12"
classifiers = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
@ -29,6 +29,7 @@ classifiers = [
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: C++",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: System :: Operating System Kernels :: Linux",
@ -47,7 +48,7 @@ Issues = "https://github.com/pythonbpf/pylibbpf/issues"
[tool.mypy]
files = "setup.py"
python_version = "3.8"
python_version = "3.12"
strict = true
show_error_codes = true
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
@ -68,7 +69,7 @@ filterwarnings = [
testpaths = ["tests"]
[tool.ruff]
target-version = "py38"
target-version = "py312"
line-length = 88
[tool.ruff.lint]
@ -79,3 +80,7 @@ extend-select = [
"RUF", # Ruff-specific
"UP", # pyupgrade
]
[tool.setuptools]
packages = ["pylibbpf"]
package-data = {"pylibbpf" = ["*.py", "*.so", "*.pyd", "py.typed"]}

View File

@ -3,17 +3,9 @@ import subprocess
import sys
from pathlib import Path
from setuptools import Extension, find_packages, setup
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
# Convert distutils Windows platform specifiers to CMake -A arguments
PLAT_TO_CMAKE = {
"win32": "Win32",
"win-amd64": "x64",
"win-arm32": "ARM",
"win-arm64": "ARM64",
}
# A CMakeExtension needs a sourcedir instead of a file list.
# The name must be the _single_ output extension from the CMake build.
@ -45,8 +37,8 @@ class CMakeBuild(build_ext):
# from Python.
cmake_args = [
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}",
f"-DPYTHON_EXECUTABLE={sys.executable}",
f"-DCMAKE_BUILD_TYPE={cfg}", # not used on MSVC, but no harm
f"-DPython_EXECUTABLE={sys.executable}",
]
build_args = []
@ -122,44 +114,6 @@ if readme_path.exists():
long_description = readme_path.read_text(encoding="utf-8")
setup(
name="pylibbpf",
version="0.0.1",
author="varun-r-mallya, r41k0u",
author_email="varunrmallyagmail.com",
description="Python Bindings for Libbpf",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/pythonbpf/pylibbpf",
packages=find_packages(where="."),
package_dir={"": "."},
py_modules=[], # Empty since we use packages
ext_modules=[CMakeExtension("pylibbpf.pylibbpf")],
cmdclass={"build_ext": CMakeBuild},
zip_safe=False,
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Operating System :: POSIX :: Linux",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: C++",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: System :: Operating System Kernels :: Linux",
],
install_requires=[
"llvmlite>=0.40.0", # Required for struct conversion
],
extras_require={"test": ["pytest>=6.0"]},
python_requires=">=3.8",
package_data={
"pylibbpf": [
"*.py",
"py.typed", # For type hints
],
},
include_package_data=True,
)

View File

@ -42,7 +42,7 @@ py::object BpfMap::lookup(const py::object &key) const {
value_span.data(), value_size_, BPF_ANY);
if (ret < 0) {
if (ret == -ENOENT)
throw py::key_error("Key not found in map '" + map_name_ + "'");
return py::none();
throw BpfException("Failed to lookup key in map '" + map_name_ +
"': " + std::strerror(-ret));
}