[Python-checkins] r83266 - in python/branches/py3k: Doc/library/pdb.rst Lib/bdb.py Lib/test/test_pdb.py

georg.brandl python-checkins at python.org
Fri Jul 30 11:14:20 CEST 2010


Author: georg.brandl
Date: Fri Jul 30 11:14:20 2010
New Revision: 83266

Log:
#1472251: remove addition of "\n" to code given to pdb.run[eval](), the bug in exec() that made this necessary has been fixed.  Also document that you can give code objects to run() and runeval(), and add some tests to test_pdb.

Modified:
   python/branches/py3k/Doc/library/pdb.rst
   python/branches/py3k/Lib/bdb.py
   python/branches/py3k/Lib/test/test_pdb.py

Modified: python/branches/py3k/Doc/library/pdb.rst
==============================================================================
--- python/branches/py3k/Doc/library/pdb.rst	(original)
+++ python/branches/py3k/Doc/library/pdb.rst	Fri Jul 30 11:14:20 2010
@@ -85,21 +85,21 @@
 
 .. function:: run(statement, globals=None, locals=None)
 
-   Execute the *statement* (given as a string) under debugger control.  The
-   debugger prompt appears before any code is executed; you can set breakpoints
-   and type :pdbcmd:`continue`, or you can step through the statement using
-   :pdbcmd:`step` or :pdbcmd:`next` (all these commands are explained below).
-   The optional *globals* and *locals* arguments specify the environment in
-   which the code is executed; by default the dictionary of the module
-   :mod:`__main__` is used.  (See the explanation of the built-in :func:`exec`
-   or :func:`eval` functions.)
+   Execute the *statement* (given as a string or a code object) under debugger
+   control.  The debugger prompt appears before any code is executed; you can
+   set breakpoints and type :pdbcmd:`continue`, or you can step through the
+   statement using :pdbcmd:`step` or :pdbcmd:`next` (all these commands are
+   explained below).  The optional *globals* and *locals* arguments specify the
+   environment in which the code is executed; by default the dictionary of the
+   module :mod:`__main__` is used.  (See the explanation of the built-in
+   :func:`exec` or :func:`eval` functions.)
 
 
 .. function:: runeval(expression, globals=None, locals=None)
 
-   Evaluate the *expression* (given as a string) under debugger control.  When
-   :func:`runeval` returns, it returns the value of the expression.  Otherwise
-   this function is similar to :func:`run`.
+   Evaluate the *expression* (given as a string or a code object) under debugger
+   control.  When :func:`runeval` returns, it returns the value of the
+   expression.  Otherwise this function is similar to :func:`run`.
 
 
 .. function:: runcall(function, *args, **kwds)

Modified: python/branches/py3k/Lib/bdb.py
==============================================================================
--- python/branches/py3k/Lib/bdb.py	(original)
+++ python/branches/py3k/Lib/bdb.py	Fri Jul 30 11:14:20 2010
@@ -364,8 +364,9 @@
         if line: s = s + lprefix + line.strip()
         return s
 
-    # The following two methods can be called by clients to use
-    # a debugger to debug a statement, given as a string.
+    # The following methods can be called by clients to use
+    # a debugger to debug a statement or an expression.
+    # Both can be given as a string, or a code object.
 
     def run(self, cmd, globals=None, locals=None):
         if globals is None:
@@ -375,8 +376,6 @@
             locals = globals
         self.reset()
         sys.settrace(self.trace_dispatch)
-        if not isinstance(cmd, types.CodeType):
-            cmd = cmd+'\n'
         try:
             exec(cmd, globals, locals)
         except BdbQuit:
@@ -393,8 +392,6 @@
             locals = globals
         self.reset()
         sys.settrace(self.trace_dispatch)
-        if not isinstance(expr, types.CodeType):
-            expr = expr+'\n'
         try:
             return eval(expr, globals, locals)
         except BdbQuit:

Modified: python/branches/py3k/Lib/test/test_pdb.py
==============================================================================
--- python/branches/py3k/Lib/test/test_pdb.py	(original)
+++ python/branches/py3k/Lib/test/test_pdb.py	Fri Jul 30 11:14:20 2010
@@ -1,5 +1,4 @@
-# A test suite for pdb; at the moment, this only validates skipping of
-# specified test modules (RFE #5142).
+# A test suite for pdb; not very comprehensive at the moment.
 
 import imp
 import sys
@@ -123,6 +122,50 @@
     """
 
 
+def pdb_invoke(method, arg):
+    """Run pdb.method(arg)."""
+    import pdb; getattr(pdb, method)(arg)
+
+
+def test_pdb_run_with_incorrect_argument():
+    """Testing run and runeval with incorrect first argument.
+
+    >>> pti = PdbTestInput(['continue',])
+    >>> with pti:
+    ...     pdb_invoke('run', lambda x: x)
+    Traceback (most recent call last):
+    TypeError: exec() arg 1 must be a string, bytes or code object
+
+    >>> with pti:
+    ...     pdb_invoke('runeval', lambda x: x)
+    Traceback (most recent call last):
+    TypeError: eval() arg 1 must be a string, bytes or code object
+    """
+
+
+def test_pdb_run_with_code_object():
+    """Testing run and runeval with code object as a first argument.
+
+    >>> with PdbTestInput(['step','x', 'continue']):
+    ...     pdb_invoke('run', compile('x=1', '<string>', 'exec'))
+    > <string>(1)<module>()
+    (Pdb) step
+    --Return--
+    > <string>(1)<module>()->None
+    (Pdb) x
+    1
+    (Pdb) continue
+
+    >>> with PdbTestInput(['x', 'continue']):
+    ...     x=0
+    ...     pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
+    > <string>(1)<module>()->None
+    (Pdb) x
+    1
+    (Pdb) continue
+    """
+
+
 def test_main():
     from test import test_pdb
     support.run_doctest(test_pdb, verbosity=True)


More information about the Python-checkins mailing list