Given a Python object, make the best (ie, most appropriate) VARIANT. Should be used when the specific type of the variant is not known NOTE that passing by reference is not supported using this function you need to use the complicated ArgHelpers class for that!
| 110 | // NOTE that passing by reference is not supported using this function |
| 111 | // you need to use the complicated ArgHelpers class for that! |
| 112 | BOOL PyCom_VariantFromPyObject(PyObject *obj, VARIANT *var) |
| 113 | { |
| 114 | // First see if a special Python VARIANT object. |
| 115 | BOOL didPyVariant; |
| 116 | if (!ConvertPyVariant(obj, var, &didPyVariant)) |
| 117 | return FALSE; |
| 118 | if (didPyVariant) |
| 119 | return TRUE; |
| 120 | BOOL bGoodEmpty = FALSE; // Set if VT_EMPTY should really be used. |
| 121 | V_VT(var) = VT_EMPTY; |
| 122 | if ( |
| 123 | // In py3k we don't convert PyString_Check objects (ie, bytes) to BSTR... |
| 124 | #if (PY_VERSION_HEX < 0x03000000) |
| 125 | PyString_Check(obj) || |
| 126 | #endif |
| 127 | PyUnicode_Check(obj) ) |
| 128 | { |
| 129 | if ( !PyWinObject_AsBstr(obj, &V_BSTR(var)) ) { |
| 130 | PyErr_SetString(PyExc_MemoryError, "Making BSTR for variant"); |
| 131 | return FALSE; |
| 132 | } |
| 133 | V_VT(var) = VT_BSTR; |
| 134 | } |
| 135 | // For 3.x, bool checks need to be above PyLong_Check, which now succeeds for booleans. |
| 136 | else if (obj == Py_True) |
| 137 | { |
| 138 | V_VT(var) = VT_BOOL; |
| 139 | V_BOOL(var) = VARIANT_TRUE; |
| 140 | } |
| 141 | else if (obj == Py_False) |
| 142 | { |
| 143 | V_VT(var) = VT_BOOL; |
| 144 | V_BOOL(var) = VARIANT_FALSE; |
| 145 | } |
| 146 | else if (PyLong_Check(obj)) |
| 147 | { |
| 148 | int sign = _PyLong_Sign(obj); |
| 149 | size_t nbits = _PyLong_NumBits(obj); |
| 150 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 151 | return FALSE; |
| 152 | if (64 < nbits) { |
| 153 | // too big for 64 bits! Use a double. |
| 154 | V_VT(var) = VT_R8; |
| 155 | V_R8(var) = PyLong_AsDouble(obj); |
| 156 | } |
| 157 | else if (32 < nbits) { |
| 158 | // between 32 and 64 use longlong |
| 159 | // signed and using all bits use unsigned |
| 160 | if (sign > 0 && 64 == nbits) { |
| 161 | V_VT(var) = VT_UI8; |
| 162 | V_UI8(var) = PyLong_AsUnsignedLongLong(obj); |
| 163 | } else { |
| 164 | // Negative so use signed |
| 165 | V_VT(var) = VT_I8; |
| 166 | V_I8(var) = PyLong_AsLongLong(obj); |
| 167 | // Problem if value is between LLONG_MIN and -ULLONG_MAX |
| 168 | if (PyErr_Occurred()) { |
| 169 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
no test coverage detected