Given a variant, turn it into a Python object of the closest type. Note that ByRef params are not supported here.
| 319 | // Given a variant, turn it into a Python object of the closest type. |
| 320 | // Note that ByRef params are not supported here. |
| 321 | PyObject *PyCom_PyObjectFromVariant(const VARIANT *var) |
| 322 | { |
| 323 | HRESULT hr; |
| 324 | VARIANT varValue; |
| 325 | PyObject * result = NULL; |
| 326 | |
| 327 | if (!var) { |
| 328 | Py_INCREF(Py_None); |
| 329 | return Py_None; |
| 330 | } |
| 331 | /* skip past any variant references to a "real" variant |
| 332 | (Why do we do this? Why is it only a VARIANT? whats the story, morning glory? |
| 333 | */ |
| 334 | while ( V_VT(var) == (VT_BYREF | VT_VARIANT) ) |
| 335 | var = V_VARIANTREF(var); |
| 336 | |
| 337 | /* ### note: we shouldn't see this, it is illegal in a VARIANT */ |
| 338 | if (V_ISVECTOR(var)) { |
| 339 | return OleSetTypeError(_T("Cant convert vectors!")); |
| 340 | } |
| 341 | |
| 342 | if (V_ISARRAY(var)) { |
| 343 | SAFEARRAY FAR *psa; |
| 344 | if (V_ISBYREF(var)) |
| 345 | psa = *V_ARRAYREF(var); |
| 346 | else |
| 347 | psa=V_ARRAY(var); |
| 348 | if (psa==NULL) { // A NULL array |
| 349 | Py_INCREF(Py_None); |
| 350 | return Py_None; |
| 351 | } |
| 352 | VARENUM rawVT = (VARENUM)(V_VT(var) & VT_TYPEMASK); |
| 353 | return PyCom_PyObjectFromSAFEARRAY(psa, rawVT); |
| 354 | } |
| 355 | |
| 356 | /* get a fully dereferenced copy of the variant */ |
| 357 | /* ### we may want to optimize this sometime... avoid copying values */ |
| 358 | VariantInit(&varValue); |
| 359 | VariantCopyInd(&varValue, (VARIANT*)var); |
| 360 | |
| 361 | switch (V_VT(&varValue)) |
| 362 | { |
| 363 | case VT_BOOL: |
| 364 | result = V_BOOL(&varValue) ? Py_True : Py_False; |
| 365 | Py_INCREF(result); |
| 366 | break; |
| 367 | |
| 368 | case VT_UI1: |
| 369 | case VT_UI2: |
| 370 | case VT_UI4: |
| 371 | case VT_UINT: |
| 372 | hr = VariantChangeType(&varValue, &varValue, 0, VT_UI4); |
| 373 | if ( FAILED(hr) ) |
| 374 | { |
| 375 | TCHAR buf[200]; |
| 376 | wsprintf(buf, _T("Error converting integer variant (%08lx)"), hr); |
| 377 | OleSetTypeError(buf); |
| 378 | break; |
no test coverage detected