Python style: exceptions vs. sys.exit()

R

Ross Ridge

Bruno Desthuilliers said:
Also note that there are quite a couples cases where the library authors
themselves cannot predict which exception types may be raised - as soon
as the library functions expect callback functions, file-like or
dict-like or whatever-like objects etc, it's the caller's responsability
to handle the exceptions that may be raised by what *he* passes to the
library...

Ross Ridge a écrit :
Ug... that's another documentation pet-peeve of mine. Libraries that
say they take file-like (or whatever-like) object, but don't say exactly
how file-like it needs.

Bruno Desthuilliers said:
Seems not to be such a problem in practice...

Not if you don't mind figuring out such things by trial and error or
looking at the source.

Ross Ridge
 
L

Lie

Steven D'Aprano a écrit :



Also note that there are quite a couples cases where the library authors
themselves cannot predict which exception types may be raised - as soon
as the library functions expect callback functions, file-like or
dict-like or whatever-like objects etc, it's the caller's responsability
to handle the exceptions that may be raised by what *he* passes to the
library...

No, the library author can always predict which exception his library
may raise. If a callback function, file-like, dict-like, etc raises an
exception, it is not his libraries' exception anymore and is not his
responsibility. In that case we should refer to the documentation for
the callback/etc/etc instead of the documentation for the library.
 
B

Bruno Desthuilliers

Lawrence D'Oliveiro a écrit :
So what's wrong with using the source as documentation? :)

Don't know... Ok, having higher-level documentation (the big picture,
and quick description of what and how for classes and functions) really
helps. But when it comes to nitty-gritty details, source code is the
best documentation ever, since it's always accurate and up to date.

FWIW, I'm often surprised by people asking questions about some
implementation detail of some open-source library or framework that are
very easily answered just looking at the source code. Reading the source
is 1/ the best way to really know how something is implemented and 2/
usually very instructive.
 
S

Steven D'Aprano

Lawrence D'Oliveiro a écrit :

Don't know... Ok, having higher-level documentation (the big picture,
and quick description of what and how for classes and functions) really
helps. But when it comes to nitty-gritty details, source code is the
best documentation ever, since it's always accurate and up to date.

FWIW, I'm often surprised by people asking questions about some
implementation detail of some open-source library or framework that are
very easily answered just looking at the source code. Reading the source
is 1/ the best way to really know how something is implemented and 2/
usually very instructive.

Reading the source code is good, but it's not a panacea. There are at
least four things wrong with the advice to read the source code:


(1) It's not always available.

(2) Even when the source is available, it is sometimes a legal trap to
read it with respect to patents and copyright. E.g. some Microsoft so-
called "open" licences (but not all) allow you to read the source, but if
you do then everything you program in a related field from that point is
legally contaminated and could be considered a derivative work of
Microsoft's software.

(3) Source code not always understandable without significant effort.
Code can be obfuscated, either to hide the algorithm, as an optimization,
or simply because the coder is cleverer than you are. It might be in a
language you don't understand (e.g. Python built-ins are written in C,
not Python. I have to learn C to find out what exceptions sorted() can
raise?).

That's why accurate documentation should be preferred in the first place:
it is easier to read and understand. It might take you hours of hard
study to learn that function spam(x) raises ValueError if the argument is
invalid, or two seconds to read it in the docs.

Yes, documentation can fall behind the source code, but once the code is
stable and the documentation has caught up and is accurate, there's no
reason to re-invent the wheel by slugging through the source just to find
out something already documented.

(4) Even when the source code is available, legally unencumbered, in a
language you understand and not too complicated for you to grasp, there's
the combinatorial explosion: you may need to read and understand an
arbitrarily large and complex chain of software merely to know what a
single function can do. E.g. you want to know what exceptions function
spam() can raise:

def spam(x):
a = 1
b = 2
c = ham(x)
return fried_eggs(a, b, c)

Now you need to understand ham() and fried_eggs(), but they aren't
documented either. So you turn to their source code, and each of them
call two functions, each of which call another two functions, each of
which call *another* two functions...

How much source code are you expected to read just to understand the four-
line function spam()?
 
L

Lawrence D'Oliveiro

Steven D'Aprano said:
(1) It's not always available.

But we're talking about Python libraries here, right?
(2) Even when the source is available, it is sometimes a legal trap to
read it with respect to patents and copyright.

That's not how patents work.
 
B

Bruno Desthuilliers

Steven D'Aprano a écrit :
Reading the source code is good, but it's not a panacea.

Not what I implied.
There are at
least four things wrong with the advice to read the source code:

My "advice to read the source code" was not meant as a *replacement* for
documentation, but as a *complement* to it. What I meant is that you
just can't document each and every detail of implementation.
(1) It's not always available.

(2) Even when the source is available, it is sometimes a legal trap to
read it with respect to patents and copyright. E.g. some Microsoft so-
called "open" licences (but not all) allow you to read the source, but if
you do then everything you program in a related field from that point is
legally contaminated and could be considered a derivative work of
Microsoft's software.

I obviously implied that source was freely available and you had the
right to read it. Else it just makes no sense.
(3) Source code not always understandable without significant effort.

That's why reading the source can have a great educational value, isn't
it ?-)
Code can be obfuscated, either to hide the algorithm,

same problem as closed-source software - not concerned by this advice.
as an optimization,
or simply because the coder is cleverer than you are. It might be in a
language you don't understand (e.g. Python built-ins are written in C,
not Python. I have to learn C to find out what exceptions sorted() can
raise?).

Every developer should have at least basic knowledge of C. MHO of course.
That's why accurate documentation should be preferred in the first place.

Indeed. Did I say otherwise ? Now not all code has accurate
documentation, and then you're happy to be able to access and possibly
understand the source code. I'm not talking about an ideal world here.

(snip)
Yes, documentation can fall behind the source code, but once the code is
stable and the documentation has caught up and is accurate, there's no
reason to re-invent the wheel by slugging through the source just to find
out something already documented.

Once again, that's not what I said.
(4) Even when the source code is available, legally unencumbered, in a
language you understand and not too complicated for you to grasp, there's
the combinatorial explosion: you may need to read and understand an
arbitrarily large and complex chain of software merely to know what a
single function can do.

Yes, this happens when hi-level documentation is lacking. At least you
have a chance to gain some insight !-)
E.g. you want to know what exceptions function
spam() can raise:

def spam(x):
a = 1
b = 2
c = ham(x)
return fried_eggs(a, b, c)

Now you need to understand ham() and fried_eggs(), but they aren't
documented either. So you turn to their source code, and each of them
call two functions, each of which call another two functions, each of
which call *another* two functions...

And still you're a lucky guy if there's no callback, conditional import
and / or polymorphic dispatch involved.


Steve, I may not have been clear, but I didn't meant that code shouldn't
be documented. I was :

1/ answering to the question "So what's wrong with using the source as
documentation?", my own personal answer being that it's something I
often do, whether because I want to find out some detail not covered by
the available documentation, whatever this available documention is worth

2/ digressing about the fact that, to my suprise, few developpers seem
to *first* have a look at the source code when either documentation is
lacking or they'd like to know more about some implementation detail.
But FWIW, it seems that few developpers even bother reading the
documentation at all :(
 
G

greg

Lawrence said:
That's not how patents work.

I don't think that's how copyrights work either. As far as
I know, whether something is deemed a derivative work is
judged on the basis of how similar it is to another work,
not whether its author had knowledge of the other work.
As long as you express an idea in an original way, it
shouldn't matter where you got the idea from.
 
S

Steven D'Aprano

I don't think that's how copyrights work either. As far as I know,
whether something is deemed a derivative work is judged on the basis of
how similar it is to another work, not whether its author had knowledge
of the other work. As long as you express an idea in an original way, it
shouldn't matter where you got the idea from.

That is absolutely not the case with patents. It is *supposed* to be the
case with copyrights, but in practice the courts are interpreting
"derivative work" more and more broadly these days.

As for Microsoft Shared Source licences, there are two which are approved
by the FSF, but the others are a whole different story.

You will note that both the GNU project and the Mono project warn against
reading proprietary source code before contributing. Mono even goes so
far as to say that if you have read the source code to .NET, they cannot
accept your contributions:

If you have looked at Microsoft's implementation of .NET or
their shared source code, you will not be able to contribute
to Mono.
In general, be careful when you are implementing free software
and you have access to proprietary code. We need to make sure
that we are not using someone else's copyrighted code
accidentally.

http://www.mono-project.com/Contributing
 
J

J. Cliff Dyer

IANAL, but IIRC it does matter when it comes to establishing
punative damages. If you knowingly and intentionally infringe
a patent, I think you're libel for more damages than if you
accidentally re-invent something. At least that's what I was
told...

s/libel/liable/

When talking about legal matters, it's kind of an important distinction.
 
S

Steven D'Aprano

IANAL, but IIRC it does matter when it comes to establishing punative
damages. If you knowingly and intentionally infringe a patent, I think
you're libel for more damages than if you accidentally re-invent
something. At least that's what I was told...

Yes. Under US Patent Law, if you intentionally infringe a patent you are
liable (not libel, which is like slander) to triple damages.
Unfortunately the meaning of "intentionally" is horribly broad: if you do
a patent search and find a patent which you don't think covers your
invention, but a court later decides that it does, that counts as
intentional infringement. But if you don't do a patent search at all, any
such infringement must be accidental.

Yes, this is incredibly broken. Economists and lawyers call it a
"perverse incentive":

"Finally, the provision for treble damages for willful infringement
actually creates a perverse incentive not to know about patents that
might be infringed. If a search is undertaken and knowledge of potential
infringement is obtained, then the willfulness provision will be
triggered and the infringer may be liable for treble damages. On the
other hand, without a search there can be no knowledge, and no willful
infringement."

http://www.law.washington.edu/lct/swp/Law/patents.html


Under Germany patent law, there is no difference in damages for
intentional, negligent or accidental infringement, hence no punitive
damages and no triple punishment for doing a patent search:

http://www.ficpi.org/library/lisbonForum/2-6_Hufnagel.ppt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,010
Latest member
MerrillEic

Latest Threads

Top