From: Jochen V. <vo...@se...> - 2004-11-24 12:07:05
|
Hello, On Fri, Nov 19, 2004 at 10:33:43AM -0600, John Hunter wrote: > >>>>> "Jochen" =3D=3D Jochen Voss <vo...@se...> writes: > Jochen> Slight problem: it might now be a little bit more > Jochen> difficult to include the name of the file which could not > Jochen> be opened in the error message. The IOError exception > Jochen> will probably only have "permission denied" associated > Jochen> with it. >=20 > Looks OK, at least on linux >=20 >=20 > >>> file('/sbin/ldconfig', 'w') > Traceback (most recent call last): > File "<stdin>", line 1, in ? > IOError: [Errno 13] Permission denied: '/sbin/ldconfig' But sometimes it doesn't give the file name: >>> from matplotlib.matlab import * >>> plot([1,2,3],[2,3,1]) [<matplotlib.lines.Line2D instance at 0x41ef774c>] >>> savefig("/forbidden.png") Traceback (most recent call last): File "<stdin>", line 1, in ? File ".../matlab.py", line 1009, in savefig manager.canvas.print_figure(*args, **kwargs) File ".../backends/backend_gtkagg.py", line 69, in print_figure agg.print_figure(filename, dpi, facecolor, edgecolor, orientation) File ".../backends/backend_agg.py", line 379, in print_figure self.renderer._renderer.write_png(str(filename)) RuntimeError: could not open file I did not investigate what happens here, but if there is an easy way to get the file name into the exception we should probably use it. All the best, Jochen --=20 http://seehuhn.de/ |
From: Steve C. <ste...@ya...> - 2004-11-24 13:23:22
|
On Wed, 2004-11-24 at 12:06 +0000, Jochen Voss wrote: > Hello, > > On Fri, Nov 19, 2004 at 10:33:43AM -0600, John Hunter wrote: > > >>>>> "Jochen" == Jochen Voss <vo...@se...> writes: > > Jochen> Slight problem: it might now be a little bit more > > Jochen> difficult to include the name of the file which could not > > Jochen> be opened in the error message. The IOError exception > > Jochen> will probably only have "permission denied" associated > > Jochen> with it. > > > > Looks OK, at least on linux > > > > > > >>> file('/sbin/ldconfig', 'w') > > Traceback (most recent call last): > > File "<stdin>", line 1, in ? > > IOError: [Errno 13] Permission denied: '/sbin/ldconfig' > > But sometimes it doesn't give the file name: > > >>> from matplotlib.matlab import * > >>> plot([1,2,3],[2,3,1]) > [<matplotlib.lines.Line2D instance at 0x41ef774c>] > >>> savefig("/forbidden.png") > Traceback (most recent call last): > File "<stdin>", line 1, in ? > File ".../matlab.py", line 1009, in savefig > manager.canvas.print_figure(*args, **kwargs) > File ".../backends/backend_gtkagg.py", line 69, in print_figure > agg.print_figure(filename, dpi, facecolor, edgecolor, orientation) > File ".../backends/backend_agg.py", line 379, in print_figure > self.renderer._renderer.write_png(str(filename)) > RuntimeError: could not open file > > I did not investigate what happens here, but if there is an easy way to > get the file name into the exception we should probably use it. > > All the best, > Jochen For an IOError the exception attribute 'filename' is set to the filename. With your example above self.renderer._renderer.write_png(str (filename)) is Agg C++ extension code The line fp = fopen(file_name, "wb"); could be changed to something like if ((fp = fopen(file_name, "wb")) == NULL) throw Py::IOError("could not open file", filename); Does this look right John? Steve |
From: John H. <jdh...@ac...> - 2004-11-24 15:35:03
|
>>>>> "Steve" == Steve Chaplin <ste...@ya...> writes: Steve> For an IOError the exception attribute 'filename' is set to Steve> the filename. With your example above Steve> self.renderer._renderer.write_png(str (filename)) is Agg Steve> C++ extension code The line fp = fopen(file_name, "wb"); Steve> could be changed to something like if ((fp = Steve> fopen(file_name, "wb")) == NULL) throw Py::IOError("could Steve> not open file", filename); Steve> Does this look right John? Right in principle, but not in practice. For one thing, cxx strangely doesn't define an IOError. I don't know if this is simply an oversight. Perhaps I'll file a bug on the sf site.... The larger problem is that the exception constructor doesn't accept multiple args and concatenate them. It would be nice if it did. I added a Printf class to mplutils to ease the burden of making printf style strings in C++ to make better exceptions, and updated the exceptions in the image, agg and ft2font extensions. The standard usage is if ((fp = fopen(file_name, "wb")) == NULL) throw Py::RuntimeError( Printf("Could not open file %s", file_name).str() ); JDH |
From: Steve C. <ste...@ya...> - 2004-11-24 16:51:18
|
On Wed, 2004-11-24 at 09:34 -0600, John Hunter wrote: > For one thing, cxx strangely doesn't define an IOError. I don't know > if this is simply an oversight. Perhaps I'll file a bug on the sf > site.... The Python C API uses PyErr_SetFromErrnoWithFilename(PyObject *type, char *filename) to raise IOError exceptions, perhaps cxx has an equivalent function. Steve |