PyObject *PyDISPLAY_DEVICE::getattro(PyObject *self, PyObject *obname) { PDISPLAY_DEVICE pdisplay_device=&((PyDISPLAY_DEVICE *)self)->display_device; char *name=PYWIN_ATTR_CONVERT(obname); if (name==NULL) return NULL; if (strcmp(name,"DeviceName")==0) if (pdisplay_device->DeviceName[31]==0) // in case DeviceName fills space and has no trailing NULL return PyWinObject_FromTCHAR(pdisplay_device->DeviceName); else return PyWinObject_FromTCHAR(pdisplay_device->DeviceName, 32); if (strcmp(name,"DeviceString")==0) if (pdisplay_device->DeviceString[127]==0) // in case DeviceString fills space and has no trailing NULL return PyWinObject_FromTCHAR(pdisplay_device->DeviceString); else return PyWinObject_FromTCHAR(pdisplay_device->DeviceString, 128); if (strcmp(name,"DeviceID")==0) if (pdisplay_device->DeviceID[127]==0) // in case DeviceID fills space and has no trailing NULL return PyWinObject_FromTCHAR(pdisplay_device->DeviceID); else return PyWinObject_FromTCHAR(pdisplay_device->DeviceID, 128); if (strcmp(name,"DeviceKey")==0) if (pdisplay_device->DeviceKey[127]==0) // in case DeviceKey fills space and has no trailing NULL return PyWinObject_FromTCHAR(pdisplay_device->DeviceKey); else return PyWinObject_FromTCHAR(pdisplay_device->DeviceKey, 128); return PyObject_GenericGetAttr(self,obname); }
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); }
PyObject* PyIADs_getattro(PyObject *ob, PyObject *obname) { char *name = PYWIN_ATTR_CONVERT(obname); if (!name) return NULL; IADs *p = PyIADs::GetI(ob); // These are all BSTR values BSTR ret = NULL; HRESULT hr; BOOL bad = FALSE; Py_BEGIN_ALLOW_THREADS // docs refer to 'property' as AdsPath, but function is ADsPath // allow both // @prop <o PyUnicode>|ADsPath| // @prop <o PyUnicode>|AdsPath|Synonym for ADsPath if (strcmp(name, "AdsPath")==0 || strcmp(name, "ADsPath")==0) hr = p->get_ADsPath(&ret); // @prop <o PyUnicode>|Class| else if (strcmp(name, "Class")==0) hr = p->get_Class(&ret); // @prop <o PyUnicode>|GUID|Like the IADs method, this returns a string rather than a GUID object. else if (strcmp(name, "GUID")==0) hr = p->get_GUID(&ret); // @prop <o PyUnicode>|Name| else if (strcmp(name, "Name")==0) hr = p->get_Name(&ret); // @prop <o PyUnicode>|Parent| else if (strcmp(name, "Parent")==0) hr = p->get_Parent(&ret); // @prop <o PyUnicode>|Schema| else if (strcmp(name, "Schema")==0) hr = p->get_Schema(&ret); else bad = TRUE; Py_END_ALLOW_THREADS if (bad) return PyIBase::getattro(ob, obname); if (FAILED(hr)) return PyCom_BuildPyException(hr, p, IID_IADs ); PyObject *rc = MakeBstrToObj(ret); SysFreeString(ret); return rc; }
int PyNETRESOURCE::setattro(PyObject *self, PyObject *obname, PyObject *v) { if (v == NULL) { PyErr_SetString(PyExc_AttributeError, "can't delete NETRESOURCE attributes"); 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); }
int PyDSBUFFERDESC::setattro(PyObject *self, PyObject *obname, PyObject *obvalue) { PyDSBUFFERDESC *obself = (PyDSBUFFERDESC*)self; char *name=PYWIN_ATTR_CONVERT(obname); if (name==NULL) return -1; if (strcmp(name,"lpwfxFormat") == 0) { if (obvalue == Py_None) { obself->m_dsbd.lpwfxFormat = NULL; } else if (!PyWAVEFORMATEX_Check(obvalue)) { PyErr_SetString(PyExc_ValueError,"lpwfxFormat must be a WAVEFORMATEX instance"); return -1; } else { obself->m_dsbd.lpwfxFormat = &((PyWAVEFORMATEX*)obvalue)->m_wfx; } } return PyObject_GenericSetAttr(self, obname, obvalue); }
static PyObject *getattro(PyObject *self, PyObject *obname) { char *name=PYWIN_ATTR_CONVERT(obname); if (name==NULL) return NULL; if (strcmp(name, "__members__")==0) { PyObject *ret = PyList_New(4); if (ret) { PyList_SET_ITEM(ret, 0, PyString_FromString("AttrName")); PyList_SET_ITEM(ret, 1, PyString_FromString("ControlCode")); PyList_SET_ITEM(ret, 2, PyString_FromString("ADsType")); PyList_SET_ITEM(ret, 3, PyString_FromString("Values")); } return ret; } if (strcmp(name, "Values")==0) { PyObject *ret = ((PyADS_ATTR_INFO *)self)->obValues ? ((PyADS_ATTR_INFO *)self)->obValues : Py_None; Py_INCREF(ret); return ret; } return PyObject_GenericGetAttr(self, obname); }
PyObject *PySTGMEDIUM::getattro(PyObject *self, PyObject *obname) { char *name=PYWIN_ATTR_CONVERT(obname); if (name==NULL) return NULL; PySTGMEDIUM *ps = (PySTGMEDIUM *)self; // @prop int|tymed|An integer indicating the type of data in the stgmedium if (strcmp(name, "tymed")==0) return PyInt_FromLong(ps->medium.tymed); // @prop object|data|The data in the stgmedium. // The result depends on the value of the 'tymed' property of the <o PySTGMEDIUM> object. // @flagh tymed|Result Type if (strcmp(name, "data")==0) { switch (ps->medium.tymed) { // @flag TYMED_GDI|An integer GDI handle case TYMED_GDI: return PyLong_FromVoidPtr(ps->medium.hBitmap); // @flag TYMED_MFPICT|An integer METAFILE handle case TYMED_MFPICT: return PyLong_FromVoidPtr(ps->medium.hMetaFilePict); // @flag TYMED_ENHMF|An integer ENHMETAFILE handle case TYMED_ENHMF: return PyLong_FromVoidPtr(ps->medium.hEnhMetaFile); // @flag TYMED_HGLOBAL|A string with the bytes of the global memory object. case TYMED_HGLOBAL: { PyObject *ret; void *p = GlobalLock(ps->medium.hGlobal); if (p) { ret = PyString_FromStringAndSize( (char *)p, GlobalSize(ps->medium.hGlobal)); GlobalUnlock(ps->medium.hGlobal); } else { ret = Py_None; Py_INCREF(Py_None); } return ret; } // @flag TYMED_FILE|A string/unicode filename case TYMED_FILE: return PyWinObject_FromWCHAR(ps->medium.lpszFileName); // @flag TYMED_ISTREAM|A <o PyIStream> object case TYMED_ISTREAM: // @flag TYMED_ISTORAGE|A <o PyIStorage> object return PyCom_PyObjectFromIUnknown(ps->medium.pstm, IID_IStream, TRUE); case TYMED_ISTORAGE: return PyCom_PyObjectFromIUnknown(ps->medium.pstg, IID_IStorage, TRUE); case TYMED_NULL: PyErr_SetString(PyExc_ValueError, "This STGMEDIUM has no data"); return NULL; default: PyErr_SetString(PyExc_RuntimeError, "Unknown tymed"); return NULL; } } // @prop int|data_handle|The raw 'integer' representation of the data. // For TYMED_HGLOBAL, this is the handle rather than the string data. // For the string and interface types, this is an integer holding the pointer. if (strcmp(name, "data_handle")==0) { switch (ps->medium.tymed) { case TYMED_GDI: return PyLong_FromVoidPtr(ps->medium.hBitmap); case TYMED_MFPICT: return PyLong_FromVoidPtr(ps->medium.hMetaFilePict); case TYMED_ENHMF: return PyLong_FromVoidPtr(ps->medium.hEnhMetaFile); case TYMED_HGLOBAL: return PyLong_FromVoidPtr(ps->medium.hGlobal); // and may as well hand out the pointers for these. // We are all consenting adults :) case TYMED_FILE: return PyLong_FromVoidPtr(ps->medium.lpszFileName); case TYMED_ISTREAM: return PyLong_FromVoidPtr(ps->medium.pstm); case TYMED_ISTORAGE: return PyLong_FromVoidPtr(ps->medium.pstg); case TYMED_NULL: PyErr_SetString(PyExc_ValueError, "This STGMEDIUM has no data"); return NULL; default: PyErr_SetString(PyExc_RuntimeError, "Unknown tymed"); return NULL; } } return PyObject_GenericGetAttr(self, obname); }
int PyDISPLAY_DEVICE::setattro(PyObject *self, PyObject *obname, PyObject *obvalue) { char *name=PYWIN_ATTR_CONVERT(obname); if (name==NULL) return -1; TCHAR *value=NULL; DWORD valuelen; if (strcmp(name,"DeviceName")==0){ PDISPLAY_DEVICE pdisplay_device=&((PyDISPLAY_DEVICE *)self)->display_device; DWORD cch_max=sizeof(pdisplay_device->DeviceName)/sizeof(TCHAR); if (!PyWinObject_AsTCHAR(obvalue, &value, FALSE, &valuelen)) return -1; if (valuelen > cch_max){ PyErr_Format(PyExc_ValueError,"DeviceName must be a string of length %d or less", cch_max); PyWinObject_FreeTCHAR(value); return -1; } ZeroMemory(&pdisplay_device->DeviceName, sizeof(pdisplay_device->DeviceName)); memcpy(&pdisplay_device->DeviceName, value, valuelen * sizeof(TCHAR)); PyWinObject_FreeTCHAR(value); return 0; } if (strcmp(name,"DeviceString")==0){ PDISPLAY_DEVICE pdisplay_device=&((PyDISPLAY_DEVICE *)self)->display_device; DWORD cch_max=sizeof(pdisplay_device->DeviceString)/sizeof(TCHAR); if (!PyWinObject_AsTCHAR(obvalue, &value, FALSE, &valuelen)) return -1; if (valuelen > cch_max){ PyErr_Format(PyExc_ValueError,"DeviceString must be a string of length %d or less", cch_max); PyWinObject_FreeTCHAR(value); return -1; } ZeroMemory(&pdisplay_device->DeviceString, sizeof(pdisplay_device->DeviceString)); memcpy(&pdisplay_device->DeviceString, value, valuelen * sizeof(TCHAR)); PyWinObject_FreeTCHAR(value); return 0; } if (strcmp(name,"DeviceID")==0){ PDISPLAY_DEVICE pdisplay_device=&((PyDISPLAY_DEVICE *)self)->display_device; DWORD cch_max=sizeof(pdisplay_device->DeviceID)/sizeof(TCHAR); if (!PyWinObject_AsTCHAR(obvalue, &value, FALSE, &valuelen)) return -1; if (valuelen > cch_max){ PyErr_Format(PyExc_ValueError,"DeviceID must be a string of length %d or less", cch_max); PyWinObject_FreeTCHAR(value); return -1; } ZeroMemory(&pdisplay_device->DeviceID, sizeof(pdisplay_device->DeviceID)); memcpy(&pdisplay_device->DeviceID, value, valuelen * sizeof(TCHAR)); PyWinObject_FreeTCHAR(value); return 0; } if (strcmp(name,"DeviceKey")==0){ PDISPLAY_DEVICE pdisplay_device=&((PyDISPLAY_DEVICE *)self)->display_device; DWORD cch_max=sizeof(pdisplay_device->DeviceKey)/sizeof(TCHAR); if (!PyWinObject_AsTCHAR(obvalue, &value, FALSE, &valuelen)) return -1; if (valuelen > cch_max){ PyErr_Format(PyExc_ValueError,"DeviceKey must be a string of length %d or less", cch_max); PyWinObject_FreeTCHAR(value); return -1; } ZeroMemory(&pdisplay_device->DeviceKey, sizeof(pdisplay_device->DeviceKey)); memcpy(&pdisplay_device->DeviceKey, value, valuelen * sizeof(TCHAR)); PyWinObject_FreeTCHAR(value); return 0; } return PyObject_GenericSetAttr(self, obname, obvalue); }
PyObject *PyRecord::getattro(PyObject *self, PyObject *obname) { PyObject *res; PyRecord *pyrec = (PyRecord *)self; char *name=PYWIN_ATTR_CONVERT(obname); if (name==NULL) return NULL; if (strcmp(name, "__members__")==0) { ULONG cnames = 0; HRESULT hr = pyrec->pri->GetFieldNames(&cnames, NULL); if (FAILED(hr)) return PyCom_BuildPyException(hr, pyrec->pri, IID_IRecordInfo); BSTR *strs = (BSTR *)malloc(sizeof(BSTR) * cnames); if (strs==NULL) return PyErr_NoMemory(); hr = pyrec->pri->GetFieldNames(&cnames, strs); if (FAILED(hr)) { free(strs); return PyCom_BuildPyException(hr, pyrec->pri, IID_IRecordInfo); } res = PyList_New(cnames); for (ULONG i=0;i<cnames && res != NULL;i++) { PyObject *item = PyWinCoreString_FromString(strs[i]); SysFreeString(strs[i]); if (item==NULL) { Py_DECREF(res); res = NULL; } else PyList_SET_ITEM(res, i, item); // ref count swallowed. } free(strs); return res; } res = PyObject_GenericGetAttr(self, obname); if (res != NULL) return res; PyErr_Clear(); WCHAR *wname; if (!PyWinObject_AsWCHAR(obname, &wname)) return NULL; VARIANT vret; VariantInit(&vret); void *sub_data = NULL; PY_INTERFACE_PRECALL; HRESULT hr = pyrec->pri->GetFieldNoCopy(pyrec->pdata, wname, &vret, &sub_data); PyWinObject_FreeWCHAR(wname); PY_INTERFACE_POSTCALL; if (FAILED(hr)) { if (hr == TYPE_E_FIELDNOTFOUND){ // This is slightly suspect - throwing a unicode // object for an AttributeError in py2k - but this // is the value we asked COM for, so it makes sense... // (and PyErr_Format doesn't handle unicode in py2x) PyErr_SetObject(PyExc_AttributeError, obname); return NULL; } return PyCom_BuildPyException(hr, pyrec->pri, IID_IRecordInfo); } // Short-circuit sub-structs and arrays here, so we dont allocate a new chunk // of memory and copy it - we need sub-structs to persist. if (V_VT(&vret)==(VT_BYREF | VT_RECORD)) return new PyRecord(V_RECORDINFO(&vret), V_RECORD(&vret), pyrec->owner); else if (V_VT(&vret)==(VT_BYREF | VT_ARRAY | VT_RECORD)) { SAFEARRAY *psa = *V_ARRAYREF(&vret); int d = SafeArrayGetDim(psa); if (sub_data==NULL) return PyErr_Format(PyExc_RuntimeError, "Did not get a buffer for the array!"); if (SafeArrayGetDim(psa) != 1) return PyErr_Format(PyExc_TypeError, "Only support single dimensional arrays of records"); IRecordInfo *sub = NULL; long ubound, lbound, nelems; int i; BYTE *this_data; PyObject *ret_tuple = NULL; ULONG element_size = 0; hr = SafeArrayGetUBound(psa, 1, &ubound); if (FAILED(hr)) goto array_end; hr = SafeArrayGetLBound(psa, 1, &lbound); if (FAILED(hr)) goto array_end; hr = SafeArrayGetRecordInfo(psa, &sub); if (FAILED(hr)) goto array_end; hr = sub->GetSize(&element_size); if (FAILED(hr)) goto array_end; nelems = ubound-lbound; ret_tuple = PyTuple_New(nelems); if (ret_tuple==NULL) goto array_end; this_data = (BYTE *)sub_data; for (i=0;i<nelems;i++) { PyTuple_SET_ITEM(ret_tuple, i, new PyRecord(sub, this_data, pyrec->owner)); this_data += element_size; } array_end: if (sub) sub->Release(); if (FAILED(hr)) return PyCom_BuildPyException(hr, pyrec->pri, IID_IRecordInfo); return ret_tuple; } // This default conversion we use is a little slow (but it will do!) // For arrays, the pparray->pvData member is *not* set, since the actual data // pointer from the record is returned in sub_data, so set it here. if (V_ISARRAY(&vret) && V_ISBYREF(&vret)) (*V_ARRAYREF(&vret))->pvData = sub_data; PyObject *ret = PyCom_PyObjectFromVariant(&vret); // VariantClear(&vret); return ret; }