Skip to content

Commit 0a6734b

Browse files
committed
Revert Optimize.cpp
1 parent 565f476 commit 0a6734b

1 file changed

Lines changed: 9 additions & 147 deletions

File tree

Libs/Optimize/Optimize.cpp

Lines changed: 9 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
#include <Libs/Particles/ParticleFile.h>
2828
#include <Project/Project.h>
2929

30-
#include <iostream>
31-
3230
#include "Libs/Optimize/Domain/Surface.h"
3331
#include "Libs/Optimize/Utils/ObjectReader.h"
3432
#include "Libs/Optimize/Utils/ObjectWriter.h"
@@ -78,165 +76,29 @@ bool Optimize::Run() {
7876
ShapeWorksUtils::setup_threads();
7977

8078
if (m_python_filename != "") {
81-
82-
83-
#ifdef _WIN32
84-
{
85-
std::string found_path = find_in_path("python.exe");
86-
if (found_path != "") {
87-
std::cerr << "python.exe found in: " << found_path << "\n";
88-
_putenv_s("PYTHONHOME", found_path.c_str());
89-
90-
// Add all potential conda DLL directories to PATH
91-
std::string dll_paths = found_path + "\\Library\\bin;" +
92-
found_path + "\\Library\\mingw-w64\\bin;" +
93-
found_path + "\\DLLs;" +
94-
found_path + "\\bin;" +
95-
found_path + "\\Scripts";
96-
97-
char* current_path = getenv("PATH");
98-
std::string new_path = dll_paths + ";" + (current_path ? current_path : "");
99-
_putenv_s("PATH", new_path.c_str());
100-
101-
std::cerr << "Updated PATH for conda DLLs\n";
102-
}
103-
}
104-
#endif
105-
10679
#ifdef _WIN32
107-
// Save current directory
108-
char original_dir[MAX_PATH];
109-
_getcwd(original_dir, MAX_PATH);
110-
std::cerr << "Original CWD: " << original_dir << "\n";
111-
11280
// need to set PYTHONHOME to the same directory as python.exe on Windows
11381
std::string found_path = find_in_path("python.exe");
11482
if (found_path != "") {
11583
std::cerr << "python.exe found in: " << found_path << "\n";
11684
_putenv_s("PYTHONHOME", found_path.c_str());
11785
}
118-
// Change to safe directory for Python init
119-
_chdir("C:\\");
12086
#endif
12187

122-
std::cerr << "about to call py::initialize_interpreter\n";
12388
py::initialize_interpreter();
124-
std::cerr << "Done calling py::initialize_interpreter\n";
125-
126-
// Before importing anything numpy-related
127-
py::exec(R"(
128-
import sys
129-
import types
130-
131-
# Create a fake numpy.__config__ module
132-
config_module = types.ModuleType('numpy.__config__')
133-
config_module.show_config = lambda: None
134-
sys.modules['numpy.__config__'] = config_module
135-
)");
136-
137-
// Test if we can import the core numpy extension directly
138-
std::cerr << "Testing numpy core extension import...\n";
139-
try {
140-
py::exec("import numpy.core.multiarray");
141-
std::cerr << "numpy.core.multiarray import: SUCCESS\n";
142-
} catch (py::error_already_set& e) {
143-
std::cerr << "numpy.core.multiarray FAILED: " << e.what() << "\n";
144-
}
145-
146-
// Test if we can import numpy's _internal module
147-
std::cerr << "Testing numpy._internal import...\n";
148-
try {
149-
py::exec("import numpy._internal");
150-
std::cerr << "numpy._internal import: SUCCESS\n";
151-
} catch (py::error_already_set& e) {
152-
std::cerr << "numpy._internal FAILED: " << e.what() << "\n";
153-
}
154-
155-
// Test basic DLL loading capability
156-
std::cerr << "Testing basic extension import...\n";
157-
try {
158-
py::exec("import _ctypes"); // This is a built-in extension
159-
std::cerr << "_ctypes import: SUCCESS\n";
160-
} catch (py::error_already_set& e) {
161-
std::cerr << "_ctypes FAILED: " << e.what() << "\n";
162-
}
163-
164-
// Test the actual failing import to get the real error
165-
std::cerr << "Testing numpy.__config__ import directly...\n";
166-
try {
167-
py::exec("from numpy.__config__ import show_config");
168-
std::cerr << "numpy.__config__ import: SUCCESS\n";
169-
} catch (py::error_already_set& e) {
170-
std::cerr << "REAL ERROR from numpy.__config__: " << e.what() << "\n";
171-
}
172-
173-
std::cerr << "Attempting to call import numpy\n";
174-
// Right after py::initialize_interpreter()
175-
try {
176-
py::exec("import numpy; print('NumPy version:', numpy.__version__)");
177-
} catch (py::error_already_set& e) {
178-
std::cerr << "NumPy test: " << e.what() << "\n";
179-
}
180-
std::cerr << "Finished calling import numpy\n";
181-
182-
// Diagnostic with explicit error handling
183-
try {
184-
std::cerr << "Starting diagnostic...\n";
185-
py::exec("import sys");
186-
py::exec("print('Python executable:', sys.executable)");
187-
py::exec("print('Python version:', sys.version)");
188-
189-
std::cerr << "Testing numpy.__config__ import...\n";
190-
py::exec(R"(
191-
try:
192-
from numpy.__config__ import show_config
193-
print("numpy.__config__ import: SUCCESS")
194-
except ImportError as e:
195-
print("numpy.__config__ import FAILED:", str(e))
196-
)");
197-
198-
std::cerr << "Testing basic numpy import...\n";
199-
py::exec(R"(
200-
try:
201-
import numpy
202-
print("numpy import: SUCCESS")
203-
except ImportError as e:
204-
print("numpy import FAILED:", str(e))
205-
)");
206-
207-
} catch (py::error_already_set& e) {
208-
std::cerr << "Diagnostic failed with Python error: " << e.what() << "\n";
209-
}
210-
211-
#ifdef _WIN32
212-
// Restore original directory
213-
_chdir(original_dir);
214-
#endif
21589

21690
auto dir = m_python_filename;
217-
auto filename = dir.substr(dir.find_last_of("/\\") + 1); // Handle both / and \ on Windows
91+
92+
auto filename = dir.substr(dir.find_last_of("/") + 1);
21893
SW_LOG("Running Python File: {}", filename);
21994
filename = filename.substr(0, filename.length() - 3); // remove .py
220-
dir = dir.substr(0, dir.find_last_of("/\\") + 1); // Handle both / and \ on Windows
221-
222-
// Debug the actual values
223-
std::cerr << "m_python_filename: '" << m_python_filename << "'\n";
224-
std::cerr << "Parsed dir: '" << dir << "'\n";
225-
std::cerr << "Parsed filename: '" << filename << "'\n";
226-
227-
// Do sys.path manipulation in raw Python to avoid triggering imports
228-
std::string python_code = R"(
229-
import sys
230-
print("sys.path before:", sys.path)
231-
sys.path.insert(1, r")" + dir +
232-
R"(")
233-
print("sys.path after:", sys.path)
234-
)";
235-
236-
std::cerr << "About to execute Python code:\n" << python_code << "\n";
237-
py::exec(python_code);
238-
239-
std::cerr << "About to import module: " << filename << "\n";
95+
dir = dir.substr(0, dir.find_last_of("/") + 1);
96+
97+
py::module sys = py::module::import("sys");
98+
py::print(sys.attr("path"));
99+
sys.attr("path").attr("insert")(1, dir);
100+
py::print(sys.attr("path"));
101+
240102
py::module module = py::module::import(filename.c_str());
241103
py::object result = module.attr("run")(this);
242104
}

0 commit comments

Comments
 (0)