Phase 10A/10B Inc 0: build system, handle model, first module surface
Builds n3xd-ocp end to end and publishes 7.9.3.1.dev1 to the Gitea registry, where it installs anonymously and passes its suite. - occt/Dockerfile: OCCT 7.9.3 compiled once into a manylinux_2_28 builder image (base digest + tarball sha256 pinned), Draw/VTK/Tk/Xlib/OpenGL off, FreeType on, -O2 without fast-math or march=native. A final layer asserts TKService/TKV3d exist with no libGL/libX11 DT_NEEDED, which is what lets the app image drop libgl1/libx11-6. Mounted into, never built FROM. - scikit-build-core + nanobind STABLE_ABI -> one cp312-abi3 extension that registers every OCP.* submodule via PyImport_AddModule, so `import OCP.TopoDS` needs no shim and cls.__module__ is right. Version <occt>.N is asserted against the OCCT found, keeping occt_version() truthful. - occt_handle.h: type caster for opencascade::handle<T> over OCCT's intrusive refcount. Wrappers are non-owning instances holding exactly one handle in their keep-alive list, reusing an existing wrapper so identity survives a round trip. Transient constructors go through ocp_new (never nb::init<>, which would let OCCT delete nanobind's storage); the caster refuses a refcount-0 object rather than corrupt the heap. Verified under ASAN with no memory-safety errors, plus an RSS bound over 50k create/destroy cycles. - Sub-shapes are returned by value everywhere, making the TShape lifetime class that segfaulted a process-global face memo unrepresentable. - Standard_Failure derives RuntimeError, with ~20 concrete types dispatched on the dynamic OCCT type (cad_pool marshals failures home by type name). - Inc 0 surface: gp subset, TopAbs, TopoDS (+ downcasts), TopExp, TopLoc, TopTools, BRep, BinTools, Poly, Standard. 34 of the app's 139 symbols. - n3xd_ocp: additive APIs kept out of the OCP namespace so parity testing stays meaningful. bintools (shape <-> bytes, GIL-free, byte-identical) and _debug. Two findings worth the record, both verified against the stock wheel rather than assumed: upstream binds __hash__ but leaves __eq__ at identity, which is exactly what geom_memo.py's hash-bucket + IsSame scan is built around, so we match it instead of "fixing" it; and BinTools can release the GIL after all, by slurping the file-like object instead of bridging a streambuf that would call back into Python. Gate: BREP round-trips are byte-identical to cadquery-ocp-novtk across six fixtures (the generator asserts stock idempotency first). That matters beyond IPC — derive.py content-addresses BREP payloads by sha256 and stores the ref.
This commit is contained in:
124
src/common/occt_handle.h
Normal file
124
src/common/occt_handle.h
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
Type caster for opencascade::handle<T> — the ownership core of this binding.
|
||||
|
||||
OCCT transients carry their own intrusive, atomic reference count
|
||||
(Standard_Transient), so a handle keeps an object alive entirely on the C++
|
||||
side. That is what lets us release the GIL around kernel calls: OCCT may
|
||||
copy handles on its own worker threads without ever touching Python.
|
||||
|
||||
The design mirrors nanobind's own stl/shared_ptr.h caster:
|
||||
|
||||
C++ -> Python the wrapper is a *non-owning* nanobind instance pointing
|
||||
at the C++ object, plus one handle stored in the object's
|
||||
keep-alive list. Python holds exactly one OCCT reference
|
||||
per wrapper, released at deallocation. An existing
|
||||
wrapper is reused (is_new == false), so `a is b` holds for
|
||||
as long as a wrapper stays alive.
|
||||
|
||||
Python -> C++ a plain handle copy (one incref), balanced when the caster
|
||||
dies after the call. Unlike shared_ptr we do *not* need
|
||||
to keep the PyObject alive: the OCCT refcount, not the
|
||||
Python instance, owns the object's memory.
|
||||
|
||||
That last sentence is only true if every wrapped transient was heap
|
||||
allocated and is handle-owned. See occt_transient.h — transient
|
||||
constructors are bound through OCP_TRANSIENT_NEW, never nb::init<>, so
|
||||
Python never owns transient storage. from_python re-checks the invariant
|
||||
rather than trusting it, because the failure mode is a double free.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <nanobind/nanobind.h>
|
||||
|
||||
#include <Standard_Handle.hxx>
|
||||
#include <Standard_Transient.hxx>
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
NAMESPACE_BEGIN(NB_NAMESPACE)
|
||||
NAMESPACE_BEGIN(detail)
|
||||
|
||||
// Marked NB_NOINLINE so the (identical) body is not duplicated into every
|
||||
// instantiation of the caster below — there is one per bound transient class.
|
||||
inline NB_NOINLINE void
|
||||
occt_handle_keep_alive(opencascade::handle<Standard_Transient> &&h,
|
||||
PyObject *o) noexcept {
|
||||
keep_alive(o, new opencascade::handle<Standard_Transient>(std::move(h)),
|
||||
[](void *p) noexcept {
|
||||
delete (opencascade::handle<Standard_Transient> *) p;
|
||||
});
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct type_caster<
|
||||
opencascade::handle<T>,
|
||||
enable_if_t<std::is_base_of_v<Standard_Transient, std::decay_t<T>>>> {
|
||||
static constexpr bool IsClass = true;
|
||||
using Caster = make_caster<T>;
|
||||
using Td = std::decay_t<T>;
|
||||
|
||||
NB_TYPE_CASTER(opencascade::handle<T>, Caster::Name)
|
||||
|
||||
static_assert(is_base_caster_v<Caster>,
|
||||
"Conversion of opencascade::handle<T> requires that T is "
|
||||
"bound through nanobind's regular class mechanism.");
|
||||
|
||||
bool from_python(handle src, uint8_t flags,
|
||||
cleanup_list *cleanup) noexcept {
|
||||
// A null handle is OCCT's absent value and maps to None in both
|
||||
// directions; several APIs (BRep_Tool::Surface on a degenerate edge,
|
||||
// for one) legitimately return one.
|
||||
if (src.is_none()) {
|
||||
value = Value();
|
||||
return true;
|
||||
}
|
||||
|
||||
flags &= ~((uint8_t) cast_flags::convert);
|
||||
|
||||
Caster caster;
|
||||
if (!caster.from_python(src, flags, cleanup))
|
||||
return false;
|
||||
|
||||
Td *ptr = caster.operator Td *();
|
||||
|
||||
// Invariant check, not defensive programming: a refcount of zero means
|
||||
// this wrapper owns its storage (nb::init<>, or a by-value return),
|
||||
// and taking a handle to it would hand OCCT the right to `delete` a
|
||||
// nanobind instance's memory. Refuse instead — a TypeError beats a
|
||||
// heap corruption, and it can only be reached by a binding bug.
|
||||
if (ptr && ptr->GetRefCount() == 0) {
|
||||
assert(!"OCCT transient is not handle-owned (see occt_transient.h)");
|
||||
return false;
|
||||
}
|
||||
|
||||
value = Value(ptr);
|
||||
return true;
|
||||
}
|
||||
|
||||
static handle from_cpp(const Value &v, rv_policy,
|
||||
cleanup_list *cleanup) noexcept {
|
||||
Td *ptr = v.get();
|
||||
if (!ptr)
|
||||
return none().release();
|
||||
|
||||
// Transients are polymorphic, so nb_type_put_p downcasts on the
|
||||
// dynamic type: BRep_Tool::Surface returning a Geom_Surface handle to
|
||||
// a plane arrives in Python as Geom_Plane when that class is bound.
|
||||
bool is_new = false;
|
||||
handle result = nb_type_put_p(&typeid(Td), &typeid(*ptr), (void *) ptr,
|
||||
rv_policy::reference, cleanup, &is_new);
|
||||
|
||||
// Only a freshly created wrapper takes a reference. Attaching one per
|
||||
// conversion would pile up redundant handles on a long-lived object
|
||||
// that crosses the boundary many times.
|
||||
if (result.is_valid() && is_new)
|
||||
occt_handle_keep_alive(
|
||||
opencascade::handle<Standard_Transient>(ptr), result.ptr());
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
NAMESPACE_END(detail)
|
||||
NAMESPACE_END(NB_NAMESPACE)
|
||||
Reference in New Issue
Block a user