py10x-infra 0.4.0rc4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,52 @@
1
+ cmake_minimum_required(VERSION 3.27)
2
+ project(py10x_infra)
3
+
4
+ set(CMAKE_CXX_STANDARD 20)
5
+ if(MSVC)
6
+ set(CMAKE_CXX_FLAGS_DEBUG "/Zi /Od")
7
+ else()
8
+ set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
9
+ endif()
10
+
11
+ #-- The shared helpers are in ../cmake; building the sdist copies them into cmake/ (gitignored), which it ships
12
+ set(XX_CMAKE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
13
+ if(NOT EXISTS ${XX_CMAKE_DIR})
14
+ set(XX_CMAKE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
15
+ elseif(SKBUILD_STATE STREQUAL "sdist")
16
+ file(REMOVE_RECURSE ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
17
+ file(COPY ${XX_CMAKE_DIR}/ DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
18
+ endif()
19
+
20
+ set(PY_LINKAGE_SRC "${CMAKE_CURRENT_SOURCE_DIR}/../core_10x/py_linkage.h")
21
+ if(EXISTS "${PY_LINKAGE_SRC}")
22
+ set(PY_LINKAGE_DEST "${CMAKE_CURRENT_SOURCE_DIR}/py_linkage.h")
23
+ file(COPY_FILE "${PY_LINKAGE_SRC}" "${PY_LINKAGE_DEST}" RESULT copy_result ONLY_IF_DIFFERENT)
24
+ if(NOT copy_result EQUAL 0)
25
+ message(FATAL_ERROR "Failed to copy ${PY_LINKAGE_SRC} to ${PY_LINKAGE_DEST}: ${copy_result}")
26
+ endif()
27
+ endif()
28
+
29
+ if(SKBUILD_STATE STREQUAL "sdist")
30
+ return()
31
+ endif()
32
+
33
+ find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)
34
+
35
+ include(${XX_CMAKE_DIR}/FetchPybind11Deps.cmake)
36
+ include(${XX_CMAKE_DIR}/PybindStubs.cmake)
37
+
38
+ pybind11_add_module(py10x_infra
39
+ mongo_collection_helper.cpp
40
+ py10x_infra.cpp
41
+ )
42
+
43
+ target_compile_definitions(py10x_infra PRIVATE
44
+ VERSION_INFO="${SKBUILD_PROJECT_VERSION_FULL}"
45
+ )
46
+
47
+ finalize_pybind_module(py10x_infra)
48
+
49
+ install(TARGETS py10x_infra
50
+ LIBRARY DESTINATION .
51
+ RUNTIME DESTINATION .
52
+ )
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-2026 10X CONCEPTS LLC and contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,33 @@
1
+ Metadata-Version: 2.2
2
+ Name: py10x-infra
3
+ Version: 0.4.0rc4
4
+ Summary: 10X Platform Core Infrastructure
5
+ Author-Email: 10X CONCEPTS LLC <py10x@10x-software.org>
6
+ License: MIT
7
+ Classifier: License :: OSI Approved :: MIT License
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.11
10
+ Classifier: Programming Language :: Python :: 3.12
11
+ Classifier: Programming Language :: Python :: 3.13
12
+ Classifier: Operating System :: Microsoft :: Windows
13
+ Classifier: Operating System :: POSIX :: Linux
14
+ Classifier: Operating System :: MacOS
15
+ Classifier: Development Status :: 4 - Beta
16
+ Project-URL: Homepage, https://10x-software.org
17
+ Requires-Python: <3.14,>=3.11
18
+ Description-Content-Type: text/markdown
19
+
20
+ # py10x-infra
21
+
22
+ 10X Platform Core Infrastructure is a C++ extension and Python bindings for core infrastructure related utilities.
23
+
24
+ This package is a dependency of `py10x-core` and is intended to be used in conjunction with [py10x-core](https://github.com/10x-software/py10x/blob/main/README.md) only.
25
+
26
+ ## Requirements
27
+
28
+ - Python 3.11 or 3.12
29
+ - Modern windows, macOS, or Linux
30
+
31
+ ## License
32
+
33
+ MIT License. See [LICENSE](https://github.com/10x-software/cxx10x/blob/main/infra_10x/LICENSE).
@@ -0,0 +1,14 @@
1
+ # py10x-infra
2
+
3
+ 10X Platform Core Infrastructure is a C++ extension and Python bindings for core infrastructure related utilities.
4
+
5
+ This package is a dependency of `py10x-core` and is intended to be used in conjunction with [py10x-core](https://github.com/10x-software/py10x/blob/main/README.md) only.
6
+
7
+ ## Requirements
8
+
9
+ - Python 3.11 or 3.12
10
+ - Modern windows, macOS, or Linux
11
+
12
+ ## License
13
+
14
+ MIT License. See [LICENSE](https://github.com/10x-software/cxx10x/blob/main/infra_10x/LICENSE).
@@ -0,0 +1,24 @@
1
+ include(${CMAKE_CURRENT_LIST_DIR}/versions.cmake)
2
+ include(${CMAKE_CURRENT_LIST_DIR}/Py10xMsvcVersion.cmake)
3
+
4
+ include(FetchContent)
5
+ FetchContent_Declare(
6
+ pybind11
7
+ GIT_REPOSITORY https://github.com/pybind/pybind11
8
+ GIT_TAG ${PYBIND11_VERSION}
9
+ GIT_SHALLOW TRUE
10
+ OVERRIDE_FIND_PACKAGE
11
+ )
12
+ FetchContent_Declare(
13
+ backward
14
+ GIT_REPOSITORY https://github.com/bombela/backward-cpp.git
15
+ GIT_TAG master
16
+ GIT_SHALLOW TRUE
17
+ SOURCE_SUBDIR _headers_only
18
+ )
19
+ # pybind11-stubgen is a Python build dependency (build-system.requires), not fetched: its repo's test
20
+ # tree is too deep for Windows' MAX_PATH under a build dir.
21
+ FetchContent_MakeAvailable(pybind11 backward)
22
+ find_package(pybind11 REQUIRED)
23
+
24
+ py10x_check_msvc_version()
@@ -0,0 +1,19 @@
1
+ include_guard(GLOBAL)
2
+
3
+ include(${CMAKE_CURRENT_LIST_DIR}/versions.cmake)
4
+
5
+ # py10x_check_msvc_version()
6
+ #
7
+ # Ensures py10x-kernel and dependent extension modules are built with the same
8
+ # MSVC toolset. No-op on non-Windows platforms.
9
+ function(py10x_check_msvc_version)
10
+ if(NOT WIN32)
11
+ return()
12
+ endif()
13
+ if(NOT MSVC)
14
+ message(FATAL_ERROR "py10x requires MSVC on Windows")
15
+ endif()
16
+ if(NOT MSVC_VERSION EQUAL PY10X_REQUIRED_MSVC_VERSION)
17
+ message(FATAL_ERROR "py10x requires MSVC_VERSION=${PY10X_REQUIRED_MSVC_VERSION}")
18
+ endif()
19
+ endfunction()
@@ -0,0 +1,139 @@
1
+ option(XX_CXX_SANITIZERS "Enable AddressSanitizer and UndefinedBehaviorSanitizer" OFF)
2
+
3
+ # ---------------------------------------------------------------------------
4
+ # Internal helpers
5
+ # ---------------------------------------------------------------------------
6
+
7
+ function(_resolve_lib_output_dir)
8
+ if(NOT DEFINED CMAKE_LIBRARY_OUTPUT_DIRECTORY)
9
+ if(WIN32)
10
+ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG> PARENT_SCOPE)
11
+ else()
12
+ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} PARENT_SCOPE)
13
+ endif()
14
+ endif()
15
+ endfunction()
16
+
17
+ # Install the MSVC import library (.lib) generated for an exporting module
18
+ # (ENABLE_EXPORTS ON). Mirrors how the .pyi is installed: uses the resolved
19
+ # library output dir and a standard install() command so scikit-build-core
20
+ # tracks and packages it. Modules that export nothing (e.g. cxxfin) simply have
21
+ # no matching .lib and nothing is installed.
22
+ function(_install_import_lib target)
23
+ if(NOT MSVC)
24
+ return()
25
+ endif()
26
+ _resolve_lib_output_dir()
27
+ install(DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/"
28
+ DESTINATION .
29
+ FILES_MATCHING PATTERN "${target}*.lib")
30
+ endfunction()
31
+
32
+ function(_install_pdb target)
33
+ if(NOT MSVC)
34
+ return()
35
+ endif()
36
+ install(FILES $<TARGET_PDB_FILE:${target}>
37
+ DESTINATION .
38
+ OPTIONAL)
39
+ endfunction()
40
+
41
+ function(_add_rtld_link_options target)
42
+ if(APPLE)
43
+ target_link_options(${target} PRIVATE -undefined dynamic_lookup)
44
+ elseif(UNIX)
45
+ target_link_options(${target} PRIVATE -Wl,--allow-shlib-undefined)
46
+ endif()
47
+ endfunction()
48
+
49
+ function(_add_sanitizers target)
50
+ message(STATUS "Sanitizers enabled for ${target}")
51
+ if(MSVC)
52
+ # MSVC supports AddressSanitizer only; there is no UndefinedBehaviorSanitizer.
53
+ # ASan is incompatible with the /RTC runtime checks and incremental linking.
54
+ # /Zi + /DEBUG: symbolize ASan reports and produce a .pdb next to the .pyd.
55
+ # /EHsc: C++ EH unwind (do not rely on CMAKE_CXX_FLAGS defaults surviving).
56
+ # At runtime the ASan DLL (clang_rt.asan_dynamic-*.dll) must be loadable.
57
+ target_compile_options(${target} PRIVATE /fsanitize=address /Zi /EHsc)
58
+ target_link_options(${target} PRIVATE /INCREMENTAL:NO /DEBUG)
59
+ else()
60
+ target_compile_options(${target} PRIVATE
61
+ -fsanitize=address,undefined
62
+ -fno-omit-frame-pointer
63
+ -g
64
+ )
65
+ target_link_options(${target} PRIVATE
66
+ -fsanitize=address,undefined
67
+ )
68
+ endif()
69
+ endfunction()
70
+
71
+ # ---------------------------------------------------------------------------
72
+ # add_pybind_stubs(target)
73
+ # Post-build stub generation for self-contained pybind11 modules. pybind11-stubgen comes from the build
74
+ # environment (each package's build-system.requires; the `build` group / dev extra for editable rebuilds).
75
+ # ---------------------------------------------------------------------------
76
+ function(add_pybind_stubs target)
77
+ _resolve_lib_output_dir()
78
+
79
+ add_custom_command(
80
+ TARGET ${target} POST_BUILD
81
+ COMMAND ${CMAKE_COMMAND} -E env "PYTHONPATH=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}"
82
+ ${Python3_EXECUTABLE} -m pybind11_stubgen ${target} -o ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
83
+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
84
+ COMMENT "Generating type stubs for ${target}"
85
+ VERBATIM
86
+ )
87
+ install(FILES ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${target}.pyi DESTINATION .)
88
+ endfunction()
89
+
90
+ # ---------------------------------------------------------------------------
91
+ # add_pybind_stubs_rtld(target cxx10x_cmake_dir)
92
+ # Post-build stub generation for modules that subclass py10x_kernel types.
93
+ # Uses generate_stubs.py which promotes py10x_kernel to RTLD_GLOBAL first.
94
+ # ---------------------------------------------------------------------------
95
+ function(add_pybind_stubs_rtld target cxx10x_cmake_dir)
96
+ _resolve_lib_output_dir()
97
+
98
+ add_custom_command(
99
+ TARGET ${target} POST_BUILD
100
+ COMMAND ${Python3_EXECUTABLE} ${cxx10x_cmake_dir}/generate_stubs.py
101
+ ${target}
102
+ ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
103
+ ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
104
+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
105
+ COMMENT "Generating type stubs for ${target} (RTLD)"
106
+ VERBATIM
107
+ )
108
+ install(FILES ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${target}.pyi DESTINATION .)
109
+ endfunction()
110
+
111
+ # ---------------------------------------------------------------------------
112
+ # finalize_pybind_module(target)
113
+ # Applies sanitizers or stub generation depending on XX_CXX_SANITIZERS.
114
+ # For self-contained modules (core_10x, infra_10x style).
115
+ # ---------------------------------------------------------------------------
116
+ function(finalize_pybind_module target)
117
+ if(XX_CXX_SANITIZERS)
118
+ _add_sanitizers(${target})
119
+ else()
120
+ add_pybind_stubs(${target})
121
+ endif()
122
+ _install_import_lib(${target})
123
+ _install_pdb(${target})
124
+ endfunction()
125
+
126
+ # ---------------------------------------------------------------------------
127
+ # finalize_pybind_module_rtld(target cxx10x_cmake_dir)
128
+ # As above, but uses RTLD-aware stub generation for consumer packages
129
+ # that subclass py10x_kernel types (e.g. cxxfin).
130
+ # ---------------------------------------------------------------------------
131
+ function(finalize_pybind_module_rtld target cxx10x_cmake_dir)
132
+ _add_rtld_link_options(${target})
133
+ if(XX_CXX_SANITIZERS)
134
+ _add_sanitizers(${target})
135
+ else()
136
+ add_pybind_stubs_rtld(${target} ${cxx10x_cmake_dir})
137
+ endif()
138
+ _install_pdb(${target})
139
+ endfunction()
@@ -0,0 +1,85 @@
1
+ include_guard(GLOBAL)
2
+
3
+ include(${CMAKE_CURRENT_LIST_DIR}/FetchPybind11Deps.cmake)
4
+ include(${CMAKE_CURRENT_LIST_DIR}/PybindStubs.cmake)
5
+
6
+ # xx_pybind11_add_module(<target> <source>...)
7
+ #
8
+ # Convenience macro for pybind11 extension modules in the xx ecosystem that
9
+ # depend on py10x_kernel types. Handles:
10
+ # - fetching pybind11, backward (via FetchPybind11Deps)
11
+ # - adding include dirs for core_10x headers and backward
12
+ # - setting VERSION_INFO from scikit-build-core
13
+ # - RTLD link options (-undefined dynamic_lookup / --allow-shlib-undefined)
14
+ # - sanitizer or stub generation (via finalize_pybind_module_rtld)
15
+ #
16
+ # Requires cxx10x_core_headers_SOURCE_DIR to be set (via FetchContent) before
17
+ # calling this macro. Install rules are left to the caller.
18
+ macro(xx_pybind11_add_module target)
19
+ pybind11_add_module(${target} ${ARGN})
20
+
21
+ target_include_directories(${target} PRIVATE
22
+ ${cxx10x_core_headers_SOURCE_DIR}/core_10x
23
+ ${backward_SOURCE_DIR}
24
+ )
25
+
26
+ target_compile_definitions(${target} PRIVATE
27
+ VERSION_INFO="${SKBUILD_PROJECT_VERSION_FULL}"
28
+ )
29
+
30
+ # Locate the installed py10x_kernel binary. Windows needs it for the import library below;
31
+ # every platform needs it as a configure dependency (see CMAKE_CONFIGURE_DEPENDS further down).
32
+ #
33
+ # find_spec resolves the module's path WITHOUT importing it, which matters on both counts:
34
+ # an ASan-instrumented kernel aborts ("ASan runtime does not come first") when dlopened from a
35
+ # non-preloaded python -- and the ASan lane only preloads libasan for pytest, not for the build
36
+ # -- while a plain import may also run the kernel's import-time incremental rebuild, whose cmake
37
+ # progress lands on stdout around the path we are trying to read.
38
+ execute_process(
39
+ COMMAND ${Python3_EXECUTABLE} -c "import importlib.util as u; s = u.find_spec('py10x_kernel'); print(s.origin if s else '')"
40
+ OUTPUT_VARIABLE _PY10X_KERNEL_MODULE
41
+ OUTPUT_STRIP_TRAILING_WHITESPACE
42
+ RESULT_VARIABLE _mod_rc
43
+ )
44
+ # SKBUILD_EDITABLE_VERBOSE prints "Running cmake --build & --install in <dir>" on this
45
+ # stdout before s.origin. The module path is the last line. On Windows the .lib path
46
+ # below is derived from it, so the banner would otherwise fail the EXISTS check.
47
+ if(_PY10X_KERNEL_MODULE)
48
+ string(REGEX MATCH "[^\r\n]+$" _PY10X_KERNEL_MODULE "${_PY10X_KERNEL_MODULE}")
49
+ endif()
50
+ if(NOT _mod_rc EQUAL 0 OR NOT _PY10X_KERNEL_MODULE)
51
+ message(FATAL_ERROR "Could not locate the py10x_kernel module. Is py10x-kernel installed?")
52
+ endif()
53
+
54
+
55
+ set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${_PY10X_KERNEL_MODULE}")
56
+
57
+ # On Windows, link against py10x_kernel's import library (.lib) so the MSVC
58
+ # linker can resolve symbols like BTraitable that are dllimport-annotated.
59
+ # The .lib is installed alongside py10x_kernel.pyd by core_10x/CMakeLists.txt.
60
+ # On Linux/macOS, consumer extensions rely on py10x_kernel being promoted to
61
+ # RTLD_GLOBAL before load (see core_10x/__init__.py and generate_stubs.py).
62
+ if(WIN32)
63
+ set(_PY10X_KERNEL_PYD "${_PY10X_KERNEL_MODULE}")
64
+ # MSVC names the import library after the CMake target name (py10x_kernel),
65
+ # NOT the module's full output name. So the file is py10x_kernel.lib, sitting
66
+ # next to py10x_kernel.cp312-win_amd64.pyd. Strip the full extension chain
67
+ # (.cp312-win_amd64.pyd) down to the base module name, then append .lib.
68
+ get_filename_component(_pyd_dir "${_PY10X_KERNEL_PYD}" DIRECTORY)
69
+ get_filename_component(_pyd_name "${_PY10X_KERNEL_PYD}" NAME)
70
+ string(REGEX REPLACE "\\..*$" "" _kernel_base "${_pyd_name}") # py10x_kernel
71
+ set(_PY10X_KERNEL_LIB "${_pyd_dir}/${_kernel_base}.lib")
72
+ if(NOT EXISTS "${_PY10X_KERNEL_LIB}")
73
+ message(FATAL_ERROR
74
+ "py10x_kernel import library not found at ${_PY10X_KERNEL_LIB}.\n"
75
+ "Rebuild and reinstall py10x-kernel to generate the .lib.")
76
+ endif()
77
+ add_library(py10x_kernel_import SHARED IMPORTED)
78
+ set_target_properties(py10x_kernel_import PROPERTIES
79
+ IMPORTED_IMPLIB "${_PY10X_KERNEL_LIB}"
80
+ )
81
+ target_link_libraries(${target} PRIVATE py10x_kernel_import)
82
+ endif()
83
+
84
+ finalize_pybind_module_rtld(${target} ${cxx10x_core_headers_SOURCE_DIR}/cmake)
85
+ endmacro()
@@ -0,0 +1,35 @@
1
+ """
2
+ Shared post-build stub generation for pybind11 modules that depend on py10x_kernel.
3
+
4
+ Promotes py10x_kernel to RTLD_GLOBAL before importing the target module so
5
+ that BTraitable (and other exported symbols) are visible in the flat namespace
6
+ on macOS/Linux.
7
+
8
+ Usage (from CMake):
9
+ python generate_stubs.py <module_name> <so_dir> <stub_out_dir>
10
+
11
+ module_name : name of the pybind11 module to generate stubs for
12
+ so_dir : directory containing <module_name>.so
13
+ stub_out_dir : directory to write the generated .pyi file
14
+
15
+ pybind11_stubgen is imported from the build environment (build-system.requires).
16
+ """
17
+ import os
18
+ import sys
19
+ import ctypes
20
+
21
+ module_name, so_dir, stub_out_dir = sys.argv[1], sys.argv[2], sys.argv[3]
22
+
23
+ sys.path.insert(0, so_dir)
24
+
25
+ # An editable py10x_kernel rebuilds on import, running ninja: it must not join the jobserver of the ninja
26
+ # running this script (CLion's), whose pipe it cannot use ("Could not initialize jobserver").
27
+ os.environ.pop("MAKEFLAGS", None)
28
+ import py10x_kernel
29
+ if sys.platform == "win32":
30
+ os.add_dll_directory(os.path.dirname(py10x_kernel.__file__))
31
+ ctypes.CDLL(py10x_kernel.__file__, ctypes.RTLD_GLOBAL)
32
+
33
+ from pybind11_stubgen.__main__ import main # noqa
34
+ sys.argv = ["pybind11_stubgen", module_name, "-o", stub_out_dir]
35
+ main()
@@ -0,0 +1,5 @@
1
+ set(PYBIND11_VERSION "v3.1.0")
2
+
3
+ # MSVC toolset version required for ABI compatibility between py10x-kernel and
4
+ # extension modules that link against it (must match across all py10x builds).
5
+ set(PY10X_REQUIRED_MSVC_VERSION 1944)
@@ -0,0 +1,6 @@
1
+ #include <iostream>
2
+
3
+ int main() {
4
+ std::cout << "Hello, World!" << std::endl;
5
+ return 0;
6
+ }
@@ -0,0 +1,166 @@
1
+ //
2
+ // Created by AMD on 2/9/2025.
3
+ //
4
+
5
+ #include "mongo_collection_helper.h"
6
+
7
+ namespace {
8
+
9
+ struct Keys {
10
+ PyObject* rev_tag; // borrowed/immortal interned
11
+ PyObject* id_tag;
12
+ PyObject* literal;
13
+ PyObject* eq;
14
+ PyObject* and_;
15
+ PyObject* cond;
16
+ PyObject* new_root;
17
+ PyObject* replace_root;
18
+ PyObject* field;
19
+ PyObject* input;
20
+ PyObject* value;
21
+ PyObject* set_field;
22
+ PyObject* replace_with;
23
+ PyObject* root;
24
+ PyObject* dollar;
25
+
26
+ Keys()
27
+ : rev_tag(nullptr),
28
+ id_tag(nullptr),
29
+ literal(intern("$literal")),
30
+ eq(intern("$eq")),
31
+ and_(intern("$and")),
32
+ cond(intern("$cond")),
33
+ new_root(intern("newRoot")),
34
+ replace_root(intern("$replaceRoot")),
35
+ field(intern("field")),
36
+ input(intern("input")),
37
+ value(intern("value")),
38
+ set_field(intern("$setField")),
39
+ replace_with(intern("$replaceWith")),
40
+ root(intern("$$ROOT")),
41
+ dollar(intern("$")) {
42
+ // Same Nucleus class PyLinkage caches after core_10x init. Resolved via
43
+ // Python import so py10x_infra stays link-independent of py10x_kernel.
44
+ py::object nucleus = py::module_::import("core_10x.nucleus").attr("Nucleus");
45
+ rev_tag = intern_obj(nucleus.attr("REVISION_TAG")());
46
+ id_tag = intern_obj(nucleus.attr("ID_TAG")());
47
+ }
48
+
49
+ private:
50
+ static PyObject* intern(const char* s) {
51
+ PyObject* o = PyUnicode_InternFromString(s);
52
+ if (!o)
53
+ throw py::error_already_set();
54
+ return o; // intentionally leaked / immortal for process lifetime
55
+ }
56
+
57
+ static PyObject* intern_obj(const py::handle& tag) {
58
+ PyObject* o = tag.ptr();
59
+ Py_INCREF(o);
60
+ PyUnicode_InternInPlace(&o);
61
+ return o; // keep the strong ref forever
62
+ }
63
+ };
64
+
65
+ const Keys& keys() {
66
+ static const Keys k;
67
+ return k;
68
+ }
69
+
70
+ py::handle h(PyObject* o) { return py::handle(o); }
71
+
72
+ py::object dict_pop(py::dict& d, PyObject* key) {
73
+ PyObject* val = PyDict_GetItemWithError(d.ptr(), key); // borrowed
74
+ if (!val) {
75
+ if (PyErr_Occurred())
76
+ throw py::error_already_set();
77
+ throw py::key_error(py::str(h(key)));
78
+ }
79
+ Py_INCREF(val);
80
+ if (PyDict_DelItem(d.ptr(), key) < 0) {
81
+ Py_DECREF(val);
82
+ throw py::error_already_set();
83
+ }
84
+ return py::reinterpret_steal<py::object>(val);
85
+ }
86
+
87
+ } // namespace
88
+
89
+ void MongoCollectionHelper::prepare_filter_and_pipeline(py::dict& serialized_traitable, py::dict& filter, py::list& pipeline) {
90
+ const Keys& k = keys();
91
+
92
+ py::object revision = dict_pop(serialized_traitable, k.rev_tag);
93
+ py::object id_value = dict_pop(serialized_traitable, k.id_tag);
94
+ filter[h(k.id_tag)] = id_value;
95
+ filter[h(k.rev_tag)] = revision;
96
+
97
+ const int rev = revision.cast<int>();
98
+ if (rev <= 0)
99
+ throw py::value_error(
100
+ py::str("Revision must be >= 1: {}; serialized traitable:\n{}")
101
+ .format(revision, serialized_traitable));
102
+
103
+ py::list rev_parts;
104
+ py::list field_stages;
105
+
106
+ for (auto [key, val] : serialized_traitable) {
107
+ py::dict lit_val;
108
+ lit_val[h(k.literal)] = val;
109
+
110
+ PyObject* field_ref = PyUnicode_Concat(k.dollar, key.ptr());
111
+ if (!field_ref)
112
+ throw py::error_already_set();
113
+ auto field_path = py::reinterpret_steal<py::object>(field_ref);
114
+
115
+ py::list named_lit_val;
116
+ named_lit_val.append(field_path);
117
+ named_lit_val.append(lit_val);
118
+
119
+ py::dict eq_ed;
120
+ eq_ed[h(k.eq)] = named_lit_val;
121
+ rev_parts.append(eq_ed);
122
+
123
+ py::dict value;
124
+ value[h(k.literal)] = val;
125
+
126
+ py::dict set_field_items;
127
+ set_field_items[h(k.field)] = key;
128
+ set_field_items[h(k.input)] = h(k.root);
129
+ set_field_items[h(k.value)] = value;
130
+
131
+ py::dict set_field;
132
+ set_field[h(k.set_field)] = set_field_items;
133
+
134
+ py::dict replace_with;
135
+ replace_with[h(k.replace_with)] = set_field;
136
+ field_stages.append(replace_with);
137
+ }
138
+
139
+ py::dict rev_condition;
140
+ rev_condition[h(k.and_)] = rev_parts;
141
+
142
+ py::list conditions;
143
+ conditions.append(rev_condition);
144
+ conditions.append(revision);
145
+ conditions.append(py::int_(rev + 1));
146
+
147
+ py::dict update_revision;
148
+ update_revision[h(k.cond)] = conditions;
149
+
150
+ py::dict new_root_items;
151
+ new_root_items[h(k.id_tag)] = id_value;
152
+ new_root_items[h(k.rev_tag)] = update_revision;
153
+
154
+ py::dict new_root;
155
+ new_root[h(k.new_root)] = new_root_items;
156
+
157
+ py::dict replace_root;
158
+ replace_root[h(k.replace_root)] = new_root;
159
+ pipeline.append(replace_root);
160
+
161
+ if (PyList_SetSlice(pipeline.ptr(),
162
+ PyList_GET_SIZE(pipeline.ptr()),
163
+ PyList_GET_SIZE(pipeline.ptr()),
164
+ field_stages.ptr()) != 0)
165
+ throw py::error_already_set();
166
+ }
@@ -0,0 +1,11 @@
1
+ //
2
+ // Created by AMD on 2/9/2025.
3
+ //
4
+ #pragma once
5
+
6
+ #include "py_linkage.h"
7
+
8
+ class MongoCollectionHelper {
9
+ public:
10
+ static void prepare_filter_and_pipeline(py::dict& serialized_traitable, py::dict& filter, py::list& pipeline);
11
+ };
@@ -0,0 +1,166 @@
1
+ //
2
+ // Created by AMD on 2/4/2025.
3
+ //
4
+
5
+ #include <bsoncxx/builder/basic/document.hpp>
6
+
7
+ #include "mongo_db_driver.h"
8
+
9
+ MongoDbDriver MongoDbDriver::Mongo;
10
+
11
+ using namespace mongocxx;
12
+ using namespace bsoncxx::types;
13
+ using namespace bsoncxx::builder::basic;
14
+
15
+ //==== Supported types: bool, int, float, str, datetime, tuple, list, dict
16
+ document dict_to_bson_doc(const py::dict& data); // fwd-decl
17
+
18
+ bson_value::value to_bson(const py::handle& handle) {
19
+ auto value = py::reinterpret_borrow<py::object>(handle);
20
+
21
+ if (py::isinstance<py::bool_>(value))
22
+ return {value.cast<bool>()};
23
+
24
+ if (py::isinstance<py::int_>(value))
25
+ return {static_cast<int64_t>(value.cast<int>())};
26
+
27
+ if (py::isinstance<py::float_>(value))
28
+ return {value.cast<double>()};
29
+
30
+ if (py::isinstance<py::str>(value))
31
+ return {value.cast<std::string>()};
32
+
33
+ if (PyLinkage::is_instance(value, "datetime", "datetime")) {
34
+ try {
35
+ auto timestamp = value.attr("timestamp")().cast<double>(); // get seconds
36
+ auto millis = static_cast<int64_t>(timestamp * 1000); // convert to milliseconds
37
+ return {b_date{std::chrono::milliseconds(millis)}};
38
+
39
+ } catch (const py::cast_error&) {
40
+ throw py::value_error(py::str("Conversion to BSON failed for datetime = {}").format(value));
41
+ }
42
+ }
43
+
44
+ if (py::isinstance<py::list>(value) || py::isinstance<py::tuple>(value)) {
45
+ bsoncxx::builder::basic::array bson_array;
46
+ //for (const auto& item : value.cast<py::list>()) {
47
+ for (const auto& item : value)
48
+ bson_array.append(to_bson(item));
49
+
50
+ return {bson_array.extract()};
51
+ }
52
+
53
+ if (py::isinstance<py::dict>(value)) {
54
+ auto bson_doc = dict_to_bson_doc(value.cast<py::dict>());
55
+ return {bson_doc.extract()};
56
+ }
57
+
58
+ throw py::value_error(py::str("Conversion to BSON: unsupported type {}").format(value));
59
+ }
60
+
61
+ document dict_to_bson_doc(const py::dict& data) {
62
+ bsoncxx::builder::basic::document bson_doc;
63
+ for (auto item : data) {
64
+ auto sub_key = item.first.cast<std::string>();
65
+ bson_doc.append(kvp(sub_key, to_bson(item.second)));
66
+ }
67
+ return bson_doc;
68
+ }
69
+
70
+ int MongoDbDriver::save_via_update_one(mongocxx::collection& coll, py::dict& serialized_data) {
71
+ if (!serialized_data.contains(ID_TAG) || !serialized_data.contains(REV_TAG))
72
+ throw py::value_error(
73
+ py::str("Missing required fields: '{}' or '{}' in '{}'").format(ID_TAG, REV_TAG, serialized_data));
74
+
75
+ //-- Extract `_id` and `_rev`
76
+ auto doc_id = serialized_data[ID_TAG].cast<std::string>();
77
+ auto cur_revision = serialized_data[REV_TAG].cast<int>();
78
+ if (cur_revision < 0)
79
+ throw py::value_error(py::str("{} = {}, but must be >= 0\n{}").format(REV_TAG, cur_revision, serialized_data));
80
+
81
+ if (cur_revision == 0) { // new object
82
+ serialized_data[REV_TAG] = 1;
83
+ auto bson_doc = dict_to_bson_doc(serialized_data);
84
+ auto res = coll.insert_one(bson_doc.view());
85
+ return res ? 1 : -1;
86
+ }
87
+
88
+ //---- This is an existing object - update it
89
+
90
+ PyDict_DelItemString(serialized_data.ptr(), ID_TAG); // get rid of _id
91
+ PyDict_DelItemString(serialized_data.ptr(), REV_TAG); // and _rev
92
+ auto the_doc = dict_to_bson_doc(serialized_data);
93
+
94
+ auto filter = make_document(
95
+ kvp(ID_TAG, doc_id),
96
+ kvp(REV_TAG, cur_revision)
97
+ );
98
+
99
+ //-- build a _rev condition
100
+
101
+ bsoncxx::builder::basic::array conditions;
102
+ for (auto doc_elem : the_doc.extract().view()) {
103
+ auto name = doc_elem.key();
104
+ auto value = doc_elem.get_value();
105
+ conditions.append(
106
+ make_document(kvp("$eq", make_array("$" + std::string(name), make_document(kvp("$literal", value)))))
107
+ );
108
+ }
109
+ auto rev_condition = make_document(kvp("$and", conditions));
110
+
111
+ auto update_revision = make_document(
112
+ kvp(
113
+ "$cond",
114
+ make_array(
115
+ rev_condition, // if each field is equal to is prev value
116
+ cur_revision, // then, keep the revision as is
117
+ cur_revision+1 // else, increment it
118
+ )
119
+ )
120
+ );
121
+
122
+ //-- build cmds
123
+
124
+ pipeline cmds;
125
+ cmds.add_fields(
126
+ make_document(kvp("$replaceRoot", make_document(kvp("newRoot", make_document(kvp(ID_TAG, doc_id), kvp(REV_TAG, update_revision))))))
127
+ );
128
+ for (auto doc_elem : the_doc.extract().view()) {
129
+ auto name = doc_elem.key();
130
+ auto value = doc_elem.get_value();
131
+ cmds.add_fields(
132
+ make_document(
133
+ kvp("$replaceWith",
134
+ make_document(
135
+ kvp("$setField",
136
+ make_document(
137
+ kvp("field", name),
138
+ kvp("input", "$$ROOT"),
139
+ kvp("value", make_document(kvp("$literal", value)))
140
+ )
141
+ )
142
+ )
143
+ )
144
+ )
145
+ );
146
+ }
147
+
148
+ //auto update_doc = make_document(kvp("$set", ))
149
+ auto res = coll.update_one(filter.view(), cmds);
150
+ if (!res)
151
+ return -1;
152
+
153
+ if (res->matched_count() == 0) { //-- e.g., restore from deleted
154
+ the_doc.append(kvp(ID_TAG, doc_id));
155
+ the_doc.append(kvp(REV_TAG, cur_revision));
156
+
157
+ auto res2 = coll.insert_one(the_doc.view());
158
+ return res2 ? cur_revision : -1;
159
+ }
160
+
161
+ if (res->matched_count() != 1)
162
+ return -1;
163
+
164
+ return res->modified_count() != 1 ? cur_revision : cur_revision+1;
165
+ }
166
+
@@ -0,0 +1,52 @@
1
+ //
2
+ // Created by AMD on 2/4/2025.
3
+ //
4
+ #pragma once
5
+
6
+ #include <mongocxx/client.hpp>
7
+ #include <mongocxx/instance.hpp>
8
+
9
+ #include "py_linkage.h"
10
+
11
+ class MongoDbDriver {
12
+ mongocxx::instance m_instance{};
13
+
14
+ public:
15
+ inline static const char ID_TAG[] = "_id";
16
+ inline static const char REV_TAG[] = "_rev";
17
+
18
+ static MongoDbDriver Mongo;
19
+
20
+ static mongocxx::client *connect(const std::string& host_name = "localhost") {
21
+ return new mongocxx::client{mongocxx::uri{std::string("mongodb://") + host_name + ":27017"}};
22
+ }
23
+
24
+ static mongocxx::collection *collection(const mongocxx::client& client, const std::string& db_name, const std::string& coll_name) {
25
+ mongocxx::database db = client[db_name];
26
+ return new mongocxx::collection( db[coll_name] );
27
+ }
28
+
29
+ static int save_via_update_one(mongocxx::collection& coll, py::dict& serialized_data);
30
+ };
31
+
32
+ //bsoncxx::document::value doc = bsoncxx::builder::basic::make_document(
33
+ // bsoncxx::builder::basic::kvp("name", "Alice"),
34
+ // bsoncxx::builder::basic::kvp("age", 30),
35
+ // bsoncxx::builder::basic::kvp("city", "New York")
36
+ //);
37
+ //
38
+ //coll.insert_one(doc.view());
39
+ //std::cout << "Inserted a document!" << std::endl;
40
+ //
41
+ //// Find a document
42
+ //auto maybe_result = coll.find_one(bsoncxx::builder::basic::make_document(
43
+ // bsoncxx::builder::basic::kvp("name", "Alice")
44
+ //));
45
+ //
46
+ //if (maybe_result) {
47
+ //std::cout << "Found document: " << bsoncxx::to_json(maybe_result->view()) << std::endl;
48
+ //} else {
49
+ //std::cout << "No document found!" << std::endl;
50
+ //}
51
+ //
52
+ //return 0;
@@ -0,0 +1,42 @@
1
+ //
2
+ // Created by IAP on 2/7/2025.
3
+ //
4
+ #include <pybind11/pybind11.h>
5
+ #include <pybind11/stl.h>
6
+
7
+ //#include "mongo_db_driver.h"
8
+ #include "mongo_collection_helper.h"
9
+
10
+ namespace py = pybind11;
11
+
12
+ PYBIND11_MODULE(py10x_infra, m)
13
+ {
14
+ m.doc() = "10x infra";
15
+ m.attr("__version__") =
16
+ #ifdef VERSION_INFO
17
+ VERSION_INFO
18
+ #else
19
+ "unknown"
20
+ #endif
21
+ ;
22
+ // py::class_<MongoDbDriver>(m, "MongoDbDriver")
23
+ // .def_static("client", &MongoDbDriver::connect, py::return_value_policy::take_ownership) // Ensures Python owns the returned client
24
+ // .def_static("collection", &MongoDbDriver::collection, py::return_value_policy::take_ownership, py::keep_alive<1, 0>()) // This alone is NOT enough
25
+ // .def_static("save_via_update_one", &MongoDbDriver::save_via_update_one);
26
+ // ;
27
+ // py::class_<mongocxx::v_noabi::client>(m, "mongocxx_client")
28
+ // .def("uri", &mongocxx::client::uri)
29
+ // ;
30
+ // py::class_<mongocxx::v_noabi::collection>(m, "mongocxx_collection")
31
+ // .def("name", &mongocxx::v_noabi::collection::name)
32
+ // ;
33
+
34
+ py::class_<MongoCollectionHelper>(m, "MongoCollectionHelper")
35
+ .def_static(
36
+ "prepare_filter_and_pipeline",
37
+ &MongoCollectionHelper::prepare_filter_and_pipeline,
38
+ py::arg("serialized_traitable"),
39
+ py::arg("filter"),
40
+ py::arg("pipeline"))
41
+ ;
42
+ }
@@ -0,0 +1,269 @@
1
+ //
2
+ // Created by AMD on 12/17/2024.
3
+ //
4
+
5
+ #pragma once
6
+ #include <iostream>
7
+ #include <pybind11/pybind11.h>
8
+ #include <pybind11/pytypes.h>
9
+ #include <pybind11/functional.h>
10
+ #include <pybind11/stl.h>
11
+
12
+ // PY10X_API: controls symbol visibility for cross-module C++ inheritance and
13
+ // use of types (vtables, typeinfo, pointers, subclassing) from other extensions
14
+ // such as cxxfin.
15
+ // On Windows: dllexport when building py10x_kernel, dllimport when consuming.
16
+ // On GCC/Clang: visibility("default") overrides -fvisibility=hidden for these classes.
17
+ #if defined(_WIN32)
18
+ # if defined(PY10X_KERNEL_EXPORTS)
19
+ # define PY10X_API __declspec(dllexport)
20
+ # else
21
+ # define PY10X_API __declspec(dllimport)
22
+ # endif
23
+ #elif defined(__GNUC__) || defined(__clang__)
24
+ # define PY10X_API __attribute__((visibility("default")))
25
+ #else
26
+ # define PY10X_API
27
+ #endif
28
+
29
+ // MSVC C4251: exported classes expose py::object/STL members whose types are
30
+ // not dll-exported. Safe here — pybind11 objects are opaque handles to the
31
+ // Python runtime, not independently allocated across the DLL boundary.
32
+ #if defined(_MSC_VER)
33
+ # pragma warning(disable: 4251)
34
+ #endif
35
+
36
+ namespace py = pybind11;
37
+
38
+ using dict_iter = py::detail::dict_iterator;
39
+
40
+ template<>
41
+ struct std::equal_to<py::object> {
42
+ bool operator() (const py::object& a, const py::object& b) const { return a.equal(b); }
43
+ };
44
+
45
+ template<>
46
+ struct std::hash<py::object> {
47
+ std::size_t operator() (py::object const& key) const noexcept { return py::hash(key); }
48
+ };
49
+
50
+ template<>
51
+ struct std::equal_to<py::args> {
52
+ bool operator() (const py::args& a, const py::args& b) const { return a.equal(b); }
53
+ };
54
+
55
+ template<>
56
+ struct std::hash<py::args> {
57
+ std::size_t operator() (const py::args& key) const noexcept { return py::hash(key); }
58
+ };
59
+
60
+ class BTraitable;
61
+ class BTraitableClass;
62
+ class BTrait;
63
+
64
+ enum class CORE_10X {
65
+ PACKAGE_NAME,
66
+ XNONE_MODULE_NAME,
67
+ XNONE_CLASS_NAME,
68
+ RC_MODULE_NAME,
69
+ RC_TRUE_NAME,
70
+ NUCLEUS_MODULE_NAME,
71
+ NUCLEUS_CLASS_NAME,
72
+ TRAITABLE_ID_MODULE_NAME,
73
+ TRAITABLE_ID_CLASS_NAME,
74
+ TRAIT_METHOD_ERROR_MODULE_NAME,
75
+ TRAIT_METHOD_ERROR_CLASS_NAME,
76
+ PACKAGE_REFACTORING_MODULE_NAME,
77
+ PACKAGE_REFACTORING_CLASS_NAME,
78
+ PACKAGE_REFACTORING_FIND_CLASS,
79
+ PACKAGE_REFACTORING_FIND_CLASS_ID
80
+ };
81
+
82
+ class PY10X_API PyLinkage {
83
+ //-- builtins
84
+ py::object m_type_cls;
85
+ py::object m_bool_cls;
86
+ py::object m_int_cls;
87
+ py::object m_float_cls;
88
+ py::object m_complex_cls;
89
+ py::object m_str_cls;
90
+ py::object m_list_cls;
91
+ py::object m_tuple_cls;
92
+ py::object m_set_cls;
93
+ py::object m_dict_cls;
94
+ py::object f_dict_get;
95
+ py::object m_bytes_cls;
96
+ py::object m_classmethod_cls;
97
+ py::object m_datetime_cls;
98
+ py::object m_date_cls;
99
+ py::object f_fromisoformat;
100
+ py::object f_fromordinal;
101
+ py::object f_toordinal;
102
+
103
+ //-- 10x related stuff
104
+ std::string m_py_package_name;
105
+ py::dict m_package_names;
106
+ py::object m_xnone;
107
+ py::object m_nucleus_class;
108
+ py::object m_traitable_id_class;
109
+ py::object m_trait_method_error_class;
110
+ py::object f_find_class;
111
+ py::object f_find_class_id;
112
+ py::object f_uuid7;
113
+
114
+ py::args m_choices_args;
115
+ py::args m_style_sheet_args;
116
+
117
+ py::object m_rc_true;
118
+
119
+ static std::streambuf *s_py_stream_buf;
120
+ static std::streambuf *s_std_stream_buf;
121
+
122
+ std::string name_from_dict(const CORE_10X& enum_key, bool module_name = false);
123
+
124
+ void get_rc_true();
125
+
126
+ void create_choices_args() {
127
+ m_choices_args = py::make_tuple(m_xnone, py::str("__choices"));
128
+ }
129
+
130
+ void create_style_sheet_args() {
131
+ m_style_sheet_args = py::make_tuple(m_xnone, py::str("__style_sheet"));
132
+ }
133
+
134
+ static void add_to_py_path(const std::string& path);
135
+
136
+ public:
137
+ static PyLinkage* s_py_linkage;
138
+
139
+ static void init(const py::dict& package_names);
140
+
141
+ explicit PyLinkage(const py::dict& package_names);
142
+
143
+ static py::object type(const py::object& v) { return py::type::of(v); }
144
+
145
+ static py::object type_class() { return s_py_linkage->m_type_cls; }
146
+ static py::object bool_class() { return s_py_linkage->m_bool_cls; }
147
+ static py::object int_class() { return s_py_linkage->m_int_cls; }
148
+ static py::object float_class() { return s_py_linkage->m_float_cls; }
149
+ static py::object complex_class() { return s_py_linkage->m_complex_cls; }
150
+ static py::object str_class() { return s_py_linkage->m_str_cls; }
151
+ static py::object bytes_class() { return s_py_linkage->m_bytes_cls; }
152
+ static py::object classmethod_class() { return s_py_linkage->m_classmethod_cls; }
153
+
154
+ static py::object datetime_class() { return s_py_linkage->m_datetime_cls; }
155
+ static py::object date_class() { return s_py_linkage->m_date_cls; }
156
+ static py::object fromisoformat(const py::object& text) { return s_py_linkage->f_fromisoformat(text); }
157
+ static py::object fromordinal(int d) { return s_py_linkage->f_fromordinal(d); }
158
+ static int toordinal(const py::object& a_date) { return s_py_linkage->f_toordinal(a_date).cast<int>(); }
159
+
160
+ static py::object list_class() { return s_py_linkage->m_list_cls; }
161
+ static py::object tuple_class() { return s_py_linkage->m_tuple_cls; }
162
+ static py::object set_class() { return s_py_linkage->m_set_cls; }
163
+ static py::object dict_class() { return s_py_linkage->m_dict_cls; }
164
+
165
+ static py::object dict_get(const py::dict& d, const py::object& key) {
166
+ return s_py_linkage->f_dict_get(d, key, XNone());
167
+ }
168
+
169
+ static py::object XNone() { return s_py_linkage->m_xnone; }
170
+ static py::object nucleus_class() { return s_py_linkage->m_nucleus_class; }
171
+ static const py::args& choices_args() { return s_py_linkage->m_choices_args; }
172
+ static const py::args& style_sheet_args() { return s_py_linkage->m_style_sheet_args; }
173
+
174
+ static const py::object& RC_TRUE() {
175
+ if (!s_py_linkage->m_rc_true)
176
+ s_py_linkage->get_rc_true();
177
+ return s_py_linkage->m_rc_true;
178
+ }
179
+
180
+ static bool issubclass(const py::object& cls, const py::object& base_class) {
181
+ //py::object base_type = py::type::of<BNucleus>();
182
+ int res = PyObject_IsSubclass(cls.ptr(), base_class.ptr());
183
+ if (res == -1)
184
+ throw py::error_already_set();
185
+ return res == 1;
186
+ }
187
+
188
+ static py::object same_exact_type(const py::object& list_like);
189
+
190
+ static py::object dict_keys(const py::object& dict) {
191
+ return dict.attr("keys")();
192
+ }
193
+
194
+ static py::object dict_values(const py::object& dict) {
195
+ return dict.attr("values")();
196
+ }
197
+
198
+ static py::object traitable_id(const py::object& id_value, const py::object& coll_name) {
199
+ return s_py_linkage->m_traitable_id_class(id_value, coll_name);
200
+ }
201
+
202
+ static py::object find_class(const py::str& class_id) {
203
+ return s_py_linkage->f_find_class(class_id);
204
+ }
205
+
206
+ static py::object find_class_id(const py::object& cls) {
207
+ return s_py_linkage->f_find_class_id(cls);
208
+ }
209
+
210
+ static std::size_t python_id(const py::object& obj) {
211
+ return py::module_::import("builtins").attr("id")(obj).cast<std::size_t>();
212
+ }
213
+
214
+ static py::object uuid7() {
215
+ auto id7 = s_py_linkage->f_uuid7();
216
+ return id7.attr("hex");
217
+ }
218
+
219
+ static py::object pickle(const py::object& data) {
220
+ try {
221
+ return py::module_::import("pickle").attr("dumps")(data);
222
+ }
223
+ catch (const py::error_already_set& e) {
224
+ throw py::value_error(py::str("Failed to pickle\n{}").format(e.what()));
225
+ }
226
+ }
227
+
228
+ static py::object unpickle(const py::object& data) {
229
+ try {
230
+ return py::module_::import("pickle").attr("loads")(data);
231
+ }
232
+ catch (const py::error_already_set& e) {
233
+ throw py::value_error(py::str("Failed to unpickle\n{}").format(e.what()));
234
+ }
235
+ }
236
+
237
+ // will throw if class_module.class_name is not importable
238
+ static bool is_instance(const py::object& obj, const char* py_class_module, const char* py_class_name) {
239
+ py::object py_class = py::module_::import(py_class_module).attr(py_class_name);
240
+ return py::isinstance(obj, py_class);
241
+ }
242
+
243
+ static void clear() {
244
+ if (s_std_stream_buf) {
245
+ std::cout.rdbuf(s_std_stream_buf);
246
+ delete s_py_stream_buf;
247
+ s_py_stream_buf = nullptr;
248
+ s_std_stream_buf = nullptr;
249
+ }
250
+ if (s_py_linkage) {
251
+ delete s_py_linkage;
252
+ s_py_linkage = nullptr;
253
+ }
254
+ }
255
+
256
+ static py::object create_trait_method_error(
257
+ BTraitable* obj,
258
+ const BTraitableClass* cls,
259
+ const py::str& trait_name,
260
+ const py::object& method_name,
261
+ const py::object* value = nullptr,
262
+ const py::args* args = nullptr,
263
+ const py::error_already_set* other_exc = nullptr
264
+ );
265
+
266
+ static void redirect_stdout_to_python();
267
+
268
+ };
269
+
@@ -0,0 +1,78 @@
1
+ [project]
2
+ name = "py10x-infra"
3
+ description = "10X Platform Core Infrastructure"
4
+ license = {text = "MIT"}
5
+ readme = "README.md"
6
+ requires-python = ">=3.11,<3.14"
7
+ dynamic = ["version"]
8
+
9
+ authors = [
10
+ { name = "10X CONCEPTS LLC", email = "py10x@10x-software.org" },
11
+ ]
12
+
13
+ classifiers = [
14
+ "License :: OSI Approved :: MIT License",
15
+ "Programming Language :: Python :: 3",
16
+ "Programming Language :: Python :: 3.11",
17
+ "Programming Language :: Python :: 3.12",
18
+ "Programming Language :: Python :: 3.13",
19
+ "Operating System :: Microsoft :: Windows",
20
+ "Operating System :: POSIX :: Linux",
21
+ "Operating System :: MacOS",
22
+ "Development Status :: 4 - Beta",
23
+ ]
24
+
25
+ [project.urls]
26
+ Homepage = "https://10x-software.org"
27
+
28
+ [build-system]
29
+ requires = [
30
+ "scikit-build-core>=0.10.7",
31
+ "setuptools-scm>=9.2.2",
32
+ "pybind11-stubgen==2.5.5", # post-build .pyi generation (cxx10x/cmake/PybindStubs.cmake)
33
+ ]
34
+ build-backend = "scikit_build_core.build"
35
+
36
+ [tool.scikit-build]
37
+ metadata.version.provider = "scikit_build_core.metadata.setuptools_scm"
38
+ build-dir = "../../build/py10x-build/py10x-infra/{build_type}/{wheel_tag}"
39
+ cmake.build-type = "Debug"
40
+ editable.rebuild = true
41
+ editable.verbose = false
42
+ #-- the sdist ships copies of what CMakeLists.txt takes from outside this directory, made by its sdist-state configure
43
+ sdist.cmake = true
44
+ sdist.include = ["cmake/", "py_linkage.h"]
45
+ sdist.exclude = [".idea/", "cmake-build-*/"]
46
+
47
+ [tool.scikit-build.cmake.define]
48
+ XX_CXX_SANITIZERS = {env = "XX_CXX_SANITIZERS", default = "OFF"}
49
+
50
+ [[tool.scikit-build.overrides]]
51
+ if.platform-system = "^win"
52
+ cmake.args = ["-T", "v143,version=14.44"]
53
+
54
+ #-- building the sdist (a configure that only copies files) and a wheel from it (pip's fallback where no wheel
55
+ #-- matches): a throwaway build dir, not the editable one, and Release like the published wheels
56
+ [[tool.scikit-build.overrides]]
57
+ if.any.state = "sdist"
58
+ if.any.from-sdist = true
59
+ build-dir = ""
60
+ cmake.build-type = "Release"
61
+ editable.rebuild = false
62
+
63
+ # cibuildwheel builds in isolation: throwaway build dir, no rebuild (it needs a persistent one).
64
+ # Published wheels are always Release, whatever the local default above is.
65
+ [tool.cibuildwheel]
66
+ config-settings = { "build-dir" = "", "editable.rebuild" = "false", "cmake.build-type" = "Release" }
67
+
68
+ # uv rebuilds this package (instead of reusing its cached wheel) when this file or the sanitizer
69
+ # switch, which CMake reads from the env, changes. Not on a new commit or tag: a pull would then
70
+ # rebuild every C++ package at once, racing (py10x-fin-base-cxx's build imports the kernel, which
71
+ # rebuilds on import in the build dir uv is reconfiguring). Source edits rebuild on import anyway;
72
+ # the setuptools-scm version stays at the last `./xx-build init` / `set-mode` / `rebuild`.
73
+ [tool.uv]
74
+ cache-keys = [{ file = "pyproject.toml" }, { env = "XX_CXX_SANITIZERS" }]
75
+
76
+ [tool.setuptools_scm]
77
+ root = "../.."
78
+ git_describe_command = "git describe --dirty --tags --long --match 'py10x-infra-*' --abbrev=40"
@@ -0,0 +1,34 @@
1
+ from py10x_infra import MongoCollectionHelper
2
+ from core_10x.traitable_id import ID
3
+ from core_10x.trait_filter import f
4
+ from core_10x.code_samples.person import Person
5
+ from infra_10x.mongodb_store import MongoStore
6
+
7
+ def test_prepare_filter_and_pipeline():
8
+ serialized_traitable = dict(
9
+ _id = 'AAA',
10
+ _rev = 10,
11
+ name = 'test',
12
+ age = 60
13
+ )
14
+ filter = {}
15
+ pipeline = []
16
+ data = dict(serialized_traitable)
17
+ MongoCollectionHelper.prepare_filter_and_pipeline(data, filter, pipeline)
18
+
19
+ assert data|filter == serialized_traitable
20
+ assert filter == {'_id': 'AAA', '_rev': 10}
21
+ assert pipeline == [{'$replaceRoot': {'newRoot': {'_id': 'AAA', '_rev': {'$cond': [{'$and': [{'$eq': ['$name', {'$literal': 'test'}]}, {'$eq': ['$age', {'$literal': 60}]}]}, 10, 11]}}}}, {'$replaceWith': {'$setField': {'field': 'name', 'input': '$$ROOT', 'value': {'$literal': 'test'}}}}, {'$replaceWith': {'$setField': {'field': 'age', 'input': '$$ROOT', 'value': {'$literal': 60}}}}]
22
+
23
+
24
+
25
+ def test_load():
26
+ with MongoStore.instance(hostname = 'localhost', dbname = 'test', username = '', password = ''):
27
+ Person(last_name='Pevzner', first_name='Ilya', weight_lbs=200, _replace=True).save()
28
+ p = Person.load_many(f(first_name='Ilya'),_at_most=1)[0]
29
+ assert p == Person.load(ID(p.id().value))
30
+
31
+
32
+ if __name__=='__main__':
33
+ test_prepare_filter_and_pipeline()
34
+ test_load()