Bugs item #1472251, was opened at 2006-04-18 05:16
Message generated for change (Comment added) made by isandler
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1472251&group_id=5470
Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.5
Status: Open
Resolution: None
Priority: 3
Submitted By: Kuba KoÅczyk (jakamkon)
Assigned to: Nobody/Anonymous (nobody)
Summary: pdb 'run' crashes when the it's first argument is non-string
Initial Comment:
Pdb 'run/runeval' commands fails to check the type of
given argument.When argument to 'run/runeval' is
non-string the functions crashes with further
impilications on (correctly)invoking this functions:
Python 2.5a1 (trunk:45527, Apr 18 2006, 11:12:31)
>>> def x(): pass
>>> import pdb
>>> pdb.run(x())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/jkk/python/python-svn/Lib/pdb.py", line
1113, in run
Pdb().run(statement, globals, locals)
File "/home/jkk/python/python-svn/Lib/bdb.py", line
363, in run
cmd = cmd+'\n'
TypeError: unsupported operand type(s) for +:
'NoneType' and 'str'
>>> pdb.run('x()')
> /home/jkk/python/python-svn/Lib/pdb.py(1113)run()
-> Pdb().run(statement, globals, locals)
(Pdb)
# CTRL-D pressed
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/jkk/python/python-svn/Lib/pdb.py", line
1113, in run
Pdb().run(statement, globals, locals)
File "/home/jkk/python/python-svn/Lib/pdb.py", line
1113, in run
Pdb().run(statement, globals, locals)
File "/home/jkk/python/python-svn/Lib/bdb.py", line
48, in trace_dispatch
return self.dispatch_line(frame)
File "/home/jkk/python/python-svn/Lib/bdb.py", line
67, in dispatch_line
if self.quitting: raise BdbQuit
bdb.BdbQuit
The solution is to simply ensure that the first
argument passed to the 'run/runeval' functions is string.
----------------------------------------------------------------------
Comment By: Ilya Sandler (isandler)
Date: 2006-07-22 10:46
Message:
Logged In: YES
user_id=971153
>> Is your comment directly related to this bug?
Yes, my suggestion was to call settrace() after
cmd = cmd+'\n'
which should solve the original (bdb leaving tracing on)
problem.. Your latest suggestion would do the same thing by
different means..
However, I think there an even better way: can we simply remove
- if not isinstance(cmd, types.CodeType):
- cmd = cmd+'\n'
from bdb.py???
This would both solve the original problem and make error
message more meaningful IMO..
> exec: arg 1 must be a string, file, or code object
Rather than:
>Unsupported operand type(s) for +: 'NoneType' and 'str'
This seems to work for me, would you be interested in
testing and submitting a patch for this?
A bit more information: it appears that older versions of
python in some cases required string passed to exec to end
with '\n', this was even mentioned in python FAQ.. E.g if I
feed
exec 'for i in range(4):\nx=i'
into python2.1, I get a Syntax error..
However, this does work in python2.3.3, 2.4,2.5...
So was this + '\n' indeed a workaround for a deficiency in exec?
apparently older versions of python required the string
passed to exec to end with '\n'. It was even mentioned in
older FAQs... However, latest versions of python seem to
accept strings without new line... Eg. even
----------------------------------------------------------------------
Comment By: Kuba KoÅczyk (jakamkon)
Date: 2006-07-11 08:02
Message:
Logged In: YES
user_id=1491175
Simple solution is to catch TypeError in bdb's run function:
try:
if not isinstance(cmd, types.CodeType):
cmd = cmd+'\n'
except TypeError:
pass
Now it seems that handling nonstring arguments is better:
>>> import pdb
>>> def x():pass
...
>>> pdb.run(x())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/jkk/python-svn/Lib/pdb.py", line 1122, in run
Pdb().run(statement, globals, locals)
File "/home/jkk/python-svn/Lib/bdb.py", line 369, in run
exec cmd in globals, locals
TypeError: exec: arg 1 must be a string, file, or code object
>>> pdb.run('x()')
> <string>(1)<module>()
(Pdb) x
<function x at 0x4024a9cc>
(Pdb) pdb.run('x()')
(Pdb)
>>>
----------------------------------------------------------------------
Comment By: Kuba KoÅczyk (jakamkon)
Date: 2006-07-11 07:02
Message:
Logged In: YES
user_id=1491175
Is your comment directly related to this bug?
Maybe you should create separete bug tracker item to discuss
problem presented in your comment?
----------------------------------------------------------------------
Comment By: Ilya Sandler (isandler)
Date: 2006-06-25 11:54
Message:
Logged In: YES
user_id=971153
I have looked at what's happening in a bit more detail and
now I agree that the current behaviour seems wrong.
Apparently, when one passes a nonstring to bdb's run(), bdb
sets tracing first and only then throws an exception...
Leaving tracing on seems like a bad thing to do
Would calling settrace() a couple lines later be the
simplest solution then?
so relevant fragment of bdb.py's run() becomes:
if not isinstance(cmd, types.CodeType):
cmd = cmd+'\n'
sys.settrace(self.trace_dispatch)
This should prevent bdb from turning tracing too early.
What do you think?
----------------------------------------------------------------------
Comment By: Kuba KoÅczyk (jakamkon)
Date: 2006-06-19 06:48
Message:
Logged In: YES
user_id=1491175
You're right,but don't you think that this kind of switching
between namespaces could confuse users?
----------------------------------------------------------------------
Comment By: Ilya Sandler (isandler)
Date: 2006-05-22 20:36
Message:
Logged In: YES
user_id=971153
Well, I don't see anything in bdb's run which could
overwrite your namespace..
What seems to be happenning in your example is that bdb's
first line event happens while bdb is still in run(), so you
when you see pdb's prompt, are in bdb's namespace not in
yours.. If you do "next" you will get where you should be..
bagira:~> python2.4
Python 2.4.1 (#2, May 5 2005, 11:32:06)
>>> def x(): print "in x"
>>> import pdb
>>> pdb.run( x())
in x
TypeError: unsupported operand type(s) for +: 'NoneType'
and 'str'
>>> pdb.run( 'x()' )
> /usr/lib/python2.4/pdb.py(987)run()
-> Pdb().run(statement, globals, locals)
(Pdb) n #now you are back in your namespace
> <string>(1)?()
(Pdb) p x #and you get your symbols back
<function x at 0x4021be2c>
(Pdb) p pdb
<module 'pdb' from '/usr/lib/python2.4/pdb.pyc'>
(Pdb) n
in x
--Return--
> <string>(1)?()->None
What do you think?
----------------------------------------------------------------------
Comment By: Kuba KoÅczyk (jakamkon)
Date: 2006-05-22 02:20
Message:
Logged In: YES
user_id=1491175
The point is that when you want to invoke pdb.run correctly
(with string argument) after getting TypeError as in above
example,
your namespace is probably overwritten or deleted so that
you don't have access to previously defined symbols.
>>> def x():pass
>>> import pdb
>>> pdb.run(x())
TypeError
>>> pdb.run('x()')
> /home/jkk/python-svn/Lib/pdb.py(1122)run()
-> Pdb().run(statement, globals, locals)
(Pdb) pdb
*** NameError: name 'pdb' is not defined
(Pdb) x
*** NameError: name 'x' is not defined
----------------------------------------------------------------------
Comment By: Ilya Sandler (isandler)
Date: 2006-05-21 16:25
Message:
Logged In: YES
user_id=971153
I would not classify your example as a crash. You passed a
wrong value (None in your case) into pdb.run() and got back
a TypeError...Isn't it an expected response?
E.g if you do: >>>max(13)
You'll also get: "TypeError"
Am I missing something? Could you clarify?
----------------------------------------------------------------------
Comment By: Kuba KoÅczyk (jakamkon)
Date: 2006-04-18 05:36
Message:
Logged In: YES
user_id=1491175
Patch is in #1472257
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1472251&group_id=5470
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com