Update of /cvsroot/pywin32/pywin32/win32/src/win32wnet
In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv9900/win32/src/win32wnet
Modified Files:
Netres.h PyNCB.cpp PyNCB.h PyNetresource.cpp win32wnet.cpp
Log Message:
Modernization of win32wnet, via the py3k branch:
* Move to getattro/setattro
* Add support for WNetAddConnection2 taking a NETRESOURCE as the first param.
* Add tp_new slots for the type, so the type can be used in-place of global functions.
* Replace NCB and NETRESOURCE methods with the actual types.
* Treat CallName and Name attributes as true strings.
* Many more tests.
Index: win32wnet.cpp
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/win32wnet/win32wnet.cpp,v
retrieving revision 1.15
retrieving revision 1.16
diff -C2 -d -r1.15 -r1.16
*** win32wnet.cpp 9 Dec 2008 07:21:06 -0000 1.15
--- win32wnet.cpp 10 Dec 2008 11:13:51 -0000 1.16
***************
*** 129,181 ****
// @pymethod |win32wnet|WNetAddConnection2|Creates a connection to a network resource. The function can redirect
// a local device to the network resource.
! static
! PyObject *
! PyWNetAddConnection2 (PyObject *self, PyObject *args)
!
{
! // @todo Eventually should update this to use a NETRESOURCE object (it was written before PyNETRESOURCE)
! DWORD Type; // @pyparm int|type||The resource type. May be either RESOURCETYPE_DISK, RESOURCETYPE_PRINT, or RESOURCETYPE_ANY (from win32netcon)
! LPSTR LocalName; // @pyparm string|localName||holds the name of a local device to map connection to; may be NULL
! LPSTR RemoteName; // @pyparm string|remoteName||holds the passed in remote machine\service name.
! LPSTR ProviderName = NULL; // @pyparm string|ProviderName|None|holds name of network provider to use (if any): NULL lets OS handle it
! LPSTR Username = NULL; // @pyparm string|userName|None|The user name to connect as.
! LPSTR Password = NULL; // @pyparm string|password|None|The password to use.
!
! DWORD ErrorNo; // holds the returned error number, if any
! DWORD flags = 0; // @pyparm int|flags|0|Specifies a DWORD value that describes connection options. The following value is currently defined.
! // @flagh Value|Meaning
! // @flag CONNECT_UPDATE_PROFILE|The network resource connection should be remembered.
! // <nl>If this bit flag is set, the operating system automatically attempts to restore the connection when the user logs on.
! // <nl>The operating system remembers only successful connections that redirect local devices. It does not remember connections that are unsuccessful or deviceless connections. (A deviceless connection occurs when the lpLocalName member is NULL or points to an empty string.)
! // <nl>If this bit flag is clear, the operating system does not automatically restore the connection at logon.
! NETRESOURCE NetResource;
! if (!PyArg_ParseTuple(args,"izs|zzzi",&Type,&LocalName,&RemoteName,&ProviderName,&Username,&Password, &flags))
! return NULL;
! // Build the NETRESOURCE structure
! Py_BEGIN_ALLOW_THREADS
! memset((void *)&NetResource, '\0', sizeof(NETRESOURCE));
! NetResource.dwType = Type;
! NetResource.lpLocalName = LocalName;
! NetResource.lpProvider = ProviderName;
! NetResource.lpRemoteName = RemoteName;
! #ifdef _WIN32_WCE_ // Windows CE only has the #3 version...use NULL for HWND to simulate #2
! ErrorNo = WNetAddConnection3(NULL,&NetResource, Password, Username, flags);
! #else
! ErrorNo = WNetAddConnection2(&NetResource, Password, Username, flags);
! #endif
Py_END_ALLOW_THREADS
-
if (ErrorNo != NO_ERROR)
! {
! return ReturnNetError("WNetAddConnection2", ErrorNo);
}
!
! Py_INCREF(Py_None);
! return Py_None;
!
};
--- 129,197 ----
// @pymethod |win32wnet|WNetAddConnection2|Creates a connection to a network resource. The function can redirect
// a local device to the network resource.
! // @comm This function also accepts backwards-compatible, positional-only
! // arguments of (dwType, lpLocalName, lpRemoteName[, lpProviderName, Username, Password, flags])
! // @comm Accepts keyword arguments.
! // @pyseeapi WNetAddConnection2
! static PyObject *PyWNetAddConnection2 (PyObject *self, PyObject *args, PyObject *kwargs)
{
! LPTSTR Username = NULL;
! LPTSTR Password = NULL;
! // values used for b/w compat args.
! DWORD Type;
! PyObject *obLocalName, *obRemoteName, *obProviderName = Py_None;
! PyObject *obnr, *obPassword=Py_None, *obUsername=Py_None, *ret=NULL;
! DWORD ErrorNo; // holds the returned error number, if any
! DWORD flags = 0;
! NETRESOURCE *pNetResource;
! NETRESOURCE tempNetResource;
! memset(&tempNetResource, 0, sizeof(tempNetResource));
! if (PyArg_ParseTuple(args,"iOO|OOOi",&Type,&obLocalName,&obRemoteName,&obProviderName,&obUsername,&obPassword, &flags)) {
! // the b/w compat args have been used - build the NETRESOURCE structure
! memset((void *)&tempNetResource, '\0', sizeof(NETRESOURCE));
! tempNetResource.dwType = Type;
! if (!PyWinObject_AsTCHAR(obLocalName, &tempNetResource.lpLocalName, TRUE)
! || !PyWinObject_AsTCHAR(obRemoteName, &tempNetResource.lpRemoteName, FALSE)
! || !PyWinObject_AsTCHAR(obProviderName, &tempNetResource.lpProvider, TRUE))
! goto done;
! pNetResource = &tempNetResource;
! } else {
! PyErr_Clear();
! static char *keywords[] = {"NetResource","Password","UserName","Flags", NULL};
! if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOk", keywords,
! &obnr, // @pyparm <o PyNETRESOURCE>|NetResource||Describes the network resource for the connection.
! &obPassword, // @pyparm str|Password|None|The password to use. Use None for default credentials.
! &obUsername, // @pyparm str|UserName|None|The user name to connect as. Use None for default credentials.
! &flags)) // @pyparm int|Flags|0|Combination win32netcon.CONNECT_* flags
! return NULL;
! if (!PyWinObject_AsNETRESOURCE(obnr, &pNetResource, FALSE))
! return NULL;
! }
! if (!PyWinObject_AsTCHAR(obPassword, &Password, TRUE)
! || !PyWinObject_AsTCHAR(obUsername, &Username, TRUE))
! goto done;
! Py_BEGIN_ALLOW_THREADS
! #ifdef _WIN32_WCE_ // Windows CE only has the #3 version...use NULL for HWND to simulate #2
! ErrorNo = WNetAddConnection3(NULL, pNetResource, Password, Username, flags);
! #else
! ErrorNo = WNetAddConnection2(pNetResource, Password, Username, flags);
! #endif
Py_END_ALLOW_THREADS
if (ErrorNo != NO_ERROR)
! ReturnNetError("WNetAddConnection2", ErrorNo);
! else{
! Py_INCREF(Py_None);
! ret = Py_None;
}
! done:
! PyWinObject_FreeTCHAR(Password);
! PyWinObject_FreeTCHAR(Username);
! PyWinObject_FreeTCHAR(tempNetResource.lpLocalName);
! PyWinObject_FreeTCHAR(tempNetResource.lpRemoteName);
! PyWinObject_FreeTCHAR(tempNetResource.lpProvider);
! return ret;
};
***************
*** 610,617 ****
// @module win32wnet|A module that exposes the Windows Networking API.
static PyMethodDef win32wnet_functions[] = {
! // @pymeth NETRESOURCE|Creates a new <o NETRESOURCE> object
! {"NETRESOURCE", PyWinMethod_NewNETRESOURCE, 1, "NETRESOURCE Structure Object. x=NETRESOURCE() to instantiate"},
! // @pymeth NCB|Creates a new <o NCB> object
! {"NCB", PyWinMethod_NewNCB, 1, "NCB Netbios command structure Object"},
// @pymeth NCBBuffer|Creates a new buffer
{"NCBBuffer", PyWinMethod_NCBBuffer, 1, "Creates a memory buffer"},
--- 626,631 ----
// @module win32wnet|A module that exposes the Windows Networking API.
static PyMethodDef win32wnet_functions[] = {
! // @pymeth NETRESOURCE|The <o PyNETRESOURCE> type - can be used to create a new <o PyNETRESOURCE> object.
! // @pymeth NCB|The <o PyNCB> type - can be used to create a new <o PyNCB> object.
// @pymeth NCBBuffer|Creates a new buffer
{"NCBBuffer", PyWinMethod_NCBBuffer, 1, "Creates a memory buffer"},
***************
*** 646,650 ****
"A module that exposes the Windows Networking API.");
-
PyDict_SetItemString(dict, "error", PyWinExc_ApiError);
--- 660,663 ----
***************
*** 653,659 ****
--- 666,676 ----
PYWIN_MODULE_INIT_RETURN_ERROR;
+ // old "deprecated" names, before types could create instances.
PyDict_SetItemString(dict, "NETRESOURCEType", (PyObject *)&PyNETRESOURCEType);
PyDict_SetItemString(dict, "NCBType", (PyObject *)&PyNCBType);
+ PyDict_SetItemString(dict, "NETRESOURCE", (PyObject *)&PyNETRESOURCEType);
+ PyDict_SetItemString(dict, "NCB", (PyObject *)&PyNCBType);
+
PYWIN_MODULE_INIT_RETURN_SUCCESS;
}
Index: PyNetresource.cpp
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/win32wnet/PyNetresource.cpp,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** PyNetresource.cpp 3 Dec 2008 22:34:21 -0000 1.4
--- PyNetresource.cpp 10 Dec 2008 11:13:51 -0000 1.5
***************
*** 36,46 ****
! /* Main PYTHON entry point for creating a new reference. Registered by win32wnet module */
!
! // @pymethod <o PyNETRESOURCE>|win32wnet|NETRESOURCE|Creates a new <o NETRESOURCE> object.
!
! PyObject *PyWinMethod_NewNETRESOURCE(PyObject *self, PyObject *args)
{
! if (!PyArg_ParseTuple(args, ":NETRESOURCE")) // no arguments
return NULL;
return new PyNETRESOURCE(); // call the C++ constructor
--- 36,43 ----
! static PyObject *NETRESOURCE_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
! static char *kwlist[] = {0};
! if (!PyArg_ParseTupleAndKeywords(args, kwds, ":NETRESOURCE", kwlist)) // no arguments
return NULL;
return new PyNETRESOURCE(); // call the C++ constructor
***************
*** 91,96 ****
PyNETRESOURCE::deallocFunc, /* tp_dealloc */
0, /* tp_print */
! PyNETRESOURCE::getattr, /* tp_getattr */
! PyNETRESOURCE::setattr, /* tp_setattr */
PyNETRESOURCE::compareFunc, /* tp_compare */
0, /* tp_repr */
--- 88,93 ----
PyNETRESOURCE::deallocFunc, /* tp_dealloc */
0, /* tp_print */
! 0, /* tp_getattr */
! 0, /* tp_setattr */
PyNETRESOURCE::compareFunc, /* tp_compare */
0, /* tp_repr */
***************
*** 101,109 ****
0, /* tp_call */
0, /* tp_str */
};
#define OFF(e) offsetof(PyNETRESOURCE, e)
! struct memberlist PyNETRESOURCE::memberlist[] =
{
{"dwScope", T_ULONG, OFF(m_nr.dwScope), 0}, // @prop integer|dwScope|
--- 98,128 ----
0, /* tp_call */
0, /* tp_str */
+ PyNETRESOURCE::getattro, /* tp_getattro */
+ PyNETRESOURCE::setattro, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ PyNETRESOURCE::members, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ NETRESOURCE_new, /* tp_new */
};
#define OFF(e) offsetof(PyNETRESOURCE, e)
! struct PyMemberDef PyNETRESOURCE::members[] =
{
{"dwScope", T_ULONG, OFF(m_nr.dwScope), 0}, // @prop integer|dwScope|
***************
*** 112,115 ****
--- 131,135 ----
{"dwUsage", T_ULONG, OFF(m_nr.dwUsage), 0}, // @prop integer|dwUsage|
+ // These are handled by getattro/setattro
{"lpLocalName", T_STRING, OFF(m_nr.lpLocalName), 0}, // @prop string|localName|
{"lpRemoteName",T_STRING, OFF(m_nr.lpRemoteName), 0},// @prop string|remoteName|
***************
*** 117,120 ****
--- 137,144 ----
{"lpProvider", T_STRING, OFF(m_nr.lpProvider), 0},// @prop string|provider|
{NULL}
+ // @comm Note that in pywin32-212 and earlier, the string attributes
+ // were always strings, but empty strings when the underlying Windows
+ // structure had NULL. On later pywin32 builds, these string attributes will
+ // return None in such cases.
};
***************
*** 124,216 ****
_Py_NewReference(this);
memset(&m_nr, 0, sizeof(m_nr));
- m_nr.lpLocalName = szLName;
- m_nr.lpRemoteName = szRName;
- m_nr.lpProvider = szProv;
- m_nr.lpComment = szComment;
- szLName[0] = _T('\0');
- szRName[0] = _T('\0');
- szProv[0] = _T('\0');
- szComment[0] = _T('\0');
}
! PyNETRESOURCE::PyNETRESOURCE(const NETRESOURCE *pO)
{
ob_type = &PyNETRESOURCEType;
_Py_NewReference(this);
! m_nr.dwScope = pO->dwScope;
! m_nr.dwType = pO->dwType;
! m_nr.dwDisplayType = pO->dwDisplayType;
! m_nr.dwUsage = pO->dwUsage;
! m_nr.lpLocalName = szLName;
! m_nr.lpRemoteName = szRName;
! m_nr.lpProvider = szProv;
! m_nr.lpComment = szComment;
!
! if (pO->lpLocalName == NULL)
! szLName[0] = _T('\0');
! else
! {
! _tcsncpy(szLName, pO->lpLocalName, MAX_NAME);
! szLName[MAX_NAME-1] = _T('\0'); // explicit termination!
! }
!
!
! if (pO->lpRemoteName == NULL)
! szRName[0] = _T('\0');
! else
! {
! _tcsncpy(szRName, pO->lpRemoteName, MAX_NAME);
! szRName[MAX_NAME-1] = _T('\0'); // explicit termination!
! }
!
!
! if (pO->lpProvider == NULL)
! szProv[0] = _T('\0');
! else
! {
! _tcsncpy(szProv, pO->lpProvider, MAX_NAME);
! szProv[MAX_NAME-1] = _T('\0'); // explicit termination!
! }
!
! if (pO->lpComment == NULL)
! szComment[0] = _T('\0');
! else
! {
! _tcsncpy(szComment, pO->lpComment, MAX_COMMENT);
! szComment[MAX_COMMENT-1] = _T('\0');
! }
}
PyNETRESOURCE::~PyNETRESOURCE(void)
{
!
}
! PyObject *PyNETRESOURCE::getattr(PyObject *self, char *name)
{
! #ifdef UNICODE
PyNETRESOURCE *This = (PyNETRESOURCE *)self;
if (strcmp(name, "lpProvider")==0)
! return PyWinObject_FromWCHAR(This->m_nr.lpProvider);
! else if
! (strcmp(name, "lpRemoteName") == 0)
! return PyWinObject_FromWCHAR(This->m_nr.lpRemoteName);
! else if
! (strcmp(name, "lpLocalName") == 0)
! return PyWinObject_FromWCHAR(This->m_nr.lpLocalName);
! else if
! (strcmp(name, "lpComment") == 0)
! return PyWinObject_FromWCHAR(This->m_nr.lpComment);
! else
! #endif
! return PyMember_Get((char *)self, memberlist, name);
}
!
!
! int PyNETRESOURCE::setattr(PyObject *self, char *name, PyObject *v)
{
if (v == NULL) {
--- 148,199 ----
_Py_NewReference(this);
memset(&m_nr, 0, sizeof(m_nr));
}
! PyNETRESOURCE::PyNETRESOURCE(const NETRESOURCE *p_nr)
{
ob_type = &PyNETRESOURCEType;
_Py_NewReference(this);
! m_nr=*p_nr;
+ // Copy strings so they can be freed in same way as when set via setattro
+ // No error checking here, no way to return an error from a constructor anyway
+ if (p_nr->lpProvider)
+ m_nr.lpProvider=PyWin_CopyString(p_nr->lpProvider);
+ if (p_nr->lpRemoteName)
+ m_nr.lpRemoteName=PyWin_CopyString(p_nr->lpRemoteName);
+ if (p_nr->lpLocalName)
+ m_nr.lpLocalName=PyWin_CopyString(p_nr->lpLocalName);
+ if (p_nr->lpComment)
+ m_nr.lpComment=PyWin_CopyString(p_nr->lpComment);
}
PyNETRESOURCE::~PyNETRESOURCE(void)
{
! PyWinObject_FreeTCHAR(m_nr.lpProvider);
! PyWinObject_FreeTCHAR(m_nr.lpRemoteName);
! PyWinObject_FreeTCHAR(m_nr.lpLocalName);
! PyWinObject_FreeTCHAR(m_nr.lpComment);
}
! PyObject *PyNETRESOURCE::getattro(PyObject *self, PyObject *obname)
{
! char *name=PYWIN_ATTR_CONVERT(obname);
! if (name==NULL)
! return NULL;
PyNETRESOURCE *This = (PyNETRESOURCE *)self;
if (strcmp(name, "lpProvider")==0)
! return PyWinObject_FromTCHAR(This->m_nr.lpProvider);
! if (strcmp(name, "lpRemoteName")==0)
! return PyWinObject_FromTCHAR(This->m_nr.lpRemoteName);
! if (strcmp(name, "lpLocalName")==0)
! return PyWinObject_FromTCHAR(This->m_nr.lpLocalName);
! if (strcmp(name, "lpComment")==0)
! return PyWinObject_FromTCHAR(This->m_nr.lpComment);
! return PyObject_GenericGetAttr(self, obname);
}
! int PyNETRESOURCE::setattro(PyObject *self, PyObject *obname, PyObject *v)
{
if (v == NULL) {
***************
*** 218,268 ****
return -1;
}
PyNETRESOURCE *This = (PyNETRESOURCE *)self;
! // the following specific string attributes can be set, all others generate an error.
!
! if (PyString_Check(v))
! {
! int ret;
! TCHAR *value;
! if (!PyWinObject_AsTCHAR(v, &value, FALSE))
return -1;
! if (strcmp (name, "lpProvider") == 0)
! {
! _tcsncpy(This->szProv, value, MAX_NAME); // no overflow allowed!
! This->szProv[MAX_NAME-1] = _T('\0'); // make sure NULL terminated!
! ret = 0;
! }
! else
! if (strcmp(name, "lpRemoteName") == 0)
! {
! _tcsncpy(This->szRName, value, MAX_NAME);
! This->szRName[MAX_NAME-1] = _T('\0');
! ret = 0;
}
! else
! if (strcmp(name, "lpLocalName") == 0)
! {
! _tcsncpy(This->szLName, value, MAX_NAME);
! This->szLName[MAX_NAME-1] = _T('\0');
! ret = 0;
}
! else
! if (strcmp(name, "lpComment") == 0)
! {
! _tcsncpy(This->szComment, value, MAX_COMMENT);
! This->szComment[MAX_COMMENT-1] = _T('\0');
! ret = 0;
}
! else
! {
! PyErr_SetString(PyExc_AttributeError, "The attribute is not a PyNETRESOURCE string");
! ret = -1;
}
! PyWinObject_FreeTCHAR(value);
! return ret;
! } // PyString_Check
!
! return PyMember_Set((char *)self, memberlist, name, v);
}
--- 201,239 ----
return -1;
}
+ char *name=PYWIN_ATTR_CONVERT(obname);
+ if (name==NULL)
+ return NULL;
PyNETRESOURCE *This = (PyNETRESOURCE *)self;
! TCHAR *value;
! if (strcmp(name, "lpProvider")==0){
! if (!PyWinObject_AsTCHAR(v, &value, TRUE))
return -1;
! PyWinObject_FreeTCHAR(This->m_nr.lpProvider);
! This->m_nr.lpProvider=value;
! return 0;
}
! if (strcmp(name, "lpRemoteName")==0){
! if (!PyWinObject_AsTCHAR(v, &value, TRUE))
! return -1;
! PyWinObject_FreeTCHAR(This->m_nr.lpRemoteName);
! This->m_nr.lpRemoteName=value;
! return 0;
}
! if (strcmp(name, "lpLocalName")==0){
! if (!PyWinObject_AsTCHAR(v, &value, TRUE))
! return -1;
! PyWinObject_FreeTCHAR(This->m_nr.lpLocalName);
! This->m_nr.lpLocalName=value;
! return 0;
}
! if (strcmp(name, "lpComment")==0){
! if (!PyWinObject_AsTCHAR(v, &value, TRUE))
! return -1;
! PyWinObject_FreeTCHAR(This->m_nr.lpComment);
! This->m_nr.lpComment=value;
! return 0;
}
! return PyObject_GenericSetAttr(self, obname, v);
}
***************
*** 278,296 ****
if (!PyWinObject_AsNETRESOURCE(ob, &p_nr, FALSE)) // sets error exception
! return NULL;
// do integer tests first
! if ((m_nr.dwType != p_nr->dwType) ||
! (m_nr.dwScope != p_nr->dwScope) ||
! (m_nr.dwUsage != p_nr->dwUsage) ||
! (m_nr.dwDisplayType != p_nr->dwDisplayType))
! return (0);
!
! if ((_tcscmp(szComment, GetComment())) ||
! (_tcscmp(szLName, GetLName())) ||
! (_tcscmp(szProv, GetProvider())) ||
! (_tcscmp(szRName, GetRName())))
! return 0;
! return 1;
};
--- 249,289 ----
if (!PyWinObject_AsNETRESOURCE(ob, &p_nr, FALSE)) // sets error exception
! return -1;
// do integer tests first
! if (m_nr.dwType != p_nr->dwType ||
! m_nr.dwScope != p_nr->dwScope ||
! m_nr.dwUsage != p_nr->dwUsage ||
! m_nr.dwDisplayType != p_nr->dwDisplayType)
! return 1;
! if (m_nr.lpComment && p_nr->lpComment){
! if (_tcscmp(m_nr.lpComment, p_nr->lpComment) != 0)
! return 1;
! }
! else if (m_nr.lpComment || p_nr->lpComment)
! return 1;
!
! if (m_nr.lpLocalName && p_nr->lpLocalName){
! if (_tcscmp(m_nr.lpLocalName, p_nr->lpLocalName) != 0)
! return 1;
! }
! else if (m_nr.lpLocalName || p_nr->lpLocalName)
! return 1;
!
! if (m_nr.lpProvider && p_nr->lpProvider){
! if (_tcscmp(m_nr.lpProvider, p_nr->lpProvider) != 0)
! return 1;
! }
! else if (m_nr.lpProvider || p_nr->lpProvider)
! return 1;
!
! if (m_nr.lpRemoteName && p_nr->lpRemoteName){
! if (_tcscmp(m_nr.lpRemoteName, p_nr->lpRemoteName) != 0)
! return 1;
! }
! else if (m_nr.lpRemoteName || p_nr->lpRemoteName)
! return 1;
!
! return 0;
};
Index: PyNCB.h
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/win32wnet/PyNCB.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** PyNCB.h 28 Mar 2000 11:34:29 -0000 1.2
--- PyNCB.h 10 Dec 2008 11:13:51 -0000 1.3
***************
*** 33,45 ****
static void deallocFunc(PyObject *ob);
! static PyObject *getattr(PyObject *self, char *name);
! static int setattr(PyObject *self, char *name, PyObject *v);
!
!
! #pragma warning( disable : 4251 )
! static struct memberlist memberlist[];
! #pragma warning( default : 4251 )
-
NCB m_ncb;
DWORD dwStatus; // status of this object (used during copy construct)
--- 33,41 ----
static void deallocFunc(PyObject *ob);
! static PyObject *getattro(PyObject *self, PyObject *obname);
! static int setattro(PyObject *self, PyObject *obname, PyObject *v);
! static struct PyMemberDef members[];
! static struct PyMethodDef methods[];
NCB m_ncb;
DWORD dwStatus; // status of this object (used during copy construct)
***************
*** 51,56 ****
#define PyNCB_Check(ob) ((ob)->ob_type == &PyNCBType)
- __declspec(dllexport) PyObject *PyWinMethod_NewNCB(PyObject *self, PyObject *args);
-
-
#endif // end of _WIN32_WCE exclude
--- 47,49 ----
Index: PyNCB.cpp
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/win32wnet/PyNCB.cpp,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** PyNCB.cpp 3 Dec 2008 22:34:21 -0000 1.5
--- PyNCB.cpp 10 Dec 2008 11:13:51 -0000 1.6
***************
*** 37,40 ****
--- 37,51 ----
#include <crtdbg.h>
+ /***************************************************************************
+ ** Create a new NCB Object
+ ***************************************************************************/
+ static PyObject *NCB_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+ {
+ static char *kwlist[] = {0};
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, ":NCB", kwlist)) // no arguments
+ return NULL;
+ return new PyNCB(); // call the C++ constructor
+ }
+
__declspec(dllexport)PyTypeObject PyNCBType =
***************
*** 46,52 ****
PyNCB::deallocFunc, /* tp_dealloc */
0, /* tp_print */
! PyNCB::getattr, /* tp_getattr */
! PyNCB::setattr, /* tp_setattr */
! // PyNCB::compareFunc, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
--- 57,63 ----
PyNCB::deallocFunc, /* tp_dealloc */
0, /* tp_print */
! 0, /* tp_getattr */
! 0, /* tp_setattr */
! 0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
***************
*** 56,59 ****
--- 67,92 ----
0, /* tp_call */
0, /* tp_str */
+ PyNCB::getattro, /* tp_getattro */
+ PyNCB::setattro, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ PyNCB::methods, /* tp_methods */
+ PyNCB::members, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ NCB_new, /* tp_new */
};
***************
*** 68,72 ****
}
! static struct PyMethodDef PyUnicode_methods[] = {
{ "Reset", PyNCB_Reset, METH_VARARGS },
{ NULL },
--- 101,105 ----
}
! struct PyMethodDef PyNCB::methods[] = {
{ "Reset", PyNCB_Reset, METH_VARARGS },
{ NULL },
***************
*** 75,79 ****
// @object NCB|A Python object that encapsulates a Win32 NCB structure.
#define OFF(e) offsetof(PyNCB, e)
! struct memberlist PyNCB::memberlist[] =
{
{"Command", T_UBYTE, OFF(m_ncb.ncb_command), 0}, // @prop int|Command|
--- 108,112 ----
// @object NCB|A Python object that encapsulates a Win32 NCB structure.
#define OFF(e) offsetof(PyNCB, e)
! struct PyMemberDef PyNCB::members[] =
{
{"Command", T_UBYTE, OFF(m_ncb.ncb_command), 0}, // @prop int|Command|
***************
*** 171,189 ****
};
- /***************************************************************************
- ** Create a new NCB Object
- ***************************************************************************/
- // @pymethod <o NCB>|win32wnet|NCB|Creates a new <o NCB> object.
- PyObject *PyWinMethod_NewNCB(PyObject *self, PyObject *args)
- {
- if (!PyArg_ParseTuple(args, ":NCB")) // no arguments
- return NULL;
- return new PyNCB(); // call the C++ constructor
- }
-
/*********************************************************************/
! PyObject *PyNCB::getattr(PyObject *self, char *name)
{
PyNCB *This = (PyNCB *)self;
if (strcmp(name, "Callname") == 0) // these "strings" are not null terminated so build
// a local representation of them and return
--- 204,217 ----
};
/*********************************************************************/
! PyObject *PyNCB::getattro(PyObject *self, PyObject *obname)
{
PyNCB *This = (PyNCB *)self;
+ char *name=PYWIN_ATTR_CONVERT(obname);
+ if (name==NULL)
+ return NULL;
+ // Our string attributes still need special handling as the NCB isn't
+ // unicode aware.
+ // These 2 string attributes are logically "strings" rather than "bytes"
if (strcmp(name, "Callname") == 0) // these "strings" are not null terminated so build
// a local representation of them and return
***************
*** 192,196 ****
char TempString[17];
TempString[16] = '\0';
! return(PyString_FromString(strncpy((char *)TempString,(char *)This->m_ncb.ncb_callname,NCBNAMSZ)));
}
else if(strcmp(name, "Name") == 0)
--- 220,224 ----
char TempString[17];
TempString[16] = '\0';
! return(PyWinCoreString_FromString(strncpy((char *)TempString,(char *)This->m_ncb.ncb_callname,NCBNAMSZ)));
}
else if(strcmp(name, "Name") == 0)
***************
*** 198,205 ****
char TempString[17];
TempString[16] = '\0';
! return(PyString_FromString(strncpy((char *)TempString,(char *)This->m_ncb.ncb_name,NCBNAMSZ)));
}
else if(strcmp(name, "Buffer") == 0)
{
if (This->m_obuserbuffer != NULL) {
Py_INCREF(This->m_obuserbuffer);
--- 226,234 ----
char TempString[17];
TempString[16] = '\0';
! return(PyWinCoreString_FromString(strncpy((char *)TempString,(char *)This->m_ncb.ncb_name,NCBNAMSZ)));
}
else if(strcmp(name, "Buffer") == 0)
{
+ // This is logically bytes
if (This->m_obuserbuffer != NULL) {
Py_INCREF(This->m_obuserbuffer);
***************
*** 212,227 ****
return PyBuffer_FromMemory(This->m_ncb.ncb_buffer, This->m_ncb.ncb_length);
}
! else {
! PyObject *ret = PyMember_Get((char *)self, memberlist, name);
! if (ret==NULL) {
! PyErr_Clear();
! ret = Py_FindMethod(PyUnicode_methods, self, name);
! }
! return ret;
! }
};
/********************************************************************/
! int PyNCB::setattr(PyObject *self, char *name, PyObject *v)
{
if (v == NULL) {
--- 241,249 ----
return PyBuffer_FromMemory(This->m_ncb.ncb_buffer, This->m_ncb.ncb_length);
}
! return PyObject_GenericGetAttr(self, obname);
};
/********************************************************************/
! int PyNCB::setattro(PyObject *self, PyObject *obname, PyObject *v)
{
if (v == NULL) {
***************
*** 229,308 ****
return -1;
}
! // the following specific string attributes can be set
! if (PyString_Check(v))
! {
! PyNCB *This = (PyNCB *)self;
!
! if (strcmp(name, "Callname") == 0)
! {
! int srclen = lstrlenA(PyString_AsString(v));
! if (srclen > NCBNAMSZ) // cap string length at NCBNAMSZ(16)
! srclen = NCBNAMSZ;
! memset (This->m_ncb.ncb_callname, ' ', NCBNAMSZ); // make sure that the name is space padded
! strncpy ((char *)This->m_ncb.ncb_callname, PyString_AsString(v), srclen);
! if (srclen == 0) // source was null string
! This->m_ncb.ncb_callname[0] = '\0';
! return 0;
}
- else
- if (strcmp(name, "Name") == 0)
- {
- int srclen = lstrlenA(PyString_AsString(v));
- if (srclen > NCBNAMSZ)
- srclen = NCBNAMSZ;
! memset (This->m_ncb.ncb_name, ' ', NCBNAMSZ);
! strncpy ((char *)This->m_ncb.ncb_name, PyString_AsString(v), srclen);
! if (srclen == 0) // source was null string
! This->m_ncb.ncb_callname[0] = '\0';
! return 0;
}
- } // PyString_Check
if (strcmp(name, "Buffer") == 0)
{
PyNCB *This = (PyNCB *)self;
! PyObject *ob_buf = v;
! if (PyInstance_Check(v)) {
! ob_buf = PyObject_GetAttrString(v, "_buffer_");
! if (ob_buf==NULL) {
! PyErr_Clear();
! PyErr_SetString(PyExc_TypeError, "The instance must have a _buffer_ attribute");
! return -1;
}
! }
! PyBufferProcs *pb = ob_buf->ob_type->tp_as_buffer;
! if ( pb == NULL || pb->bf_getwritebuffer == NULL ||
! pb->bf_getsegcount == NULL ) {
! PyErr_SetString(PyExc_TypeError, "The object must support the write-buffer interface");
! return -1;
! }
! if ( (*pb->bf_getsegcount)(ob_buf, NULL) != 1 ) {
! PyErr_SetString(PyExc_TypeError, "The object must be a single-segment write-buffer");
return -1;
! }
! This->m_ncb.ncb_length = pb->bf_getwritebuffer(ob_buf, 0, (void **)&This->m_ncb.ncb_buffer);
! if (This->m_ncb.ncb_length==-1) {
! This->m_ncb.ncb_length = 0;
return -1;
! }
Py_XDECREF(This->m_obbuffer);
Py_XDECREF(This->m_obuserbuffer);
- This->m_obbuffer = ob_buf;
- Py_INCREF(ob_buf);
- This->m_obuserbuffer = v;
Py_INCREF(v);
! if (ob_buf != v)
! Py_DECREF(ob_buf); // for the temp refcount from the GetAttrString
return 0;
}
!
!
!
! return PyMember_Set((char *)self, memberlist, name, v);
}
--- 251,323 ----
return -1;
}
+ char *name=PYWIN_ATTR_CONVERT(obname);
+ if (name==NULL)
+ return NULL;
+ PyNCB *This = (PyNCB *)self;
! if (strcmp(name, "Callname") == 0){
! char *value;
! DWORD valuelen;
! if (!PyWinObject_AsString(v, &value, FALSE, &valuelen))
! return -1;
! if (valuelen > NCBNAMSZ) // cap string length at NCBNAMSZ(16)
! valuelen = NCBNAMSZ;
! memset (This->m_ncb.ncb_callname, ' ', NCBNAMSZ); // make sure that the name is space padded
! strncpy ((char *)This->m_ncb.ncb_callname, value, valuelen);
! if (valuelen == 0) // source was null string
! This->m_ncb.ncb_callname[0] = '\0';
! PyWinObject_FreeString(value);
! return 0;
}
! if (strcmp(name, "Name") == 0){
! char *value;
! DWORD valuelen;
! if (!PyWinObject_AsString(v, &value, FALSE, &valuelen))
! return -1;
! if (valuelen > NCBNAMSZ) // cap string length at NCBNAMSZ(16)
! valuelen = NCBNAMSZ;
! memset (This->m_ncb.ncb_name, ' ', NCBNAMSZ);
! strncpy ((char *)This->m_ncb.ncb_name, value, valuelen);
! if (valuelen == 0) // source was null string
! This->m_ncb.ncb_callname[0] = '\0';
! PyWinObject_FreeString(value);
! return 0;
}
if (strcmp(name, "Buffer") == 0)
{
PyNCB *This = (PyNCB *)self;
! PyObject *ob_buf = PyObject_GetAttrString(v, "_buffer_");
! if (ob_buf==NULL){
! PyErr_Clear();
! ob_buf=v;
! Py_INCREF(ob_buf);
}
!
! void *buf;
! DWORD buflen;
! if (!PyWinObject_AsWriteBuffer(ob_buf, &buf, &buflen)){
! Py_DECREF(ob_buf);
return -1;
! }
! if (buflen > USHRT_MAX){
! Py_DECREF(ob_buf);
! PyErr_Format(PyExc_ValueError, "Buffer can be at most %d bytes", USHRT_MAX);
return -1;
! }
Py_XDECREF(This->m_obbuffer);
+ This->m_obbuffer=ob_buf;
Py_XDECREF(This->m_obuserbuffer);
Py_INCREF(v);
! This->m_obuserbuffer=v;
! This->m_ncb.ncb_length = (WORD)buflen;
! This->m_ncb.ncb_buffer = (PUCHAR)buf;
return 0;
}
! return PyObject_GenericSetAttr(self, obname, v);
}
Index: Netres.h
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/win32wnet/Netres.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** Netres.h 19 Apr 2006 03:15:10 -0000 1.2
--- Netres.h 10 Dec 2008 11:13:51 -0000 1.3
***************
*** 30,37 ****
public:
NETRESOURCE *GetNetresource() {return &m_nr;}
- LPTSTR GetComment() {return szComment;}
- LPTSTR GetLName() {return szLName;}
- LPTSTR GetRName() {return szRName;}
- LPTSTR GetProvider() {return szProv;}
PyNETRESOURCE(void);
--- 30,33 ----
***************
*** 43,66 ****
static void deallocFunc(PyObject *ob);
! static PyObject *getattr(PyObject *self, char *name);
! static int setattr(PyObject *self, char *name, PyObject *v);
static int compareFunc(PyObject *ob1, PyObject *ob2);
!
!
! #pragma warning( disable : 4251 )
! static struct memberlist memberlist[];
! #pragma warning( default : 4251 )
protected:
! /* NETRESOURCE contains pointer to strings (LPTSTR) to four items. we are
! copying the values to strings local to each NETRESOURCE instance, thereby keeping
! each NETRESOURCE object self contained. Note that the NETRESOURCE Object's
! strings are not Python Strings!*/
!
! NETRESOURCE m_nr;
! TCHAR szLName[MAX_NAME]; // length??
! TCHAR szRName[MAX_NAME]; // probably overkill
! TCHAR szProv[MAX_NAME];
! TCHAR szComment[MAX_COMMENT];
};
--- 39,52 ----
static void deallocFunc(PyObject *ob);
! static PyObject *getattro(PyObject *self, PyObject *obname);
! static int setattro(PyObject *self, PyObject *obname, PyObject *v);
static int compareFunc(PyObject *ob1, PyObject *ob2);
! static struct PyMemberDef members[];
protected:
! /* NETRESOURCE contains pointer to strings (LPTSTR) to four items.
! These are allocated and released by PyWinObject_AsWCHAR and PyWinObject_FreeWCHAR
! */
! NETRESOURCE m_nr;
};
***************
*** 68,73 ****
#define PyNETRESOURCE_Check(ob) ((ob)->ob_type == &PyNETRESOURCEType)
- __declspec(dllexport) PyObject *PyWinMethod_NewNETRESOURCE(PyObject *self, PyObject *args);
__declspec(dllexport) BOOL PyWinObject_AsNETRESOURCE(PyObject *ob, NETRESOURCE **ppNetresource, BOOL bNoneOK = TRUE);
__declspec(dllexport) PyObject *PyWinObject_FromNETRESOURCE(const NETRESOURCE *pNetresource);
-
--- 54,57 ----
|