Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
cb9bcfc
updated cpp version and replaced boost smart pointer usages
SYMSCAE Jun 24, 2026
a77b086
added nanobind source to build system
SYMSCAE Jun 24, 2026
107ea7c
ran docformatter
SYMSCAE Jun 24, 2026
330e612
update precommit deps
SYMSCAE Jun 25, 2026
70778b9
fix docformatter conflicts
SYMSCAE Jun 26, 2026
790eff8
Merge pull request #87 from SYMSCAE/precommit-deps
sbillinge Jun 26, 2026
5094a2a
Merge branch 'main' into nanobind-deps
SYMSCAE Jun 26, 2026
63006a8
news
SYMSCAE Jun 26, 2026
71ddfd5
migrated syntax for srreal_converters and srreal_validators
SYMSCAE Jun 24, 2026
50e706b
migrated syntax for srreal_ext and srreal_registry
SYMSCAE Jun 24, 2026
eebb59d
reworked pickling and migrated syntax for wrap_AtomicStructureAdapter
SYMSCAE Jun 25, 2026
49430b4
migrated bindings for AtomRadiiTable, Attributes, BaseBondGenerator a…
SYMSCAE Jun 25, 2026
774b475
migrated bindings for BVParametersTable, BVSCalculator, EventTicker a…
SYMSCAE Jun 25, 2026
3920040
migrated bindings for ObjCrystAdapters, OverlapCalculator, PairQuanti…
SYMSCAE Jun 25, 2026
0066c2b
migrated bindings for PDFCalculators and PDFenvelope
SYMSCAE Jun 25, 2026
7f55adb
migrated bindings for PeakProfile, PeakWidthModel and ScatteringFacto…
SYMSCAE Jun 25, 2026
9fe4a79
migrated bindings for StructureAdapter and StructureDifference
SYMSCAE Jun 25, 2026
25c1e74
fix build
SYMSCAE Jun 25, 2026
ee7ef63
restore default constructors
SYMSCAE Jun 26, 2026
a56ab24
corrected code behaviors
SYMSCAE Jun 26, 2026
c4f74cd
restore PDFCalculator behavior
SYMSCAE Jun 26, 2026
334cfcd
reworked pickling system
SYMSCAE Jun 26, 2026
72deaae
fix tests
SYMSCAE Jun 26, 2026
711a416
ran docformatter
SYMSCAE Jul 7, 2026
8ef7657
cleanup boost imports
SYMSCAE Jul 8, 2026
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
Prev Previous commit
Next Next commit
migrated syntax for srreal_ext and srreal_registry
  • Loading branch information
SYMSCAE committed Jul 7, 2026
commit 50e706b0829269e4ca185e7d55b27f5e920ce782
120 changes: 62 additions & 58 deletions src/extensions/srreal_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,37 @@
*
*****************************************************************************/

#include <boost/python/module.hpp>
#include <boost/python/import.hpp>
#include <boost/python/scope.hpp>
#include <boost/python/extract.hpp>
#include <boost/python/dict.hpp>
#include "srreal_numpy_symbol.hpp"
#include <nanobind/nanobind.h>

#include <numpy/arrayobject.h>

// Declaration of the external wrappers --------------------------------------

namespace nb = nanobind;

namespace srrealmodule {

void wrap_libdiffpy_version();
void wrap_libdiffpy_version(nb::module_& m);
void wrap_exceptions();
void wrap_EventTicker();
void wrap_Attributes();
void wrap_StructureDifference();
void wrap_StructureAdapter();
void wrap_AtomicStructureAdapter();
void wrap_ObjCrystAdapters();
void wrap_BaseBondGenerator();
void wrap_PairQuantity();
void wrap_PeakWidthModel();
void wrap_ScatteringFactorTable();
void wrap_PeakProfile();
void wrap_BVParametersTable();
void wrap_BVSCalculator();
void wrap_PDFBaseline();
void wrap_PDFEnvelope();
void wrap_PDFCalculators();
void wrap_BondCalculator();
void wrap_AtomRadiiTable();
void wrap_OverlapCalculator();
void wrap_EventTicker(nb::module_& m);
void wrap_Attributes(nb::module_& m);
void wrap_StructureDifference(nb::module_& m);
void wrap_StructureAdapter(nb::module_& m);
void wrap_AtomicStructureAdapter(nb::module_& m);
void wrap_ObjCrystAdapters(nb::module_& m);
void wrap_BaseBondGenerator(nb::module_& m);
void wrap_PairQuantity(nb::module_& m);
void wrap_PeakWidthModel(nb::module_& m);
void wrap_ScatteringFactorTable(nb::module_& m);
void wrap_PeakProfile(nb::module_& m);
void wrap_BVParametersTable(nb::module_& m);
void wrap_BVSCalculator(nb::module_& m);
void wrap_PDFBaseline(nb::module_& m);
void wrap_PDFEnvelope(nb::module_& m);
void wrap_PDFCalculators(nb::module_& m);
void wrap_BondCalculator(nb::module_& m);
void wrap_AtomRadiiTable(nb::module_& m);
void wrap_OverlapCalculator(nb::module_& m);

} // namespace srrealmodule

Expand All @@ -64,46 +62,52 @@ namespace {

// Module Definitions --------------------------------------------------------

BOOST_PYTHON_MODULE(srreal_ext)
NB_MODULE(srreal_ext, m)
{
using namespace srrealmodule;
// initialize numpy module
initialize_numpy();
// execute external wrappers
wrap_libdiffpy_version();
wrap_libdiffpy_version(m);
wrap_exceptions();
wrap_EventTicker();
wrap_Attributes();
wrap_StructureDifference();
wrap_StructureAdapter();
wrap_AtomicStructureAdapter();
wrap_ObjCrystAdapters();
wrap_BaseBondGenerator();
wrap_PairQuantity();
wrap_PeakWidthModel();
wrap_ScatteringFactorTable();
wrap_PeakProfile();
wrap_BVParametersTable();
wrap_BVSCalculator();
wrap_PDFBaseline();
wrap_PDFEnvelope();
wrap_PDFCalculators();
wrap_BondCalculator();
wrap_AtomRadiiTable();
wrap_OverlapCalculator();
wrap_EventTicker(m);
wrap_Attributes(m);
wrap_StructureDifference(m);
wrap_StructureAdapter(m);
wrap_AtomicStructureAdapter(m);
wrap_ObjCrystAdapters(m);
wrap_BaseBondGenerator(m);
wrap_PairQuantity(m);
wrap_PeakWidthModel(m);
wrap_ScatteringFactorTable(m);
wrap_PeakProfile(m);
wrap_BVParametersTable(m);
wrap_BVSCalculator(m);
wrap_PDFBaseline(m);
wrap_PDFEnvelope(m);
wrap_PDFCalculators(m);
wrap_BondCalculator(m);
wrap_AtomRadiiTable(m);
wrap_OverlapCalculator(m);
// load Python modules that tweak the wrapped classes
using boost::python::object;
using boost::python::import;
using boost::python::scope;
using boost::python::extract;
using boost::python::dict;
object srreal = import("diffpy.srreal");
if (!PyObject_HasAttrString(srreal.ptr(), "_final_imports"))
nb::module_ srreal = nb::module_::import_("diffpy.srreal");
if (!nb::hasattr(srreal, "_final_imports"))
{
dict sysmods = extract<dict>(import("sys").attr("modules"));
sysmods.setdefault("diffpy.srreal.srreal_ext", scope());
object import_now =
import("diffpy.srreal._final_imports").attr("import_now");
PyObject* sysmods = PyImport_GetModuleDict();

const char* fqname = "diffpy.srreal.srreal_ext";
if (PyDict_GetItemString(sysmods, fqname) == nullptr)
{
if (PyDict_SetItemString(sysmods, fqname, m.ptr()) < 0)
{
nb::raise_python_error();
}
}

nb::module_ final_imports =
nb::module_::import_("diffpy.srreal._final_imports");

nb::object import_now = final_imports.attr("import_now");
import_now();
}
}
Expand Down
19 changes: 9 additions & 10 deletions src/extensions/srreal_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,26 @@
*
*****************************************************************************/

#include <boost/python/object.hpp>
#include <boost/python/import.hpp>
#include <nanobind/nanobind.h>

namespace srrealmodule {
namespace nb = nanobind;

using namespace boost::python;
namespace srrealmodule {

void register_for_cleanup(PyObject* pobj)
{
object obj(borrowed(pobj));
object reg = import("diffpy.srreal._cleanup").attr("registerForCleanUp");
nb::object obj = nb::borrow(pobj);
nb::object reg = nb::module_::import_("diffpy.srreal._cleanup").attr("registerForCleanUp");
reg(obj);
}


/// get dictionary of Python-defined docstrings for the cls class.
object get_registry_docstrings(object& cls)
nb::object get_registry_docstrings(nb::object& cls)
{
object mod = import("diffpy.srreal._docstrings");
object getdocs = mod.attr("get_registry_docstrings");
object rv = getdocs(cls);
nb::object mod = nb::module_::import_("diffpy.srreal._docstrings");
nb::object getdocs = mod.attr("get_registry_docstrings");
nb::object rv = getdocs(cls);
return rv;
}

Expand Down
99 changes: 48 additions & 51 deletions src/extensions/srreal_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
#ifndef SRREAL_REGISTRY_HPP_INCLUDED
#define SRREAL_REGISTRY_HPP_INCLUDED

#include <boost/python/object.hpp>
#include <boost/python/extract.hpp>
#include <nanobind/nanobind.h>

namespace nb = nanobind;

namespace srrealmodule {

Expand All @@ -44,9 +45,9 @@ class wrapper_registry_configurator
/// the fetch method should be called only from the wrapped method
/// create() to remember pointers to the last Python object and
/// the C++ instance that it wraps.
TSharedPtr fetch(::boost::python::object& obj) const
TSharedPtr fetch(nb::object& obj) const
{
TSharedPtr p = ::boost::python::extract<TSharedPtr>(obj);
TSharedPtr p = nb::cast<TSharedPtr>(obj);
mcptr = p.get();
mpyptr = obj.ptr();
return p;
Expand All @@ -73,73 +74,69 @@ class wrapper_registry_configurator


/// retrieve a dictionary of Python-defined docstrings for the cls class.
::boost::python::object get_registry_docstrings(::boost::python::object& cls);
nb::object get_registry_docstrings(nb::object& cls);


/// helper wrapper functions for return value conversions.

template <class W>
::boost::python::object getAliasedTypes_asdict()
nb::object getAliasedTypes_asdict()
{
return convertToPythonDict(W::getAliasedTypes());
}

template <class W>
::boost::python::object getRegisteredTypes_asset()
nb::object getRegisteredTypes_asset()
{
return convertToPythonSet(W::getRegisteredTypes());
}


/// template function that wraps HasClassRegistry methods
template <class C>
C& wrap_registry_methods(C& boostpythonclass)
template <class C, class... Extra>
nb::class_<C, Extra...>& wrap_registry_methods(nb::class_<C, Extra...>& cls)
{
namespace bp = boost::python;
using namespace boost::python;
typedef typename C::wrapped_type::base B;
typedef extract<const char*> CString;
// get docstrings for the class registry methods.
object d = get_registry_docstrings(boostpythonclass);
const char* doc_create = CString(d["create"]);
const char* doc_clone = CString(d["clone"]);
const char* doc_type = CString(d["type"]);
const char* doc__registerThisType = CString(d["_registerThisType"]);
const char* doc__aliasType = CString(d["_aliasType"]);
const char* doc__deregisterType = CString(d["_deregisterType"]);
const char* doc_createByType = CString(d["createByType"]);
const char* doc_isRegisteredType = CString(d["isRegisteredType"]);
const char* doc_getAliasedTypes = CString(d["getAliasedTypes"]);
const char* doc_getRegisteredTypes = CString(d["getRegisteredTypes"]);
nb::object d = get_registry_docstrings(cls);
std::string doc_create = nb::cast<std::string>(d["create"]);
std::string doc_clone = nb::cast<std::string>(d["clone"]);
std::string doc_type = nb::cast<std::string>(d["type"]);
std::string doc__registerThisType = nb::cast<std::string>(d["_registerThisType"]);
std::string doc__aliasType = nb::cast<std::string>(d["_aliasType"]);
std::string doc__deregisterType = nb::cast<std::string>(d["_deregisterType"]);
std::string doc_createByType = nb::cast<std::string>(d["createByType"]);
std::string doc_isRegisteredType = nb::cast<std::string>(d["isRegisteredType"]);
std::string doc_getAliasedTypes = nb::cast<std::string>(d["getAliasedTypes"]);
std::string doc_getRegisteredTypes = nb::cast<std::string>(d["getRegisteredTypes"]);
// define the class registry related methods.
boostpythonclass
.def("create", &B::create, doc_create)
.def("clone", &B::clone, doc_clone)
.def("type", &B::type,
return_value_policy<copy_const_reference>(),
doc_type)
.def("_registerThisType", &B::registerThisType,
doc__registerThisType)
.def("_aliasType", &B::aliasType,
(bp::arg("tp"), bp::arg("alias")), doc__aliasType)
.staticmethod("_aliasType")
.def("_deregisterType", &B::deregisterType,
bp::arg("tp"), doc__deregisterType)
.staticmethod("_deregisterType")
.def("createByType", &B::createByType,
bp::arg("tp"), doc_createByType)
.staticmethod("createByType")
.def("isRegisteredType", &B::isRegisteredType,
bp::arg("tp"), doc_isRegisteredType)
.staticmethod("isRegisteredType")
.def("getAliasedTypes", getAliasedTypes_asdict<B>,
doc_getAliasedTypes)
.staticmethod("getAliasedTypes")
.def("getRegisteredTypes", getRegisteredTypes_asset<B>,
doc_getRegisteredTypes)
.staticmethod("getRegisteredTypes")
cls
.def("create", &C::create,
doc_create.c_str())
.def("clone", &C::clone,
doc_clone.c_str())
.def("type", &C::type,
nb::rv_policy::copy,
doc_type.c_str())
.def("_registerThisType", &C::registerThisType,
doc__registerThisType.c_str())
.def_static("_aliasType", &C::aliasType,
nb::arg("tp"), nb::arg("alias"),
doc__aliasType.c_str())
.def_static("_deregisterType", &C::deregisterType,
nb::arg("tp"),
doc__deregisterType.c_str())
.def_static("createByType", &C::createByType,
nb::arg("tp"),
doc_createByType.c_str())
.def_static("isRegisteredType", &C::isRegisteredType,
nb::arg("tp"),
doc_isRegisteredType.c_str())
.def_static("getAliasedTypes", &getAliasedTypes_asdict<C>,
doc_getAliasedTypes.c_str())
.def_static("getRegisteredTypes", &getRegisteredTypes_asset<C>,
doc_getRegisteredTypes.c_str())
;
return boostpythonclass;
return cls;
}


Expand Down