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