Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: Copyright (c) 2023-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. # noqa
# SPDX-FileCopyrightText: Copyright (c) 2023-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0


Expand Down Expand Up @@ -292,6 +292,9 @@ cdef class DataModel:
return self.problem_name

def set_data_model_view(self):
# Rebind from scratch so optional fields that were previously set but
# are now empty (e.g. Q after QP -> LP) do not stick in the C++ view.
self.c_data_model_view.reset(new data_model_view_t[int, double]())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/usr/bin/env bash
set -euo pipefail
rg -n -C 6 'data_model_view_t|except \+' \
  python/cuopt --glob '*.pxd' --glob '*.pyx'

Repository: NVIDIA/cuopt

Length of output: 50368


🏁 Script executed:

#!/usr/bin/env bash
set -euo pipefail

printf '%s\n' '--- data_model.pxd ---'
cat -n python/cuopt/cuopt/linear_programming/data_model/data_model.pxd | sed -n '25,55p'

printf '%s\n' '--- C++ declarations/definitions ---'
rg -n -C 5 'class data_model_view|data_model_view\(|~data_model_view' \
  cpp python --glob '*.{hpp,h,cpp,cc,pyx,pxd}'

printf '%s\n' '--- related constructor declarations ---'
rg -n -C 3 'cdef cppclass data_model_view_t|data_model_view_t\(\)' \
  python/cuopt --glob '*.pxd' --glob '*.pyx'

Repository: NVIDIA/cuopt

Length of output: 18011


🏁 Script executed:

#!/usr/bin/env bash
set -euo pipefail

printf '%s\n' '--- data_model_view_t class definition ---'
cat -n cpp/include/cuopt/mathematical_optimization/io/data_model_view.hpp | sed -n '45,95p'

printf '%s\n' '--- Cython availability ---'
if command -v cython >/dev/null 2>&1; then
  cython --version
else
  printf '%s\n' 'cython: unavailable'
fi

Repository: NVIDIA/cuopt

Length of output: 2916


🌐 Web query:

Cython cppclass omitted default constructor new Foo() exception handling except + implicit constructor declaration

💡 Result:

In Cython, when using a C++ class that has no explicitly declared constructor, Cython does not automatically assume a default constructor exists for the purposes of code generation, often leading to compilation errors or crashes if you attempt to instantiate the class [1][2]. If a class has only one constructor and it is a nullary (default) constructor, Cython may allow it without explicit declaration, but it is best practice to declare it [3][4]. Regarding exception handling: 1. Constructor Exception Handling: If your C++ constructor can throw an exception, you must explicitly declare it with except + [3][4]. If you omit this, C++ exceptions thrown during construction will not be caught or translated into Python exceptions, which can lead to undefined behavior or process termination [3][4][5]. 2. Heap Allocation (new Foo()): When you use new Foo() in Cython, the generated C++ code performs a raw call to the new operator. If you have not declared the constructor with except +, any exception raised during the execution of that constructor (or the allocation itself) will not be handled by Cython [5]. Even if the constructor logic is simple, declaring the constructor as Foo() except + is recommended to ensure that memory allocation failures or constructor-internal exceptions are safely translated into Python exceptions (e.g., MemoryError or others based on the C++ exception type) [3][4][5]. 3. Implicit Constructor Declarations: Because Cython requires explicit declarations for exception propagation, relying on "implicit" declarations is discouraged [5]. If a class lacks a nullary constructor, Cython will fail to stack-allocate it [6][7]. Developers working with such classes often use workarounds such as: - Defining a static factory function that returns the object and declaring it with except + [8]. - Using std::shared_ptr or std::unique_ptr to manage the object on the heap, avoiding the need for a stack-allocated default constructor [9]. In summary, you should explicitly declare your constructors and use except + whenever the C++ code has the potential to raise an exception, including during memory allocation or object initialization, to ensure robust Python-C++ exception bridging [3][4][5].

Citations:


Declare data_model_view_t() with except + in data_model.pxd.

The new data_model_view_t[int, double]() call can otherwise let C++ allocation or construction failures escape the Python boundary.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@python/cuopt/cuopt/linear_programming/data_model/data_model_wrapper.pyx` at
line 297, Update the Cython declaration for data_model_view_t in data_model.pxd
to use except +, ensuring failures from the constructor invoked by
self.c_data_model_view.reset are propagated through the Python boundary.

Source: Path instructions

cdef data_model_view_t[int, double]* c_data_model_view = (
self.c_data_model_view.get()
)
Expand Down
Loading
Loading