Skip to content

Commit 4d2bb4a

Browse files
committed
Set sys.argv correctly (needed for various modules to work correctly)
1 parent dea932d commit 4d2bb4a

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

PythonScript/src/PythonHandler.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,38 @@ void PythonHandler::initPython()
152152

153153
PyRun_SimpleString(initBuffer);
154154

155+
initSysArgv();
156+
157+
155158
// Init Notepad++/Scintilla modules
156159
initModules();
157160

158161
mp_mainThreadState = PyEval_SaveThread();
159162

160163
}
161164

165+
void PythonHandler::initSysArgv()
166+
{
167+
LPWSTR commandLine = ::GetCommandLineW();
168+
int argc;
169+
LPWSTR* argv = ::CommandLineToArgvW(commandLine, &argc);
170+
171+
172+
boost::python::list argvList;
173+
for(int currentArg = 0; currentArg != argc; ++currentArg)
174+
{
175+
std::shared_ptr<char> argInUtf8 = WcharMbcsConverter::wchar2char(argv[currentArg]);
176+
PyObject* unicodeArg = PyUnicode_FromString(argInUtf8.get());
177+
178+
argvList.append(boost::python::handle<>(unicodeArg));
179+
}
180+
181+
boost::python::object sysModule(boost::python::handle<>(boost::python::borrowed(PyImport_AddModule("sys"))));
182+
sysModule.attr("argv") = argvList;
183+
184+
185+
}
186+
162187
void PythonHandler::initModules()
163188
{
164189
importScintilla(mp_scintilla, mp_scintilla1, mp_scintilla2);

PythonScript/src/PythonHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class PythonHandler : public PyProducerConsumer<RunScriptArgs>
8989

9090
// Private methods
9191
void initModules();
92-
92+
void initSysArgv();
9393
static void stopScriptWorker(PythonHandler *handler);
9494
bool containsExtendedChars(char *s);
9595

0 commit comments

Comments
 (0)