Contributing to SharpBind
Use .NET 10, conventional 64-bit CPython 3.11 or higher, and Native AOT build tools for your target OS. Consumer binding projects support .NET 9.0 or higher.
Build and test
From the repository root:
python -m pip install hatchling twine setuptools numpy
dotnet test tests/SharpBind.Tests/SharpBind.Tests.csproj --filter "TestCategory!=BuildIntegration"
python tests/package_smoke.py
The default smoke test builds the runtime wheel and packages the source fixture through the MSBuild target, then runs the integration tests and examples against an isolated installation with .NET absent from PATH.
The managed filter skips tests that launch the generator or MSBuild in another process. Run the unfiltered command when changing CLI diagnostics or build targets.
After a successful smoke test, inspect the generated wrappers in artifacts/fixtures/sharpbind_fixture/_bindings.py and _bindings.pyi.
These files contain the last successfully tested output.
The completed run writes artifacts/latest-build.json with the exact wheel and workspace paths.
Test the same build on additional interpreters with:
python tests/python_matrix.py --python /path/to/python3.11 --python /path/to/python3.12
Packaging unit tests use source build helpers and need no installed fixture or runtime. Run:
python -m unittest discover -s tests -p "test_*.py" -v
Installed-package tests live in tests/integration, grouped by API behavior.
Prefer tests with one behavioral purpose, descriptive input names, and subTest for tables of cases.
Assert observable behavior rather than internal aliases or generated formatting.
Keep memory ownership and interop checks when refactoring.
Build local packages
Use this check when changing NuGet contents, templates, MSBuild integration, or Python packaging, or when you need packages to try in another project:
dotnet test tests/SharpBind.Tests/SharpBind.Tests.csproj
python tests/package_smoke.py --full
The unfiltered managed command includes tests that launch the generator and MSBuild.
The --full smoke test then:
- Packs
src/SharpBind/SharpBind.csprojinto a NuGet package containing the library, generator, build helpers, and Python project template. - Installs that template in an isolated template directory, creates a consumer, and restores SharpBind from the local NuGet package.
- Builds a fixture wheel, removes a temporary export, and rebuilds to check that deleted exports disappear and handwritten Python sources survive.
- Builds a second independently named package, installs both fixture wheels offline in a fresh virtual environment, and checks native ownership and package isolation with .NET absent from PATH.
Find the packages
After the command succeeds, inspect the manifest again:
python -m json.tool artifacts/latest-build.json
Check that full is true. In the paths below, replace <workspace> with its workspace value and <wheels> with its wheels value.
| Location | Contents or purpose |
|---|---|
<workspace>/packages/SharpBind.<version>.nupkg |
NuGet library, generator, build helpers, and template |
<wheels>/sharpbind_fixture-*.whl |
Generated bindings, private support, CPython adapter, and Native AOT fixture |
<wheels>/sharpbind_peer-*.whl |
Independently bundled package for isolation and array interchange tests |
<wheels>/sharpbind_scalar-*.whl |
Scalar-only package tested without any Python dependencies |
<workspace>/NuGet.Config |
Routes SharpBind restores to packages and uses a workspace-local NuGet cache |
<workspace>/template-hive |
Template installation created by the test |
<workspace>/Managed and <workspace>/python |
Managed library and generated Python project used by the test |
<workspace>/consumer-test |
Virtual environment and integration tests used to verify installation |
These outputs are retained after the test.
Ordinary --full runs put wheels in <workspace>/wheels and skip platform repair.
For release checks, install abi3audit and the platform repair tools,
then run python tests/package_smoke.py --audit. This includes the full package test,
repairs the wheels, and audits every consumer wheel with abi3audit --strict before testing installation.
Try the local packages in a new project
Create your trial project inside the reported workspace so its NuGet.Config selects the freshly built SharpBind package:
cd "<workspace>"
dotnet new classlib -n Example
dotnet add Example/Example.csproj package SharpBind --version <version>
Add the documented Counter class from Create the C# API to Example/Counter.cs.
In Example/Example.csproj, enable XML documentation inside its PropertyGroup:
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Create the Python project using the template installation from this build, then build a local wheel.
Run these commands while still in <workspace>:
dotnet new sharpbind-python -n ExampleNative -o example-python --module example_lib --library ../../Example/Example.csproj --debug:custom-hive "<workspace>/template-hive"
dotnet msbuild example-python/native/ExampleNative.csproj -restore -t:SharpBindPackage -p:Configuration=Release -p:SharpBindLocalWheel=true
The --library path is relative to the generated example-python/native directory.
--debug:custom-hive selects the isolated template installed by --full.
SharpBindLocalWheel=true skips platform repair.
The resulting wheel is in example-python/dist.
Install your generated wheel. Its implementation is self-contained; numerical APIs declare NumPy automatically:
python -m pip install --force-reinstall --find-links example-python/dist "example-lib==<version>"
python -c "from example_lib import Counter; counter = Counter(10); print(counter.add(5)); counter.close()"
The last command should print 15.
After changing your example, rerun the MSBuild command and reinstall its wheel.
After changing SharpBind itself, rerun --full and use the new manifest paths so the trial project uses the new packages and cache.
To return to source development, change back to the repository root.
The routine smoke test uses tests/NativeFixture/NativeFixture.csproj with project references and an explicit generator path, so it tests source changes without packing NuGet or installing the template.
Code organization
ABI declarations and primitive mappings are maintained directly in C, C# and Python. See ABI maintenance for the files that must agree.
The binding generator produces consumer wrappers and native exports from annotated managed APIs. Rebuild those outputs and native binaries together after changing ABI contracts.
Read the implementation in the order one API call follows:
src/SharpBind.Generator/BindingGenerator.cscoordinates discovery, validation and rendering.TypeMapping.csholds the type representations;BindingSchema.cswrites their call descriptions.PythonEmitter.csproduces wrappers importing package-private_sharpbindsupport frompython/common-runtime. That directory owns the interoperability implementation;build/python/sharpbind_pack/adapter.pyhandles bundling its sources and compiling the private CPython adapter._binding.pyprepares arguments and reads results._provider.pyloads the library, binds ctypes signatures from_abi.py, and translates native errors.src/SharpBind.Generator/NativeEmitter.csemits native entry points that call the managed API throughsrc/SharpBind/Interop.- For shared arrays, follow
_borrow.pyand_result.pyintopython/common-runtime/native.abi.hdefines the C layouts and is embedded directly by the generator when it emits a consumer header.
Edit generator emitters and src/SharpBind.Generator/Templates/SupportExports.cs.txt to change generated code.
Edit src/SharpBind/Interop or python/common-runtime/native to change compiled interop support.
Generated wrappers and native exports are build outputs; regenerate them after changing their source.
Add managed regression cases in tests/SharpBind.Tests and Python behavior cases in the appropriate tests/integration/test_*.py file.
Ownership and lifetime changes need a fresh smoke test so the checks exercise newly compiled native libraries through installed wheels.
Keep methods at one abstraction level. Explain ownership transitions and protocol constraints in comments. Avoid one-line blocks that hide cleanup and failure paths.
Build helpers live in build/python and ship inside NuGet.
They use the Python prerequisites installed at the start of this guide, plus platform repair tools for releases.
Set SharpBindPythonExecutable to an interpreter's full path when calling MSBuild directly to use an environment other than python on PATH.
For Python formatting, install and run Black from the repository root:
python -m pip install black
python -m black --exclude '/(artifacts|bin|obj)/' build/python python/common-runtime/src tests
Use the repository .clang-format for native C. C# follows .editorconfig.
Documentation checks
After editing documentation, restore the pinned DocFX tool and build the site from the repository root:
dotnet tool restore
dotnet docfx docs/docfx.json --warningsAsErrors
The rendered site is written to docs/_site.
Fix warnings, including unresolved links, before submitting the change.
Use relative Markdown links, keep the handwritten attribute reference consistent with the public attributes, and check that quickstart commands still follow the current build workflow.