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);
530 void __wxPyCleanup() {
531 wxPyDoingCleanup
= True
;
533 wxPyDoCleanup
= False
;
536 #ifdef WXP_WITH_THREAD
539 wxPyTStates
->Empty();
546 // Save a reference to the dictionary of the wx._core module, and inject
547 // a few more things into it.
548 PyObject
* __wxPySetDictionary(PyObject
* /* self */, PyObject
* args
)
551 if (!PyArg_ParseTuple(args
, "O", &wxPython_dict
))
554 if (!PyDict_Check(wxPython_dict
)) {
555 PyErr_SetString(PyExc_TypeError
, "_wxPySetDictionary must have dictionary object!");
559 if (! wxPyPtrTypeMap
)
560 wxPyPtrTypeMap
= PyDict_New();
561 PyDict_SetItemString(wxPython_dict
, "__wxPyPtrTypeMap", wxPyPtrTypeMap
);
563 // Create an exception object to use for wxASSERTions
564 wxPyAssertionError
= PyErr_NewException("wx._core.PyAssertionError",
565 PyExc_AssertionError
, NULL
);
566 PyDict_SetItemString(wxPython_dict
, "PyAssertionError", wxPyAssertionError
);
570 #define wxPlatform "__WXMOTIF__"
571 #define wxPlatName "wxMotif"
574 #define wxPlatform "__WXX11__"
575 #define wxPlatName "wxX11"
578 #define wxPlatform "__WXGTK__"
579 #define wxPlatName "wxGTK"
582 #define wxPlatform "__WXMSW__"
583 #define wxPlatName "wxMSW"
586 #define wxPlatform "__WXMAC__"
587 #define wxPlatName "wxMac"
596 // These should be deprecated in favor of the PlatformInfo tuple built below...
597 PyDict_SetItemString(wxPython_dict
, "Platform", PyString_FromString(wxPlatform
));
598 PyDict_SetItemString(wxPython_dict
, "USE_UNICODE", PyInt_FromLong(wxUSE_UNICODE
));
599 PyDict_SetItemString(wxPython_dict
, "__WXDEBUG__", PyInt_FromLong(wxdebug
));
602 PyObject
* PlatInfo
= PyList_New(0);
605 #define _AddInfoString(st) \
606 obj = PyString_FromString(st); \
607 PyList_Append(PlatInfo, obj); \
610 _AddInfoString(wxPlatform
);
611 _AddInfoString(wxPlatName
);
613 _AddInfoString("unicode");
615 _AddInfoString("ascii");
619 _AddInfoString("gtk2");
621 _AddInfoString("gtk1");
625 #undef _AddInfoString
627 PyObject
* PlatInfoTuple
= PyList_AsTuple(PlatInfo
);
629 PyDict_SetItemString(wxPython_dict
, "PlatformInfo", PlatInfoTuple
);
635 //---------------------------------------------------------------------------
637 // Python's PyInstance_Check does not return True for instances of new-style
638 // classes. This should get close enough for both new and old classes but I
639 // should re-evaluate the need for doing instance checks...
640 bool wxPyInstance_Check(PyObject
* obj
) {
641 return PyObject_HasAttrString(obj
, "__class__") != 0;
645 // This one checks if the object is an instance of a SWIG proxy class (it has
646 // a .this attribute)
647 bool wxPySwigInstance_Check(PyObject
* obj
) {
648 return PyObject_HasAttrString(obj
, "this") != 0;
651 //---------------------------------------------------------------------------
653 // The stock objects are no longer created when the wxc module is imported,
654 // but only after the app object has been created. The
655 // wxPy_ReinitStockObjects function will be called 3 times to pass the stock
656 // objects though various stages of evolution:
658 // pass 1: Set all the pointers to a non-NULL value so the Python proxy
659 // object will be created (otherwise it will just use None.)
661 // pass 2: After the module has been imported and the python proxys have
662 // been created, then set the __class__ to be _wxPyUnbornObject so
663 // it will catch any access to the object and will raise an exception.
665 // pass 3: Finally, from OnInit patch things up so the stock objects can
669 PyObject
* __wxPyFixStockObjects(PyObject
* /* self */, PyObject
* args
)
671 wxPy_ReinitStockObjects(2);
676 static void rsoPass2(const char* name
)
678 static PyObject
* unbornObjectClass
= NULL
;
681 if (unbornObjectClass
== NULL
) {
682 unbornObjectClass
= PyDict_GetItemString(wxPython_dict
, "_wxPyUnbornObject");
683 Py_INCREF(unbornObjectClass
);
686 // Find the object instance
687 obj
= PyDict_GetItemString(wxPython_dict
, (char*)dropwx(name
));
688 wxCHECK_RET(obj
!= NULL
, wxT("Unable to find stock object"));
689 wxCHECK_RET(wxPySwigInstance_Check(obj
), wxT("Not a swig instance"));
692 PyObject_SetAttrString(obj
, "__class__", unbornObjectClass
);
696 static void rsoPass3(const char* name
, const char* classname
, void* ptr
)
702 // Find the object instance
703 obj
= PyDict_GetItemString(wxPython_dict
, (char*)dropwx(name
));
704 wxCHECK_RET(obj
!= NULL
, wxT("Unable to find stock object"));
705 wxCHECK_RET(wxPySwigInstance_Check(obj
), wxT("Not a swig instance"));
707 // Find the class object and put it back in the instance
708 classobj
= PyDict_GetItemString(wxPython_dict
, (char*)dropwx(classname
));
709 wxCHECK_RET(classobj
!= NULL
, wxT("Unable to find stock class object"));
710 PyObject_SetAttrString(obj
, "__class__", classobj
);
712 // Rebuild the .this swigified pointer with the new value of the C++ pointer
713 ptrobj
= wxPyMakeSwigPtr(ptr
, wxString(classname
, *wxConvCurrent
));
714 PyObject_SetAttrString(obj
, "this", ptrobj
);
720 void wxPy_ReinitStockObjects(int pass
)
723 #define REINITOBJ(name, classname) \
724 if (pass == 1) { name = (classname*)0xC0C0C0C0; } \
725 else if (pass == 2) { rsoPass2(#name); } \
726 else if (pass == 3) { rsoPass3(#name, #classname, (void*)name); }
729 #define REINITOBJ2(name, classname) \
731 else if (pass == 2) { rsoPass2(#name); } \
732 else if (pass == 3) { rsoPass3(#name, #classname, (void*)&name); }
734 // If there is already an App object then wxPython is probably embedded in
735 // a wx C++ application, so there is no need to do all this.
736 static bool embedded
= false;
737 if ((pass
== 1 || pass
== 2) && wxTheApp
) {
741 if (pass
== 3 && embedded
)
745 REINITOBJ(wxNORMAL_FONT
, wxFont
);
746 REINITOBJ(wxSMALL_FONT
, wxFont
);
747 REINITOBJ(wxITALIC_FONT
, wxFont
);
748 REINITOBJ(wxSWISS_FONT
, wxFont
);
750 REINITOBJ(wxRED_PEN
, wxPen
);
751 REINITOBJ(wxCYAN_PEN
, wxPen
);
752 REINITOBJ(wxGREEN_PEN
, wxPen
);
753 REINITOBJ(wxBLACK_PEN
, wxPen
);
754 REINITOBJ(wxWHITE_PEN
, wxPen
);
755 REINITOBJ(wxTRANSPARENT_PEN
, wxPen
);
756 REINITOBJ(wxBLACK_DASHED_PEN
, wxPen
);
757 REINITOBJ(wxGREY_PEN
, wxPen
);
758 REINITOBJ(wxMEDIUM_GREY_PEN
, wxPen
);
759 REINITOBJ(wxLIGHT_GREY_PEN
, wxPen
);
761 REINITOBJ(wxBLUE_BRUSH
, wxBrush
);
762 REINITOBJ(wxGREEN_BRUSH
, wxBrush
);
763 REINITOBJ(wxWHITE_BRUSH
, wxBrush
);
764 REINITOBJ(wxBLACK_BRUSH
, wxBrush
);
765 REINITOBJ(wxTRANSPARENT_BRUSH
, wxBrush
);
766 REINITOBJ(wxCYAN_BRUSH
, wxBrush
);
767 REINITOBJ(wxRED_BRUSH
, wxBrush
);
768 REINITOBJ(wxGREY_BRUSH
, wxBrush
);
769 REINITOBJ(wxMEDIUM_GREY_BRUSH
, wxBrush
);
770 REINITOBJ(wxLIGHT_GREY_BRUSH
, wxBrush
);
772 REINITOBJ(wxBLACK
, wxColour
);
773 REINITOBJ(wxWHITE
, wxColour
);
774 REINITOBJ(wxRED
, wxColour
);
775 REINITOBJ(wxBLUE
, wxColour
);
776 REINITOBJ(wxGREEN
, wxColour
);
777 REINITOBJ(wxCYAN
, wxColour
);
778 REINITOBJ(wxLIGHT_GREY
, wxColour
);
780 REINITOBJ(wxSTANDARD_CURSOR
, wxCursor
);
781 REINITOBJ(wxHOURGLASS_CURSOR
, wxCursor
);
782 REINITOBJ(wxCROSS_CURSOR
, wxCursor
);
784 REINITOBJ2(wxNullBitmap
, wxBitmap
);
785 REINITOBJ2(wxNullIcon
, wxIcon
);
786 REINITOBJ2(wxNullCursor
, wxCursor
);
787 REINITOBJ2(wxNullPen
, wxPen
);
788 REINITOBJ2(wxNullBrush
, wxBrush
);
789 REINITOBJ2(wxNullPalette
, wxPalette
);
790 REINITOBJ2(wxNullFont
, wxFont
);
791 REINITOBJ2(wxNullColour
, wxColour
);
793 REINITOBJ(wxTheFontList
, wxFontList
);
794 REINITOBJ(wxThePenList
, wxPenList
);
795 REINITOBJ(wxTheBrushList
, wxBrushList
);
796 REINITOBJ(wxTheColourDatabase
, wxColourDatabase
);
799 REINITOBJ(wxTheClipboard
, wxClipboard
);
800 REINITOBJ2(wxDefaultValidator
, wxValidator
);
801 REINITOBJ2(wxNullImage
, wxImage
);
802 REINITOBJ2(wxNullAcceleratorTable
, wxAcceleratorTable
);
808 //---------------------------------------------------------------------------
810 void wxPyClientData_dtor(wxPyClientData
* self
) {
811 if (! wxPyDoingCleanup
) { // Don't do it during cleanup as Python
812 // may have already garbage collected the object...
813 bool blocked
= wxPyBeginBlockThreads();
814 Py_DECREF(self
->m_obj
);
816 wxPyEndBlockThreads(blocked
);
820 void wxPyUserData_dtor(wxPyUserData
* self
) {
821 if (! wxPyDoingCleanup
) {
822 bool blocked
= wxPyBeginBlockThreads();
823 Py_DECREF(self
->m_obj
);
825 wxPyEndBlockThreads(blocked
);
830 // This is called when an OOR controled object is being destroyed. Although
831 // the C++ object is going away there is no way to force the Python object
832 // (and all references to it) to die too. This causes problems (crashes) in
833 // wxPython when a python shadow object attempts to call a C++ method using
834 // the now bogus pointer... So to try and prevent this we'll do a little black
835 // magic and change the class of the python instance to a class that will
836 // raise an exception for any attempt to call methods with it. See
837 // _wxPyDeadObject in _core_ex.py for the implementation of this class.
838 void wxPyOORClientData_dtor(wxPyOORClientData
* self
) {
840 static PyObject
* deadObjectClass
= NULL
;
842 bool blocked
= wxPyBeginBlockThreads();
843 if (deadObjectClass
== NULL
) {
844 deadObjectClass
= PyDict_GetItemString(wxPython_dict
, "_wxPyDeadObject");
845 // TODO: Can not wxASSERT here because inside a wxPyBeginBlock Threads,
846 // will lead to a deadlock when it tries to aquire the GIL again.
847 //wxASSERT_MSG(deadObjectClass != NULL, wxT("Can't get _wxPyDeadObject class!"));
848 Py_INCREF(deadObjectClass
);
852 // Only if there is more than one reference to the object
853 if ( !wxPyDoingCleanup
&& self
->m_obj
->ob_refcnt
> 1 ) {
854 // bool isInstance = wxPyInstance_Check(self->m_obj);
856 //wxASSERT_MSG(isInstance, wxT("m_obj not an instance!?!?!"));
858 // Call __del__, if there is one.
859 PyObject
* func
= PyObject_GetAttrString(self
->m_obj
, "__del__");
861 PyObject
* rv
= PyObject_CallMethod(self
->m_obj
, "__del__", NULL
);
865 if (PyErr_Occurred())
866 PyErr_Clear(); // just ignore it for now
869 PyObject
* dict
= PyObject_GetAttrString(self
->m_obj
, "__dict__");
871 // Clear the instance's dictionary
874 // put the name of the old class into the instance, and then reset the
875 // class to be the dead class.
876 PyObject
* klass
= PyObject_GetAttrString(self
->m_obj
, "__class__");
877 PyObject
* name
= PyObject_GetAttrString(klass
, "__name__");
878 PyDict_SetItemString(dict
, "_name", name
);
879 PyObject_SetAttrString(self
->m_obj
, "__class__", deadObjectClass
);
880 //Py_INCREF(deadObjectClass);
886 // m_obj is DECREF'd in the base class dtor...
887 wxPyEndBlockThreads(blocked
);
891 //---------------------------------------------------------------------------
892 // Stuff used by OOR to find the right wxPython class type to return and to
896 // The pointer type map is used when the "pointer" type name generated by SWIG
897 // is not the same as the shadow class name, for example wxPyTreeCtrl
898 // vs. wxTreeCtrl. It needs to be referenced in Python as well as from C++,
899 // so we'll just make it a Python dictionary in the wx module's namespace.
900 // (See __wxSetDictionary)
901 void wxPyPtrTypeMap_Add(const char* commonName
, const char* ptrName
) {
902 if (! wxPyPtrTypeMap
)
903 wxPyPtrTypeMap
= PyDict_New();
904 PyDict_SetItemString(wxPyPtrTypeMap
,
906 PyString_FromString((char*)ptrName
));
912 PyObject
* wxPyMake_wxObject(wxObject
* source
, bool checkEvtHandler
) {
913 PyObject
* target
= NULL
;
914 bool isEvtHandler
= False
;
917 // If it's derived from wxEvtHandler then there may
918 // already be a pointer to a Python object that we can use
920 if (checkEvtHandler
&& wxIsKindOf(source
, wxEvtHandler
)) {
922 wxEvtHandler
* eh
= (wxEvtHandler
*)source
;
923 wxPyOORClientData
* data
= (wxPyOORClientData
*)eh
->GetClientObject();
925 target
= data
->m_obj
;
932 // Otherwise make it the old fashioned way by making a new shadow
933 // object and putting this pointer in it. Look up the class
934 // heirarchy until we find a class name that is located in the
936 const wxClassInfo
* info
= source
->GetClassInfo();
937 wxString name
= info
->GetClassName();
938 bool exists
= wxPyCheckSwigType(name
);
939 while (info
&& !exists
) {
940 info
= info
->GetBaseClass1();
941 name
= info
->GetClassName();
942 exists
= wxPyCheckSwigType(name
);
945 target
= wxPyConstructObject((void*)source
, name
, False
);
946 if (target
&& isEvtHandler
)
947 ((wxEvtHandler
*)source
)->SetClientObject(new wxPyOORClientData(target
));
949 wxString
msg(wxT("wxPython class not found for "));
950 msg
+= source
->GetClassInfo()->GetClassName();
951 PyErr_SetString(PyExc_NameError
, msg
.mbc_str());
955 } else { // source was NULL so return None.
956 Py_INCREF(Py_None
); target
= Py_None
;
962 PyObject
* wxPyMake_wxSizer(wxSizer
* source
) {
963 PyObject
* target
= NULL
;
965 if (source
&& wxIsKindOf(source
, wxSizer
)) {
966 // If it's derived from wxSizer then there may already be a pointer to
967 // a Python object that we can use in the OOR data.
968 wxSizer
* sz
= (wxSizer
*)source
;
969 wxPyOORClientData
* data
= (wxPyOORClientData
*)sz
->GetClientObject();
971 target
= data
->m_obj
;
977 target
= wxPyMake_wxObject(source
, False
);
978 if (target
!= Py_None
)
979 ((wxSizer
*)source
)->SetClientObject(new wxPyOORClientData(target
));
985 //---------------------------------------------------------------------------
988 #ifdef WXP_WITH_THREAD
990 unsigned long wxPyGetCurrentThreadId() {
991 return wxThread::GetCurrentId();
994 static wxPyThreadState gs_shutdownTState
;
997 wxPyThreadState
* wxPyGetThreadState() {
998 if (wxPyTMutex
== NULL
) // Python is shutting down...
999 return &gs_shutdownTState
;
1001 unsigned long ctid
= wxPyGetCurrentThreadId();
1002 wxPyThreadState
* tstate
= NULL
;
1005 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
1006 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
1007 if (info
.tid
== ctid
) {
1012 wxPyTMutex
->Unlock();
1013 wxASSERT_MSG(tstate
, wxT("PyThreadState should not be NULL!"));
1019 void wxPySaveThreadState(PyThreadState
* tstate
) {
1020 if (wxPyTMutex
== NULL
) { // Python is shutting down, assume a single thread...
1021 gs_shutdownTState
.tstate
= tstate
;
1024 unsigned long ctid
= wxPyGetCurrentThreadId();
1026 for(size_t i
=0; i
< wxPyTStates
->GetCount(); i
++) {
1027 wxPyThreadState
& info
= wxPyTStates
->Item(i
);
1028 if (info
.tid
== ctid
) {
1030 if (info
.tstate
!= tstate
)
1031 wxLogMessage("*** tstate mismatch!???");
1033 // info.tstate = tstate; *** DO NOT update existing ones???
1034 // Normally it will never change, but apparently COM callbacks
1035 // (i.e. ActiveX controls) will (incorrectly IMHO) use a transient
1036 // tstate which will then be garbage the next time we try to use
1038 wxPyTMutex
->Unlock();
1042 // not found, so add it...
1043 wxPyTStates
->Add(new wxPyThreadState(ctid
, tstate
));
1044 wxPyTMutex
->Unlock();
1051 // Calls from Python to wxWindows code are wrapped in calls to these
1054 PyThreadState
* wxPyBeginAllowThreads() {
1055 #ifdef WXP_WITH_THREAD
1056 PyThreadState
* saved
= PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS;
1057 wxPySaveThreadState(saved
);
1064 void wxPyEndAllowThreads(PyThreadState
* saved
) {
1065 #ifdef WXP_WITH_THREAD
1066 PyEval_RestoreThread(saved
); // Py_END_ALLOW_THREADS;
1072 // Calls from wxWindows back to Python code, or even any PyObject
1073 // manipulations, PyDECREF's and etc. are wrapped in calls to these functions:
1075 bool wxPyBeginBlockThreads() {
1076 #ifdef WXP_WITH_THREAD
1077 // This works in for 2.3, maybe a good alternative to find the needed tstate?
1078 // PyThreadState *check = PyGILState_GetThisThreadState();
1080 PyThreadState
*current
= _PyThreadState_Current
;
1082 // Only block if there wasn't already a tstate, or if the current one is
1083 // not the one we are wanting to change to. This should prevent deadlock
1084 // if there are nested calls to wxPyBeginBlockThreads
1085 bool blocked
= false;
1086 wxPyThreadState
* tstate
= wxPyGetThreadState();
1087 if (current
!= tstate
->tstate
) {
1088 PyEval_RestoreThread(tstate
->tstate
);
1096 void wxPyEndBlockThreads(bool blocked
) {
1097 #ifdef WXP_WITH_THREAD
1098 // Only unblock if we blocked in the last call to wxPyBeginBlockThreads.
1099 // The value of blocked passed in needs to be the same as that returned
1100 // from wxPyBeginBlockThreads at the same nesting level.
1102 PyEval_SaveThread();
1108 //---------------------------------------------------------------------------
1109 // wxPyInputStream and wxPyCBInputStream methods
1112 void wxPyInputStream::close() {
1113 /* do nothing for now */
1116 void wxPyInputStream::flush() {
1117 /* do nothing for now */
1120 bool wxPyInputStream::eof() {
1122 return m_wxis
->Eof();
1127 wxPyInputStream::~wxPyInputStream() {
1134 PyObject
* wxPyInputStream::read(int size
) {
1135 PyObject
* obj
= NULL
;
1137 const int BUFSIZE
= 1024;
1139 // check if we have a real wxInputStream to work with
1141 bool blocked
= wxPyBeginBlockThreads();
1142 PyErr_SetString(PyExc_IOError
, "no valid C-wxInputStream");
1143 wxPyEndBlockThreads(blocked
);
1148 // read while bytes are available on the stream
1149 while ( m_wxis
->CanRead() ) {
1150 m_wxis
->Read(buf
.GetAppendBuf(BUFSIZE
), BUFSIZE
);
1151 buf
.UngetAppendBuf(m_wxis
->LastRead());
1154 } else { // Read only size number of characters
1155 m_wxis
->Read(buf
.GetWriteBuf(size
), size
);
1156 buf
.UngetWriteBuf(m_wxis
->LastRead());
1160 bool blocked
= wxPyBeginBlockThreads();
1161 wxStreamError err
= m_wxis
->GetLastError();
1162 if (err
!= wxSTREAM_NO_ERROR
&& err
!= wxSTREAM_EOF
) {
1163 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
1166 // We use only strings for the streams, not unicode
1167 obj
= PyString_FromStringAndSize(buf
, buf
.GetDataLen());
1169 wxPyEndBlockThreads(blocked
);
1174 PyObject
* wxPyInputStream::readline(int size
) {
1175 PyObject
* obj
= NULL
;
1180 // check if we have a real wxInputStream to work with
1182 bool blocked
= wxPyBeginBlockThreads();
1183 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream");
1184 wxPyEndBlockThreads(blocked
);
1188 // read until \n or byte limit reached
1189 for (i
=ch
=0; (ch
!= '\n') && (m_wxis
->CanRead()) && ((size
< 0) || (i
< size
)); i
++) {
1190 ch
= m_wxis
->GetC();
1195 bool blocked
= wxPyBeginBlockThreads();
1196 wxStreamError err
= m_wxis
->GetLastError();
1197 if (err
!= wxSTREAM_NO_ERROR
&& err
!= wxSTREAM_EOF
) {
1198 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
1201 // We use only strings for the streams, not unicode
1202 obj
= PyString_FromStringAndSize((char*)buf
.GetData(), buf
.GetDataLen());
1204 wxPyEndBlockThreads(blocked
);
1209 PyObject
* wxPyInputStream::readlines(int sizehint
) {
1212 // check if we have a real wxInputStream to work with
1214 bool blocked
= wxPyBeginBlockThreads();
1215 PyErr_SetString(PyExc_IOError
,"no valid C-wxInputStream");
1216 wxPyEndBlockThreads(blocked
);
1221 bool blocked
= wxPyBeginBlockThreads();
1222 pylist
= PyList_New(0);
1223 wxPyEndBlockThreads(blocked
);
1226 bool blocked
= wxPyBeginBlockThreads();
1228 wxPyEndBlockThreads(blocked
);
1232 // read sizehint bytes or until EOF
1234 for (i
=0; (m_wxis
->CanRead()) && ((sizehint
< 0) || (i
< sizehint
));) {
1235 PyObject
* s
= this->readline();
1237 bool blocked
= wxPyBeginBlockThreads();
1239 wxPyEndBlockThreads(blocked
);
1242 bool blocked
= wxPyBeginBlockThreads();
1243 PyList_Append(pylist
, s
);
1244 i
+= PyString_Size(s
);
1245 wxPyEndBlockThreads(blocked
);
1249 wxStreamError err
= m_wxis
->GetLastError();
1250 if (err
!= wxSTREAM_NO_ERROR
&& err
!= wxSTREAM_EOF
) {
1251 bool blocked
= wxPyBeginBlockThreads();
1253 PyErr_SetString(PyExc_IOError
,"IOError in wxInputStream");
1254 wxPyEndBlockThreads(blocked
);
1262 void wxPyInputStream::seek(int offset
, int whence
) {
1264 m_wxis
->SeekI(offset
, wxSeekMode(whence
));
1267 int wxPyInputStream::tell(){
1269 return m_wxis
->TellI();
1276 wxPyCBInputStream::wxPyCBInputStream(PyObject
*r
, PyObject
*s
, PyObject
*t
, bool block
)
1277 : wxInputStream(), m_read(r
), m_seek(s
), m_tell(t
), m_block(block
)
1281 wxPyCBInputStream::~wxPyCBInputStream() {
1283 if (m_block
) blocked
= wxPyBeginBlockThreads();
1287 if (m_block
) wxPyEndBlockThreads(blocked
);
1291 wxPyCBInputStream
* wxPyCBInputStream::create(PyObject
*py
, bool block
) {
1293 if (block
) blocked
= wxPyBeginBlockThreads();
1295 PyObject
* read
= getMethod(py
, "read");
1296 PyObject
* seek
= getMethod(py
, "seek");
1297 PyObject
* tell
= getMethod(py
, "tell");
1300 PyErr_SetString(PyExc_TypeError
, "Not a file-like object");
1304 if (block
) wxPyEndBlockThreads(blocked
);
1308 if (block
) wxPyEndBlockThreads(blocked
);
1309 return new wxPyCBInputStream(read
, seek
, tell
, block
);
1313 wxPyCBInputStream
* wxPyCBInputStream_create(PyObject
*py
, bool block
) {
1314 return wxPyCBInputStream::create(py
, block
);
1317 PyObject
* wxPyCBInputStream::getMethod(PyObject
* py
, char* name
) {
1318 if (!PyObject_HasAttrString(py
, name
))
1320 PyObject
* o
= PyObject_GetAttrString(py
, name
);
1321 if (!PyMethod_Check(o
) && !PyCFunction_Check(o
)) {
1329 size_t wxPyCBInputStream::GetSize() const {
1330 wxPyCBInputStream
* self
= (wxPyCBInputStream
*)this; // cast off const
1331 if (m_seek
&& m_tell
) {
1332 off_t temp
= self
->OnSysTell();
1333 off_t ret
= self
->OnSysSeek(0, wxFromEnd
);
1334 self
->OnSysSeek(temp
, wxFromStart
);
1342 size_t wxPyCBInputStream::OnSysRead(void *buffer
, size_t bufsize
) {
1346 bool blocked
= wxPyBeginBlockThreads();
1347 PyObject
* arglist
= Py_BuildValue("(i)", bufsize
);
1348 PyObject
* result
= PyEval_CallObject(m_read
, arglist
);
1352 if ((result
!= NULL
) && PyString_Check(result
)) {
1353 o
= PyString_Size(result
);
1355 m_lasterror
= wxSTREAM_EOF
;
1358 memcpy((char*)buffer
, PyString_AsString(result
), o
); // strings only, not unicode...
1363 m_lasterror
= wxSTREAM_READ_ERROR
;
1364 wxPyEndBlockThreads(blocked
);
1368 size_t wxPyCBInputStream::OnSysWrite(const void *buffer
, size_t bufsize
) {
1369 m_lasterror
= wxSTREAM_WRITE_ERROR
;
1373 off_t
wxPyCBInputStream::OnSysSeek(off_t off
, wxSeekMode mode
) {
1374 bool blocked
= wxPyBeginBlockThreads();
1376 // off_t is a 64-bit value...
1377 PyObject
* arglist
= Py_BuildValue("(Li)", off
, mode
);
1379 PyObject
* arglist
= Py_BuildValue("(ii)", off
, mode
);
1381 PyObject
* result
= PyEval_CallObject(m_seek
, arglist
);
1384 wxPyEndBlockThreads(blocked
);
1389 off_t
wxPyCBInputStream::OnSysTell() const {
1390 bool blocked
= wxPyBeginBlockThreads();
1391 PyObject
* arglist
= Py_BuildValue("()");
1392 PyObject
* result
= PyEval_CallObject(m_tell
, arglist
);
1395 if (result
!= NULL
) {
1397 if (PyLong_Check(result
))
1398 o
= PyLong_AsLongLong(result
);
1401 o
= PyInt_AsLong(result
);
1404 wxPyEndBlockThreads(blocked
);
1408 //----------------------------------------------------------------------
1410 IMPLEMENT_ABSTRACT_CLASS(wxPyCallback
, wxObject
);
1412 wxPyCallback::wxPyCallback(PyObject
* func
) {
1417 wxPyCallback::wxPyCallback(const wxPyCallback
& other
) {
1418 m_func
= other
.m_func
;
1422 wxPyCallback::~wxPyCallback() {
1423 bool blocked
= wxPyBeginBlockThreads();
1425 wxPyEndBlockThreads(blocked
);
1429 #define wxPy_PRECALLINIT "_preCallInit"
1430 #define wxPy_POSTCALLCLEANUP "_postCallCleanup"
1432 // This function is used for all events destined for Python event handlers.
1433 void wxPyCallback::EventThunker(wxEvent
& event
) {
1434 wxPyCallback
* cb
= (wxPyCallback
*)event
.m_callbackUserData
;
1435 PyObject
* func
= cb
->m_func
;
1439 bool checkSkip
= False
;
1441 bool blocked
= wxPyBeginBlockThreads();
1442 wxString className
= event
.GetClassInfo()->GetClassName();
1444 // If the event is one of these types then pass the original
1445 // event object instead of the one passed to us.
1446 if ( className
== wxT("wxPyEvent") ) {
1447 arg
= ((wxPyEvent
*)&event
)->GetSelf();
1448 checkSkip
= ((wxPyEvent
*)&event
)->GetCloned();
1450 else if ( className
== wxT("wxPyCommandEvent") ) {
1451 arg
= ((wxPyCommandEvent
*)&event
)->GetSelf();
1452 checkSkip
= ((wxPyCommandEvent
*)&event
)->GetCloned();
1455 arg
= wxPyConstructObject((void*)&event
, className
);
1461 // "intern" the pre/post method names to speed up the HasAttr
1462 static PyObject
* s_preName
= NULL
;
1463 static PyObject
* s_postName
= NULL
;
1464 if (s_preName
== NULL
) {
1465 s_preName
= PyString_FromString(wxPy_PRECALLINIT
);
1466 s_postName
= PyString_FromString(wxPy_POSTCALLCLEANUP
);
1469 // Check if the event object needs some preinitialization
1470 if (PyObject_HasAttr(arg
, s_preName
)) {
1471 result
= PyObject_CallMethodObjArgs(arg
, s_preName
, arg
, NULL
);
1473 Py_DECREF(result
); // result is ignored, but we still need to decref it
1474 PyErr_Clear(); // Just in case...
1480 // Call the event handler, passing the event object
1481 tuple
= PyTuple_New(1);
1482 PyTuple_SET_ITEM(tuple
, 0, arg
); // steals ref to arg
1483 result
= PyEval_CallObject(func
, tuple
);
1485 Py_DECREF(result
); // result is ignored, but we still need to decref it
1486 PyErr_Clear(); // Just in case...
1491 // Check if the event object needs some post cleanup
1492 if (PyObject_HasAttr(arg
, s_postName
)) {
1493 result
= PyObject_CallMethodObjArgs(arg
, s_postName
, arg
, NULL
);
1495 Py_DECREF(result
); // result is ignored, but we still need to decref it
1496 PyErr_Clear(); // Just in case...
1503 // if the event object was one of our special types and
1504 // it had been cloned, then we need to extract the Skipped
1505 // value from the original and set it in the clone.
1506 result
= PyObject_CallMethod(arg
, "GetSkipped", "");
1508 event
.Skip(PyInt_AsLong(result
));
1516 wxPyEndBlockThreads(blocked
);
1520 //----------------------------------------------------------------------
1522 wxPyCallbackHelper::wxPyCallbackHelper(const wxPyCallbackHelper
& other
) {
1524 m_self
= other
.m_self
;
1525 m_class
= other
.m_class
;
1533 void wxPyCallbackHelper::setSelf(PyObject
* self
, PyObject
* klass
, int incref
) {
1544 #if PYTHON_API_VERSION >= 1011
1546 // Prior to Python 2.2 PyMethod_GetClass returned the class object
1547 // in which the method was defined. Starting with 2.2 it returns
1548 // "class that asked for the method" which seems totally bogus to me
1549 // but apprently it fixes some obscure problem waiting to happen in
1550 // Python. Since the API was not documented Guido and the gang felt
1551 // safe in changing it. Needless to say that totally screwed up the
1552 // logic below in wxPyCallbackHelper::findCallback, hence this icky
1553 // code to find the class where the method is actually defined...
1556 PyObject
* PyFindClassWithAttr(PyObject
*klass
, PyObject
*name
)
1560 if (PyType_Check(klass
)) { // new style classes
1561 // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
1562 PyTypeObject
* type
= (PyTypeObject
*)klass
;
1563 PyObject
*mro
, *res
, *base
, *dict
;
1564 /* Look in tp_dict of types in MRO */
1566 assert(PyTuple_Check(mro
));
1567 n
= PyTuple_GET_SIZE(mro
);
1568 for (i
= 0; i
< n
; i
++) {
1569 base
= PyTuple_GET_ITEM(mro
, i
);
1570 if (PyClass_Check(base
))
1571 dict
= ((PyClassObject
*)base
)->cl_dict
;
1573 assert(PyType_Check(base
));
1574 dict
= ((PyTypeObject
*)base
)->tp_dict
;
1576 assert(dict
&& PyDict_Check(dict
));
1577 res
= PyDict_GetItem(dict
, name
);
1584 else if (PyClass_Check(klass
)) { // old style classes
1585 // This code is borrowed/adapted from class_lookup in classobject.c
1586 PyClassObject
* cp
= (PyClassObject
*)klass
;
1587 PyObject
*value
= PyDict_GetItem(cp
->cl_dict
, name
);
1588 if (value
!= NULL
) {
1589 return (PyObject
*)cp
;
1591 n
= PyTuple_Size(cp
->cl_bases
);
1592 for (i
= 0; i
< n
; i
++) {
1593 PyObject
* base
= PyTuple_GetItem(cp
->cl_bases
, i
);
1594 PyObject
*v
= PyFindClassWithAttr(base
, name
);
1606 PyObject
* PyMethod_GetDefiningClass(PyObject
* method
, const char* name
)
1608 PyObject
* mgc
= PyMethod_GET_CLASS(method
);
1610 #if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
1612 #else // 2.2 and after, the hard way...
1614 PyObject
* nameo
= PyString_FromString(name
);
1615 PyObject
* klass
= PyFindClassWithAttr(mgc
, nameo
);
1623 bool wxPyCallbackHelper::findCallback(const char* name
) const {
1624 wxPyCallbackHelper
* self
= (wxPyCallbackHelper
*)this; // cast away const
1625 self
->m_lastFound
= NULL
;
1627 // If the object (m_self) has an attibute of the given name...
1628 if (m_self
&& PyObject_HasAttrString(m_self
, (char*)name
)) {
1629 PyObject
*method
, *klass
;
1630 method
= PyObject_GetAttrString(m_self
, (char*)name
);
1632 // ...and if that attribute is a method, and if that method's class is
1633 // not from a base class...
1634 if (PyMethod_Check(method
) &&
1635 (klass
= PyMethod_GetDefiningClass(method
, (char*)name
)) != NULL
&&
1636 ((klass
== m_class
) || PyObject_IsSubclass(klass
, m_class
))) {
1638 // ...then we'll save a pointer to the method so callCallback can call it.
1639 self
->m_lastFound
= method
;
1645 return m_lastFound
!= NULL
;
1649 int wxPyCallbackHelper::callCallback(PyObject
* argTuple
) const {
1653 result
= callCallbackObj(argTuple
);
1654 if (result
) { // Assumes an integer return type...
1655 retval
= PyInt_AsLong(result
);
1657 PyErr_Clear(); // forget about it if it's not...
1662 // Invoke the Python callable object, returning the raw PyObject return
1663 // value. Caller should DECREF the return value and also manage the GIL.
1664 PyObject
* wxPyCallbackHelper::callCallbackObj(PyObject
* argTuple
) const {
1667 // Save a copy of the pointer in case the callback generates another
1668 // callback. In that case m_lastFound will have a different value when
1669 // it gets back here...
1670 PyObject
* method
= m_lastFound
;
1672 result
= PyEval_CallObject(method
, argTuple
);
1673 Py_DECREF(argTuple
);
1682 void wxPyCBH_setCallbackInfo(wxPyCallbackHelper
& cbh
, PyObject
* self
, PyObject
* klass
, int incref
) {
1683 cbh
.setSelf(self
, klass
, incref
);
1686 bool wxPyCBH_findCallback(const wxPyCallbackHelper
& cbh
, const char* name
) {
1687 return cbh
.findCallback(name
);
1690 int wxPyCBH_callCallback(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1691 return cbh
.callCallback(argTuple
);
1694 PyObject
* wxPyCBH_callCallbackObj(const wxPyCallbackHelper
& cbh
, PyObject
* argTuple
) {
1695 return cbh
.callCallbackObj(argTuple
);
1699 void wxPyCBH_delete(wxPyCallbackHelper
* cbh
) {
1700 if (cbh
->m_incRef
) {
1701 bool blocked
= wxPyBeginBlockThreads();
1702 Py_XDECREF(cbh
->m_self
);
1703 Py_XDECREF(cbh
->m_class
);
1704 wxPyEndBlockThreads(blocked
);
1708 //---------------------------------------------------------------------------
1709 //---------------------------------------------------------------------------
1710 // These event classes can be derived from in Python and passed through the event
1711 // system without losing anything. They do this by keeping a reference to
1712 // themselves and some special case handling in wxPyCallback::EventThunker.
1715 wxPyEvtSelfRef::wxPyEvtSelfRef() {
1716 //m_self = Py_None; // **** We don't do normal ref counting to prevent
1717 //Py_INCREF(m_self); // circular loops...
1721 wxPyEvtSelfRef::~wxPyEvtSelfRef() {
1722 bool blocked
= wxPyBeginBlockThreads();
1725 wxPyEndBlockThreads(blocked
);
1728 void wxPyEvtSelfRef::SetSelf(PyObject
* self
, bool clone
) {
1729 bool blocked
= wxPyBeginBlockThreads();
1737 wxPyEndBlockThreads(blocked
);
1740 PyObject
* wxPyEvtSelfRef::GetSelf() const {
1746 IMPLEMENT_ABSTRACT_CLASS(wxPyEvent
, wxEvent
);
1747 IMPLEMENT_ABSTRACT_CLASS(wxPyCommandEvent
, wxCommandEvent
);
1750 wxPyEvent::wxPyEvent(int winid
, wxEventType commandType
)
1751 : wxEvent(winid
, commandType
) {
1755 wxPyEvent::wxPyEvent(const wxPyEvent
& evt
)
1758 SetSelf(evt
.m_self
, True
);
1762 wxPyEvent::~wxPyEvent() {
1766 wxPyCommandEvent::wxPyCommandEvent(wxEventType commandType
, int id
)
1767 : wxCommandEvent(commandType
, id
) {
1771 wxPyCommandEvent::wxPyCommandEvent(const wxPyCommandEvent
& evt
)
1772 : wxCommandEvent(evt
)
1774 SetSelf(evt
.m_self
, True
);
1778 wxPyCommandEvent::~wxPyCommandEvent() {
1785 //---------------------------------------------------------------------------
1786 //---------------------------------------------------------------------------
1787 // Convert a wxList to a Python List, only works for lists of wxObjects
1789 PyObject
* wxPy_ConvertList(wxListBase
* listbase
) {
1790 wxList
* list
= (wxList
*)listbase
; // this is probably bad...
1794 wxNode
* node
= list
->GetFirst();
1796 bool blocked
= wxPyBeginBlockThreads();
1797 pyList
= PyList_New(0);
1799 wxObj
= node
->GetData();
1800 pyObj
= wxPyMake_wxObject(wxObj
);
1801 PyList_Append(pyList
, pyObj
);
1802 node
= node
->GetNext();
1804 wxPyEndBlockThreads(blocked
);
1808 //----------------------------------------------------------------------
1810 long wxPyGetWinHandle(wxWindow
* win
) {
1813 return (long)win
->GetHandle();
1816 #if defined(__WXGTK__) || defined(__WXX11)
1817 return (long)GetXWindow(win
);
1821 //return (long)MAC_WXHWND(win->MacGetTopLevelWindowRef());
1822 return (long)win
->GetHandle();
1828 //----------------------------------------------------------------------
1829 // Some helper functions for typemaps in my_typemaps.i, so they won't be
1830 // included in every file over and over again...
1832 wxString
* wxString_in_helper(PyObject
* source
) {
1833 wxString
* target
= NULL
;
1835 if (!PyString_Check(source
) && !PyUnicode_Check(source
)) {
1836 PyErr_SetString(PyExc_TypeError
, "String or Unicode type required");
1840 PyObject
* uni
= source
;
1841 if (PyString_Check(source
)) {
1842 uni
= PyUnicode_FromObject(source
);
1843 if (PyErr_Occurred()) return NULL
;
1845 target
= new wxString();
1846 size_t len
= PyUnicode_GET_SIZE(uni
);
1848 PyUnicode_AsWideChar((PyUnicodeObject
*)uni
, target
->GetWriteBuf(len
), len
);
1849 target
->UngetWriteBuf();
1852 if (PyString_Check(source
))
1855 char* tmpPtr
; int tmpSize
;
1856 if (PyString_AsStringAndSize(source
, &tmpPtr
, &tmpSize
) == -1) {
1857 PyErr_SetString(PyExc_TypeError
, "Unable to convert string");
1860 target
= new wxString(tmpPtr
, tmpSize
);
1861 #endif // wxUSE_UNICODE
1867 // Similar to above except doesn't use "new" and doesn't set an exception
1868 wxString
Py2wxString(PyObject
* source
)
1873 // Convert to a unicode object, if not already, then to a wxString
1874 PyObject
* uni
= source
;
1875 if (!PyUnicode_Check(source
)) {
1876 uni
= PyUnicode_FromObject(source
);
1877 if (PyErr_Occurred()) return wxEmptyString
; // TODO: should we PyErr_Clear?
1879 size_t len
= PyUnicode_GET_SIZE(uni
);
1881 PyUnicode_AsWideChar((PyUnicodeObject
*)uni
, target
.GetWriteBuf(len
), len
);
1882 target
.UngetWriteBuf();
1885 if (!PyUnicode_Check(source
))
1888 // Convert to a string object if it isn't already, then to wxString
1889 PyObject
* str
= source
;
1890 if (!PyString_Check(source
)) {
1891 str
= PyObject_Str(source
);
1892 if (PyErr_Occurred()) return wxEmptyString
; // TODO: should we PyErr_Clear?
1894 char* tmpPtr
; int tmpSize
;
1895 PyString_AsStringAndSize(str
, &tmpPtr
, &tmpSize
);
1896 target
= wxString(tmpPtr
, tmpSize
);
1898 if (!PyString_Check(source
))
1900 #endif // wxUSE_UNICODE
1906 // Make either a Python String or Unicode object, depending on build mode
1907 PyObject
* wx2PyString(const wxString
& src
)
1911 str
= PyUnicode_FromWideChar(src
.c_str(), src
.Len());
1913 str
= PyString_FromStringAndSize(src
.c_str(), src
.Len());
1919 //----------------------------------------------------------------------
1922 byte
* byte_LIST_helper(PyObject
* source
) {
1923 if (!PyList_Check(source
)) {
1924 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1927 int count
= PyList_Size(source
);
1928 byte
* temp
= new byte
[count
];
1930 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1933 for (int x
=0; x
<count
; x
++) {
1934 PyObject
* o
= PyList_GetItem(source
, x
);
1935 if (! PyInt_Check(o
)) {
1936 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1939 temp
[x
] = (byte
)PyInt_AsLong(o
);
1945 int* int_LIST_helper(PyObject
* source
) {
1946 if (!PyList_Check(source
)) {
1947 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1950 int count
= PyList_Size(source
);
1951 int* temp
= new int[count
];
1953 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1956 for (int x
=0; x
<count
; x
++) {
1957 PyObject
* o
= PyList_GetItem(source
, x
);
1958 if (! PyInt_Check(o
)) {
1959 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1962 temp
[x
] = PyInt_AsLong(o
);
1968 long* long_LIST_helper(PyObject
* source
) {
1969 if (!PyList_Check(source
)) {
1970 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1973 int count
= PyList_Size(source
);
1974 long* temp
= new long[count
];
1976 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
1979 for (int x
=0; x
<count
; x
++) {
1980 PyObject
* o
= PyList_GetItem(source
, x
);
1981 if (! PyInt_Check(o
)) {
1982 PyErr_SetString(PyExc_TypeError
, "Expected a list of integers.");
1985 temp
[x
] = PyInt_AsLong(o
);
1991 char** string_LIST_helper(PyObject
* source
) {
1992 if (!PyList_Check(source
)) {
1993 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
1996 int count
= PyList_Size(source
);
1997 char** temp
= new char*[count
];
1999 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2002 for (int x
=0; x
<count
; x
++) {
2003 PyObject
* o
= PyList_GetItem(source
, x
);
2004 if (! PyString_Check(o
)) {
2005 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
2008 temp
[x
] = PyString_AsString(o
);
2013 //--------------------------------
2014 // Part of patch from Tim Hochberg
2015 static inline bool wxPointFromObjects(PyObject
* o1
, PyObject
* o2
, wxPoint
* point
) {
2016 if (PyInt_Check(o1
) && PyInt_Check(o2
)) {
2017 point
->x
= PyInt_AS_LONG(o1
);
2018 point
->y
= PyInt_AS_LONG(o2
);
2021 if (PyFloat_Check(o1
) && PyFloat_Check(o2
)) {
2022 point
->x
= (int)PyFloat_AS_DOUBLE(o1
);
2023 point
->y
= (int)PyFloat_AS_DOUBLE(o2
);
2026 if (wxPySwigInstance_Check(o1
) || wxPySwigInstance_Check(o2
)) { // TODO: Why???
2027 // Disallow instances because they can cause havok
2030 if (PyNumber_Check(o1
) && PyNumber_Check(o2
)) {
2031 // I believe this excludes instances, so this should be safe without INCREFFing o1 and o2
2032 point
->x
= PyInt_AsLong(o1
);
2033 point
->y
= PyInt_AsLong(o2
);
2040 wxPoint
* wxPoint_LIST_helper(PyObject
* source
, int *count
) {
2041 // Putting all of the declarations here allows
2042 // us to put the error handling all in one place.
2045 PyObject
*o
, *o1
, *o2
;
2046 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
2048 if (!PySequence_Check(source
)) {
2052 // The length of the sequence is returned in count.
2053 *count
= PySequence_Length(source
);
2058 temp
= new wxPoint
[*count
];
2060 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2063 for (x
=0; x
<*count
; x
++) {
2064 // Get an item: try fast way first.
2066 o
= PySequence_Fast_GET_ITEM(source
, x
);
2069 o
= PySequence_GetItem(source
, x
);
2075 // Convert o to wxPoint.
2076 if ((PyTuple_Check(o
) && PyTuple_GET_SIZE(o
) == 2) ||
2077 (PyList_Check(o
) && PyList_GET_SIZE(o
) == 2)) {
2078 o1
= PySequence_Fast_GET_ITEM(o
, 0);
2079 o2
= PySequence_Fast_GET_ITEM(o
, 1);
2080 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
2084 else if (wxPySwigInstance_Check(o
)) {
2086 if (! wxPyConvertSwigPtr(o
, (void **)&pt
, wxT("wxPoint"))) {
2091 else if (PySequence_Check(o
) && PySequence_Length(o
) == 2) {
2092 o1
= PySequence_GetItem(o
, 0);
2093 o2
= PySequence_GetItem(o
, 1);
2094 if (!wxPointFromObjects(o1
, o2
, &temp
[x
])) {
2118 PyErr_SetString(PyExc_TypeError
, "Expected a sequence of length-2 sequences or wxPoints.");
2122 //------------------------------
2125 wxBitmap
** wxBitmap_LIST_helper(PyObject
* source
) {
2126 if (!PyList_Check(source
)) {
2127 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2130 int count
= PyList_Size(source
);
2131 wxBitmap
** temp
= new wxBitmap
*[count
];
2133 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2136 for (int x
=0; x
<count
; x
++) {
2137 PyObject
* o
= PyList_GetItem(source
, x
);
2138 if (wxPySwigInstance_Check(o
)) {
2140 if (! wxPyConvertSwigPtr(o
, (void **) &pt
, wxT("wxBitmap"))) {
2141 PyErr_SetString(PyExc_TypeError
,"Expected wxBitmap.");
2147 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxBitmaps.");
2156 wxString
* wxString_LIST_helper(PyObject
* source
) {
2157 if (!PyList_Check(source
)) {
2158 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2161 int count
= PyList_Size(source
);
2162 wxString
* temp
= new wxString
[count
];
2164 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2167 for (int x
=0; x
<count
; x
++) {
2168 PyObject
* o
= PyList_GetItem(source
, x
);
2169 #if PYTHON_API_VERSION >= 1009
2170 if (! PyString_Check(o
) && ! PyUnicode_Check(o
)) {
2171 PyErr_SetString(PyExc_TypeError
, "Expected a list of string or unicode objects.");
2175 if (! PyString_Check(o
)) {
2176 PyErr_SetString(PyExc_TypeError
, "Expected a list of strings.");
2181 wxString
* pStr
= wxString_in_helper(o
);
2189 wxAcceleratorEntry
* wxAcceleratorEntry_LIST_helper(PyObject
* source
) {
2190 if (!PyList_Check(source
)) {
2191 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2194 int count
= PyList_Size(source
);
2195 wxAcceleratorEntry
* temp
= new wxAcceleratorEntry
[count
];
2197 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2200 for (int x
=0; x
<count
; x
++) {
2201 PyObject
* o
= PyList_GetItem(source
, x
);
2202 if (wxPySwigInstance_Check(o
)) {
2203 wxAcceleratorEntry
* ae
;
2204 if (! wxPyConvertSwigPtr(o
, (void **) &ae
, wxT("wxAcceleratorEntry"))) {
2205 PyErr_SetString(PyExc_TypeError
,"Expected wxAcceleratorEntry.");
2210 else if (PyTuple_Check(o
)) {
2211 PyObject
* o1
= PyTuple_GetItem(o
, 0);
2212 PyObject
* o2
= PyTuple_GetItem(o
, 1);
2213 PyObject
* o3
= PyTuple_GetItem(o
, 2);
2214 temp
[x
].Set(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
2217 PyErr_SetString(PyExc_TypeError
, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
2225 wxPen
** wxPen_LIST_helper(PyObject
* source
) {
2226 if (!PyList_Check(source
)) {
2227 PyErr_SetString(PyExc_TypeError
, "Expected a list object.");
2230 int count
= PyList_Size(source
);
2231 wxPen
** temp
= new wxPen
*[count
];
2233 PyErr_SetString(PyExc_MemoryError
, "Unable to allocate temporary array");
2236 for (int x
=0; x
<count
; x
++) {
2237 PyObject
* o
= PyList_GetItem(source
, x
);
2238 if (wxPySwigInstance_Check(o
)) {
2240 if (! wxPyConvertSwigPtr(o
, (void **)&pt
, wxT("wxPen"))) {
2242 PyErr_SetString(PyExc_TypeError
,"Expected wxPen.");
2249 PyErr_SetString(PyExc_TypeError
, "Expected a list of wxPens.");
2257 bool wxPy2int_seq_helper(PyObject
* source
, int* i1
, int* i2
) {
2258 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
2261 if (!PySequence_Check(source
) || PySequence_Length(source
) != 2)
2265 o1
= PySequence_Fast_GET_ITEM(source
, 0);
2266 o2
= PySequence_Fast_GET_ITEM(source
, 1);
2269 o1
= PySequence_GetItem(source
, 0);
2270 o2
= PySequence_GetItem(source
, 1);
2273 *i1
= PyInt_AsLong(o1
);
2274 *i2
= PyInt_AsLong(o2
);
2284 bool wxPy4int_seq_helper(PyObject
* source
, int* i1
, int* i2
, int* i3
, int* i4
) {
2285 bool isFast
= PyList_Check(source
) || PyTuple_Check(source
);
2286 PyObject
*o1
, *o2
, *o3
, *o4
;
2288 if (!PySequence_Check(source
) || PySequence_Length(source
) != 4)
2292 o1
= PySequence_Fast_GET_ITEM(source
, 0);
2293 o2
= PySequence_Fast_GET_ITEM(source
, 1);
2294 o3
= PySequence_Fast_GET_ITEM(source
, 2);
2295 o4
= PySequence_Fast_GET_ITEM(source
, 3);
2298 o1
= PySequence_GetItem(source
, 0);
2299 o2
= PySequence_GetItem(source
, 1);
2300 o3
= PySequence_GetItem(source
, 2);
2301 o4
= PySequence_GetItem(source
, 3);
2304 *i1
= PyInt_AsLong(o1
);
2305 *i2
= PyInt_AsLong(o2
);
2306 *i3
= PyInt_AsLong(o3
);
2307 *i4
= PyInt_AsLong(o4
);
2319 //----------------------------------------------------------------------
2321 bool wxPySimple_typecheck(PyObject
* source
, const wxChar
* classname
, int seqLen
)
2325 if (wxPySwigInstance_Check(source
) &&
2326 wxPyConvertSwigPtr(source
, (void **)&ptr
, classname
))
2330 if (PySequence_Check(source
) && PySequence_Length(source
) == seqLen
)
2336 bool wxSize_helper(PyObject
* source
, wxSize
** obj
)
2338 if (source
== Py_None
) {
2339 **obj
= wxSize(-1,-1);
2342 return wxPyTwoIntItem_helper(source
, obj
, wxT("wxSize"));
2346 bool wxPoint_helper(PyObject
* source
, wxPoint
** obj
)
2348 if (source
== Py_None
) {
2349 **obj
= wxPoint(-1,-1);
2352 return wxPyTwoIntItem_helper(source
, obj
, wxT("wxPoint"));
2357 bool wxRealPoint_helper(PyObject
* source
, wxRealPoint
** obj
) {
2359 if (source
== Py_None
) {
2360 **obj
= wxRealPoint(-1,-1);
2364 // If source is an object instance then it may already be the right type
2365 if (wxPySwigInstance_Check(source
)) {
2367 if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxRealPoint")))
2372 // otherwise a 2-tuple of floats is expected
2373 else if (PySequence_Check(source
) && PyObject_Length(source
) == 2) {
2374 PyObject
* o1
= PySequence_GetItem(source
, 0);
2375 PyObject
* o2
= PySequence_GetItem(source
, 1);
2376 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
2381 **obj
= wxRealPoint(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
2388 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxRealPoint object.");
2394 bool wxRect_helper(PyObject
* source
, wxRect
** obj
) {
2396 if (source
== Py_None
) {
2397 **obj
= wxRect(-1,-1,-1,-1);
2401 // If source is an object instance then it may already be the right type
2402 if (wxPySwigInstance_Check(source
)) {
2404 if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxRect")))
2409 // otherwise a 4-tuple of integers is expected
2410 else if (PySequence_Check(source
) && PyObject_Length(source
) == 4) {
2411 PyObject
* o1
= PySequence_GetItem(source
, 0);
2412 PyObject
* o2
= PySequence_GetItem(source
, 1);
2413 PyObject
* o3
= PySequence_GetItem(source
, 2);
2414 PyObject
* o4
= PySequence_GetItem(source
, 3);
2415 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
) ||
2416 !PyNumber_Check(o3
) || !PyNumber_Check(o4
)) {
2423 **obj
= wxRect(PyInt_AsLong(o1
), PyInt_AsLong(o2
),
2424 PyInt_AsLong(o3
), PyInt_AsLong(o4
));
2433 PyErr_SetString(PyExc_TypeError
, "Expected a 4-tuple of integers or a wxRect object.");
2439 bool wxColour_helper(PyObject
* source
, wxColour
** obj
) {
2441 if (source
== Py_None
) {
2442 **obj
= wxNullColour
;
2446 // If source is an object instance then it may already be the right type
2447 if (wxPySwigInstance_Check(source
)) {
2449 if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxColour")))
2454 // otherwise check for a string
2455 else if (PyString_Check(source
) || PyUnicode_Check(source
)) {
2456 wxString spec
= Py2wxString(source
);
2457 if (spec
.GetChar(0) == '#' && spec
.Length() == 7) { // It's #RRGGBB
2458 long red
, green
, blue
;
2459 red
= green
= blue
= 0;
2460 spec
.Mid(1,2).ToLong(&red
, 16);
2461 spec
.Mid(3,2).ToLong(&green
, 16);
2462 spec
.Mid(5,2).ToLong(&blue
, 16);
2464 **obj
= wxColour(red
, green
, blue
);
2467 else { // it's a colour name
2468 **obj
= wxColour(spec
);
2472 // last chance: 3-tuple of integers is expected
2473 else if (PySequence_Check(source
) && PyObject_Length(source
) == 3) {
2474 PyObject
* o1
= PySequence_GetItem(source
, 0);
2475 PyObject
* o2
= PySequence_GetItem(source
, 1);
2476 PyObject
* o3
= PySequence_GetItem(source
, 2);
2477 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
) || !PyNumber_Check(o3
)) {
2483 **obj
= wxColour(PyInt_AsLong(o1
), PyInt_AsLong(o2
), PyInt_AsLong(o3
));
2491 PyErr_SetString(PyExc_TypeError
,
2492 "Expected a wxColour object or a string containing a colour name or '#RRGGBB'.");
2497 bool wxColour_typecheck(PyObject
* source
) {
2499 if (wxPySimple_typecheck(source
, wxT("wxColour"), 3))
2502 if (PyString_Check(source
) || PyUnicode_Check(source
))
2510 bool wxPoint2D_helper(PyObject
* source
, wxPoint2D
** obj
) {
2512 if (source
== Py_None
) {
2513 **obj
= wxPoint2D(-1,-1);
2517 // If source is an object instance then it may already be the right type
2518 if (wxPySwigInstance_Check(source
)) {
2520 if (! wxPyConvertSwigPtr(source
, (void **)&ptr
, wxT("wxPoint2D")))
2525 // otherwise a length-2 sequence of floats is expected
2526 if (PySequence_Check(source
) && PySequence_Length(source
) == 2) {
2527 PyObject
* o1
= PySequence_GetItem(source
, 0);
2528 PyObject
* o2
= PySequence_GetItem(source
, 1);
2529 // This should really check for floats, not numbers -- but that would break code.
2530 if (!PyNumber_Check(o1
) || !PyNumber_Check(o2
)) {
2535 **obj
= wxPoint2D(PyFloat_AsDouble(o1
), PyFloat_AsDouble(o2
));
2541 PyErr_SetString(PyExc_TypeError
, "Expected a 2-tuple of floats or a wxPoint2D object.");
2546 //----------------------------------------------------------------------
2548 PyObject
* wxArrayString2PyList_helper(const wxArrayString
& arr
) {
2550 PyObject
* list
= PyList_New(0);
2551 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
2553 PyObject
* str
= PyUnicode_FromWideChar(arr
[i
].c_str(), arr
[i
].Len());
2555 PyObject
* str
= PyString_FromStringAndSize(arr
[i
].c_str(), arr
[i
].Len());
2557 PyList_Append(list
, str
);
2564 PyObject
* wxArrayInt2PyList_helper(const wxArrayInt
& arr
) {
2566 PyObject
* list
= PyList_New(0);
2567 for (size_t i
=0; i
< arr
.GetCount(); i
++) {
2568 PyObject
* number
= PyInt_FromLong(arr
[i
]);
2569 PyList_Append(list
, number
);
2576 //----------------------------------------------------------------------
2577 //----------------------------------------------------------------------