Skip to content

Commit 2c178d8

Browse files
committed
Return handles of notepad, editor1, editor2 #256
- provide scintilla HWND of console dialog via console.getScintillaHwnd() - some whitespace changes
1 parent d0b420e commit 2c178d8

File tree

3 files changed

+49
-48
lines changed

3 files changed

+49
-48
lines changed

PythonScript/src/PythonConsole.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ PythonConsole::PythonConsole(HWND hNotepad) :
2828
m_nppData(new NppData)
2929
{
3030
}
31-
/*
31+
/*
3232
//lint -e1554 Direct pointer copy of member 'name' within copy constructor: 'PythonConsole::PythonConsole(const PythonConsole &)')
33-
// We indeed copy pointers, and it's okay. These are not allocated within the
33+
// We indeed copy pointers, and it's okay. These are not allocated within the
3434
// scope of this class but rather passed in and copied anyway.
3535
PythonConsole::PythonConsole(const PythonConsole& other) :
3636
PyProducerConsumer<std::string>(other),
@@ -56,7 +56,6 @@ PythonConsole::~PythonConsole()
5656
{
5757
delete mp_consoleDlg;
5858
delete m_nppData;
59-
6059
}
6160
catch (...)
6261
{
@@ -92,27 +91,27 @@ void PythonConsole::initPython(PythonHandler *pythonHandler)
9291
try
9392
{
9493
mp_mainThreadState = pythonHandler->getMainThreadState();
95-
96-
GILLock gilLock;
94+
95+
GILLock gilLock;
9796

9897
boost::python::object main_module(boost::python::handle<>(boost::python::borrowed(PyImport_AddModule("__main__"))));
9998
boost::python::object main_namespace = main_module.attr("__dict__");
100-
99+
101100
// import code
102101
boost::python::object code = boost::python::import("code");
103102
main_namespace["code"] = code;
104103

105104
// import __main__
106105
main_namespace["__main__"] = main_namespace;
107-
106+
108107
// get ref to code.InteractiveConsole().push()
109108
m_console = eval("code.InteractiveConsole(__main__)", main_namespace, main_namespace);
110109
m_pushFunc = m_console.attr("push");
111110

112111
m_sys = main_namespace["sys"];
113-
114-
115-
}
112+
113+
114+
}
116115
catch(...)
117116
{
118117
PyErr_Print();
@@ -146,7 +145,7 @@ void PythonConsole::showDialog()
146145
assert(mp_consoleDlg);
147146
if (mp_consoleDlg)
148147
{
149-
GILRelease release;
148+
GILRelease release;
150149
mp_consoleDlg->doDialog();
151150
}
152151
}
@@ -156,7 +155,7 @@ void PythonConsole::hideDialog()
156155
assert(mp_consoleDlg);
157156
if (mp_consoleDlg)
158157
{
159-
GILRelease release;
158+
GILRelease release;
160159
mp_consoleDlg->hide();
161160
}
162161
}
@@ -166,8 +165,8 @@ void PythonConsole::message(const char *msg)
166165
assert(mp_consoleDlg);
167166
if (mp_consoleDlg)
168167
{
169-
GILRelease release;
170-
mp_consoleDlg->writeCmdText(strlen(msg), msg);
168+
GILRelease release;
169+
mp_consoleDlg->writeCmdText(strlen(msg), msg);
171170
}
172171
}
173172

@@ -176,7 +175,7 @@ void PythonConsole::clear()
176175
assert(mp_consoleDlg);
177176
if (mp_consoleDlg)
178177
{
179-
GILRelease release;
178+
GILRelease release;
180179
mp_consoleDlg->clearText();
181180
}
182181
}
@@ -191,12 +190,12 @@ void PythonConsole::writeText(boost::python::object text)
191190
assert(mp_consoleDlg);
192191
if (mp_consoleDlg)
193192
{
194-
if (PyUnicode_Check(text.ptr()))
193+
if (PyUnicode_Check(text.ptr()))
195194
{
196195

197196

198-
std::string textToWrite((const char *)boost::python::extract<const char *>(text.attr("__str__")()));
199-
GILRelease release;
197+
std::string textToWrite((const char *)boost::python::extract<const char *>(text.attr("__str__")()));
198+
GILRelease release;
200199
if (m_runStatementExecuted)
201200
{
202201
mp_consoleDlg->writeColoredText(textToWrite.size(), textToWrite.c_str());
@@ -208,8 +207,8 @@ void PythonConsole::writeText(boost::python::object text)
208207
}
209208
else
210209
{
211-
std::string textToWrite((const char *)boost::python::extract<const char *>(text.attr("__str__")()), _len(text));
212-
GILRelease release;
210+
std::string textToWrite((const char *)boost::python::extract<const char *>(text.attr("__str__")()), _len(text));
211+
GILRelease release;
213212
if (m_runStatementExecuted)
214213
{
215214
mp_consoleDlg->writeColoredText(textToWrite.size(), textToWrite.c_str());
@@ -231,15 +230,15 @@ void PythonConsole::writeError(boost::python::object text)
231230
{
232231

233232

234-
std::string textToWrite((const char *)boost::python::extract<const char *>(text.attr("__str__")()));
235-
GILRelease release;
236-
mp_consoleDlg->writeError(textToWrite.size(), textToWrite.c_str());
233+
std::string textToWrite((const char *)boost::python::extract<const char *>(text.attr("__str__")()));
234+
GILRelease release;
235+
mp_consoleDlg->writeError(textToWrite.size(), textToWrite.c_str());
237236
}
238237
else
239238
{
240-
std::string textToWrite((const char *)boost::python::extract<const char *>(text.attr("__str__")()));
241-
GILRelease release;
242-
mp_consoleDlg->writeError(textToWrite.size(),textToWrite.c_str());
239+
std::string textToWrite((const char *)boost::python::extract<const char *>(text.attr("__str__")()));
240+
GILRelease release;
241+
mp_consoleDlg->writeError(textToWrite.size(),textToWrite.c_str());
243242
}
244243
}
245244
}
@@ -248,7 +247,7 @@ void PythonConsole::stopStatement()
248247
{
249248
DWORD threadID;
250249
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PythonConsole::stopStatementWorker, this, 0, &threadID);
251-
250+
252251
}
253252

254253
DWORD PythonConsole::runCommand(boost::python::str text, boost::python::object pyStdout, boost::python::object pyStderr)
@@ -267,9 +266,9 @@ void PythonConsole::runStatement(const char *statement)
267266
mp_consoleDlg->runEnabled(false);
268267
}
269268

270-
// Console statements executed whilst a script is in progress MUST run on a separate
269+
// Console statements executed whilst a script is in progress MUST run on a separate
271270
// thread. Otherwise, we wait for the GIL, no problem, except that that blocks the UI thread
272-
// so if the script happens to be sending a message to scintilla (likely), then
271+
// so if the script happens to be sending a message to scintilla (likely), then
273272
// it will deadlock.
274273
// PyProducerConsumer used here to keep one thread running the actual statements
275274

@@ -326,10 +325,10 @@ void PythonConsole::consume(std::shared_ptr<std::string> statement)
326325

327326
void PythonConsole::stopStatementWorker(PythonConsole *console)
328327
{
329-
GILLock gilLock;
330-
328+
GILLock gilLock;
329+
331330
PyThreadState_SetAsyncExc((long)console->getConsumerThreadID(), PyExc_KeyboardInterrupt);
332-
331+
333332
}
334333

335334
void export_console()
@@ -345,6 +344,7 @@ void export_console()
345344
.def("run", &PythonConsole::runCommand, "Runs a command on the console")
346345
.def("run", &PythonConsole::runCommandNoStderr, "Runs a command on the console")
347346
.def("run", &PythonConsole::runCommandNoStdout, "Runs a command on the console")
347+
.def("getScintillaHwnd", &PythonConsole::getScintillaHwndPython, "Provide HWnd for scintilla of console")
348348
.add_static_property("encoding", &PythonConsole::getEncoding)
349349
.add_property("editor", &PythonConsole::getScintillaWrapper);
350350
//lint +e1793

PythonScript/src/PythonConsole.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef _PYTHONCONSOLE_H
22
#define _PYTHONCONSOLE_H
33

4-
#ifndef _CONSOLEINTERFACE_H
4+
#ifndef _CONSOLEINTERFACE_H
55
#include "ConsoleInterface.h"
66
#endif
77

@@ -24,7 +24,7 @@ class PythonConsole : public PyProducerConsumer<std::string>, public ConsoleInte
2424
public:
2525
explicit PythonConsole(HWND hNotepad);
2626
~PythonConsole();
27-
27+
2828
void init(HINSTANCE hInst, NppData& nppData);
2929
void initPython(PythonHandler *pythonHandler);
3030

@@ -42,37 +42,38 @@ class PythonConsole : public PyProducerConsumer<std::string>, public ConsoleInte
4242
void runStatement(const char *statement);
4343
void stopStatement();
4444
void openFile(const char *filename, idx_t lineNumber);
45-
45+
4646
/* ConsoleInterface end */
4747

4848

4949
static void stopStatementWorker(PythonConsole *console);
50-
50+
5151
DWORD runCommand(boost::python::str text, boost::python::object pyStdout, boost::python::object pyStderr);
5252
DWORD runCommandNoStderr(boost::python::str text, boost::python::object pyStdout)
53-
{
53+
{
5454
boost::python::object sys_module( (boost::python::handle<>(PyImport_ImportModule("sys"))) );
55-
boost::python::object sys_namespace = sys_module.attr("__dict__");
56-
return runCommand(text, pyStdout, sys_namespace["stderr"]);
57-
}
55+
boost::python::object sys_namespace = sys_module.attr("__dict__");
56+
return runCommand(text, pyStdout, sys_namespace["stderr"]);
57+
}
5858

5959
DWORD runCommandNoStdout(boost::python::str text)
60-
{
60+
{
6161
boost::python::object sys_module( (boost::python::handle<>(PyImport_ImportModule("sys"))) );
62-
boost::python::object sys_namespace = sys_module.attr("__dict__");
62+
boost::python::object sys_namespace = sys_module.attr("__dict__");
6363
boost::python::object npp_module( (boost::python::handle<>(PyImport_ImportModule("Npp"))) );
64-
boost::python::object npp_namespace = npp_module.attr("__dict__");
65-
return runCommand(text, npp_namespace["console"], sys_namespace["stderr"]);
66-
}
64+
boost::python::object npp_namespace = npp_module.attr("__dict__");
65+
return runCommand(text, npp_namespace["console"], sys_namespace["stderr"]);
66+
}
6767

6868
HWND getScintillaHwnd();
69+
intptr_t getScintillaHwndPython() { return (intptr_t)getScintillaHwnd(); }
6970

7071
boost::shared_ptr<ScintillaWrapper> getScintillaWrapper() { return mp_scintillaWrapper; }
7172

7273
boost::shared_ptr<ScintillaWrapper> mp_scintillaWrapper;
7374

7475
static boost::python::str getEncoding() { return boost::python::str("utf-8"); }
75-
76+
7677
protected:
7778
virtual void consume(std::shared_ptr<std::string> statement);
7879
virtual void queueComplete();
@@ -83,7 +84,7 @@ class PythonConsole : public PyProducerConsumer<std::string>, public ConsoleInte
8384
PythonConsole(const PythonConsole& other);
8485

8586
ConsoleDialog *mp_consoleDlg;
86-
87+
8788
boost::python::object m_console;
8889
boost::python::object m_pushFunc;
8990
boost::python::object m_sys;

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-
void initSysArgv();
92+
void initSysArgv();
9393
static void stopScriptWorker(PythonHandler *handler);
9494
bool containsExtendedChars(char *s);
9595

0 commit comments

Comments
 (0)