473,796 Members | 2,621 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Will Python exceptions be documented?

From whet I can see, Python documentation is lacking a very important
piece of information. Very few functions have documented what
exceptions they raise, and under what cicumstances. Is this a task on
the schedule?
In my opinion, every function that might raise an exception should
state so explicitly in the documentation, even uncaught exceptions
from a subfunction.

I have seen many different examples for catching exceptions from
open(). Which is the right one?

The error from function math.log(0) is a by unpredictable, but at
least that is documentet. What about all the others? math.asin(2),
shutil, os, sys?

All know that asin(2) is wrong, but how do I know that ValueError is
the exception I should catch, and how do I know that is the only one?
(Sorry, try it is the wrong answere here.)
My other issue with is concerning the documentation of the exceptions
themselves. How can I find out that I can use 'filename', 'errno' and
'strerror' for the exception IOError without having to use
dir(IOError())? And is it valid in all versions/platforms?

Since I'm critisising the lack of work, I will voulunteer to help out
with the process. But I will need some guidance and pointers in the
beginning.
Python is too good to have a B+ documentation. We can do better.
Cheers,
Vegard
--
Vegard Bakke

My spelling is wobbly. It's good spelling, but it
wobbles, and the letters get it the wrong places.
Winnie the Pooh
:wq
Jul 18 '05 #1
1 2187
ve****@mail.com (Vegard Bakke) writes:
From whet I can see, Python documentation is lacking a very important
piece of information. Very few functions have documented what
exceptions they raise, and under what cicumstances. Is this a task on
the schedule?
IIRC, yes.
[...] I have seen many different examples for catching exceptions from
open(). Which is the right one?
What examples, specifically? The docs say IOError can be raised.
Just about everything can raise ValueError, TypeError,
KeyboardInterru pt and MemoryError, so they aren't typically
documented. I'm not sure exactly when WindowsError (rather than
something more specific) gets raised.

Not answering your question, but as a BTW: in that particular case, if
you're getting uncontrolled input you might sometimes want to
normalise the exceptions raised by catching everything but a few
exceptions. This is useful because you might not have anticipated
every way that weird input might trip up your code (ie. your code may
be buggy). See this recipe and my comment

http://aspn.activestate.com/ASPN/Coo.../Recipe/101853
In fact, I didn't show the try / finally needed to close the file
there, so it should really be:

import traceback
from cStringIO import StringIO

DEBUG = True

def debug(msg):
print msg

def load(filename):
f = open(filename)
try:
try:
# Some code that might raise IOError, or another exception that you
# weren't expecting, if the user is imaginitive enough...
except (AssertionError , KeyboardInterru pt, IOError):
# NOTE WELL the brackets around the exception classes -- an except
# with two arguments means something quite different!
raise
except:
if DEBUG:
f = StringIO()
traceback.print _exc(None, f)
debug("uncaught exception:\n%s" % f.getvalue())
raise IOError, "invalid file '%s'" % filename
finally:
f.close()

load("/some/nonsense/file.txt")

The error from function math.log(0) is a by unpredictable, but at
least that is documentet. What about all the others? math.asin(2),
shutil, os, sys?

All know that asin(2) is wrong, but how do I know that ValueError is
the exception I should catch, and how do I know that is the only one?
(Sorry, try it is the wrong answere here.)
For the math module, the log(0) situation applies. From the 2.3
library docs for the math module:

Note: The math module consists mostly of thin wrappers around the
platform C math library functions. Behavior in exceptional cases is
loosely specified by the C standards, and Python inherits much of
its math-function error-reporting behavior from the platform C
implementation. As a result, the specific exceptions raised in
error cases (and even whether some arguments are considered to be
exceptional at all) are not defined in any useful cross-platform or
cross-release way. For example, whether math.log(0) returns -Inf or
raises ValueError or OverflowError isn't defined, and in cases
where math.log(0) raises OverflowError, math.log(0L) may raise
ValueError instead.

My other issue with is concerning the documentation of the exceptions
themselves. How can I find out that I can use 'filename', 'errno' and
'strerror' for the exception IOError without having to use
dir(IOError())? And is it valid in all versions/platforms?
That's documented under the base class, EnvironmentErro r.

Since I'm critisising the lack of work, I will voulunteer to help out
with the process. But I will need some guidance and pointers in the

[...]

Great. I'd say just submit some specific doc patches with the
knowledge you already have (as long as you make it clear where you're
unsure). Since that shows you're prepared to do some work on it, it's
likely to get you feedback from the people who know all the details
but don't have time to work on it themselves. Of course, the core
people are probably particularly busy with 2.3 ATM, so don't expect a
rapid response.
John
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1262
by: Craig Ringer | last post by:
QOTW: "Such infrastructure building is in fact fun and instructive -- as long as you don't fall into the trap of *using* such complications in production code, where Python's simplicity rules;-)." -- Alex Martelli http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/41a6c0e1e260cd72/ "C++ to Python is a steep 'unlearning' curve..." -- Philip Smith "URK -- _my_ feeling is that we have entirely *too many* options for...
1
1339
by: Milan Gornik | last post by:
Hello guys, I consulted both MSDN and Borland Builder's documentation to try to find something about this, but I haven't found it. I saw that both VC++ and Builder support different kinds of exception handling mechanisms. As I understand, the standard way is to use exception handling through exception classes and try/catch statements. Now, in Java, it is mandatory to supply method prototype with info on which exceptions can occur....
22
2742
by: Drew | last post by:
How do I know which exceptions are thrown by certain methods? For example, reading a file might throw an IO Exception, etc. In Java, the compiler won't even let you compile unless you put your code in try/catch blocks but the C# compiler doesn't seem to mind? I am particularly interested in what Exceptions are thrown by HttpWebResponse.
0
361
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 378 open ( +3) / 3298 closed (+34) / 3676 total (+37) Bugs : 886 open (-24) / 5926 closed (+75) / 6812 total (+51) RFE : 224 open ( +7) / 227 closed ( +7) / 451 total (+14) New / Reopened Patches ______________________
5
2086
by: Rennie deGraaf | last post by:
I know that if an exception is thrown from a destructor while unwinding the stack because of another exception, terminate() is called (FAQ 17.3). How exactly does this rule work? Is it acceptable to both throw /and/ catch an exception inside a destructor, as in the following code? struct Foo { void finalize() { throw 1.0;
7
2230
by: JackPot | last post by:
Actually, I'm interested in learning where the different types of exceptions are documented for a specific type of class, let us say for instance classes in the System.Net.Mail Namespace. Shouldn't the types be documented on the same page as the class, its members and properties?
35
3515
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly, especially as programs get large. Most of the issues of exceptions are not specific to Python, but I sometimes feel that Python makes them more acute because of the free-n- easy manner in which it employs exceptions for its own uses and allows users...
34
5036
by: Drake | last post by:
I have a general question of Python style, or perhaps just good programming practice. My group is developing a medium-sized library of general-purpose Python functions, some of which do I/O. Therefore it is possible for many of the library functions to raise IOError Exceptions. The question is: should the library function be able to just dump to sys.exit() with a message about the error (like "couldn't open this file"), or should the...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10018
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.