Skip to content

Commit 14c3432

Browse files
committed
PY6/Revert "Remove old support for py2k/non-unicode/windows-ce/vista (mhammond#1874)"
This reverts commit c08c3d9 Conflicts resolved: win32/src/PyDEVMODE.cpp win32/src/PyWinTypesmodule.cpp win32/src/win32apimodule.cpp
1 parent d96ae56 commit 14c3432

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1934
-276
lines changed

Pythonwin/win32uimodule.cpp

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
346350
CString 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
}
352361
void 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...
711724
void 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+
723744
int 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.
14631484
static 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+
18351879
extern PyObject *ui_get_dialog_resource(PyObject *, PyObject *args);
18361880
extern PyObject *ui_create_app(PyObject *, PyObject *args);
18371881
extern 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,

Pythonwin/win32util.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,12 +1136,28 @@ CString GetReprText(PyObject *objectUse)
11361136
{
11371137
PyObject *s;
11381138
CString csRet;
1139+
#ifdef UNICODE
1140+
// PyObject_Unicode disappeared in Py3k, where PyObject_Str returns unicode object
1141+
#if (PY_VERSION_HEX < 0x03000000)
1142+
s = PyObject_Unicode(objectUse);
1143+
#else
11391144
s = PyObject_Str(objectUse);
1145+
#endif
11401146
if (s) {
11411147
csRet = CString(PyUnicode_AsUnicode(s));
11421148
Py_DECREF(s);
11431149
return csRet;
11441150
}
1151+
#else
1152+
// Assumes that this will always be compiled with UNICODE defined for py3k
1153+
s = PyObject_Str(objectUse);
1154+
if (s) {
1155+
csRet = CString(PyBytes_AsString(s));
1156+
Py_DECREF(s);
1157+
return csRet;
1158+
}
1159+
#endif
1160+
11451161
PyErr_Clear();
11461162
s = PyObject_Repr(objectUse);
11471163
if (s == NULL) {
@@ -1150,7 +1166,7 @@ CString GetReprText(PyObject *objectUse)
11501166
return csRet;
11511167
}
11521168

1153-
// repr() should always return a unicode string, but for hysterical raisens we check if it is bytes.
1169+
// repr() should return either a string or unicode object, but not sure if this is enforced.
11541170
if (PyUnicode_Check(s))
11551171
csRet = CString(PyUnicode_AS_UNICODE(s));
11561172
else if (PyBytes_Check(s))
@@ -1160,4 +1176,16 @@ CString GetReprText(PyObject *objectUse)
11601176
s->ob_type->tp_name);
11611177
Py_DECREF(s);
11621178
return csRet;
1179+
1180+
/* This was apparently trying to remove enclosing quotes, parens, and brackets but will only succeed for quotes
1181+
Forget about it for now
1182+
Py_ssize_t len=strlen(szRepr);
1183+
if (len > 2 && strchr("\"'[(", *szRepr)) {
1184+
if (szRepr[len-1]==*szRepr) {
1185+
++szRepr;
1186+
len-=2; // drop first and last chars.
1187+
}
1188+
}
1189+
csRet= CString( szRepr, PyWin_SAFE_DOWNCAST(len, Py_ssize_t, int) );
1190+
*/
11631191
}

SWIG/swig_lib/python/pywintypes.i

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ typedef unsigned long ULONG;
9898
}
9999
}
100100

101-
// String support
101+
// String and UniCode support
102102
%typemap(python,in) char *inNullString {
103103
if ($source==Py_None) {
104104
$target = NULL;
@@ -588,7 +588,13 @@ typedef float HWND;
588588
#ifndef SWIG_PYTHONCOM
589589
/* This code only valid if non COM SWIG builds */
590590
#ifndef PYCOM_EXPORT
591-
PyDict_SetItemString(d,"UNICODE", PyLong_FromLong(1));
591+
PyDict_SetItemString(d,"UNICODE", PyLong_FromLong(
592+
#ifdef UNICODE
593+
1
594+
#else
595+
0
596+
#endif
597+
));
592598
#endif
593599
PyWinGlobals_Ensure();
594600
PyDict_SetItemString(d, "error", PyWinExc_ApiError);

com/win32com/src/MiscTypes.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ PyComEnumTypeObject::PyComEnumTypeObject(const char *name, PyComTypeObject *pBas
103103
tp_iter = iter;
104104
tp_iternext = iternext;
105105
// Py3k does not have this flag, and depends just on presence of tp_iter
106+
#if (PY_VERSION_HEX < 0x03000000)
107+
tp_flags |= Py_TPFLAGS_HAVE_ITER;
108+
#endif
106109
}
107110

108111
// PyIEnum iter methods - generic for any "standard" COM IEnum interface, but
@@ -150,6 +153,9 @@ PyComEnumProviderTypeObject::PyComEnumProviderTypeObject(const char *name, PyCom
150153
{
151154
tp_iter = iter;
152155
// tp_iternext remains NULL
156+
#if (PY_VERSION_HEX < 0x03000000)
157+
tp_flags |= Py_TPFLAGS_HAVE_ITER;
158+
#endif
153159
}
154160

155161
// PyIEnumProvider iter methods - generic for COM object that can provide an IEnum*

com/win32com/src/PyComHelpers.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ PyObject *get_Decimal_class(void)
5353

5454
PyObject *PyObject_FromCurrency(CURRENCY &cy)
5555
{
56+
#if (PY_VERSION_HEX < 0x03000000)
57+
static char *divname = "__div__";
58+
#else
5659
static char *divname = "__truediv__";
60+
#endif
5761
if (Decimal_class == NULL) {
5862
Decimal_class = get_Decimal_class();
5963
if (Decimal_class == NULL)

com/win32com/src/PyRecord.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ static void _FreeFieldNames(BSTR *strings, ULONG num_names)
348348
delete[] strings;
349349
}
350350

351+
#if (PY_VERSION_HEX < 0x03000000)
352+
#define PyWinCoreString_ConcatAndDel PyBytes_ConcatAndDel
353+
#define PyWinCoreString_Concat PyBytes_Concat
354+
#else
355+
// Unicode versions of '_Concat' etc have different sigs. Make them the
356+
// same here...
351357
void PyWinCoreString_Concat(register PyObject **pv, register PyObject *w)
352358
{
353359
if (!w) { // hrm - string version doesn't do this, but I saw PyObject_Repr() return NULL...
@@ -366,6 +372,8 @@ void PyWinCoreString_ConcatAndDel(register PyObject **pv, register PyObject *w)
366372
Py_XDECREF(w);
367373
}
368374

375+
#endif
376+
369377
PyObject *PyRecord::tp_repr(PyObject *self)
370378
{
371379
ULONG i;

com/win32com/src/dllmain.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,11 @@ HRESULT DoRegisterUnregister(LPCSTR fileName, int argc, char **argv)
305305
PyCom_DLLAddRef();
306306
{ // A scope for _celp
307307
CEnterLeavePython _celp;
308+
#if (PY_VERSION_HEX < 0x03000000)
309+
PySys_SetArgv(argc, argv);
310+
#else
308311
PySys_SetArgv(argc, __wargv);
312+
#endif;
309313

310314
if (PyRun_SimpleFile(fp, (char *)fileName) != 0) {
311315
// Convert the Python error to a HRESULT.

com/win32com/src/extensions/PyIPropertyStorage.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,13 @@ BOOL PyObject_AsPROPVARIANT(PyObject *ob, PROPVARIANT *pVar)
382382
pVar->vt = VT_UI4;
383383
}
384384
}
385+
#if (PY_VERSION_HEX < 0x03000000)
386+
// Not needed in Py3k, as PyLong_Check is defined to PyLong_Check
387+
}
388+
else if (PyLong_Check(ob)) {
389+
pVar->lVal = PyLong_AsLong(ob);
390+
pVar->vt = VT_I4;
391+
#endif
385392
}
386393
else if (PyFloat_Check(ob)) {
387394
pVar->dblVal = PyFloat_AsDouble(ob);

com/win32com/src/oleargs.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ static PyObject *PyVariant_Type;
1919
#define BYREF_ARRAY_USE_EXISTING_ARRAY
2020

2121
// Need to put this in pywintypes.h with rest of compatibility macros
22+
#if (PY_VERSION_HEX < 0x03000000)
23+
#define PYWIN_BUFFER_CHECK PyBuffer_Check
24+
#else
2225
#define PYWIN_BUFFER_CHECK(obj) (PyBytes_Check(obj) || PyByteArray_Check(obj) || PyMemoryView_Check(obj))
26+
#endif
2327

2428
// A little helper just for this file
2529
static PyObject *OleSetTypeError(TCHAR *msg)
@@ -120,6 +124,9 @@ BOOL PyCom_VariantFromPyObject(PyObject *obj, VARIANT *var)
120124
V_VT(var) = VT_EMPTY;
121125
if (
122126
// In py3k we don't convert PyBytes_Check objects (ie, bytes) to BSTR...
127+
#if (PY_VERSION_HEX < 0x03000000)
128+
PyBytes_Check(obj) ||
129+
#endif
123130
PyUnicode_Check(obj)) {
124131
if (!PyWinObject_AsBstr(obj, &V_BSTR(var))) {
125132
PyErr_SetString(PyExc_MemoryError, "Making BSTR for variant");
@@ -216,6 +223,15 @@ BOOL PyCom_VariantFromPyObject(PyObject *obj, VARIANT *var)
216223
else if (obj == Py_None) {
217224
V_VT(var) = VT_NULL;
218225
}
226+
#if (PY_VERSION_HEX < 0x03000000)
227+
// This is redundant in 3.x, since PyLong_Check is #defined to PyLong_Check
228+
else if (PyLong_Check(obj)) {
229+
V_VT(var) = VT_I4;
230+
V_I4(var) = PyLong_AsLong(obj);
231+
if (V_I4(var) == -1 && PyErr_Occurred())
232+
return FALSE;
233+
}
234+
#endif
219235
else if (PyObject_HasAttrString(obj, "_oleobj_")) {
220236
if (PyCom_InterfaceFromPyInstanceOrObject(obj, IID_IDispatch, (void **)&V_DISPATCH(var), FALSE))
221237
V_VT(var) = VT_DISPATCH;

com/win32com/src/univgw.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ static void free_vtbl(gw_vtbl *vtbl)
349349
VirtualFree(vtbl, 0, MEM_RELEASE);
350350
}
351351

352+
#if PY_VERSION_HEX > 0x03010000
352353
// Use the new capsule API
353354
const char *capsule_name = "win32com universal gateway";
354355

@@ -358,6 +359,20 @@ static PyObject *PyVTable_Create(void *vtbl) { return PyCapsule_New(vtbl, capsul
358359
static gw_vtbl *PyVTable_Get(PyObject *ob) { return (gw_vtbl *)PyCapsule_GetPointer(ob, capsule_name); }
359360

360361
static bool PyVTable_Check(PyObject *ob) { return PyCapsule_IsValid(ob, capsule_name) != 0; }
362+
#else
363+
// Use the old CObject API.
364+
static void __cdecl do_free_vtbl(void *cobject)
365+
{
366+
gw_vtbl *vtbl = (gw_vtbl *)cobject;
367+
free_vtbl(vtbl);
368+
}
369+
370+
static PyObject *PyVTable_Create(void *vtbl) { return PyCObject_FromVoidPtr(vtbl, do_free_vtbl); }
371+
372+
static gw_vtbl *PyVTable_Get(PyObject *ob) { return (gw_vtbl *)PyCObject_AsVoidPtr(ob); }
373+
374+
static bool PyVTable_Check(PyObject *ob) { return PyCObject_Check(ob) != 0; }
375+
#endif
361376

362377
static PyObject *univgw_CreateVTable(PyObject *self, PyObject *args)
363378
{
@@ -716,9 +731,13 @@ BOOL initunivgw(PyObject *parentDict)
716731

717732
PyObject *module;
718733

734+
#if (PY_VERSION_HEX < 0x03000000)
735+
module = Py_InitModule("pythoncom.__univgw", univgw_functions);
736+
#else
719737
static PyModuleDef univgw_def = {PyModuleDef_HEAD_INIT, "pythoncom.__univgw", "Univeral gateway", -1,
720738
univgw_functions};
721739
module = PyModule_Create(&univgw_def);
740+
#endif
722741
if (!module) /* Eeek - some serious error! */
723742
return FALSE;
724743

0 commit comments

Comments
 (0)