Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if(DEFINED ENV{CONDA_PREFIX})
list(APPEND CMAKE_PREFIX_PATH "$ENV{CONDA_PREFIX}" "$ENV{CONDA_PREFIX}/Library")
endif()

find_package(Python 3.12 REQUIRED COMPONENTS Interpreter Development.Module NumPy)
find_package(Python 3.12 REQUIRED COMPONENTS Interpreter Development.Module)
execute_process(
COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
OUTPUT_VARIABLE nanobind_DIR
Expand Down Expand Up @@ -52,10 +52,9 @@ target_compile_definitions(diffpy_core PRIVATE DIFFPYRUNTIMERELPATH=data)
target_link_libraries(diffpy_core PUBLIC Boost::serialization GSL::gsl GSL::gslcblas ${CMAKE_DL_LIBS})

file(GLOB SRREAL_SOURCES CONFIGURE_DEPENDS "src/extensions/*.cpp")
# Keep the CPython-specific ABI while the bindings use the NumPy C API and
# CPython macros. Do not label these wheels abi3 without porting those calls.
# Retain CPython-specific wheels; stable-ABI packaging is a separate change.
nanobind_add_module(srreal_ext NB_STATIC ${SRREAL_SOURCES})
target_link_libraries(srreal_ext PRIVATE diffpy_core Python::NumPy)
target_link_libraries(srreal_ext PRIVATE diffpy_core)
set_target_properties(srreal_ext PROPERTIES INSTALL_RPATH_USE_LINK_PATH ON)
install(TARGETS srreal_ext LIBRARY DESTINATION diffpy/srreal)
install(DIRECTORY "${LIBDIFFPY_SOURCE_DIR}/src/runtime/"
Expand Down
22 changes: 22 additions & 0 deletions news/nanobind-arrays.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
**Changed:**

* Use typed nanobind arrays for numeric input, copied results, and writable
Atom views. Keep NumPy output types, shapes, native dtypes, and the existing
scalar-versus-array return conventions.
* Remove the NumPy C API and build-time NumPy dependency. NumPy remains a
runtime dependency; wheel Python ABI requirements are unchanged.
* Retain NumPy-owned writable storage for copied results and explicit storage
owners for Atom views, including slices retained after structural edits.
* Preserve endpoint-specific conversion rules, NumPy scalar casting warnings,
and coordinate/matrix broadcasting. Invalid coordinate/matrix assignments
no longer partially modify an Atom.

**Fixed:**

* Read strided and reversed integer index arrays in logical order instead of
treating their storage as contiguous.
* Interpret byte-swapped FFT inputs and integer index arrays in their actual
byte order, and safely handle unaligned buffers.
* Reject integer-array indices outside the C++ ``int`` range with
``OverflowError`` instead of silently wrapping. These three corrections
intentionally change erroneous behavior inherited from Boost.Python.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
requires = [
"scikit-build-core>=0.10",
"setuptools-scm>=8",
"numpy",
"nanobind>=3.1,<4",
]
build-backend = "scikit_build_core.build"
Expand Down
7 changes: 4 additions & 3 deletions src/extensions/srreal_atom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,17 @@ struct AtomViewPin
}
};

inline nb::object pin_atom_view(PythonAtom& atom, nb::object view)
inline nb::object atom_view_owner(PythonAtom& atom)
{
nb::object python_atom = nb::cast(&atom, nb::rv_policy::reference);
if (atom.references())
{
nb::capsule owner(new AtomViewPin(atom.references()), [](void* p) noexcept {
delete static_cast<AtomViewPin*>(p);
});
setNumPyArrayBase(view, std::move(owner));
return nb::make_tuple(python_atom, owner);
}
return view;
return python_atom;
}

inline void preserve_atom_views(const std::shared_ptr<AtomReferences>& refs)
Expand Down
Loading
Loading