This is kinda akin to creating your own libc.so in a folder where your compiling and executing c programs and then wondering why your program bugs out....<br><br><div class="gmail_quote">On Mon, Feb 1, 2010 at 9:34 PM, kj <span dir="ltr"><no.email@please.post></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><br>
<br>
I just spent about 1-1/2 hours tracking down a bug.<br>
<br>
An innocuous little script, let's call it buggy.py, only 10 lines<br>
long, and whose output should have been, at most two lines, was<br>
quickly dumping tens of megabytes of non-printable characters to<br>
my screen (aka gobbledygook), and in the process was messing up my<br>
terminal *royally*. Here's buggy.py:<br>
<br>
<br>
<br>
import sys<br>
import psycopg2<br>
connection_params = "dbname='%s' user='%s' password='%s'" % tuple(sys.argv[1:])<br>
conn = psycopg2.connect(connection_params)<br>
cur = conn.cursor()<br>
cur.execute('SELECT * FROM version;')<br>
print '\n'.join(x[-1] for x in cur.fetchall())<br>
<br>
<br>
(Of course, buggy.py is pretty useless; I reduced the original,<br>
more useful, script to this to help me debug it.)<br>
<br>
Through a *lot* of trial an error I finally discovered that the<br>
root cause of the problem was the fact that, in the same directory<br>
as buggy.py, there is *another* innocuous little script, totally<br>
unrelated, whose name happens to be numbers.py. (This second script<br>
is one I wrote as part of a little Python tutorial I put together<br>
months ago, and is not much more of a script than hello_world.py;<br>
it's baby-steps for the absolute beginner. But apparently, it has<br>
a killer name! I had completely forgotten about it.)<br>
<br>
Both scripts live in a directory filled with *hundreds* little<br>
one-off scripts like the two of them. I'll call this directory<br>
myscripts in what follows.<br>
<br>
It turns out that buggy.py imports psycopg2, as you can see, and<br>
apparently psycopg2 (or something imported by psycopg2) tries to<br>
import some standard Python module called numbers; instead it ends<br>
up importing the innocent myscript/numbers.py, resulting in *absolute<br>
mayhem*.<br>
<br>
(This is no mere Python "wart"; this is a suppurating chancre, and<br>
the fact that it remains unfixed is a neverending source of puzzlement<br>
for me.)<br>
<br>
How can the average Python programmer guard against this sort of<br>
time-devouring bug in the future (while remaining a Python programmer)?<br>
The only solution I can think of is to avoid like the plague the<br>
basenames of all the 200 or so /usr/lib/pythonX.XX/xyz.py{,c} files,<br>
and *pray* that whatever name one chooses for one's script does<br>
not suddenly pop up in the appropriate /usr/lib/pythonX.XX directory<br>
of a future release.<br>
<br>
What else can one do? Let's see, one should put every script in its<br>
own directory, thereby containing the damage.<br>
<br>
Anything else?<br>
<br>
Any suggestion would be appreciated.<br>
<br>
TIA!<br>
<br>
~k<br>
<font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br>