7258: Work around PyCairo path issue by opening the target file first.

PyCairo, even the latest version, insists on using the 'mbcs' codec on path names on Windows.
That destroys pathnames with characters that aren't in the current default code-page.
This commit is contained in:
John Ralls 2014-04-19 15:02:46 -07:00
parent f62a38ef9a
commit 7962403216

View File

@ -94,18 +94,12 @@ class CairoDocgen(libcairodoc.CairoDoc):
# create cairo context and pango layout
filename = self._backend.filename
# Work around a bug in pycairo 1.8.10 and earlier. OSX and
# Win32 AIOs use pycairo 1.10.0, in which the bug is fixed.
if (sys.version_info[0] < 3 and lin()):
filename = filename.encode(glocale.getfilesystemencoding())
# Cairo can't reliably handle unicode filenames on Linux or
# Windows, so open the file for it.
with open(filename, 'wb') as fd:
try:
surface = self.create_cairo_surface(filename, paper_width, paper_height)
except IOError as msg:
errmsg = "%s\n%s" % (_("Could not create %s") % filename, msg)
raise ReportError(errmsg)
except Exception as err:
errmsg = "%s\n%s" % (_("Could not create %s") % filename, err)
raise ReportError(errmsg)
surface = self.create_cairo_surface(fd, paper_width,
paper_height)
surface.set_fallback_resolution(300, 300)
cr = cairo.Context(surface)
fontmap = PangoCairo.font_map_new()
@ -117,7 +111,8 @@ class CairoDocgen(libcairodoc.CairoDoc):
layout = Pango.Layout(pango_context)
PangoCairo.update_context(cr, pango_context)
# paginate the document
self.paginate_document(layout, page_width, page_height, DPI, DPI)
self.paginate_document(layout, page_width, page_height,
DPI, DPI)
body_pages = self._pages
# build the table of contents and alphabetical index
@ -143,8 +138,8 @@ class CairoDocgen(libcairodoc.CairoDoc):
# paginate the table of contents
rebuild_required = False
if toc_page is not None:
toc_pages = self.__generate_toc(layout, page_width, page_height,
toc)
toc_pages = self.__generate_toc(layout, page_width,
page_height, toc)
offset = len(toc_pages) - 1
if offset > 0:
self.__increment_pages(toc, index, toc_page, offset)
@ -154,8 +149,8 @@ class CairoDocgen(libcairodoc.CairoDoc):
# paginate the index
if index_page is not None:
index_pages = self.__generate_index(layout, page_width, page_height,
index)
index_pages = self.__generate_index(layout, page_width,
page_height, index)
offset = len(index_pages) - 1
if offset > 0:
self.__increment_pages(toc, index, index_page, offset)
@ -166,8 +161,8 @@ class CairoDocgen(libcairodoc.CairoDoc):
# rebuild the table of contents and index if required
if rebuild_required:
if toc_page is not None:
toc_pages = self.__generate_toc(layout, page_width, page_height,
toc)
toc_pages = self.__generate_toc(layout, page_width,
page_height, toc)
if index_page is not None:
index_pages = self.__generate_index(layout, page_width,
page_height, index)
@ -192,6 +187,13 @@ class CairoDocgen(libcairodoc.CairoDoc):
# close the surface (file)
surface.finish()
except IOError as msg:
errmsg = "%s\n%s" % (_("Could not create %s") % filename, msg)
raise ReportError(errmsg)
except Exception as err:
errmsg = "%s\n%s" % (_("Could not create %s") % filename, err)
raise ReportError(errmsg)
def __increment_pages(self, toc, index, start_page, offset):
"""
Increment the page numbers in the table of contents and index.