nuwa_sdk
SDK for Nuwa Build - provides compile-time metadata for Python stub generation and numpy array wrappers
Summary
| Latest Version | 0.4.4 |
|---|---|
| License | MIT |
| CI Status | Failing |
| Downloads | 0 |
| Last Indexed | 2026-09-04 07:26 |
Tags
Authors
- Martin Eastwood
Installation
nimble install nuwa_sdk
choosenim install nuwa_sdk
git clone https://github.com/martineastwood/nuwa-sdk
OS Compatibility
| Platform | Linux | macOS | Windows | FreeBSD | OpenBSD | NetBSD | Android | iOS | WASM | Embedded |
|---|---|---|---|---|---|---|---|---|---|---|
| nuwa_sdk | ✓ | ✓ | ✓ | - | - | - | - | - | - | - |
Dependencies
| Package | Version | Optional |
|---|---|---|
| nim >= | 1.6.0 | No |
| nimpy >= | 0.2.0 | No |
Source
| Repository | https://github.com/martineastwood/nuwa-sdk |
|---|---|
| Homepage | https://github.com/martineastwood/nuwa-sdk |
| Registry Source | nimble_official |
README
nuwa-sdk
Python interoperability utilities for Nuwa Build
This package provides utilities for building high-performance Python extensions with Nim:
nuwa_export- Macro for automatic generation of Python type stubs (.pyifiles)withNogil- Template for releasing the Python GIL during pure Nim code executionasNumpyArray/asNumpyArrayWrite- Zero-copy views of existing NumPy (or other PEP 3118) buffers. This does not allocate ndarrays.
Usage
import nuwa_sdk
proc add(a: int, b: int): int {.nuwa_export.} =
## Add two integers together
return a + b
proc greet(name: string): string {.nuwa_export.} =
## Greet a person by name
return "Hello, " & name
When you build your project with nuwa develop or nuwa build, this macro will:
- Export the function to Python (via
nimpy) - Write compile-time metadata about function signatures (JSON files when
-d:nuwaStubDir=is set, otherwiseNUWA_STUB:lines on stdout) - Let nuwa-build generate
.pyistub files from that metadata
GIL Release with withNogil
The withNogil template allows you to release Python's Global Interpreter Lock (GIL) during performance-critical Nim code. This enables true parallelism when your extension is used from multi-threaded Python code.
import nuwa_sdk
proc computePi(iterations: int): float {.nuwa_export.} =
## Compute Pi using Monte Carlo method (GIL-released)
var count = 0
withNogil:
# Pure Nim code runs without Python interpreter interference
# Other Python threads can run concurrently
for i in 0..<iterations:
let x = rand(1.0)
let y = rand(1.0)
if x*x + y*y <= 1.0:
count += 1
return 4.0 * float(count) / float(iterations)
When to use withNogil
- CPU-bound computations: Mathematical operations, numeric algorithms
- Batch processing: Processing large datasets without Python interaction
- Parallel workloads: Allow other Python threads to run while Nim computes
When NOT to use withNogil
- Python API calls: Any code that calls Python objects or nimpy functions
- Data transfer: Converting between Nim and Python types
- I/O operations: File/network operations that release GIL automatically
How it works
PyEval_SaveThread()releases the GIL- Pure Nim code executes without Python interference
PyEval_RestoreThread()reacquires the GIL before returning to Python
Equivalent to Cython's with nogil: block or CPython's Py_BEGIN_ALLOW_THREADS.
NumPy / buffer views
asNumpyArray wraps an existing Python object that exports the buffer protocol (typically a NumPy array). It does not create arrays or copy data.
import nuwa_sdk
proc sumInt64(arr: PyObject): int64 {.nuwa_export.} =
var view = asNumpyArray(arr, int64)
result = 0
for x in view:
result += x
proc scaleInPlace(arr: PyObject, s: float64) {.nuwa_export.} =
var view = asNumpyArrayWrite(arr, float64)
for x in mitems(view):
x = x * s
- Use a concrete element type (
int64,float64, …), not a genericndarray. isContiguousmeans C-order. Fortran-contiguous arrays still exposedata(memory order is column-major).arr[i]is only for 1D arrays. Usearr[i, j]for 2D.itemswalks logical C-order even when storage is Fortran.- Read-only arrays (
setflags(write=False)) work withasNumpyArrayand fail withasNumpyArrayWrite. close()is optional (RAII releases the buffer) and requiresvar, notlet.
See NumPy buffers.
How It Works
nuwa_export macro
- Inspects your Nim functions at compile time
- Extracts parameter names, types, return types, and docstrings
- Maps Nim types to Python type annotations
- Writes JSON metadata to
-d:nuwaStubDir=when nuwa-build provides that path - Falls back to
NUWA_STUB:lines on stdout - nuwa-build reads that metadata and generates
.pyifiles
withNogil template
The withNogil template:
- Wraps a block of Nim code with GIL release/reacquire calls
- Dynamically loads Python C API functions (lazy initialization)
- Ensures GIL is always reacquired even if an exception occurs
- Allows multiple Python threads to execute Nim code concurrently
Supported Type Mappings
| Nim Type | Python Type |
|---|---|
int, int32, int64 |
int |
float, float32, float64 |
float |
string |
str |
bool |
bool |
void |
None |
seq[T] |
list[T] |
array[N, T] |
list[T] |
| Other types | Any |
Installation
This package is automatically installed as a dependency when you use nuwa new to create a new project.
You can also install it manually:
nimble install nuwa_sdk
License
MIT