Skip to content

Commit

Permalink
Project: Windows sucks
Browse files Browse the repository at this point in the history
  • Loading branch information
DatCaptainHorse committed Aug 19, 2024
1 parent efd9bef commit b9dd5f4
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions Source/scripting.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import runner;
import filesystem;
import pythonmodule;

// Helper to convert backslashes to forward slashes
std::string to_posix_path(const std::string &path) {
std::string posix_path = path;
std::replace(posix_path.begin(), posix_path.end(), '\\', '/');
return posix_path;
}

// Helper for decrementing Python reference count, checking that GIL is held
void decref_pyobject(PyObject *obj,
const std::source_location &loc = std::source_location::current()) {
Expand Down Expand Up @@ -182,12 +189,15 @@ public:
return;
}

// Add scripts paths and subdirectories to sys.path
// Add scripts paths and subdirectories to sys.path, always use POSIX path since '\'s
// break Python
std::string scriptPath = "import sys\n";
scriptPath += std::format("sys.path.append('{}')\n", get_scripts_path().string());
scriptPath +=
std::format("sys.path.append('{}')\n", to_posix_path(get_scripts_path().string()));
for (const auto &entry : std::filesystem::directory_iterator(get_scripts_path())) {
if (entry.is_directory())
scriptPath += std::format("sys.path.append('{}')\n", entry.path().string());
scriptPath += std::format("sys.path.append('{}')\n",
to_posix_path(entry.path().string()));
}

PyRun_SimpleStringFlags(scriptPath.c_str(), nullptr);
Expand Down

0 comments on commit b9dd5f4

Please sign in to comment.