Use mbcs encoding when passing filenames to windows

This commit is contained in:
Kovid Goyal 2019-08-07 20:16:23 +05:30
parent 470e6b56fe
commit 5e7c625685
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 29 additions and 18 deletions

View File

@ -163,6 +163,7 @@ def test_save_to(src, dest):
def test_podofo():
import tempfile
from io import BytesIO
from calibre.ebooks.metadata.book.base import Metadata
from calibre.ebooks.metadata.xmp import metadata_to_xmp_packet
@ -178,14 +179,19 @@ def test_podofo():
buf = BytesIO()
p.save_to_fileobj(buf)
raw = buf.getvalue()
p = podofo.PDFDoc()
p.load(raw)
if (p.title, p.author) != (mi.title, mi.authors[0]):
raise ValueError('podofo failed to set title and author in Info dict %s != %s' % (
(p.title, p.author), (mi.title, mi.authors[0])))
if not p.get_xmp_metadata():
raise ValueError('podofo failed to write XMP packet')
del p
with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as f:
f.write(raw)
try:
p = podofo.PDFDoc()
p.open(f.name)
if (p.title, p.author) != (mi.title, mi.authors[0]):
raise ValueError('podofo failed to set title and author in Info dict %s != %s' % (
(p.title, p.author), (mi.title, mi.authors[0])))
if not p.get_xmp_metadata():
raise ValueError('podofo failed to write XMP packet')
del p
finally:
os.remove(f.name)
if __name__ == '__main__':

View File

@ -62,16 +62,21 @@ PDFDoc_load(PDFDoc *self, PyObject *args) {
static PyObject *
PDFDoc_open(PDFDoc *self, PyObject *args) {
char *fname;
if (PyArg_ParseTuple(args, "s", &fname)) {
try {
self->doc->Load(fname);
} catch(const PdfError & err) {
podofo_set_exception(err);
return NULL;
}
} else return NULL;
#ifdef _WIN32
#define ENCODING "mbcs"
#else
#define ENCODING "utf-8"
#endif
if (!PyArg_ParseTuple(args, "es", ENCODING, &fname)) return NULL;
#undef ENCODING
try {
self->doc->Load(fname);
} catch(const PdfError & err) {
podofo_set_exception(err);
PyMem_Free(fname);
return NULL;
}
PyMem_Free(fname);
Py_RETURN_NONE;
}