This commit is contained in:
Kovid Goyal 2019-06-11 19:52:25 +05:30
parent 59264be1f0
commit f66a709dfe
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 29 additions and 29 deletions

View File

@ -446,19 +446,19 @@ be a unicode string. Returns unicode strings."
"move_file()\n\nRename the specified file." "move_file()\n\nRename the specified file."
}, },
{"add_to_recent_docs", (PyCFunction)add_to_recent_docs, METH_VARARGS, {"add_to_recent_docs", (PyCFunction)winutil_add_to_recent_docs, METH_VARARGS,
"add_to_recent_docs()\n\nAdd a path to the recent documents list" "add_to_recent_docs()\n\nAdd a path to the recent documents list"
}, },
{"file_association", (PyCFunction)file_association, METH_VARARGS, {"file_association", (PyCFunction)winutil_file_association, METH_VARARGS,
"file_association()\n\nGet the executable associated with the given file extension" "file_association()\n\nGet the executable associated with the given file extension"
}, },
{"friendly_name", (PyCFunction)friendly_name, METH_VARARGS, {"friendly_name", (PyCFunction)winutil_friendly_name, METH_VARARGS,
"friendly_name()\n\nGet the friendly name for the specified prog_id/exe" "friendly_name()\n\nGet the friendly name for the specified prog_id/exe"
}, },
{"notify_associations_changed", (PyCFunction)notify_associations_changed, METH_VARARGS, {"notify_associations_changed", (PyCFunction)winutil_notify_associations_changed, METH_VARARGS,
"notify_associations_changed()\n\nNotify the OS that file associations have changed" "notify_associations_changed()\n\nNotify the OS that file associations have changed"
}, },

View File

@ -14,21 +14,6 @@
#include <atlbase.h> // for CComPtr #include <atlbase.h> // for CComPtr
#include <Python.h> #include <Python.h>
static inline int
py_to_wchar(PyObject *obj, wchar_t **output) {
if (!PyUnicode_Check(obj)) {
if (obj == Py_None) { *output = NULL; return 1; }
PyErr_SetString(PyExc_TypeError, "unicode object expected");
return 0;
}
#if PY_MAJOR_VERSION < 3
*output = PyUnicode_AS_UNICODE(obj);
#else
*output = PyUnicode_AsWideCharString(obj, NULL);
#endif
return 1;
}
class wchar_raii { class wchar_raii {
private: private:
wchar_t **handle; wchar_t **handle;
@ -38,7 +23,7 @@ class wchar_raii {
wchar_raii & operator=( const wchar_raii & ) ; wchar_raii & operator=( const wchar_raii & ) ;
public: public:
wchar_raii(wchar_t **buf) : handle(*buf) {} wchar_raii() : handle(NULL) {}
~wchar_raii() { ~wchar_raii() {
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
@ -48,15 +33,30 @@ class wchar_raii {
} }
wchar_t *ptr() { return *handle; } wchar_t *ptr() { return *handle; }
void set_ptr(wchar_t **val) { handle = val; }
}; };
static inline int
py_to_wchar(PyObject *obj, wchar_raii *output) {
if (!PyUnicode_Check(obj)) {
if (obj == Py_None) { return 1; }
PyErr_SetString(PyExc_TypeError, "unicode object expected");
return 0;
}
#if PY_MAJOR_VERSION < 3
output->set_ptr(&PyUnicode_AS_UNICODE(obj));
#else
output->set_ptr(&PyUnicode_AsWideCharString(obj, NULL));
#endif
return 1;
}
extern "C" { extern "C" {
PyObject * PyObject *
winutil_add_to_recent_docs(PyObject *self, PyObject *args) { winutil_add_to_recent_docs(PyObject *self, PyObject *args) {
wchar_t *path_, *app_id_; wchar_raii path, app_id;
if (!PyArg_ParseTuple(args, "O&O&", py_to_wchar, &path_, py_to_wchar, &app_id_)) return NULL; if (!PyArg_ParseTuple(args, "O&O&", py_to_wchar, &path, py_to_wchar, &app_id)) return NULL;
wchar_raii path(path_), app_id(app_id_);
if (app_id.ptr()) { if (app_id.ptr()) {
CComPtr<IShellItem> item; CComPtr<IShellItem> item;
HRESULT hr = SHCreateItemFromParsingName(path.ptr(), NULL, IID_PPV_ARGS(&item)); HRESULT hr = SHCreateItemFromParsingName(path.ptr(), NULL, IID_PPV_ARGS(&item));
@ -75,10 +75,10 @@ winutil_add_to_recent_docs(PyObject *self, PyObject *args) {
PyObject * PyObject *
winutil_file_association(PyObject *self, PyObject *args) { winutil_file_association(PyObject *self, PyObject *args) {
wchar_t *ext_, buf[2048]; wchar_t buf[2048];
wchar_raii ext;
DWORD sz = sizeof(buf); DWORD sz = sizeof(buf);
if (!PyArg_ParseTuple(args, "O&", py_to_wchar, &ext_)) return NULL; if (!PyArg_ParseTuple(args, "O&", py_to_wchar, &ext)) return NULL;
wchar_raii ext(ext_);
HRESULT hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, ext.ptr(), NULL, buf, &sz); HRESULT hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, ext.ptr(), NULL, buf, &sz);
if (!SUCCEEDED(hr) || sz < 1) Py_RETURN_NONE; if (!SUCCEEDED(hr) || sz < 1) Py_RETURN_NONE;
return Py_BuildValue("u", buf); return Py_BuildValue("u", buf);
@ -86,10 +86,10 @@ winutil_file_association(PyObject *self, PyObject *args) {
PyObject * PyObject *
winutil_friendly_name(PyObject *self, PyObject *args) { winutil_friendly_name(PyObject *self, PyObject *args) {
wchar_t *exe_, *prog_id_, buf[2048], *p; wchar_t buf[2048], *p;
wchar_raii exe, prog_id;
DWORD sz = sizeof(buf); DWORD sz = sizeof(buf);
if (!PyArg_ParseTuple(args, "O&O&", py_to_wchar, &prog_id_, py_to_wchar, &exe_)) return NULL; if (!PyArg_ParseTuple(args, "O&O&", py_to_wchar, &prog_id, py_to_wchar, &exe)) return NULL;
wchar_raii exe(exe_), prog_id(prog_id_);
ASSOCF flags = ASSOCF_REMAPRUNDLL; ASSOCF flags = ASSOCF_REMAPRUNDLL;
if (exe.ptr()) { if (exe.ptr()) {
p = exe.ptr(); p = exe.ptr();