@@ -173,6 +173,10 @@ ui_type::ui_type(const char *name, ui_type *pBase, Py_ssize_t typeSize,
173173 tp_methods = methodList;
174174 // #define funky_offsetof_weakreflist ((size_t) &((PyObject *)(ui_base_class *)0)->weakreflist)
175175
176+ #if (PY_VERSION_HEX < 0x03000000)
177+ tp_flags |= Py_TPFLAGS_HAVE_WEAKREFS; // flag doesn't exist in py3k
178+ #endif
179+
176180 tp_weaklistoffset -= pyobjOffset;
177181 // cast away const, as Python doesnt use it.
178182 tp_name = (char *)name;
@@ -346,7 +350,12 @@ int ui_base_class::setattro(PyObject *obname, PyObject *v)
346350CString ui_base_class::repr ()
347351{
348352 CString csRet;
353+ #if (PY_VERSION_HEX < 0x03000000)
354+ USES_CONVERSION;
355+ csRet.Format (_T (" object '%s'" ), A2T ((LPSTR)ob_type->tp_name ));
356+ #else
349357 csRet.Format (_T (" object '%S'" ), ob_type->tp_name );
358+ #endif
350359 return csRet;
351360}
352361void ui_base_class::cleanup ()
@@ -706,6 +715,10 @@ PyObject *Python_do_callback(PyObject *themeth, PyObject *thearglst)
706715}
707716
708717// Copied from PyRecord.cpp, should move into pywintypes.h
718+ #if (PY_VERSION_HEX < 0x03000000)
719+ #define PyWinCoreString_ConcatAndDel PyBytes_ConcatAndDel
720+ #define PyWinCoreString_Concat PyBytes_Concat
721+ #else
709722// Unicode versions of '_Concat' etc have different sigs. Make them the
710723// same here...
711724void PyWinCoreString_Concat (register PyObject **pv, register PyObject *w)
@@ -720,6 +733,14 @@ void PyWinCoreString_Concat(register PyObject **pv, register PyObject *w)
720733 *pv = tmp;
721734}
722735
736+ void PyWinCoreString_ConcatAndDel (register PyObject **pv, register PyObject *w)
737+ {
738+ PyWinCoreString_Concat (pv, w);
739+ Py_XDECREF (w);
740+ }
741+
742+ #endif
743+
723744int Python_do_int_callback (PyObject *themeth, PyObject *thearglst)
724745{
725746 int retVal = INT_MAX; // an identifiable, but unlikely genuine value
@@ -1459,7 +1480,7 @@ static PyObject *ui_get_resource(PyObject *self, PyObject *args)
14591480 return ret;
14601481}
14611482
1462- // @pymethod string |win32ui|LoadString|Loads a string from a resource file.
1483+ // @pymethod <o PyUnicode> |win32ui|LoadString|Loads a string from a resource file.
14631484static PyObject *ui_load_string (PyObject *self, PyObject *args)
14641485{
14651486 UINT stringId;
@@ -1832,6 +1853,29 @@ static PyObject *ui_translate_vk(PyObject *, PyObject *args)
18321853 return PyBytes_FromStringAndSize (result, nc);
18331854}
18341855
1856+ /* * Seems to have problems on 9x for some people (not me, though?)
1857+ // @pymethod <o PyUnicode>/None|win32ui|TranslateVirtualKeyW|
1858+ static PyObject *ui_translate_vkW(PyObject *, PyObject *args)
1859+ {
1860+ int vk;
1861+ // @pyparm int|vk||The key to translate
1862+ if (!PyArg_ParseTuple(args, "i", &vk))
1863+ return NULL;
1864+ static HKL layout=GetKeyboardLayout(0);
1865+ static BYTE State[256];
1866+ if (GetKeyboardState(State)==FALSE)
1867+ RETURN_ERR("Can't get keyboard state");
1868+ WCHAR result[2];
1869+ UINT sc=MapVirtualKeyEx(vk,0,layout);
1870+ int nc = ToUnicodeEx(vk,sc,State,result,2, 0,layout);
1871+ if (nc==-1) { // a dead char.
1872+ Py_INCREF(Py_None);
1873+ return Py_None;
1874+ }
1875+ return PyWinObject_FromWCHAR(result, nc);
1876+ }
1877+ **/
1878+
18351879extern PyObject *ui_get_dialog_resource (PyObject *, PyObject *args);
18361880extern PyObject *ui_create_app (PyObject *, PyObject *args);
18371881extern PyObject *ui_get_app (PyObject *, PyObject *args);
@@ -2043,7 +2087,13 @@ int AddConstants(PyObject *module)
20432087 int debug = 0 ;
20442088#endif
20452089 ADD_CONSTANT (debug); // @const win32ui|debug|1 if we are current using a _DEBUG build of win32ui, else 0.
2046- if (PyModule_AddIntConstant (module , " UNICODE" , 1 ) == -1 )
2090+ if (PyModule_AddIntConstant (module , " UNICODE" ,
2091+ #ifdef UNICODE
2092+ 1
2093+ #else
2094+ 0
2095+ #endif
2096+ ) == -1 )
20472097 return -1 ;
20482098 ADD_CONSTANT (AFX_IDW_PANE_FIRST); // @const win32ui|AFX_IDW_PANE_FIRST|Id of the first splitter pane
20492099 ADD_CONSTANT (AFX_IDW_PANE_LAST); // @const win32ui|AFX_IDW_PANE_LAST|Id of the last splitter pane
@@ -2396,7 +2446,11 @@ PYWIN_MODULE_INIT_FUNC(win32ui)
23962446 PYWIN_MODULE_INIT_RETURN_ERROR;
23972447 }
23982448 if (existing_module)
2449+ #if (PY_VERSION_HEX < 0x03000000)
2450+ return ;
2451+ #else
23992452 return existing_module;
2453+ #endif
24002454
24012455 PYWIN_MODULE_INIT_PREPARE (win32ui, ui_functions, " A module, encapsulating the Microsoft Foundation Classes." );
24022456
@@ -2555,6 +2609,12 @@ extern "C" PYW_EXPORT BOOL Win32uiApplicationInit(Win32uiHostGlue *pGlue, const
25552609 // a risk that when Python does "import win32ui", it
25562610 // will locate a different one, causing obvious grief!
25572611 PyObject *argv = PySys_GetObject (" argv" );
2612+ #if (PY_VERSION_HEX < 0x03000000)
2613+ initwin32ui ();
2614+ // Set sys.argv if not already done!
2615+ if (argv == NULL && __targv != NULL && __argc > 0 )
2616+ PySys_SetArgv (__argc - 1 , __targv + 1 );
2617+ #else
25582618 PyInit_win32ui ();
25592619 // Decide if we render sys.argv from command line.
25602620 // PY3.6- Py_Initialize sets sys.argv=NULL .
@@ -2573,6 +2633,7 @@ extern "C" PYW_EXPORT BOOL Win32uiApplicationInit(Win32uiHostGlue *pGlue, const
25732633 LocalFree (myargv);
25742634 }
25752635 }
2636+ #endif
25762637 // If the versions of the .h file are not in synch, then we are in trouble!
25772638 if (pGlue->versionNo != WIN32UIHOSTGLUE_VERSION) {
25782639 MessageBox (0 ,
0 commit comments