Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d3283cc

Browse files
committed
Merged revisions 5024-5025 via svnmerge from
https://matplotlib.svn.sourceforge.net/svnroot/matplotlib/branches/v0_91_maint ........ r5025 | mdboom | 2008-03-27 10:26:19 -0400 (Thu, 27 Mar 2008) | 4 lines Fix saving to Unicode filenames in Agg backend. Fix Qt and Qt4 GUI's to support saving to Unicode filenames in file save dialogs. Wx, Gtk and Tk GUIs already appear to work. ........ svn path=/trunk/matplotlib/; revision=5026
1 parent 91f3ab0 commit d3283cc

5 files changed

Lines changed: 47 additions & 14 deletions

File tree

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2008-03-27 Fix saving to Unicode filenames with Agg backend
2+
(other backends appear to already work...)
3+
(Thanks, Christopher Barker) - MGD
4+
15
2008-03-26 Fix SVG backend bug that prevents copying and pasting in
26
Inkscape (thanks Kaushik Ghose) - MGD
37

lib/matplotlib/backends/backend_agg.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,19 +286,23 @@ def buffer_rgba(self,x,y):
286286
def get_default_filetype(self):
287287
return 'png'
288288

289-
def print_raw(self, filename, *args, **kwargs):
289+
def print_raw(self, filename_or_obj, *args, **kwargs):
290290
FigureCanvasAgg.draw(self)
291+
renderer = self.get_renderer()
291292
original_dpi = renderer.dpi
292293
renderer.dpi = self.figure.dpi
293-
renderer._renderer.write_rgba(str(filename))
294+
if type(filename_or_obj) in (str, unicode):
295+
filename_or_obj = open(filename_or_obj, 'w')
296+
renderer._renderer.write_rgba(filename_or_obj)
294297
renderer.dpi = original_dpi
295298
print_rgba = print_raw
296299

297-
def print_png(self, filename, *args, **kwargs):
300+
def print_png(self, filename_or_obj, *args, **kwargs):
298301
FigureCanvasAgg.draw(self)
299302
renderer = self.get_renderer()
300303
original_dpi = renderer.dpi
301304
renderer.dpi = self.figure.dpi
302-
filename = str(filename) # until we figure out unicode handling
303-
renderer._renderer.write_png(filename, self.figure.dpi)
305+
if type(filename_or_obj) in (str, unicode):
306+
filename_or_obj = open(filename_or_obj, 'w')
307+
self.get_renderer()._renderer.write_png(filename_or_obj, self.figure.dpi)
304308
renderer.dpi = original_dpi

lib/matplotlib/backends/backend_qt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ def save_figure( self ):
427427
selectedFilter)
428428
if fname:
429429
try:
430-
self.canvas.print_figure( fname.latin1() )
430+
self.canvas.print_figure( unicode(fname) )
431431
except Exception, e:
432432
qt.QMessageBox.critical(
433433
self, "Error saving file", str(e),

lib/matplotlib/backends/backend_qt4.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ def save_figure( self ):
376376
self, "Choose a filename to save to", start, filters, selectedFilter)
377377
if fname:
378378
try:
379-
self.canvas.print_figure( str(fname.toLatin1()) )
379+
self.canvas.print_figure( unicode(fname) )
380380
except Exception, e:
381381
QtGui.QMessageBox.critical(
382382
self, "Error saving file", str(e),

src/_backend_agg.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,12 +1286,33 @@ RendererAgg::write_rgba(const Py::Tuple& args) {
12861286
_VERBOSE("RendererAgg::write_rgba");
12871287

12881288
args.verify_length(1);
1289-
std::string fname = Py::String(args[0]);
12901289

1291-
std::ofstream of2( fname.c_str(), std::ios::binary|std::ios::out);
1292-
for (size_t i=0; i<NUMBYTES; i++) {
1293-
of2.write((char*)&(pixBuffer[i]), sizeof(char));
1290+
FILE *fp = NULL;
1291+
bool close_file = false;
1292+
Py::Object py_fileobj = Py::Object(args[0]);
1293+
if (py_fileobj.isString()) {
1294+
std::string fileName = Py::String(py_fileobj);
1295+
const char *file_name = fileName.c_str();
1296+
if ((fp = fopen(file_name, "wb")) == NULL)
1297+
throw Py::RuntimeError( Printf("Could not open file %s", file_name).str() );
1298+
fwrite(pixBuffer, 1, NUMBYTES, fp);
1299+
close_file = true;
1300+
fclose(fp);
1301+
} else if (PyFile_CheckExact(py_fileobj.ptr())) {
1302+
fp = PyFile_AsFile(py_fileobj.ptr());
1303+
fwrite(pixBuffer, 1, NUMBYTES, fp);
1304+
} else {
1305+
PyObject* write_method = PyObject_GetAttrString(py_fileobj.ptr(), "write");
1306+
if (!(write_method && PyCallable_Check(write_method))) {
1307+
Py_XDECREF(write_method);
1308+
throw Py::TypeError("Object does not appear to be a 8-bit string path or a Python file-like object");
1309+
}
1310+
1311+
PyObject_CallFunction(write_method, "s#", pixBuffer, NUMBYTES);
1312+
1313+
Py_XDECREF(write_method);
12941314
}
1315+
12951316
return Py::Object();
12961317
}
12971318

@@ -1326,18 +1347,22 @@ RendererAgg::write_png(const Py::Tuple& args)
13261347
args.verify_length(1, 2);
13271348

13281349
FILE *fp = NULL;
1350+
bool close_file = false;
13291351
Py::Object py_fileobj = Py::Object(args[0]);
13301352
if (py_fileobj.isString()) {
13311353
std::string fileName = Py::String(py_fileobj);
13321354
const char *file_name = fileName.c_str();
13331355
if ((fp = fopen(file_name, "wb")) == NULL)
13341356
throw Py::RuntimeError( Printf("Could not open file %s", file_name).str() );
1357+
close_file = true;
1358+
} else if (PyFile_CheckExact(py_fileobj.ptr())) {
1359+
fp = PyFile_AsFile(py_fileobj.ptr());
13351360
}
13361361
else {
13371362
PyObject* write_method = PyObject_GetAttrString(py_fileobj.ptr(), "write");
13381363
if (!(write_method && PyCallable_Check(write_method))) {
13391364
Py_XDECREF(write_method);
1340-
throw Py::TypeError("Object does not appear to be a path or a Python file-like object");
1365+
throw Py::TypeError("Object does not appear to be a 8-bit string path or a Python file-like object");
13411366
}
13421367
Py_XDECREF(write_method);
13431368
}
@@ -1406,15 +1431,15 @@ RendererAgg::write_png(const Py::Tuple& args)
14061431
*/
14071432

14081433
} catch (...) {
1409-
if (fp) fclose(fp);
1434+
if (fp && close_file) fclose(fp);
14101435
delete [] row_pointers;
14111436
if (png_ptr && info_ptr) png_destroy_write_struct(&png_ptr, &info_ptr);
14121437
throw;
14131438
}
14141439

14151440
png_destroy_write_struct(&png_ptr, &info_ptr);
14161441
delete [] row_pointers;
1417-
if (fp) fclose(fp);
1442+
if (fp && close_file) fclose(fp);
14181443

14191444
return Py::Object();
14201445
}

0 commit comments

Comments
 (0)