On second thoughts overload & is too magical

This commit is contained in:
Kovid Goyal 2021-04-21 11:54:23 +05:30
parent 55f52e0e09
commit 272ca315ea
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 9 additions and 10 deletions

View File

@ -254,7 +254,7 @@ get_device_information(CComPtr<IPortableDevice> &device, CComPtr<IPortableDevice
#define S(what, key) { \
com_wchar_raii temp; \
if (SUCCEEDED(values->GetStringValue(what, &temp))) { \
if (SUCCEEDED(values->GetStringValue(what, temp.unsafe_address()))) { \
pyobject_raii t(PyUnicode_FromWideChar(temp.ptr(), -1)); \
if (t) if (PyDict_SetItemString(ans, key, t.ptr()) != 0) PyErr_Clear(); \
}}
@ -305,9 +305,9 @@ get_device_information(CComPtr<IPortableDevice> &device, CComPtr<IPortableDevice
pyobject_raii storage(get_storage_info(device));
if (!storage) {
pyobject_raii exc_type, exc_value, exc_tb;
PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
PyErr_Fetch(exc_type.unsafe_address(), exc_value.unsafe_address(), exc_tb.unsafe_address());
if (exc_type) {
PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb);
PyErr_NormalizeException(exc_type.unsafe_address(), exc_value.unsafe_address(), exc_tb.unsafe_address());
PyDict_SetItemString(ans, "storage_error", exc_value.ptr());
} else {
pyobject_raii t(PyUnicode_FromString("get_storage_info() failed without an error set"));

View File

@ -35,9 +35,8 @@ class generic_raii {
T ptr() noexcept { return handle; }
T detach() noexcept { T ans = handle; handle = null; return ans; }
void attach(T val) noexcept { release(); handle = val; }
T* address() noexcept { return &handle; }
T* unsafe_address() noexcept { return &handle; }
explicit operator bool() const noexcept { return handle != null; }
T* operator &() noexcept { return &handle; }
};
typedef generic_raii<wchar_t*, PyMem_Free> wchar_raii;
@ -46,26 +45,26 @@ typedef generic_raii<PyObject*, python_object_destructor> pyobject_raii;
static inline int
py_to_wchar(PyObject *obj, wchar_t **output) {
py_to_wchar(PyObject *obj, wchar_raii *output) {
if (!PyUnicode_Check(obj)) {
if (obj == Py_None) { *output = NULL; return 1; }
if (obj == Py_None) { output->release(); return 1; }
PyErr_SetString(PyExc_TypeError, "unicode object expected");
return 0;
}
wchar_t *buf = PyUnicode_AsWideCharString(obj, NULL);
if (!buf) { PyErr_NoMemory(); return 0; }
*output = buf;
output->attach(buf);
return 1;
}
static inline int
py_to_wchar_no_none(PyObject *obj, wchar_t **output) {
py_to_wchar_no_none(PyObject *obj, wchar_raii *output) {
if (!PyUnicode_Check(obj)) {
PyErr_SetString(PyExc_TypeError, "unicode object expected");
return 0;
}
wchar_t *buf = PyUnicode_AsWideCharString(obj, NULL);
if (!buf) { PyErr_NoMemory(); return 0; }
*output = buf;
output->attach(buf);
return 1;
}