1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Helper functions/classes for the wxPython extension module
7 // Created: 1-July-1997
9 // Copyright: (c) 1998 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
16 #include "wx/wxPython/wxPython_int.h"
17 #include "wx/wxPython/pyistream.h"
20 #include <wx/msw/private.h>
21 #include <wx/msw/winundef.h>
22 #include <wx/msw/msvcrt.h>
29 #include <gdk/gdkprivate.h>
30 #include <wx/gtk/win_gtk.h>
31 #define GetXWindow(wxwin) GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)
35 #include "wx/x11/privx.h"
36 #define GetXWindow(wxwin) ((Window)(wxwin)->GetHandle())
40 #include <wx/mac/private.h>
43 #include <wx/clipbrd.h>
44 #include <wx/mimetype.h>
47 //----------------------------------------------------------------------
49 #if PYTHON_API_VERSION <= 1007 && wxUSE_UNICODE
50 #error Python must support Unicode to use wxWindows Unicode
53 //----------------------------------------------------------------------
55 wxPyApp
* wxPythonApp
= NULL
; // Global instance of application object
56 bool wxPyDoCleanup
= False
;
57 bool wxPyDoingCleanup
= False
;
60 #ifdef WXP_WITH_THREAD
61 struct wxPyThreadState
{
63 PyThreadState
* tstate
;
65 wxPyThreadState(unsigned long _tid
=0, PyThreadState
* _tstate
=NULL
)
66 : tid(_tid
), tstate(_tstate
) {}
69 #include <wx/dynarray.h>
70 WX_DECLARE_OBJARRAY(wxPyThreadState
, wxPyThreadStateArray
);
71 #include <wx/arrimpl.cpp>
72 WX_DEFINE_OBJARRAY(wxPyThreadStateArray
);
74 wxPyThreadStateArray
* wxPyTStates
= NULL
;
75 wxMutex
* wxPyTMutex
= NULL
;
79 static PyObject
* wxPython_dict
= NULL
;
80 static PyObject
* wxPyAssertionError
= NULL
;
82 PyObject
* wxPyPtrTypeMap
= NULL
;
85 #ifdef __WXMSW__ // If building for win32...
86 //----------------------------------------------------------------------
87 // This gets run when the DLL is loaded. We just need to save a handle.
88 //----------------------------------------------------------------------
91 HINSTANCE hinstDLL
, // handle to DLL module
92 DWORD fdwReason
, // reason for calling function
93 LPVOID lpvReserved
// reserved
96 // If wxPython is embedded in another wxWindows app then
97 // the inatance has already been set.
98 if (! wxGetInstance())
99 wxSetInstance(hinstDLL
);
104 //----------------------------------------------------------------------
105 // Classes for implementing the wxp main application shell.
106 //----------------------------------------------------------------------
108 IMPLEMENT_ABSTRACT_CLASS(wxPyApp
, wxApp
);
112 m_assertMode
= wxPYAPP_ASSERT_EXCEPTION
;
113 m_startupComplete
= False
;
117 wxPyApp::~wxPyApp() {
121 // This one isn't acutally called... We fake it with _BootstrapApp
122 bool wxPyApp::OnInit() {
127 int wxPyApp::MainLoop() {
130 DeletePendingObjects();
131 bool initialized
= wxTopLevelWindows
.GetCount() != 0;
133 if ( m_exitOnFrameDelete
== Later
) {
134 m_exitOnFrameDelete
= Yes
;
137 retval
= wxApp::MainLoop();
144 bool wxPyApp::OnInitGui() {
146 wxApp::OnInitGui(); // in this case always call the base class version
147 bool blocked
= wxPyBeginBlockThreads();
148 if (wxPyCBH_findCallback(m_myInst
, "OnInitGui"))
149 rval
= wxPyCBH_callCallback(m_myInst
, Py_BuildValue("()"));
150 wxPyEndBlockThreads(blocked
);
155 int wxPyApp::OnExit() {
157 bool blocked
= wxPyBeginBlockThreads();
158 if (wxPyCBH_findCallback(m_myInst
, "OnExit"))
159 rval
= wxPyCBH_callCallback(m_myInst
, Py_BuildValue("()"));
160 wxPyEndBlockThreads(blocked
);
161 wxApp::OnExit(); // in this case always call the base class version
167 void wxPyApp::OnAssert(const wxChar
*file
,
172 // if we're not fully initialized then just log the error
173 if (! m_startupComplete
) {
176 buf
.Printf(wxT("%s(%d): assert \"%s\" failed"),
186 // If the OnAssert is overloaded in the Python class then call it...
188 bool blocked
= wxPyBeginBlockThreads();
189 if ((found
= wxPyCBH_findCallback(m_myInst
, "OnAssert"))) {
190 PyObject
* fso
= wx2PyString(file
);
191 PyObject
* cso
= wx2PyString(file
);
194 mso
= wx2PyString(file
);
196 mso
= Py_None
; Py_INCREF(Py_None
);
198 wxPyCBH_callCallback(m_myInst
, Py_BuildValue("(OiOO)", fso
, line
, cso
, mso
));
203 wxPyEndBlockThreads(blocked
);
205 // ...otherwise do our own thing with it
208 if (m_assertMode
& wxPYAPP_ASSERT_SUPPRESS
)
211 // turn it into a Python exception?
212 if (m_assertMode
& wxPYAPP_ASSERT_EXCEPTION
) {
215 buf
.Printf(wxT("C++ assertion \"%s\" failed in %s(%d)"), cond
, file
, line
);
222 bool blocked
= wxPyBeginBlockThreads();
223 PyObject
* s
= wx2PyString(buf
);
224 PyErr_SetObject(wxPyAssertionError
, s
);
226 wxPyEndBlockThreads(blocked
);
228 // Now when control returns to whatever API wrapper was called from
229 // Python it should detect that an exception is set and will return
230 // NULL, signalling the exception to Python.
233 // Send it to the normal log destination, but only if
234 // not _DIALOG because it will call this too
235 if ( (m_assertMode
& wxPYAPP_ASSERT_LOG
) && !(m_assertMode
& wxPYAPP_ASSERT_DIALOG
)) {
238 buf
.Printf(wxT("%s(%d): assert \"%s\" failed"),
247 // do the normal wx assert dialog?
248 if (m_assertMode
& wxPYAPP_ASSERT_DIALOG
)
249 wxApp::OnAssert(file
, line
, cond
, msg
);
254 // For catching Apple Events
255 void wxPyApp::MacOpenFile(const wxString
&fileName
)
257 bool blocked
= wxPyBeginBlockThreads();
258 if (wxPyCBH_findCallback(m_myInst
, "MacOpenFile")) {
259 PyObject
* s
= wx2PyString(fileName
);
260 wxPyCBH_callCallback(m_myInst
, Py_BuildValue("(O)", s
));
263 wxPyEndBlockThreads(blocked
);
266 void wxPyApp::MacPrintFile(const wxString
&fileName
)
268 bool blocked
= wxPyBeginBlockThreads();
269 if (wxPyCBH_findCallback(m_myInst
, "MacPrintFile")) {
270 PyObject
* s
= wx2PyString(fileName
);
271 wxPyCBH_callCallback(m_myInst
, Py_BuildValue("(O)", s
));
274 wxPyEndBlockThreads(blocked
);
277 void wxPyApp::MacNewFile()
279 bool blocked
= wxPyBeginBlockThreads();
280 if (wxPyCBH_findCallback(m_myInst
, "MacNewFile"))
281 wxPyCBH_callCallback(m_myInst
, Py_BuildValue("()"));
282 wxPyEndBlockThreads(blocked
);
285 void wxPyApp::MacReopenApp()
287 bool blocked
= wxPyBeginBlockThreads();
288 if (wxPyCBH_findCallback(m_myInst
, "MacReopenApp"))
289 wxPyCBH_callCallback(m_myInst
, Py_BuildValue("()"));
290 wxPyEndBlockThreads(blocked
);
295 bool wxPyApp::GetMacSupportPCMenuShortcuts() {
297 return s_macSupportPCMenuShortcuts
;
304 long wxPyApp::GetMacAboutMenuItemId() {
306 return s_macAboutMenuItemId
;
313 long wxPyApp::GetMacPreferencesMenuItemId() {
315 return s_macPreferencesMenuItemId
;
322 long wxPyApp::GetMacExitMenuItemId() {
324 return s_macExitMenuItemId
;
331 wxString
wxPyApp::GetMacHelpMenuTitleName() {
333 return s_macHelpMenuTitleName
;
335 return wxEmptyString
;
340 void wxPyApp::SetMacSupportPCMenuShortcuts(bool val
) {
342 s_macSupportPCMenuShortcuts
= val
;
347 void wxPyApp::SetMacAboutMenuItemId(long val
) {
349 s_macAboutMenuItemId
= val
;
354 void wxPyApp::SetMacPreferencesMenuItemId(long val
) {
356 s_macPreferencesMenuItemId
= val
;
361 void wxPyApp::SetMacExitMenuItemId(long val
) {
363 s_macExitMenuItemId
= val
;
368 void wxPyApp::SetMacHelpMenuTitleName(const wxString
& val
) {
370 s_macHelpMenuTitleName
= val
;
375 // This finishes the initialization of wxWindows and then calls the OnInit
376 // that should be present in the derived (Python) class.
377 void wxPyApp::_BootstrapApp()
380 PyObject
* retval
= NULL
;
381 PyObject
* pyint
= NULL
;
384 // Get any command-line args passed to this program from the sys module
387 bool blocked
= wxPyBeginBlockThreads();
388 PyObject
* sysargv
= PySys_GetObject("argv");
389 if (sysargv
!= NULL
) {
390 argc
= PyList_Size(sysargv
);
391 argv
= new char*[argc
+1];
393 for(x
=0; x
<argc
; x
++) {
394 PyObject
*pyArg
= PyList_GetItem(sysargv
, x
);
395 argv
[x
] = PyString_AsString(pyArg
);
399 wxPyEndBlockThreads(blocked
);
401 result
= wxEntryStart(argc
, argv
);
404 blocked
= wxPyBeginBlockThreads();
406 PyErr_SetString(PyExc_SystemError
, "wxEntryStart failed!");
410 // The stock objects were all NULL when they were loaded into
411 // SWIG generated proxies, so re-init those now...
412 wxPy_ReinitStockObjects(3);
414 // It's now ok to generate exceptions for assertion errors.
415 wxPythonApp
->SetStartupComplete(True
);
417 // Call the Python wxApp's OnInit function
418 if (wxPyCBH_findCallback(m_myInst
, "OnInit")) {
420 PyObject
* method
= m_myInst
.GetLastFound();
421 PyObject
* argTuple
= PyTuple_New(0);
422 retval
= PyEval_CallObject(method
, argTuple
);
428 pyint
= PyNumber_Int(retval
);
430 PyErr_SetString(PyExc_TypeError
, "OnInit should return a boolean value");
433 result
= PyInt_AS_LONG(pyint
);
436 // Is it okay if there is no OnInit? Probably so...
442 PyErr_SetString(PyExc_SystemExit
, "OnInit returned False, exiting...");
449 wxPyEndBlockThreads(blocked
);
452 //---------------------------------------------------------------------
453 //----------------------------------------------------------------------
457 static char* wxPyCopyCString(const wxChar
* src
)
459 wxWX2MBbuf buff
= (wxWX2MBbuf
)wxConvCurrent
->cWX2MB(src
);
460 size_t len
= strlen(buff
);
461 char* dest
= new char[len
+1];
467 static char* wxPyCopyCString(const char* src
) // we need a char version too
469 size_t len
= strlen(src
);
470 char* dest
= new char[len
+1];
476 static wxChar
* wxPyCopyWString(const char *src
)
478 //wxMB2WXbuf buff = wxConvCurrent->cMB2WX(src);
479 wxString
str(src
, *wxConvCurrent
);
480 return copystring(str
);
484 static wxChar
* wxPyCopyWString(const wxChar
*src
)
486 return copystring(src
);
492 inline const char* dropwx(const char* name
) {
493 if (name
[0] == 'w' && name
[1] == 'x')
499 //----------------------------------------------------------------------
501 // This function is called when the wxc module is imported to do some initial
502 // setup. (Before there is a wxApp object.) The rest happens in
503 // wxPyApp::_BootstrapApp
504 void __wxPyPreStart(PyObject
* moduleDict
)
508 // wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
511 #ifdef WXP_WITH_THREAD
512 PyEval_InitThreads();
513 wxPyTStates
= new wxPyThreadStateArray
;
514 wxPyTMutex
= new wxMutex
;
516 // Save the current (main) thread state in our array
517 PyThreadState
* tstate
= wxPyBeginAllowThreads();
518 wxPyEndAllowThreads(tstate
);
521 // Ensure that the build options in the DLL (or whatever) match this build
522 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "wxPython");
524 // Init the stock objects to a non-NULL value so SWIG doesn't create them as None
525 wxPy_ReinitStockObjects(1);
527 wxInitAllImageHandlers();
532 void __wxPyCleanup() {
533 wxPyDoingCleanup
= True
;
535 wxPyDoCleanup
= False
;
538 #ifdef WXP_WITH_THREAD
541 wxPyTStates
->Empty();
548 // Save a reference to the dictionary of the wx._core module, and inject
549 // a few more things into it.
550 PyObject
* __wxPySetDictionary(PyObject
* /* self */, PyObject
* args
)
553 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
556 if (!PyDict_Check(wxPython_dict
)) {
557 PyErr_SetString(PyExc_TypeError
, "_wxPySetDictionary must have dictionary object!");
561 if (! wxPyPtrTypeMap
)
562 wxPyPtrTypeMap
= PyDict_New();
563 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
565 // Create an exception object to use for wxASSERTions
566 wxPyAssertionError
= PyErr_NewException("wx._core.PyAssertionError",
567 PyExc_AssertionError
, NULL
);
568 PyDict_SetItemString(wxPython_dict
, "PyAssertionError", wxPyAssertionError
);
572 #define wxPlatform "__WXMOTIF__"
573 #define wxPlatName "wxMotif"
576 #define wxPlatform "__WXX11__"
577 #define wxPlatName "wxX11"
580 #define wxPlatform "__WXGTK__"
581 #define wxPlatName "wxGTK"
584 #define wxPlatform "__WXMSW__"
585 #define wxPlatName "wxMSW"
588 #define wxPlatform "__WXMAC__"
589 #define wxPlatName "wxMac"
598 // These should be deprecated in favor of the PlatformInfo tuple built below...
599 PyDict_SetItemString(wxPython_dict
, "Platform", PyString_FromString(wxPlatform
));
600 PyDict_SetItemString(wxPython_dict
, "USE_UNICODE", PyInt_FromLong(wxUSE_UNICODE
));
601 PyDict_SetItemString(wxPython_dict
, "__WXDEBUG__", PyInt_FromLong(wxdebug
));
604 PyObject
* PlatInfo
= PyList_New(0);
607 #define _AddInfoString(st) \
608 obj = PyString_FromString(st); \
609 PyList_Append(PlatInfo, obj); \
612 _AddInfoString(wxPlatform
);
613 _AddInfoString(wxPlatName
);
615 _AddInfoString("unicode");
617 _AddInfoString("ascii");
621 _AddInfoString("gtk2");
623 _AddInfoString("gtk1");
627 #undef _AddInfoString
629 PyObject
* PlatInfoTuple
= PyList_AsTuple(PlatInfo
);
631 PyDict_SetItemString(wxPython_dict
, "PlatformInfo", PlatInfoTuple
);
637 //---------------------------------------------------------------------------
639 // Python's PyInstance_Check does not return True for instances of new-style
640 // classes. This should get close enough for both new and old classes but I
641 // should re-evaluate the need for doing instance checks...
642 bool wxPyInstance_Check(PyObject
* obj
) {
643 return PyObject_HasAttrString(obj
, "__class__") != 0;
647 // This one checks if the object is an instance of a SWIG proxy class (it has
648 // a .this attribute)
649 bool wxPySwigInstance_Check(PyObject
* obj
) {
650 return PyObject_HasAttrString(obj
, "this") != 0;
653 //---------------------------------------------------------------------------
655 // The stock objects are no longer created when the wxc module is imported,
656 // but only after the app object has been created. The
657 // wxPy_ReinitStockObjects function will be called 3 times to pass the stock
658 // objects though various stages of evolution:
660 // pass 1: Set all the pointers to a non-NULL value so the Python proxy
661 // object will be created (otherwise it will just use None.)
663 // pass 2: After the module has been imported and the python proxys have
664 // been created, then set the __class__ to be _wxPyUnbornObject so
665 // it will catch any access to the object and will raise an exception.
667 // pass 3: Finally, from OnInit patch things up so the stock objects can
671 PyObject
* __wxPyFixStockObjects(PyObject
* /* self */, PyObject
* args
)
673 wxPy_ReinitStockObjects(2);
678 static void rsoPass2(const char* name
)
680 static PyObject
* unbornObjectClass
= NULL
;
683 if (unbornObjectClass
== NULL
) {
684 unbornObjectClass
= PyDict_GetItemString(wxPython_dict
, "_wxPyUnbornObject");
685 Py_INCREF(unbornObjectClass
);
688 // Find the object instance
689 obj
= PyDict_GetItemString(wxPython_dict
, (char*)dropwx(name
));
690 wxCHECK_RET(obj
!= NULL
, wxT("Unable to find stock object"));
691 wxCHECK_RET(wxPySwigInstance_Check(obj
), wxT("Not a swig instance"));
694 PyObject_SetAttrString(obj
, "__class__", unbornObjectClass
);
698 static void rsoPass3(const char* name
, const char* classname
, void* ptr
)
704 // Find the object instance
705 obj
= PyDict_GetItemString(wxPython_dict
, (char*)dropwx(name
));
706 wxCHECK_RET(obj
!= NULL
, wxT("Unable to find stock object"));
707 wxCHECK_RET(wxPySwigInstance_Check(obj
), wxT("Not a swig instance"));
709 // Find the class object and put it back in the instance
710 classobj
= PyDict_GetItemString(wxPython_dict
, (char*)dropwx(classname
));
711 wxCHECK_RET(classobj
!= NULL
, wxT("Unable to find stock class object"));
712 PyObject_SetAttrString(obj
, "__class__", classobj
);
714 // Rebuild the .this swigified pointer with the new value of the C++ pointer
715 ptrobj
= wxPyMakeSwigPtr(ptr
, wxString(classname
, *wxConvCurrent
));
716 PyObject_SetAttrString(obj
, "this", ptrobj
);
722 void wxPy_ReinitStockObjects(int pass
)
725 #define REINITOBJ(name, classname) \
726 if (pass == 1) { name = (classname*)0xC0C0C0C0; } \
727 else if (pass == 2) { rsoPass2(#name); } \
728 else if (pass == 3) { rsoPass3(#name, #classname, (void*)name); }
731 #define REINITOBJ2(name, classname) \
733 else if (pass == 2) { rsoPass2(#name); } \
734 else if (pass == 3) { rsoPass3(#name, #classname, (void*)&name); }
736 // If there is already an App object then wxPython is probably embedded in
737 // a wx C++ application, so there is no need to do all this.
738 static bool embedded
= false;
739 if ((pass
== 1 || pass
== 2) && wxTheApp
) {
743 if (pass
== 3 && embedded
)
747 REINITOBJ(wxNORMAL_FONT
, wxFont
);
748 REINITOBJ(wxSMALL_FONT
, wxFont
);
749 REINITOBJ(wxITALIC_FONT
, wxFont
);
750 REINITOBJ(wxSWISS_FONT
, wxFont
);
752 REINITOBJ(wxRED_PEN
, wxPen
);
753 REINITOBJ(wxCYAN_PEN
, wxPen
);
754 REINITOBJ(wxGREEN_PEN
, wxPen
);
755 REINITOBJ(wxBLACK_PEN
, wxPen
);
756 REINITOBJ(wxWHITE_PEN
, wxPen
);
757 REINITOBJ(wxTRANSPARENT_PEN
, wxPen
);
758 REINITOBJ(wxBLACK_DASHED_PEN
, wxPen
);
759 REINITOBJ(wxGREY_PEN
, wxPen
);
760 REINITOBJ(wxMEDIUM_GREY_PEN
, wxPen
);
761 REINITOBJ(wxLIGHT_GREY_PEN
, wxPen
);
763 REINITOBJ(wxBLUE_BRUSH
, wxBrush
);
764 REINITOBJ(wxGREEN_BRUSH
, wxBrush
);
765 REINITOBJ(wxWHITE_BRUSH
, wxBrush
);
766 REINITOBJ(wxBLACK_BRUSH
, wxBrush
);
767 REINITOBJ(wxTRANSPARENT_BRUSH
, wxBrush
);
768 REINITOBJ(wxCYAN_BRUSH
, wxBrush
);
769 REINITOBJ(wxRED_BRUSH
, wxBrush
);
770 REINITOBJ(wxGREY_BRUSH
, wxBrush
);
771 REINITOBJ(wxMEDIUM_GREY_BRUSH
, wxBrush
);
772 REINITOBJ(wxLIGHT_GREY_BRUSH
, wxBrush
);
774 REINITOBJ(wxBLACK
, wxColour
);
775 REINITOBJ(wxWHITE
, wxColour
);
776 REINITOBJ(wxRED
, wxColour
);
777 REINITOBJ(wxBLUE
, wxColour
);
778 REINITOBJ(wxGREEN
, wxColour
);
779 REINITOBJ(wxCYAN
, wxColour
);
780 REINITOBJ(wxLIGHT_GREY
, wxColour
);
782 REINITOBJ(wxSTANDARD_CURSOR
, wxCursor
);
783 REINITOBJ(wxHOURGLASS_CURSOR
, wxCursor
);
784 REINITOBJ(wxCROSS_CURSOR
, wxCursor
);
786 REINITOBJ2(wxNullBitmap
, wxBitmap
);
787 REINITOBJ2(wxNullIcon
, wxIcon
);
788 REINITOBJ2(wxNullCursor
, wxCursor
);
789 REINITOBJ2(wxNullPen
, wxPen
);
790 REINITOBJ2(wxNullBrush
, wxBrush
);
791 REINITOBJ2(wxNullPalette
, wxPalette
);
792 REINITOBJ2(wxNullFont
, wxFont
);
793 REINITOBJ2(wxNullColour
, wxColour
);
795 REINITOBJ(wxTheFontList
, wxFontList
);
796 REINITOBJ(wxThePenList
, wxPenList
);
797 REINITOBJ(wxTheBrushList
, wxBrushList
);
798 REINITOBJ(wxTheColourDatabase
, wxColourDatabase
);
801 REINITOBJ(wxTheClipboard
, wxClipboard
);
802 REINITOBJ2(wxDefaultValidator
, wxValidator
);
803 REINITOBJ2(wxNullImage
, wxImage
);
804 REINITOBJ2(wxNullAcceleratorTable
, wxAcceleratorTable
);
810 //---------------------------------------------------------------------------
812 void wxPyClientData_dtor(wxPyClientData
* self
) {
813 if (! wxPyDoingCleanup
) { // Don't do it during cleanup as Python
814 // may have already garbage collected the object...
815 bool blocked
= wxPyBeginBlockThreads();
816 Py_DECREF(self
->m_obj
);
818 wxPyEndBlockThreads(blocked
);
822 void wxPyUserData_dtor(wxPyUserData
* self
) {
823 if (! wxPyDoingCleanup
) {
824 bool blocked
= wxPyBeginBlockThreads();
825 Py_DECREF(self
->m_obj
);
827 wxPyEndBlockThreads(blocked
);
832 // This is called when an OOR controled object is being destroyed. Although
833 // the C++ object is going away there is no way to force the Python object
834 // (and all references to it) to die too. This causes problems (crashes) in
835 // wxPython when a python shadow object attempts to call a C++ method using
836 // the now bogus pointer... So to try and prevent this we'll do a little black
837 // magic and change the class of the python instance to a class that will
838 // raise an exception for any attempt to call methods with it. See
839 // _wxPyDeadObject in _core_ex.py for the implementation of this class.
840 void wxPyOORClientData_dtor(wxPyOORClientData
* self
) {
842 static PyObject
* deadObjectClass
= NULL
;
844 bool blocked
= wxPyBeginBlockThreads();
845 if (deadObjectClass
== NULL
) {
846 deadObjectClass
= PyDict_GetItemString(wxPython_dict
, "_wxPyDeadObject");
847 // TODO: Can not wxASSERT here because inside a wxPyBeginBlock Threads,
848 // will lead to a deadlock when it tries to aquire the GIL again.
849 //wxASSERT_MSG(deadObjectClass != NULL, wxT("Can't get _wxPyDeadObject class!"));
850 Py_INCREF(deadObjectClass
);
854 // Only if there is more than one reference to the object
855 if ( !wxPyDoingCleanup
&& self
->m_obj
->ob_refcnt
> 1 ) {
856 // bool isInstance = wxPyInstance_Check(self->m_obj);
858 //wxASSERT_MSG(isInstance, wxT("m_obj not an instance!?!?!"));
860 // Call __del__, if there is one.
861 PyObject
* func
= PyObject_GetAttrString(self
->m_obj
, "__del__");
863 PyObject
* rv
= PyObject_CallMethod(self
->m_obj
, "__del__", NULL
);
867 if (PyErr_Occurred())
868 PyErr_Clear(); // just ignore it for now
871 PyObject
* dict
= PyObject_GetAttrString(self
->m_obj
, "__dict__");
873 // Clear the instance's dictionary
876 // put the name of the old class into the instance, and then reset the
877 // class to be the dead class.
878 PyObject
* klass
= PyObject_GetAttrString(self
->m_obj
, "__class__");
879 PyObject
* name
= PyObject_GetAttrString(klass
, "__name__");
880 PyDict_SetItemString(dict
, "_name", name
);
881 PyObject_SetAttrString(self
->m_obj
, "__class__", deadObjectClass
);
882 //Py_INCREF(deadObjectClass);
888 // m_obj is DECREF'd in the base class dtor...
889 wxPyEndBlockThreads(blocked
);
893 //---------------------------------------------------------------------------
894 // Stuff used by OOR to find the right wxPython class type to return and to
898 // The pointer type map is used when the "pointer" type name generated by SWIG
899 // is not the same as the shadow class name, for example wxPyTreeCtrl
900 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
901 // so we'll just make it a Python dictionary in the wx module's namespace.
902 // (See __wxSetDictionary)
903 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
904 if (! wxPyPtrTypeMap
)
905 wxPyPtrTypeMap
= PyDict_New();
906 PyDict_SetItemString(wxPyPtrTypeMap
,
908 PyString_FromString((char*)ptrName
));
914 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool checkEvtHandler
) {
915 PyObject
* target
= NULL
;
916 bool isEvtHandler
= False
;
919 // If it's derived from wxEvtHandler then there may
920 // already be a pointer to a Python object that we can use
922 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
924 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
925 wxPyOORClientData
* data
= (wxPyOORClientData
*)eh
->GetClientObject();
927 target
= data
->m_obj
;
934 // Otherwise make it the old fashioned way by making a new shadow
935 // object and putting this pointer in it. Look up the class
936 // heirarchy until we find a class name that is located in the
938 const wxClassInfo
* info
= source
->GetClassInfo();
939 wxString name
= info
->GetClassName();
940 bool exists
= wxPyCheckSwigType(name
);
941 while (info
&& !exists
) {
942 info
= info
->GetBaseClass1();
943 name
= info
->GetClassName();
944 exists
= wxPyCheckSwigType(name
);
947 target
= wxPyConstructObject((void*)source
, name
, False
);
948 if (target
&& isEvtHandler
)
949 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyOORClientData(target
));
951 wxString
msg(wxT("wxPython class not found for "));
952 msg
+= source
->GetClassInfo()->GetClassName();
953 PyErr_SetString(PyExc_NameError
, msg
.mbc_str());
957 } else { // source was NULL so return None.
958 Py_INCREF(Py_None
); target
= Py_None
;
964 PyObject
* wxPyMake_wxSizer(wxSizer
* source
) {
965 PyObject
* target
= NULL
;
967 if (source
&& wxIsKindOf(source
, wxSizer
)) {
968 // If it's derived from wxSizer then there may already be a pointer to
969 // a Python object that we can use in the OOR data.
970 wxSizer
* sz
= (wxSizer
*)source
;
971 wxPyOORClientData
* data
= (wxPyOORClientData
*)sz
->GetClientObject();
973 target
= data
->m_obj
;
979 target
= wxPyMake_wxObject(source
, False
);
980 if (target
!= Py_None
)
981 ((wxSizer
*)source
)->SetClientObject(new wxPyOORClientData(target
));
987 //---------------------------------------------------------------------------
990 #ifdef WXP_WITH_THREAD
992 unsigned long wxPyGetCurrentThreadId() {
993 return wxThread::GetCurrentId();
996 static wxPyThreadState gs_shutdownTState
;
999 wxPyThreadState
* wxPyGetThreadState() {
1000 if (wxPyTMutex
== NULL
) // Python is shutting down...
1001 return &gs_shutdownTState
;
1003 unsigned long ctid
= wxPyGetCurrentThreadId();
1004 wxPyThreadState
* tstate
= NULL
;
1007 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
1008 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
1009 if (info
.tid
== ctid
) {
1014 wxPyTMutex
->Unlock();
1015 wxASSERT_MSG(tstate
, wxT("PyThreadState should not be NULL!"));
1021 void wxPySaveThreadState(PyThreadState
* tstate
) {
1022 if (wxPyTMutex
== NULL
) { // Python is shutting down, assume a single thread...
1023 gs_shutdownTState
.tstate
= tstate
;
1026 unsigned long ctid
= wxPyGetCurrentThreadId();
1028 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
1029 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
1030 if (info
.tid
== ctid
) {
1032 if (info
.tstate
!= tstate
)
1033 wxLogMessage("*** tstate mismatch!???");
1035 // info.tstate = tstate; *** DO NOT update existing ones???
1036 // Normally it will never change, but apparently COM callbacks
1037 // (i.e. ActiveX controls) will (incorrectly IMHO) use a transient
1038 // tstate which will then be garbage the next time we try to use
1040 wxPyTMutex
->Unlock();
1044 // not found, so add it...
1045 wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
));
1046 wxPyTMutex
->Unlock();
1053 // Calls from Python to wxWindows code are wrapped in calls to these
1056 PyThreadState
* wxPyBeginAllowThreads() {
1057 #ifdef WXP_WITH_THREAD
1058 PyThreadState
* saved
= PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
1059 wxPySaveThreadState(saved
);
1066 void wxPyEndAllowThreads(PyThreadState
* saved
) {
1067 #ifdef WXP_WITH_THREAD
1068 PyEval_RestoreThread(saved
); // Py_END_ALLOW_THREADS;
1074 // Calls from wxWindows back to Python code, or even any PyObject
1075 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
1077 bool wxPyBeginBlockThreads() {
1078 #ifdef WXP_WITH_THREAD
1079 // This works in for 2.3, maybe a good alternative to find the needed tstate?
1080 // PyThreadState *check = PyGILState_GetThisThreadState();
1082 PyThreadState
*current
= _PyThreadState_Current
;
1084 // Only block if there wasn't already a tstate, or if the current one is
1085 // not the one we are wanting to change to. This should prevent deadlock
1086 // if there are nested calls to wxPyBeginBlockThreads
1087 bool blocked
= false;
1088 wxPyThreadState
* tstate
= wxPyGetThreadState();
1089 if (current
!= tstate
->tstate
) {
1090 PyEval_RestoreThread(tstate
->tstate
);
1098 void wxPyEndBlockThreads(bool blocked
) {
1099 #ifdef WXP_WITH_THREAD
1100 // Only unblock if we blocked in the last call to wxPyBeginBlockThreads.
1101 // The value of blocked passed in needs to be the same as that returned
1102 // from wxPyBeginBlockThreads at the same nesting level.
1104 PyEval_SaveThread();
1110 //---------------------------------------------------------------------------
1111 // wxPyInputStream and wxPyCBInputStream methods
1114 void wxPyInputStream::close() {
1115 /* do nothing for now */
1118 void wxPyInputStream::flush() {
1119 /* do nothing for now */
1122 bool wxPyInputStream::eof() {
1124 return m_wxis
->Eof();
1129 wxPyInputStream::~wxPyInputStream() {
1136 PyObject
* wxPyInputStream::read(int size
) {
1137 PyObject
* obj
= NULL
;
1139 const int BUFSIZE
= 1024;
1141 // check if we have a real wxInputStream to work with
1143 bool blocked
= wxPyBeginBlockThreads();
1144 PyErr_SetString(PyExc_IOError
, "no valid C-wxInputStream");
1145 wxPyEndBlockThreads(blocked
);
1150 // read while bytes are available on the stream
1151 while ( m_wxis
->CanRead() ) {
1152 m_wxis
->Read(buf
.GetAppendBuf(BUFSIZE
), BUFSIZE
);
1153 buf
.UngetAppendBuf(m_wxis
->LastRead());
1156 } else { // Read only size number of characters
1157 m_wxis
->Read(buf
.GetWriteBuf(size
), size
);
1158 buf
.UngetWriteBuf(m_wxis
->LastRead());
1162 bool blocked
= wxPyBeginBlockThreads();
1163 wxStreamError err
= m_wxis
->GetLastError();
1164 if (err
!= wxSTREAM_NO_ERROR
&& err
!= wxSTREAM_EOF
) {
1165 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
1168 // We use only strings for the streams, not unicode
1169 obj
= PyString_FromStringAndSize(buf
, buf
.GetDataLen());
1171 wxPyEndBlockThreads(blocked
);
1176 PyObject
* wxPyInputStream::readline(int size
) {
1177 PyObject
* obj
= NULL
;
1182 // check if we have a real wxInputStream to work with
1184 bool blocked
= wxPyBeginBlockThreads();
1185 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream");
1186 wxPyEndBlockThreads(blocked
);
1190 // read until \n or byte limit reached
1191 for (i
=ch
=0; (ch
!= '\n') && (m_wxis
->CanRead()) && ((size
< 0) || (i
< size
)); i
++) {
1192 ch
= m_wxis
->GetC();
1197 bool blocked
= wxPyBeginBlockThreads();
1198 wxStreamError err
= m_wxis
->GetLastError();
1199 if (err
!= wxSTREAM_NO_ERROR
&& err
!= wxSTREAM_EOF
) {
1200 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
1203 // We use only strings for the streams, not unicode
1204 obj
= PyString_FromStringAndSize((char*)buf
.GetData(), buf
.GetDataLen());
1206 wxPyEndBlockThreads(blocked
);
1211 PyObject
* wxPyInputStream::readlines(int sizehint
) {
1214 // check if we have a real wxInputStream to work with
1216 bool blocked
= wxPyBeginBlockThreads();
1217 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream");
1218 wxPyEndBlockThreads(blocked
);
1223 bool blocked
= wxPyBeginBlockThreads();
1224 pylist
= PyList_New(0);
1225 wxPyEndBlockThreads(blocked
);
1228 bool blocked
= wxPyBeginBlockThreads();
1230 wxPyEndBlockThreads(blocked
);
1234 // read sizehint bytes or until EOF
1236 for (i
=0; (m_wxis
->CanRead()) && ((sizehint
< 0) || (i
< sizehint
));) {
1237 PyObject
* s
= this->readline();
1239 bool blocked
= wxPyBeginBlockThreads();
1241 wxPyEndBlockThreads(blocked
);
1244 bool blocked
= wxPyBeginBlockThreads();
1245 PyList_Append(pylist
, s
);
1246 i
+= PyString_Size(s
);
1247 wxPyEndBlockThreads(blocked
);
1251 wxStreamError err
= m_wxis
->GetLastError();
1252 if (err
!= wxSTREAM_NO_ERROR
&& err
!= wxSTREAM_EOF
) {
1253 bool blocked
= wxPyBeginBlockThreads();
1255 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
1256 wxPyEndBlockThreads(blocked
);
1264 void wxPyInputStream::seek(int offset
, int whence
) {
1266 m_wxis
->SeekI(offset
, wxSeekMode(whence
));
1269 int wxPyInputStream::tell(){
1271 return m_wxis
->TellI();
1278 wxPyCBInputStream::wxPyCBInputStream(PyObject
*r
, PyObject
*s
, PyObject
*t
, bool block
)
1279 : wxInputStream(), m_read(r
), m_seek(s
), m_tell(t
), m_block(block
)
1283 wxPyCBInputStream::~wxPyCBInputStream() {
1285 if (m_block
) blocked
= wxPyBeginBlockThreads();
1289 if (m_block
) wxPyEndBlockThreads(blocked
);
1293 wxPyCBInputStream
* wxPyCBInputStream::create(PyObject
*py
, bool block
) {
1295 if (block
) blocked
= wxPyBeginBlockThreads();
1297 PyObject
* read
= getMethod(py
, "read");
1298 PyObject
* seek
= getMethod(py
, "seek");
1299 PyObject
* tell
= getMethod(py
, "tell");
1302 PyErr_SetString(PyExc_TypeError
, "Not a file-like object");
1306 if (block
) wxPyEndBlockThreads(blocked
);
1310 if (block
) wxPyEndBlockThreads(blocked
);
1311 return new wxPyCBInputStream(read
, seek
, tell
, block
);
1315 wxPyCBInputStream
* wxPyCBInputStream_create(PyObject
*py
, bool block
) {
1316 return wxPyCBInputStream::create(py
, block
);
1319 PyObject
* wxPyCBInputStream::getMethod(PyObject
* py
, char* name
) {
1320 if (!PyObject_HasAttrString(py
, name
))
1322 PyObject
* o
= PyObject_GetAttrString(py
, name
);
1323 if (!PyMethod_Check(o
) && !PyCFunction_Check(o
)) {
1331 size_t wxPyCBInputStream::GetSize() const {
1332 wxPyCBInputStream
* self
= (wxPyCBInputStream
*)this; // cast off const
1333 if (m_seek
&& m_tell
) {
1334 off_t temp
= self
->OnSysTell();
1335 off_t ret
= self
->OnSysSeek(0, wxFromEnd
);
1336 self
->OnSysSeek(temp
, wxFromStart
);
1344 size_t wxPyCBInputStream::OnSysRead(void *buffer
, size_t bufsize
) {
1348 bool blocked
= wxPyBeginBlockThreads();
1349 PyObject
* arglist
= Py_BuildValue("(i)", bufsize
);
1350 PyObject
* result
= PyEval_CallObject(m_read
, arglist
);
1354 if ((result
!= NULL
) && PyString_Check(result
)) {
1355 o
= PyString_Size(result
);
1357 m_lasterror
= wxSTREAM_EOF
;
1360 memcpy((char*)buffer
, PyString_AsString(result
), o
); // strings only, not unicode...
1365 m_lasterror
= wxSTREAM_READ_ERROR
;
1366 wxPyEndBlockThreads(blocked
);
1370 size_t wxPyCBInputStream::OnSysWrite(const void *buffer
, size_t bufsize
) {
1371 m_lasterror
= wxSTREAM_WRITE_ERROR
;
1375 off_t
wxPyCBInputStream::OnSysSeek(off_t off
, wxSeekMode mode
) {
1376 bool blocked
= wxPyBeginBlockThreads();
1378 // off_t is a 64-bit value...
1379 PyObject
* arglist
= Py_BuildValue("(Li)", off
, mode
);
1381 PyObject
* arglist
= Py_BuildValue("(ii)", off
, mode
);
1383 PyObject
* result
= PyEval_CallObject(m_seek
, arglist
);
1386 wxPyEndBlockThreads(blocked
);
1391 off_t
wxPyCBInputStream::OnSysTell() const {
1392 bool blocked
= wxPyBeginBlockThreads();
1393 PyObject
* arglist
= Py_BuildValue("()");
1394 PyObject
* result
= PyEval_CallObject(m_tell
, arglist
);
1397 if (result
!= NULL
) {
1399 if (PyLong_Check(result
))
1400 o
= PyLong_AsLongLong(result
);
1403 o
= PyInt_AsLong(result
);
1406 wxPyEndBlockThreads(blocked
);
1410 //----------------------------------------------------------------------
1412 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
1414 wxPyCallback::wxPyCallback(PyObject
* func
) {
1419 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
1420 m_func
= other
.m_func
;
1424 wxPyCallback::~wxPyCallback() {
1425 bool blocked
= wxPyBeginBlockThreads();
1427 wxPyEndBlockThreads(blocked
);
1431 #define wxPy_PRECALLINIT "_preCallInit"
1432 #define wxPy_POSTCALLCLEANUP "_postCallCleanup"
1434 // This function is used for all events destined for Python event handlers.
1435 void wxPyCallback::EventThunker(wxEvent
& event
) {
1436 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
1437 PyObject
* func
= cb
->m_func
;
1441 bool checkSkip
= False
;
1443 bool blocked
= wxPyBeginBlockThreads();
1444 wxString className
= event
.GetClassInfo()->GetClassName();
1446 // If the event is one of these types then pass the original
1447 // event object instead of the one passed to us.
1448 if ( className
== wxT("wxPyEvent") ) {
1449 arg
= ((wxPyEvent
*)&event
)->GetSelf();
1450 checkSkip
= ((wxPyEvent
*)&event
)->GetCloned();
1452 else if ( className
== wxT("wxPyCommandEvent") ) {
1453 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
1454 checkSkip
= ((wxPyCommandEvent
*)&event
)->GetCloned();
1457 arg
= wxPyConstructObject((void*)&event
, className
);
1463 // "intern" the pre/post method names to speed up the HasAttr
1464 static PyObject
* s_preName
= NULL
;
1465 static PyObject
* s_postName
= NULL
;
1466 if (s_preName
== NULL
) {
1467 s_preName
= PyString_FromString(wxPy_PRECALLINIT
);
1468 s_postName
= PyString_FromString(wxPy_POSTCALLCLEANUP
);
1471 // Check if the event object needs some preinitialization
1472 if (PyObject_HasAttr(arg
, s_preName
)) {
1473 result
= PyObject_CallMethodObjArgs(arg
, s_preName
, arg
, NULL
);
1475 Py_DECREF(result
); // result is ignored, but we still need to decref it
1476 PyErr_Clear(); // Just in case...
1482 // Call the event handler, passing the event object
1483 tuple
= PyTuple_New(1);
1484 PyTuple_SET_ITEM(tuple
, 0, arg
); // steals ref to arg
1485 result
= PyEval_CallObject(func
, tuple
);
1487 Py_DECREF(result
); // result is ignored, but we still need to decref it
1488 PyErr_Clear(); // Just in case...
1493 // Check if the event object needs some post cleanup
1494 if (PyObject_HasAttr(arg
, s_postName
)) {
1495 result
= PyObject_CallMethodObjArgs(arg
, s_postName
, arg
, NULL
);
1497 Py_DECREF(result
); // result is ignored, but we still need to decref it
1498 PyErr_Clear(); // Just in case...
1505 // if the event object was one of our special types and
1506 // it had been cloned, then we need to extract the Skipped
1507 // value from the original and set it in the clone.
1508 result
= PyObject_CallMethod(arg
, "GetSkipped", "");
1510 event
.Skip(PyInt_AsLong(result
));
1518 wxPyEndBlockThreads(blocked
);
1522 //----------------------------------------------------------------------
1524 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
1526 m_self
= other
.m_self
;
1527 m_class
= other
.m_class
;
1535 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
1546 #if PYTHON_API_VERSION >= 1011
1548 // Prior to Python 2.2 PyMethod_GetClass returned the class object
1549 // in which the method was defined. Starting with 2.2 it returns
1550 // "class that asked for the method" which seems totally bogus to me
1551 // but apprently it fixes some obscure problem waiting to happen in
1552 // Python. Since the API was not documented Guido and the gang felt
1553 // safe in changing it. Needless to say that totally screwed up the
1554 // logic below in wxPyCallbackHelper::findCallback, hence this icky
1555 // code to find the class where the method is actually defined...
1558 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
1562 if (PyType_Check(klass
)) { // new style classes
1563 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
1564 PyTypeObject
* type
= (PyTypeObject
*)klass
;
1565 PyObject
*mro
, *res
, *base
, *dict
;
1566 /* Look in tp_dict of types in MRO */
1568 assert(PyTuple_Check(mro
));
1569 n
= PyTuple_GET_SIZE(mro
);
1570 for (i
= 0; i
< n
; i
++) {
1571 base
= PyTuple_GET_ITEM(mro
, i
);
1572 if (PyClass_Check(base
))
1573 dict
= ((PyClassObject
*)base
)->cl_dict
;
1575 assert(PyType_Check(base
));
1576 dict
= ((PyTypeObject
*)base
)->tp_dict
;
1578 assert(dict
&& PyDict_Check(dict
));
1579 res
= PyDict_GetItem(dict
, name
);
1586 else if (PyClass_Check(klass
)) { // old style classes
1587 // This code is borrowed/adapted from class_lookup in classobject.c
1588 PyClassObject
* cp
= (PyClassObject
*)klass
;
1589 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
1590 if (value
!= NULL
) {
1591 return (PyObject
*)cp
;
1593 n
= PyTuple_Size(cp
->cl_bases
);
1594 for (i
= 0; i
< n
; i
++) {
1595 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
1596 PyObject
*v
= PyFindClassWithAttr(base
, name
);
1608 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
1610 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
1612 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
1614 #else // 2.2 and after, the hard way...
1616 PyObject
* nameo
= PyString_FromString(name
);
1617 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
1625 bool wxPyCallbackHelper::findCallback(const char* name
) const {
1626 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
1627 self
->m_lastFound
= NULL
;
1629 // If the object (m_self) has an attibute of the given name...
1630 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
1631 PyObject
*method
, *klass
;
1632 method
= PyObject_GetAttrString(m_self
, (char*)name
);
1634 // ...and if that attribute is a method, and if that method's class is
1635 // not from a base class...
1636 if (PyMethod_Check(method
) &&
1637 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
1638 ((klass
== m_class
) || PyObject_IsSubclass(klass
, m_class
))) {
1640 // ...then we'll save a pointer to the method so callCallback can call it.
1641 self
->m_lastFound
= method
;
1647 return m_lastFound
!= NULL
;
1651 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
1655 result
= callCallbackObj(argTuple
);
1656 if (result
) { // Assumes an integer return type...
1657 retval
= PyInt_AsLong(result
);
1659 PyErr_Clear(); // forget about it if it's not...
1664 // Invoke the Python callable object, returning the raw PyObject return
1665 // value. Caller should DECREF the return value and also manage the GIL.
1666 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
1669 // Save a copy of the pointer in case the callback generates another
1670 // callback. In that case m_lastFound will have a different value when
1671 // it gets back here...
1672 PyObject
* method
= m_lastFound
;
1674 result
= PyEval_CallObject(method
, argTuple
);
1675 Py_DECREF(argTuple
);
1684 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
1685 cbh
.setSelf(self
, klass
, incref
);
1688 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
1689 return cbh
.findCallback(name
);
1692 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1693 return cbh
.callCallback(argTuple
);
1696 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1697 return cbh
.callCallbackObj(argTuple
);
1701 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
1702 if (cbh
->m_incRef
) {
1703 bool blocked
= wxPyBeginBlockThreads();
1704 Py_XDECREF(cbh
->m_self
);
1705 Py_XDECREF(cbh
->m_class
);
1706 wxPyEndBlockThreads(blocked
);
1710 //---------------------------------------------------------------------------
1711 //---------------------------------------------------------------------------
1712 // These event classes can be derived from in Python and passed through the event
1713 // system without losing anything. They do this by keeping a reference to
1714 // themselves and some special case handling in wxPyCallback::EventThunker.
1717 wxPyEvtSelfRef::wxPyEvtSelfRef() {
1718 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1719 //Py_INCREF(m_self); // circular loops...
1723 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
1724 bool blocked
= wxPyBeginBlockThreads();
1727 wxPyEndBlockThreads(blocked
);
1730 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
1731 bool blocked
= wxPyBeginBlockThreads();
1739 wxPyEndBlockThreads(blocked
);
1742 PyObject
* wxPyEvtSelfRef::GetSelf() const {
1748 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
1749 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
1752 wxPyEvent::wxPyEvent(int winid
, wxEventType commandType
)
1753 : wxEvent(winid
, commandType
) {
1757 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
1760 SetSelf(evt
.m_self
, True
);
1764 wxPyEvent::~wxPyEvent() {
1768 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
1769 : wxCommandEvent(commandType
, id
) {
1773 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
1774 : wxCommandEvent(evt
)
1776 SetSelf(evt
.m_self
, True
);
1780 wxPyCommandEvent::~wxPyCommandEvent() {
1787 //---------------------------------------------------------------------------
1788 //---------------------------------------------------------------------------
1789 // Convert a wxList to a Python List, only works for lists of wxObjects
1791 PyObject
* wxPy_ConvertList(wxListBase
* listbase
) {
1792 wxList
* list
= (wxList
*)listbase
; // this is probably bad...
1796 wxNode
* node
= list
->GetFirst();
1798 bool blocked
= wxPyBeginBlockThreads();
1799 pyList
= PyList_New(0);
1801 wxObj
= node
->GetData();
1802 pyObj
= wxPyMake_wxObject(wxObj
);
1803 PyList_Append(pyList
, pyObj
);
1804 node
= node
->GetNext();
1806 wxPyEndBlockThreads(blocked
);
1810 //----------------------------------------------------------------------
1812 long wxPyGetWinHandle(wxWindow
* win
) {
1815 return (long)win
->GetHandle();
1818 #if defined(__WXGTK__) || defined(__WXX11)
1819 return (long)GetXWindow(win
);
1823 //return (long)MAC_WXHWND(win->MacGetTopLevelWindowRef());
1824 return (long)win
->GetHandle();
1830 //----------------------------------------------------------------------
1831 // Some helper functions for typemaps in my_typemaps.i, so they won't be
1832 // included in every file over and over again...
1834 wxString
* wxString_in_helper(PyObject
* source
) {
1835 wxString
* target
= NULL
;
1837 if (!PyString_Check(source
) && !PyUnicode_Check(source
)) {
1838 PyErr_SetString(PyExc_TypeError
, "String or Unicode type required");
1842 PyObject
* uni
= source
;
1843 if (PyString_Check(source
)) {
1844 uni
= PyUnicode_FromObject(source
);
1845 if (PyErr_Occurred()) return NULL
;
1847 target
= new wxString();
1848 size_t len
= PyUnicode_GET_SIZE(uni
);
1850 PyUnicode_AsWideChar((PyUnicodeObject
*)uni
, target
->GetWriteBuf(len
), len
);
1851 target
->UngetWriteBuf();
1854 if (PyString_Check(source
))
1857 char* tmpPtr
; int tmpSize
;
1858 if (PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
) == -1) {
1859 PyErr_SetString(PyExc_TypeError
, "Unable to convert string");
1862 target
= new wxString(tmpPtr
, tmpSize
);
1863 #endif // wxUSE_UNICODE
1869 // Similar to above except doesn't use "new" and doesn't set an exception
1870 wxString
Py2wxString(PyObject
* source
)
1875 // Convert to a unicode object, if not already, then to a wxString
1876 PyObject
* uni
= source
;
1877 if (!PyUnicode_Check(source
)) {
1878 uni
= PyUnicode_FromObject(source
);
1879 if (PyErr_Occurred()) return wxEmptyString
; // TODO: should we PyErr_Clear?
1881 size_t len
= PyUnicode_GET_SIZE(uni
);
1883 PyUnicode_AsWideChar((PyUnicodeObject
*)uni
, target
.GetWriteBuf(len
), len
);
1884 target
.UngetWriteBuf();
1887 if (!PyUnicode_Check(source
))
1890 // Convert to a string object if it isn't already, then to wxString
1891 PyObject
* str
= source
;
1892 if (!PyString_Check(source
)) {
1893 str
= PyObject_Str(source
);
1894 if (PyErr_Occurred()) return wxEmptyString
; // TODO: should we PyErr_Clear?
1896 char* tmpPtr
; int tmpSize
;
1897 PyString_AsStringAndSize(str
, &tmpPtr
, &tmpSize
);
1898 target
= wxString(tmpPtr
, tmpSize
);
1900 if (!PyString_Check(source
))
1902 #endif // wxUSE_UNICODE
1908 // Make either a Python String or Unicode object, depending on build mode
1909 PyObject
* wx2PyString(const wxString
& src
)
1913 str
= PyUnicode_FromWideChar(src
.c_str(), src
.Len());
1915 str
= PyString_FromStringAndSize(src
.c_str(), src
.Len());
1921 //----------------------------------------------------------------------
1924 byte
* byte_LIST_helper(PyObject
* source
) {
1925 if (!PyList_Check(source
)) {
1926 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1929 int count
= PyList_Size(source
);
1930 byte
* temp
= new byte
[count
];
1932 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1935 for (int x
=0; x
<count
; x
++) {
1936 PyObject
* o
= PyList_GetItem(source
, x
);
1937 if (! PyInt_Check(o
)) {
1938 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1941 temp
[x
] = (byte
)PyInt_AsLong(o
);
1947 int* int_LIST_helper(PyObject
* source
) {
1948 if (!PyList_Check(source
)) {
1949 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1952 int count
= PyList_Size(source
);
1953 int* temp
= new int[count
];
1955 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1958 for (int x
=0; x
<count
; x
++) {
1959 PyObject
* o
= PyList_GetItem(source
, x
);
1960 if (! PyInt_Check(o
)) {
1961 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1964 temp
[x
] = PyInt_AsLong(o
);
1970 long* long_LIST_helper(PyObject
* source
) {
1971 if (!PyList_Check(source
)) {
1972 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1975 int count
= PyList_Size(source
);
1976 long* temp
= new long[count
];
1978 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1981 for (int x
=0; x
<count
; x
++) {
1982 PyObject
* o
= PyList_GetItem(source
, x
);
1983 if (! PyInt_Check(o
)) {
1984 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1987 temp
[x
] = PyInt_AsLong(o
);
1993 char** string_LIST_helper(PyObject
* source
) {
1994 if (!PyList_Check(source
)) {
1995 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1998 int count
= PyList_Size(source
);
1999 char** temp
= new char*[count
];
2001 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2004 for (int x
=0; x
<count
; x
++) {
2005 PyObject
* o
= PyList_GetItem(source
, x
);
2006 if (! PyString_Check(o
)) {
2007 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
2010 temp
[x
] = PyString_AsString(o
);
2015 //--------------------------------
2016 // Part of patch from Tim Hochberg
2017 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
2018 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
2019 point
->x
= PyInt_AS_LONG(o1
);
2020 point
->y
= PyInt_AS_LONG(o2
);
2023 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
2024 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
2025 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
2028 if (wxPySwigInstance_Check(o1
) || wxPySwigInstance_Check(o2
)) { // TODO: Why???
2029 // Disallow instances because they can cause havok
2032 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
2033 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
2034 point
->x
= PyInt_AsLong(o1
);
2035 point
->y
= PyInt_AsLong(o2
);
2042 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
2043 // Putting all of the declarations here allows
2044 // us to put the error handling all in one place.
2047 PyObject
*o
, *o1
, *o2
;
2048 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
2050 if (!PySequence_Check(source
)) {
2054 // The length of the sequence is returned in count.
2055 *count
= PySequence_Length(source
);
2060 temp
= new wxPoint
[*count
];
2062 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2065 for (x
=0; x
<*count
; x
++) {
2066 // Get an item: try fast way first.
2068 o
= PySequence_Fast_GET_ITEM(source
, x
);
2071 o
= PySequence_GetItem(source
, x
);
2077 // Convert o to wxPoint.
2078 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
2079 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
2080 o1
= PySequence_Fast_GET_ITEM(o
, 0);
2081 o2
= PySequence_Fast_GET_ITEM(o
, 1);
2082 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
2086 else if (wxPySwigInstance_Check(o
)) {
2088 if (! wxPyConvertSwigPtr(o
, (void **)&pt
, wxT("wxPoint"))) {
2093 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
2094 o1
= PySequence_GetItem(o
, 0);
2095 o2
= PySequence_GetItem(o
, 1);
2096 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
2120 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
2124 //------------------------------
2127 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
2128 if (!PyList_Check(source
)) {
2129 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2132 int count
= PyList_Size(source
);
2133 wxBitmap
** temp
= new wxBitmap
*[count
];
2135 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2138 for (int x
=0; x
<count
; x
++) {
2139 PyObject
* o
= PyList_GetItem(source
, x
);
2140 if (wxPySwigInstance_Check(o
)) {
2142 if (! wxPyConvertSwigPtr(o
, (void **) &pt
, wxT("wxBitmap"))) {
2143 PyErr_SetString(PyExc_TypeError
,"Expected wxBitmap.");
2149 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
2158 wxString
* wxString_LIST_helper(PyObject
* source
) {
2159 if (!PyList_Check(source
)) {
2160 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2163 int count
= PyList_Size(source
);
2164 wxString
* temp
= new wxString
[count
];
2166 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2169 for (int x
=0; x
<count
; x
++) {
2170 PyObject
* o
= PyList_GetItem(source
, x
);
2171 #if PYTHON_API_VERSION >= 1009
2172 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
2173 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
2177 if (! PyString_Check(o
)) {
2178 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
2183 wxString
* pStr
= wxString_in_helper(o
);
2191 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
2192 if (!PyList_Check(source
)) {
2193 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2196 int count
= PyList_Size(source
);
2197 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
2199 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2202 for (int x
=0; x
<count
; x
++) {
2203 PyObject
* o
= PyList_GetItem(source
, x
);
2204 if (wxPySwigInstance_Check(o
)) {
2205 wxAcceleratorEntry
* ae
;
2206 if (! wxPyConvertSwigPtr(o
, (void **) &ae
, wxT("wxAcceleratorEntry"))) {
2207 PyErr_SetString(PyExc_TypeError
,"Expected wxAcceleratorEntry.");
2212 else if (PyTuple_Check(o
)) {
2213 PyObject
* o1
= PyTuple_GetItem(o
, 0);
2214 PyObject
* o2
= PyTuple_GetItem(o
, 1);
2215 PyObject
* o3
= PyTuple_GetItem(o
, 2);
2216 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
2219 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
2227 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
2228 if (!PyList_Check(source
)) {
2229 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2232 int count
= PyList_Size(source
);
2233 wxPen
** temp
= new wxPen
*[count
];
2235 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2238 for (int x
=0; x
<count
; x
++) {
2239 PyObject
* o
= PyList_GetItem(source
, x
);
2240 if (wxPySwigInstance_Check(o
)) {
2242 if (! wxPyConvertSwigPtr(o
, (void **)&pt
, wxT("wxPen"))) {
2244 PyErr_SetString(PyExc_TypeError
,"Expected wxPen.");
2251 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
2259 bool wxPy2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
2260 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
2263 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
2267 o1
= PySequence_Fast_GET_ITEM(source
, 0);
2268 o2
= PySequence_Fast_GET_ITEM(source
, 1);
2271 o1
= PySequence_GetItem(source
, 0);
2272 o2
= PySequence_GetItem(source
, 1);
2275 *i1
= PyInt_AsLong(o1
);
2276 *i2
= PyInt_AsLong(o2
);
2286 bool wxPy4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
2287 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
2288 PyObject
*o1
, *o2
, *o3
, *o4
;
2290 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
2294 o1
= PySequence_Fast_GET_ITEM(source
, 0);
2295 o2
= PySequence_Fast_GET_ITEM(source
, 1);
2296 o3
= PySequence_Fast_GET_ITEM(source
, 2);
2297 o4
= PySequence_Fast_GET_ITEM(source
, 3);
2300 o1
= PySequence_GetItem(source
, 0);
2301 o2
= PySequence_GetItem(source
, 1);
2302 o3
= PySequence_GetItem(source
, 2);
2303 o4
= PySequence_GetItem(source
, 3);
2306 *i1
= PyInt_AsLong(o1
);
2307 *i2
= PyInt_AsLong(o2
);
2308 *i3
= PyInt_AsLong(o3
);
2309 *i4
= PyInt_AsLong(o4
);
2321 //----------------------------------------------------------------------
2323 bool wxPySimple_typecheck(PyObject
* source
, const wxChar
* classname
, int seqLen
)
2327 if (wxPySwigInstance_Check(source
) &&
2328 wxPyConvertSwigPtr(source
, (void **)&ptr
, classname
))
2332 if (PySequence_Check(source
) && PySequence_Length(source
) == seqLen
)
2338 bool wxSize_helper(PyObject
* source
, wxSize
** obj
)
2340 if (source
== Py_None
) {
2341 **obj
= wxSize(-1,-1);
2344 return wxPyTwoIntItem_helper(source
, obj
, wxT("wxSize"));
2348 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
)
2350 if (source
== Py_None
) {
2351 **obj
= wxPoint(-1,-1);
2354 return wxPyTwoIntItem_helper(source
, obj
, wxT("wxPoint"));
2359 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
2361 if (source
== Py_None
) {
2362 **obj
= wxRealPoint(-1,-1);
2366 // If source is an object instance then it may already be the right type
2367 if (wxPySwigInstance_Check(source
)) {
2369 if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxRealPoint")))
2374 // otherwise a 2-tuple of floats is expected
2375 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
2376 PyObject
* o1
= PySequence_GetItem(source
, 0);
2377 PyObject
* o2
= PySequence_GetItem(source
, 1);
2378 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
2383 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
2390 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
2396 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
2398 if (source
== Py_None
) {
2399 **obj
= wxRect(-1,-1,-1,-1);
2403 // If source is an object instance then it may already be the right type
2404 if (wxPySwigInstance_Check(source
)) {
2406 if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxRect")))
2411 // otherwise a 4-tuple of integers is expected
2412 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
2413 PyObject
* o1
= PySequence_GetItem(source
, 0);
2414 PyObject
* o2
= PySequence_GetItem(source
, 1);
2415 PyObject
* o3
= PySequence_GetItem(source
, 2);
2416 PyObject
* o4
= PySequence_GetItem(source
, 3);
2417 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
) ||
2418 !PyNumber_Check(o3
) || !PyNumber_Check(o4
)) {
2425 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
2426 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
2435 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
2441 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
2443 if (source
== Py_None
) {
2444 **obj
= wxNullColour
;
2448 // If source is an object instance then it may already be the right type
2449 if (wxPySwigInstance_Check(source
)) {
2451 if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxColour")))
2456 // otherwise check for a string
2457 else if (PyString_Check(source
) || PyUnicode_Check(source
)) {
2458 wxString spec
= Py2wxString(source
);
2459 if (spec
.GetChar(0) == '#' && spec
.Length() == 7) { // It's #RRGGBB
2460 long red
, green
, blue
;
2461 red
= green
= blue
= 0;
2462 spec
.Mid(1,2).ToLong(&red
, 16);
2463 spec
.Mid(3,2).ToLong(&green
, 16);
2464 spec
.Mid(5,2).ToLong(&blue
, 16);
2466 **obj
= wxColour(red
, green
, blue
);
2469 else { // it's a colour name
2470 **obj
= wxColour(spec
);
2474 // last chance: 3-tuple of integers is expected
2475 else if (PySequence_Check(source
) && PyObject_Length(source
) == 3) {
2476 PyObject
* o1
= PySequence_GetItem(source
, 0);
2477 PyObject
* o2
= PySequence_GetItem(source
, 1);
2478 PyObject
* o3
= PySequence_GetItem(source
, 2);
2479 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
) || !PyNumber_Check(o3
)) {
2485 **obj
= wxColour(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
2493 PyErr_SetString(PyExc_TypeError
,
2494 "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
2499 bool wxColour_typecheck(PyObject
* source
) {
2501 if (wxPySimple_typecheck(source
, wxT("wxColour"), 3))
2504 if (PyString_Check(source
) || PyUnicode_Check(source
))
2512 bool wxPoint2D_helper(PyObject
* source
, wxPoint2D
** obj
) {
2514 if (source
== Py_None
) {
2515 **obj
= wxPoint2D(-1,-1);
2519 // If source is an object instance then it may already be the right type
2520 if (wxPySwigInstance_Check(source
)) {
2522 if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxPoint2D")))
2527 // otherwise a length-2 sequence of floats is expected
2528 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
2529 PyObject
* o1
= PySequence_GetItem(source
, 0);
2530 PyObject
* o2
= PySequence_GetItem(source
, 1);
2531 // This should really check for floats, not numbers -- but that would break code.
2532 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
2537 **obj
= wxPoint2D(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
2543 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxPoint2D object.");
2548 //----------------------------------------------------------------------
2550 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
2552 PyObject
* list
= PyList_New(0);
2553 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
2555 PyObject
* str
= PyUnicode_FromWideChar(arr
[i
].c_str(), arr
[i
].Len());
2557 PyObject
* str
= PyString_FromStringAndSize(arr
[i
].c_str(), arr
[i
].Len());
2559 PyList_Append(list
, str
);
2566 PyObject
* wxArrayInt2PyList_helper(const wxArrayInt
& arr
) {
2568 PyObject
* list
= PyList_New(0);
2569 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
2570 PyObject
* number
= PyInt_FromLong(arr
[i
]);
2571 PyList_Append(list
, number
);
2578 //----------------------------------------------------------------------
2579 //----------------------------------------------------------------------