Update of /cvsroot/pywin32/pywin32/win32/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19185/win32/src
Modified Files:
PyUnicode.cpp PyWinTypes.h PyWinTypesmodule.cpp
Log Message:
Move some conversion functions into pywintypes
Index: PyWinTypesmodule.cpp
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/PyWinTypesmodule.cpp,v
retrieving revision 1.32
retrieving revision 1.33
diff -C2 -d -r1.32 -r1.33
*** PyWinTypesmodule.cpp 23 Jul 2007 05:16:05 -0000 1.32
--- PyWinTypesmodule.cpp 12 Aug 2007 08:10:28 -0000 1.33
***************
*** 488,494 ****
return FALSE;
}
! if ((dwords_tuple=PySequence_Tuple(obdwords))==NULL)
return FALSE; // last exit without cleaning up
- *item_cnt=PyTuple_Size(dwords_tuple);
bufsize=*item_cnt * sizeof(DWORD);
*pdwords=(DWORD *)malloc(bufsize);
--- 488,493 ----
return FALSE;
}
! if ((dwords_tuple=PyWinSequence_Tuple(obdwords, item_cnt))==NULL)
return FALSE; // last exit without cleaning up
bufsize=*item_cnt * sizeof(DWORD);
*pdwords=(DWORD *)malloc(bufsize);
***************
*** 729,732 ****
--- 728,746 ----
}
+ // Converts sequence into a tuple and verifies that length fits in length variable
+ PyObject *PyWinSequence_Tuple(PyObject *obseq, DWORD *len)
+ {
+ PyObject *obtuple=PySequence_Tuple(obseq);
+ if (obtuple==NULL)
+ return NULL;
+ Py_ssize_t py_len=PyTuple_GET_SIZE(obtuple);
+ if (py_len > MAXDWORD){
+ Py_DECREF(obtuple);
+ return PyErr_Format(PyExc_ValueError, "Sequence can contain at most %d items", MAXDWORD);
+ }
+ *len=(DWORD)py_len;
+ return obtuple;
+ }
+
/* List of functions exported by this module */
Index: PyWinTypes.h
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/PyWinTypes.h,v
retrieving revision 1.47
retrieving revision 1.48
diff -C2 -d -r1.47 -r1.48
*** PyWinTypes.h 2 Aug 2007 04:31:40 -0000 1.47
--- PyWinTypes.h 12 Aug 2007 08:10:28 -0000 1.48
***************
*** 192,195 ****
--- 192,199 ----
PYWINTYPES_EXPORT BOOL PyWinObject_AsWriteBuffer(PyObject *ob, void **buf, DWORD *buf_len, BOOL bNoneOk=FALSE);
+ // For 64-bit python compatibility, convert sequence to tuple and check length fits in a DWORD
+ PYWINTYPES_EXPORT PyObject *PyWinSequence_Tuple(PyObject *obseq, DWORD *len);
+
+
// an 'int' version (but aren't 'int' and 'DWORD' the same size?
// Maybe a signed-ness issue?
***************
*** 222,225 ****
--- 226,237 ----
PYWINTYPES_EXPORT PyObject *PyWinObject_FromMultipleString(WCHAR *multistring);
PYWINTYPES_EXPORT PyObject *PyWinObject_FromMultipleString(char *multistring);
+ // Converts a sequence of str/unicode objects into a series of consecutive null-terminated
+ // wide character strings with extra terminating null
+ PYWINTYPES_EXPORT BOOL PyWinObject_AsMultipleString(PyObject *ob, WCHAR **pmultistring, BOOL bNoneOK=TRUE);
+ PYWINTYPES_EXPORT void PyWinObject_FreeMultipleString(WCHAR *pmultistring);
+
+ // Convert a sequence of strings to an array of WCHAR pointers
+ PYWINTYPES_EXPORT void PyWinObject_FreeWCHARArray(LPWSTR *wchars, DWORD str_cnt);
+ PYWINTYPES_EXPORT BOOL PyWinObject_AsWCHARArray(PyObject *str_seq, LPWSTR **wchars, DWORD *str_cnt, BOOL bNoneOK = FALSE);
PYWINTYPES_EXPORT PyObject *PyString_FromUnicode( const OLECHAR *str );
Index: PyUnicode.cpp
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/PyUnicode.cpp,v
retrieving revision 1.24
retrieving revision 1.25
diff -C2 -d -r1.24 -r1.25
*** PyUnicode.cpp 20 May 2007 10:46:46 -0000 1.24
--- PyUnicode.cpp 12 Aug 2007 08:10:28 -0000 1.25
***************
*** 1089,1090 ****
--- 1089,1178 ----
return ret;
}
+
+ // Converts a sequence of str/unicode objects into a series of consecutive null-terminated
+ // wide character strings with extra terminating null
+ BOOL PyWinObject_AsMultipleString(PyObject *ob, WCHAR **pmultistring, BOOL bNoneOK)
+ {
+ DWORD numStrings, i;
+ WCHAR **wchars;
+ BOOL rc=FALSE;
+
+ *pmultistring=NULL;
+ if (!PyWinObject_AsWCHARArray(ob, &wchars, &numStrings, bNoneOK))
+ return FALSE;
+ // Shortcut for None
+ if (wchars==NULL)
+ return TRUE;
+
+ size_t len=numStrings+1; // One null for each string plus extra terminating null
+ // Need to loop twice - once to get the buffer length
+ for (i=0;i<numStrings;i++)
+ len += wcslen(wchars[i]);
+
+ // Allocate the buffer
+ *pmultistring = (WCHAR *)malloc(len * sizeof(WCHAR));
+ if (*pmultistring == NULL)
+ PyErr_NoMemory();
+ else{
+ WCHAR *p = *pmultistring;
+ for (i=0;i<numStrings;i++) {
+ wcscpy(p, wchars[i]);
+ p += wcslen(wchars[i]);
+ *p++ = L'\0';
+ }
+ *p = L'\0'; // Add second terminator.
+ rc = TRUE;
+ }
+ PyWinObject_FreeWCHARArray(wchars, numStrings);
+ return rc;
+ }
+
+ void PyWinObject_FreeMultipleString(WCHAR *pmultistring)
+ {
+ if (pmultistring)
+ free (pmultistring);
+ }
+
+ // Converts a aequence of string or unicode objects into an array of WCHAR
+ void PyWinObject_FreeWCHARArray(LPWSTR *wchars, DWORD str_cnt)
+ {
+ if (wchars!=NULL){
+ for (DWORD wchar_index=0; wchar_index<str_cnt; wchar_index++)
+ PyWinObject_FreeWCHAR(wchars[wchar_index]);
+ free(wchars);
+ }
+ }
+
+ BOOL PyWinObject_AsWCHARArray(PyObject *str_seq, LPWSTR **wchars, DWORD *str_cnt, BOOL bNoneOK)
+ {
+ BOOL ret=FALSE;
+ PyObject *str_tuple=NULL, *tuple_item;
+ DWORD bufsize, tuple_index;
+ *wchars=NULL;
+ *str_cnt=0;
+
+ if (bNoneOK && str_seq==Py_None)
+ return TRUE;
+ if ((str_tuple=PyWinSequence_Tuple(str_seq, str_cnt))==NULL)
+ return FALSE;
+ bufsize=*str_cnt * sizeof(LPWSTR);
+ *wchars=(LPWSTR *)malloc(bufsize);
+ if (*wchars==NULL){
+ PyErr_Format(PyExc_MemoryError, "Unable to allocate %d bytes", bufsize);
+ goto done;
+ }
+ ZeroMemory(*wchars, bufsize);
+ for (tuple_index=0;tuple_index<*str_cnt;tuple_index++){
+ tuple_item=PyTuple_GET_ITEM(str_tuple, tuple_index);
+ if (!PyWinObject_AsWCHAR(tuple_item, &((*wchars)[tuple_index]), FALSE)){
+ PyWinObject_FreeWCHARArray(*wchars, *str_cnt);
+ *wchars=NULL;
+ *str_cnt=0;
+ goto done;
+ }
+ }
+ ret=TRUE;
+ done:
+ Py_DECREF(str_tuple);
+ return ret;
+ }
|