# bufsum

Zero-copy bulk data through the buffer protocol, plus correct release of the
interpreter around pure-C work.

```bash
python -m pip install -e . --no-build-isolation -v
python -m pytest -q
```

```python
import array, bufsum

a = array.array("d", [1.0, 2.0, 3.5])
print(bufsum.sum_doubles(a))    # 6.5
bufsum.scale(a, 2.0)            # in place, writable buffer
print(list(a))                  # [2.0, 4.0, 7.0]
print(bufsum.checksum(b"hello"))
```

## What to look for in `bufsum.c`

| Pattern | Where |
|---|---|
| Acquire, validate, release on every path | `get_double_view` |
| Error recorded in plain C, raised after re-attaching | `bufsum_sum_doubles` |
| Writable buffer request | `get_double_view(..., writable=1)` |
| The `y*` parsing shortcut, still needing a release | `bufsum_checksum` |

## Things to try

1. Move `PyErr_SetString` inside the `Py_BEGIN_ALLOW_THREADS` block and run
   the tests under a debug build.
2. Replace the `Py_buffer` with `PyUnicode_AsUTF8` on a `str` argument and
   use that pointer while detached. Reason about why it is unsound even if
   it appears to work.
3. Drop `PyBUF_C_CONTIGUOUS` from the request flags and pass `arr.T` from
   NumPy. What does the function compute now?
4. Time `sum_doubles` from four threads with and without the
   `Py_BEGIN_ALLOW_THREADS` pair.
