Update of /cvsroot/pywin32/pywin32/win32/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4788/win32/src
Modified Files:
PyHANDLE.cpp PyWinObjects.h PyWinTypes.h
Log Message:
Add PyWinLong_FromHANDLE to act as a unified way to return integer handles
Index: PyWinObjects.h
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/PyWinObjects.h,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** PyWinObjects.h 22 Sep 2006 14:31:32 -0000 1.10
--- PyWinObjects.h 11 Jan 2007 13:13:48 -0000 1.11
***************
*** 183,187 ****
int print(FILE *fp, int flags);
PyObject *asStr(void);
- long asLong(void);
long hash(void);
--- 183,186 ----
***************
*** 193,196 ****
--- 192,196 ----
static PyObject * strFunc(PyObject *ob);
static PyObject * intFunc(PyObject *ob);
+ static PyObject * longFunc(PyObject *ob);
static PyObject * unaryFailureFunc(PyObject *ob);
static PyObject * binaryFailureFunc(PyObject *ob1, PyObject *ob2);
Index: PyHANDLE.cpp
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/PyHANDLE.cpp,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** PyHANDLE.cpp 22 Sep 2006 14:32:33 -0000 1.12
--- PyHANDLE.cpp 11 Jan 2007 13:13:48 -0000 1.13
***************
*** 10,14 ****
{
HANDLE hInit;
! if (!PyArg_ParseTuple(args, "|i:HANDLE", &hInit))
return NULL;
return new PyHANDLE(hInit);
--- 10,17 ----
{
HANDLE hInit;
! PyObject *obhInit=Py_None;
! if (!PyArg_ParseTuple(args, "|O:HANDLE", &obhInit))
! return NULL;
! if (!PyWinObject_AsHANDLE(obhInit, &hInit, TRUE))
return NULL;
return new PyHANDLE(hInit);
***************
*** 27,31 ****
--- 30,40 ----
*pHANDLE = (HANDLE)(*pH);
} else{ // Support integer objects for b/w compat.
+ // Can't use PyLong_AsVoidPtr here, since it calls PyLong_AsLong. See
+ // http://sourceforge.net/tracker/?func=detail&atid=105470&aid=1630863&group_id=5470
+ #ifdef _WIN64
+ *pHANDLE = (HANDLE)PyLong_AsLongLong(ob);
+ #else
*pHANDLE = (HANDLE)PyInt_AsLong(ob);
+ #endif
if ((*pHANDLE==(HANDLE)-1)&&PyErr_Occurred()){
PyErr_SetString(PyExc_TypeError, "The object is not a PyHANDLE object");
***************
*** 41,44 ****
--- 50,65 ----
}
+ // For handles that aren't returned as PyHANDLE or a subclass thereof (HDC, HWND, etc).
+ // Return as python ints or longs
+ // ??? Maybe make this a macro to avoid extra function call ???
+ PyObject *PyWinLong_FromHANDLE(HANDLE h)
+ {
+ #ifdef _WIN64
+ return PyLong_FromLongLong((long long)h);
+ #else
+ return PyInt_FromLong((long)h);
+ #endif
+ }
+
BOOL PyWinObject_CloseHANDLE(PyObject *obHandle)
{
***************
*** 81,87 ****
return NULL;
PyHANDLE *pThis = (PyHANDLE *)self;
! long ret = (long)pThis->m_handle;
! pThis->m_handle = 0;
! return PyInt_FromLong(ret);
}
--- 102,109 ----
return NULL;
PyHANDLE *pThis = (PyHANDLE *)self;
! PyObject *ret = PyWinLong_FromHANDLE(pThis->m_handle);
! if (ret!=NULL)
! pThis->m_handle = 0;
! return ret;
}
***************
*** 122,126 ****
0, /* nb_coerce (allowed to be zero) */
PyHANDLE::intFunc, /* nb_int */
! PyHANDLE::unaryFailureFunc, /* nb_long */
PyHANDLE::unaryFailureFunc, /* nb_float */
PyHANDLE::unaryFailureFunc, /* nb_oct */
--- 144,148 ----
0, /* nb_coerce (allowed to be zero) */
PyHANDLE::intFunc, /* nb_int */
! PyHANDLE::longFunc, /* nb_long */
PyHANDLE::unaryFailureFunc, /* nb_float */
PyHANDLE::unaryFailureFunc, /* nb_oct */
***************
*** 233,251 ****
}
- long PyHANDLE::asLong(void)
- {
- return (long)m_handle;
- }
-
// @pymethod |PyHANDLE|__int__|Used when the handle as an integer is required.
// @comm To get the underling win32 handle from a PyHANDLE object, use int(handleObject)
PyObject * PyHANDLE::intFunc(PyObject *ob)
{
! long result = ((PyHANDLE *)ob)->asLong();
! /* PyHANDLE::asLong sets no errors
! if ( result == -1 )
! return NULL;
! */
! return PyInt_FromLong(result);
}
--- 255,270 ----
}
// @pymethod |PyHANDLE|__int__|Used when the handle as an integer is required.
// @comm To get the underling win32 handle from a PyHANDLE object, use int(handleObject)
PyObject * PyHANDLE::intFunc(PyObject *ob)
{
! return PyWinLong_FromHANDLE(((PyHANDLE *)ob)->m_handle);
! }
!
! // @pymethod |PyHANDLE|__long__|Used when the handle as an integer is required.
! // @comm To get the underling win32 handle from a PyHANDLE object, use long(handleObject)
! PyObject * PyHANDLE::longFunc(PyObject *ob)
! {
! return PyWinLong_FromHANDLE(((PyHANDLE *)ob)->m_handle);
}
***************
*** 285,289 ****
{
TCHAR resBuf[160];
! wsprintf(resBuf, _T("<%hs at %ld (%ld)>"), GetTypeName(), (long)this, (long)m_handle);
// ### ACK! Python uses a non-debug runtime. We can't use stream
// ### functions when in DEBUG mode!! (we link against a different
--- 304,308 ----
{
TCHAR resBuf[160];
! wsprintf(resBuf, _T("<%hs at %Id (%Id)>"), GetTypeName(), this, m_handle);
// ### ACK! Python uses a non-debug runtime. We can't use stream
// ### functions when in DEBUG mode!! (we link against a different
***************
*** 306,310 ****
{
TCHAR resBuf[160];
! wsprintf(resBuf, _T("<%s:%ld>"), GetTypeName(), (long)m_handle);
return PyString_FromTCHAR(resBuf);
}
--- 325,329 ----
{
TCHAR resBuf[160];
! wsprintf(resBuf, _T("<%s:%Id>"), GetTypeName(), m_handle);
return PyString_FromTCHAR(resBuf);
}
***************
*** 319,323 ****
PyErr_Clear();
if (strcmp(name, "handle")==0)
! return PyInt_FromLong((long)((PyHANDLE *)self)->m_handle);
return PyMember_Get((char *)self, memberlist, name);
}
--- 338,342 ----
PyErr_Clear();
if (strcmp(name, "handle")==0)
! return PyWinLong_FromHANDLE(((PyHANDLE *)self)->m_handle);
return PyMember_Get((char *)self, memberlist, name);
}
***************
*** 367,371 ****
// A Registry handle.
! // @object PyHKEY|A Python object, representing a win32 HKEY (a HANDLE> to a registry key).
// See the <o PyHANDLE> object for more details
BOOL PyWinObject_AsHKEY(PyObject *ob, HKEY *pRes, BOOL bNoneOK)
--- 386,390 ----
// A Registry handle.
! // @object PyHKEY|A Python object, representing a win32 HKEY (a HANDLE to a registry key).
// See the <o PyHANDLE> object for more details
BOOL PyWinObject_AsHKEY(PyObject *ob, HKEY *pRes, BOOL bNoneOK)
***************
*** 381,385 ****
{
HANDLE hInit;
! if (!PyArg_ParseTuple(args, "|i:HANDLERegistry", &hInit))
return NULL;
return new PyHKEY(hInit);
--- 400,407 ----
{
HANDLE hInit;
! PyObject *obhInit=Py_None; // ??? hInit previously not initialized but treated as optional ???
! if (!PyArg_ParseTuple(args, "|O:HANDLERegistry", &obhInit))
! return NULL;
! if (!PyWinObject_AsHANDLE(obhInit, &hInit, TRUE))
return NULL;
return new PyHKEY(hInit);
***************
*** 388,404 ****
BOOL PyWinObject_CloseHKEY(PyObject *obHandle)
{
! BOOL ok;
! if (PyHANDLE_Check(obHandle))
// Python error already set.
! ok = ((PyHKEY *)obHandle)->Close();
! else if PyInt_Check(obHandle) {
! long rc = ::RegCloseKey((HKEY)PyInt_AsLong(obHandle));
! ok = (rc==ERROR_SUCCESS);
! if (!ok)
! PyWin_SetAPIError("RegCloseKey", rc);
! } else {
! PyErr_SetString(PyExc_TypeError, "A handle must be a HKEY object or an integer");
return FALSE;
! }
return ok;
}
--- 410,429 ----
BOOL PyWinObject_CloseHKEY(PyObject *obHandle)
{
! if (PyHANDLE_Check(obHandle)){
! // Make sure we don't call Close() for any other type of PyHANDLE
! if (strcmp(((PyHANDLE *)obHandle)->GetTypeName(),"PyHKEY")!=0){
! PyErr_SetString(PyExc_TypeError,"HANDLE must be a PyHKEY");
! return FALSE;
! }
// Python error already set.
! return ((PyHKEY *)obHandle)->Close();
! }
! HKEY hkey;
! if (!PyWinObject_AsHANDLE(obHandle, (HANDLE *)&hkey, FALSE))
return FALSE;
! long rc = ::RegCloseKey(hkey);
! BOOL ok = (rc==ERROR_SUCCESS);
! if (!ok)
! PyWin_SetAPIError("RegCloseKey", rc);
return ok;
}
Index: PyWinTypes.h
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/PyWinTypes.h,v
retrieving revision 1.31
retrieving revision 1.32
diff -C2 -d -r1.31 -r1.32
*** PyWinTypes.h 27 Dec 2006 04:41:14 -0000 1.31
--- PyWinTypes.h 11 Jan 2007 13:13:48 -0000 1.32
***************
*** 394,397 ****
--- 394,398 ----
PYWINTYPES_EXPORT BOOL PyWinObject_AsHANDLE(PyObject *ob, HANDLE *pRes, BOOL bNoneOK = FALSE);
PYWINTYPES_EXPORT PyObject *PyWinObject_FromHANDLE(HANDLE h);
+ PYWINTYPES_EXPORT PyObject *PyWinLong_FromHANDLE(HANDLE h);
// A global function that can work as a module method for making a HANDLE object.
|