Commit 50c6072f authored by Hirokazu Yamamoto's avatar Hirokazu Yamamoto

Backport r65745: Issue #2222: Fixed reference leak when occured

os.rename() fails unicode conversion on 2nd parameter. (windows only)
parent f0a41637
...@@ -473,24 +473,18 @@ static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) ...@@ -473,24 +473,18 @@ static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
{ {
} }
/* Function suitable for O& conversion */
static int static int
convert_to_unicode(PyObject *arg, void* _param) convert_to_unicode(PyObject **param)
{ {
PyObject **param = (PyObject**)_param; if (PyUnicode_CheckExact(*param))
if (PyUnicode_CheckExact(arg)) { Py_INCREF(*param);
Py_INCREF(arg); else if (PyUnicode_Check(*param))
*param = arg;
}
else if (PyUnicode_Check(arg)) {
/* For a Unicode subtype that's not a Unicode object, /* For a Unicode subtype that's not a Unicode object,
return a true Unicode object with the same data. */ return a true Unicode object with the same data. */
*param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg), *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
PyUnicode_GET_SIZE(arg)); PyUnicode_GET_SIZE(*param));
return *param != NULL;
}
else else
*param = PyUnicode_FromEncodedObject(arg, *param = PyUnicode_FromEncodedObject(*param,
Py_FileSystemDefaultEncoding, Py_FileSystemDefaultEncoding,
"strict"); "strict");
return (*param) != NULL; return (*param) != NULL;
...@@ -2397,11 +2391,14 @@ posix_rename(PyObject *self, PyObject *args) ...@@ -2397,11 +2391,14 @@ posix_rename(PyObject *self, PyObject *args)
char *p1, *p2; char *p1, *p2;
BOOL result; BOOL result;
if (unicode_file_names()) { if (unicode_file_names()) {
if (!PyArg_ParseTuple(args, "O&O&:rename", if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
convert_to_unicode, &o1, goto error;
convert_to_unicode, &o2)) if (!convert_to_unicode(&o1))
PyErr_Clear(); goto error;
else { if (!convert_to_unicode(&o2)) {
Py_DECREF(o1);
goto error;
}
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
result = MoveFileW(PyUnicode_AsUnicode(o1), result = MoveFileW(PyUnicode_AsUnicode(o1),
PyUnicode_AsUnicode(o2)); PyUnicode_AsUnicode(o2));
...@@ -2412,7 +2409,8 @@ posix_rename(PyObject *self, PyObject *args) ...@@ -2412,7 +2409,8 @@ posix_rename(PyObject *self, PyObject *args)
return win32_error("rename", NULL); return win32_error("rename", NULL);
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} error:
PyErr_Clear();
} }
if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
return NULL; return NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment