From: Roger U. <ru...@us...> - 2007-05-26 15:45:42
|
Update of /cvsroot/pywin32/pywin32/win32/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27764/win32/src Modified Files: win32file.i Log Message: Fix a few 's#' and Py_ssize_t 64 bit issues Index: win32file.i =================================================================== RCS file: /cvsroot/pywin32/pywin32/win32/src/win32file.i,v retrieving revision 1.80 retrieving revision 1.81 diff -C2 -d -r1.80 -r1.81 *** win32file.i 24 May 2007 06:01:06 -0000 1.80 --- win32file.i 26 May 2007 15:45:38 -0000 1.81 *************** *** 26,29 **** --- 26,84 ---- %include "pywin32.i" + %{ + // Helper functions that use DWORD for length + BOOL PyWinObject_AsReadBuffer(PyObject *ob, void **buf, DWORD *buf_len, BOOL bNoneOk=FALSE) + { + if (ob==Py_None){ + if (bNoneOk){ + *buf_len=0; + *buf=NULL; + return TRUE; + } + PyErr_SetString(PyExc_TypeError, "Buffer cannot be None"); + return FALSE; + } + Py_ssize_t py_len; + if (PyObject_AsReadBuffer(ob, (const void **)buf, &py_len)==-1) + return FALSE; + + #ifdef _WIN64 + if (py_len>MAXDWORD){ + PyErr_Format(PyExc_ValueError,"Buffer length can be at most %d characters", MAXDWORD); + return FALSE; + } + #endif + + *buf_len=(DWORD)py_len; + return TRUE; + } + + BOOL PyWinObject_AsWriteBuffer(PyObject *ob, void **buf, DWORD *buf_len, BOOL bNoneOk=FALSE) + { + if (ob==Py_None){ + if (bNoneOk){ + *buf_len=0; + *buf=NULL; + return TRUE; + } + PyErr_SetString(PyExc_TypeError, "Buffer cannot be None"); + return FALSE; + } + Py_ssize_t py_len; + if (PyObject_AsWriteBuffer(ob, buf, &py_len)==-1) + return FALSE; + + #ifdef _WIN64 + if (py_len>MAXDWORD){ + PyErr_Format(PyExc_ValueError,"Buffer length can be at most %d characters", MAXDWORD); + return FALSE; + } + #endif + + *buf_len=(DWORD)py_len; + return TRUE; + } + %} + #define FILE_GENERIC_READ FILE_GENERIC_READ #define FILE_GENERIC_WRITE FILE_GENERIC_WRITE *************** *** 301,305 **** { OVERLAPPED *pOverlapped; ! PyObject *obhFile; HANDLE hDevice; DWORD readSize; --- 356,360 ---- { OVERLAPPED *pOverlapped; ! PyObject *obhFile, *obwriteData; HANDLE hDevice; DWORD readSize; *************** *** 307,317 **** DWORD dwIoControlCode; ! char *writeData; DWORD writeSize; ! if (!PyArg_ParseTuple(args, "Ols#l|O:DeviceIoControl", &obhFile, // @pyparm int|hFile||Handle to the file &dwIoControlCode, // @pyparm int|dwIoControlCode||IOControl Code to use. ! &writeData, &writeSize, // @pyparm string|data||The data to write. &readSize, // @pyparm int|readSize||Size of the buffer to create for the read. &obOverlapped)) // @pyparm <o PyOVERLAPPED>|ol|None|An overlapped structure --- 362,372 ---- DWORD dwIoControlCode; ! void *writeData; DWORD writeSize; ! if (!PyArg_ParseTuple(args, "OlOl|O:DeviceIoControl", &obhFile, // @pyparm int|hFile||Handle to the file &dwIoControlCode, // @pyparm int|dwIoControlCode||IOControl Code to use. ! &obwriteData, // @pyparm string|data||The data to write. &readSize, // @pyparm int|readSize||Size of the buffer to create for the read. &obOverlapped)) // @pyparm <o PyOVERLAPPED>|ol|None|An overlapped structure *************** *** 325,330 **** if (!PyWinObject_AsHANDLE(obhFile, &hDevice)) return NULL; ! void *readData = malloc(readSize); DWORD numRead; BOOL ok; --- 380,390 ---- if (!PyWinObject_AsHANDLE(obhFile, &hDevice)) return NULL; ! // Some control codes call for NULL input buffer ! if (!PyWinObject_AsReadBuffer(obwriteData, &writeData, &writeSize, TRUE)) ! return NULL; void *readData = malloc(readSize); + if (readData==NULL) + return PyErr_Format(PyExc_MemoryError, "Unable to allocate %d bytes", readSize); + DWORD numRead; BOOL ok; *************** *** 855,865 **** PyObject *MyReadFile(PyObject *self, PyObject *args) { ! OVERLAPPED *pOverlapped; PyObject *obhFile; HANDLE hFile; DWORD bufSize; ! PyObject *obOverlapped = NULL; BOOL bBufMallocd = FALSE; ! PyObject *obBuf; if (!PyArg_ParseTuple(args, "OO|O:ReadFile", --- 915,925 ---- PyObject *MyReadFile(PyObject *self, PyObject *args) { ! OVERLAPPED *pOverlapped=NULL; PyObject *obhFile; HANDLE hFile; DWORD bufSize; ! PyObject *obOverlapped = Py_None; BOOL bBufMallocd = FALSE; ! PyObject *obBuf, *obRet=NULL; if (!PyArg_ParseTuple(args, "OO|O:ReadFile", *************** *** 868,920 **** &obOverlapped)) // @pyparm <o PyOVERLAPPED>|ol|None|An overlapped structure return NULL; // @comm in a multi-threaded overlapped environment, it is likely to be necessary to pre-allocate the read buffer using the <om win32file.AllocateReadBuffer> method, otherwise the I/O operation may complete before you can assign to the resulting buffer. ! if (obOverlapped==NULL) ! pOverlapped = NULL; ! else { if (!PyWinObject_AsOVERLAPPED(obOverlapped, &pOverlapped)) return NULL; ! } ! if (!PyWinObject_AsHANDLE(obhFile, &hFile)) ! return NULL; void *buf = NULL; - PyObject *pORB = NULL; PyBufferProcs *pb = NULL; ! if (PyInt_Check(obBuf)) { ! bufSize = PyInt_AsLong(obBuf); ! #ifndef MS_WINCE ! if (pOverlapped) { ! pORB = PyBuffer_New(bufSize); ! if (pORB==NULL) { ! PyErr_SetString(PyExc_MemoryError, "Allocating read buffer"); return NULL; } - pb = pORB->ob_type->tp_as_buffer; - (*pb->bf_getreadbuffer)(pORB, 0, &buf); - } else { - #endif - buf = malloc(bufSize); - bBufMallocd = TRUE; - #ifndef MS_WINCE } ! #endif ! if (buf==NULL) { ! PyErr_SetString(PyExc_MemoryError, "Allocating read buffer"); return NULL; } - } - #ifndef MS_WINCE - else if (obBuf->ob_type->tp_as_buffer){ - pb = obBuf->ob_type->tp_as_buffer; - pORB = obBuf; - Py_INCREF(pORB); - bufSize = (*pb->bf_getreadbuffer)(pORB, 0, &buf); - } - #endif // MS_WINCE - else { - PyErr_SetString(PyExc_TypeError, "Second param must be an integer or a buffer object"); - return NULL; - } DWORD numRead; --- 928,976 ---- &obOverlapped)) // @pyparm <o PyOVERLAPPED>|ol|None|An overlapped structure return NULL; + if (!PyWinObject_AsHANDLE(obhFile, &hFile)) + return NULL; + // @comm in a multi-threaded overlapped environment, it is likely to be necessary to pre-allocate the read buffer using the <om win32file.AllocateReadBuffer> method, otherwise the I/O operation may complete before you can assign to the resulting buffer. ! if (obOverlapped!=Py_None){ ! #ifdef MS_WINCE ! PyErr_SetString(PyExc_NotImplementedError,"Overlapped operation is not supported on this platform"); ! return NULL; ! #else if (!PyWinObject_AsOVERLAPPED(obOverlapped, &pOverlapped)) return NULL; ! #endif ! } void *buf = NULL; PyBufferProcs *pb = NULL; ! bufSize = PyLong_AsUnsignedLong(obBuf); ! if ((bufSize!=(DWORD)-1) || !PyErr_Occurred()){ ! if (pOverlapped){ // guaranteed to be NULL on CE ! obRet = PyBuffer_New(bufSize); ! if (obRet==NULL) return NULL; + pb = obRet->ob_type->tp_as_buffer; + (*pb->bf_getreadbuffer)(obRet, 0, &buf); + } + else{ + obRet=PyString_FromStringAndSize(NULL, bufSize); + if (obRet==NULL) + return NULL; + buf=PyString_AS_STRING(obRet); + bBufMallocd=TRUE; } } ! else{ ! PyErr_Clear(); ! if (!PyWinObject_AsWriteBuffer(obBuf, &buf, &bufSize,FALSE)){ ! PyErr_SetString(PyExc_TypeError, "Second param must be an integer or a buffer object"); return NULL; + } + if (pOverlapped){ + obRet = obBuf; + Py_INCREF(obBuf); + } } DWORD numRead; *************** *** 927,947 **** err = GetLastError(); if (err!=ERROR_MORE_DATA && err != ERROR_IO_PENDING) { ! Py_XDECREF(pORB); ! if (bBufMallocd) ! free(buf); return PyWin_SetAPIError("ReadFile", err); } } ! PyObject *obRet; ! if (pOverlapped) ! obRet = pORB; ! else ! obRet = PyString_FromStringAndSize((char *)buf, numRead); ! ! PyObject *result = Py_BuildValue("iO", err, obRet); ! Py_XDECREF(obRet); ! if (bBufMallocd) ! free(buf); ! return result; } --- 983,997 ---- err = GetLastError(); if (err!=ERROR_MORE_DATA && err != ERROR_IO_PENDING) { ! Py_XDECREF(obRet); return PyWin_SetAPIError("ReadFile", err); } } ! if (obRet==NULL) ! obRet=PyString_FromStringAndSize((char *)buf, numRead); ! else if (bBufMallocd && (numRead < bufSize)) ! _PyString_Resize(&obRet, numRead); ! if (obRet==NULL) ! return NULL; ! return Py_BuildValue("iN", err, obRet); } *************** *** 959,963 **** PyObject *obhFile; HANDLE hFile; ! char *writeData; DWORD writeSize; PyObject *obWriteData; --- 1009,1013 ---- PyObject *obhFile; HANDLE hFile; ! void *writeData; DWORD writeSize; PyObject *obWriteData; *************** *** 970,986 **** &obOverlapped)) // @pyparm <o PyOVERLAPPED>|ol|None|An overlapped structure return NULL; ! if (PyString_Check(obWriteData)) { ! writeData = PyString_AsString(obWriteData); ! writeSize = PyString_Size(obWriteData); ! } ! #ifndef MS_WINCE ! else if (obWriteData->ob_type->tp_as_buffer) { ! pb = obWriteData->ob_type->tp_as_buffer; ! writeSize = (*pb->bf_getreadbuffer)(obWriteData, 0, (void **)&writeData); ! } ! #endif // MS_WINCE ! else { ! return PyErr_Format(PyExc_TypeError, "Objects of type '%s' can not be directly written to a file", obWriteData->ob_type->tp_name); ! } if (obOverlapped==NULL) pOverlapped = NULL; --- 1020,1026 ---- &obOverlapped)) // @pyparm <o PyOVERLAPPED>|ol|None|An overlapped structure return NULL; ! if (!PyWinObject_AsReadBuffer(obWriteData, &writeData, &writeSize, FALSE)) ! return NULL; ! if (obOverlapped==NULL) pOverlapped = NULL; *************** *** 1434,1441 **** void *buf = NULL; ! int bufSize = 0; BOOL bBufMallocd = FALSE; ! if (PyInt_Check(obBuffer)) { ! bufSize = PyInt_AsLong(obBuffer); buf = malloc(bufSize); if (buf==NULL) { --- 1474,1481 ---- void *buf = NULL; ! DWORD bufSize = 0; BOOL bBufMallocd = FALSE; ! bufSize = PyLong_AsUnsignedLong(obBuffer); ! if ((bufSize!=(DWORD)-1) || !PyErr_Occurred()){ buf = malloc(bufSize); if (buf==NULL) { *************** *** 1445,1456 **** bBufMallocd = TRUE; } ! else if (obBuffer->ob_type->tp_as_buffer) { ! PyBufferProcs *pb = obBuffer->ob_type->tp_as_buffer; ! bufSize = (*pb->bf_getreadbuffer)(obBuffer, 0, &buf); ! } ! else { ! PyErr_SetString(PyExc_TypeError, "buffer param must be an integer or a buffer object"); ! goto done; ! } // OK, have a buffer and a size. --- 1485,1495 ---- bBufMallocd = TRUE; } ! else{ ! PyErr_Clear(); ! if (!PyWinObject_AsReadBuffer(obBuffer, &buf, &bufSize, FALSE)){ ! PyErr_SetString(PyExc_TypeError, "buffer param must be an integer or a buffer object"); ! goto done; ! } ! } // OK, have a buffer and a size. *************** *** 1492,1498 **** // will be smaller than the size of the buffer (and certainly never greater!) // @comm See <om win32file.ReadDirectoryChangesW> for more information. ! int bufSize, size; ! char *buf; ! if (!PyArg_ParseTuple(args, "s#i", &buf, &bufSize, &size)) return NULL; if (size > bufSize) --- 1531,1540 ---- // will be smaller than the size of the buffer (and certainly never greater!) // @comm See <om win32file.ReadDirectoryChangesW> for more information. ! DWORD bufSize, size; ! void *buf; ! PyObject *obbuf; ! if (!PyArg_ParseTuple(args, "Oi", &obbuf, &size)) ! return NULL; ! if (!PyWinObject_AsReadBuffer(obbuf, &buf, &bufSize, FALSE)) return NULL; if (size > bufSize) *************** *** 3125,3144 **** DWORD user_cnt; PENCRYPTION_CERTIFICATE *user_item=NULL; ! PyObject *obsid=NULL, *ret_item=NULL; PyObject *ret=PyTuple_New(pecl->nUsers); ! if (!ret){ ! PyErr_SetString(PyExc_MemoryError,"PyWinObject_FromPENCRYPTION_CERTIFICATE_LIST: unable to allocate return tuple"); return NULL; - } user_item=pecl->pUsers; for (user_cnt=0; user_cnt < pecl->nUsers; user_cnt++){ ! obsid=PyWinObject_FromSID((*user_item)->pUserSid); ! ret_item=Py_BuildValue("Ns#", obsid, (*user_item)->pCertBlob->pbData,(*user_item)->pCertBlob->cbData); if (!ret_item){ - PyErr_SetString(PyExc_MemoryError,"PyWinObject_FromPENCRYPTION_CERTIFICATE_LIST: unable to allocate tuple item"); Py_DECREF(ret); return NULL; } ! PyTuple_SetItem(ret, user_cnt, ret_item); user_item++; } --- 3167,3184 ---- DWORD user_cnt; PENCRYPTION_CERTIFICATE *user_item=NULL; ! PyObject *ret_item=NULL; PyObject *ret=PyTuple_New(pecl->nUsers); ! if (!ret) return NULL; user_item=pecl->pUsers; for (user_cnt=0; user_cnt < pecl->nUsers; user_cnt++){ ! ret_item=Py_BuildValue("NN", ! PyWinObject_FromSID((*user_item)->pUserSid), ! PyString_FromStringAndSize((char *)(*user_item)->pCertBlob->pbData, (*user_item)->pCertBlob->cbData)); if (!ret_item){ Py_DECREF(ret); return NULL; } ! PyTuple_SET_ITEM(ret, user_cnt, ret_item); user_item++; } *************** *** 3150,3174 **** DWORD user_cnt; PENCRYPTION_CERTIFICATE_HASH *user_item=NULL; ! PyObject *obsid=NULL, *obDisplayInformation=NULL, *ret_item=NULL; PyObject *ret=PyTuple_New(pechl->nCert_Hash); ! if (!ret){ ! PyErr_SetString(PyExc_MemoryError,"PyWinObject_FromPENCRYPTION_CERTIFICATE_HASH_LIST: unable to allocate return tuple"); return NULL; - } user_item=pechl->pUsers; for (user_cnt=0; user_cnt < pechl->nCert_Hash; user_cnt++){ ! obsid=PyWinObject_FromSID((*user_item)->pUserSid); ! obDisplayInformation=PyWinObject_FromWCHAR((*user_item)->lpDisplayInformation); ! if (!obDisplayInformation){ ! Py_DECREF(ret); ! return NULL; ! } ! ret_item=Py_BuildValue("Ns#N", obsid, (*user_item)->pHash->pbData,(*user_item)->pHash->cbData, obDisplayInformation); if (!ret_item){ - PyErr_SetString(PyExc_MemoryError,"PyWinObject_FromPENCRYPTION_CERTIFICATE_HASH_LIST: unable to allocate tuple item"); Py_DECREF(ret); return NULL; } ! PyTuple_SetItem(ret, user_cnt, ret_item); user_item++; } --- 3190,3208 ---- DWORD user_cnt; PENCRYPTION_CERTIFICATE_HASH *user_item=NULL; ! PyObject *ret_item=NULL; PyObject *ret=PyTuple_New(pechl->nCert_Hash); ! if (!ret) return NULL; user_item=pechl->pUsers; for (user_cnt=0; user_cnt < pechl->nCert_Hash; user_cnt++){ ! ret_item=Py_BuildValue("NNu", ! PyWinObject_FromSID((*user_item)->pUserSid), ! PyString_FromStringAndSize((char *)(*user_item)->pHash->pbData, (*user_item)->pHash->cbData), ! (*user_item)->lpDisplayInformation); if (!ret_item){ Py_DECREF(ret); return NULL; } ! PyTuple_SET_ITEM(ret, user_cnt, ret_item); user_item++; } |