python pdb not giving correct trace on exceptions - python

On my iMac, the debugger, running a faulty python script (on MAC OS Sierra), always points to the first active line of code as cause of an exception, while when launching without debugger the correct line is identified. Does anybody have an idea why this may occur and how to fix it?
Here's a simple example with a "File not found" exception:
The script exception_test.py:
1 # Some dummy lines...
2 a=1
3 b=2
4 c=a+b
5 # Lines casuing the exception:
6 with open("filename","r") as fid:
7 lines=fid.readlines()
When run without debugger, as in python exception_test.py it yields
Traceback (most recent call last):
File "exception_test.py", line 6, in <module>
with open("filename","r") as fid:
IOError: [Errno 2] No such file or directory: 'filename'
identifying the correct line, i.e. line6,
while python -m pdb exception_test.py and successive c to continue yields
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pdb.py", line 1314, in main
pdb._runscript(mainpyfile)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pdb.py", line 1233, in _runscript
self.run(statement)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/bdb.py", line 400, in run
exec cmd in globals, locals
File "<string>", line 1, in <module>
File "exception_test.py", line 2, in <module>
a=1
IOError: [Errno 2] No such file or directory: 'filename'
Uncaught exception. Entering post mortem debugging
indicating the first active line of code, i.e. line 2.

It is more likely an issue of cpython and pdb, not your code, pypy could print correct traceback line number.
the line_no of the frame where the exception occurred is not right under -m pdb.
after some guessing and tracing, I could narrow to this path:
sys.settrace(self.trace_dispatch)
-> trace_dispatch() -> dispatch_line() -> user_line() -> interaction()
the real root cause is still not clear.

Related

vscode: evaluating multiple selected lines in the debug console causes an indentation error (with python classes)

I have been struggling for a while with the problem that in VSCode I would like to evaluate multiple lines of Python code in the debug console without having to select each line individually and send it to the debug console. However, when I select multiple lines at once and want to evaluate them in the debug console, the existing indentations are copied to the debug console and cause an indentation error there. I have since been able to narrow down the problem to the point where I have determined that this occurs mainly when debugging class methods. A minimal example is given below.
class MyClass:
def __init__(self):
pass
def run(self):
a = 3
b = 4
c = a + b
MyClass().run()
If I set a breakpoint at line a = 3, which is then reached during debugging, and I then select lines a = 3 up to and including c = a + b and want to have all 3 lines evaluated in the debug console, this results in the lines being copied to the debug console as follows:
a = 3
b = 4
c = a + b
Which causes the following error:
Traceback (most recent call last):
File "/home/lukas/.vscode/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 419, in evaluate_expression
compiled = compile(_expression_to_evaluate(expression), '<string>', 'eval')
File "<string>", line 1
a = 3
^
SyntaxError: invalid syntax
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/lukas/.vscode/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 1207, in internal_evaluate_expression_json
pydevd_vars.evaluate_expression(py_db, frame, expression, is_exec=True)
File "/home/lukas/.vscode/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 371, in new_func
return _run_with_unblock_threads(original_func, py_db, curr_thread, frame, expression, is_exec)
File "/home/lukas/.vscode/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 339, in _run_with_unblock_threads
return _run_with_interrupt_thread(original_func, py_db, curr_thread, frame, expression, is_exec)
File "/home/lukas/.vscode/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 310, in _run_with_interrupt_thread
return original_func(py_db, frame, expression, is_exec)
File "/home/lukas/.vscode/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 421, in evaluate_expression
Exec(_expression_to_evaluate(expression), updated_globals, frame.f_locals)
File "/home/lukas/.vscode/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<string>", line 2
b = 4
^
IndentationError: unexpected indent
This is a very annoying problem. Any solution would be greatly appreciated.
You really have copied the indent from the second line.
You can press Alt + Shift when you copy from a to a+b to avoid this.

subprocess, invoke C-program from within Python

I am trying to invoke a C-program, named “drule.c”, from within my Python-program “drulewrapper.py”. I am trying to use "subprocess" but cannot get it to work.
1) I compile “drule.c” on the Mac’s terminal and all works okay:
$ gcc -o drule drule c
$ ./drule D11
>P>Q>RQ
Fyi, the input -- “D11” -- are axioms in predicate logic; the output -- “>P>Q>RQ” -- is the theorem that is proven and which I then want to process further in my Python program.
2) I write a short Python program (drulewrapper.py) and compile it:
From subprocess import call
def CheckString():
call(“./drule”, “D11”)
3) But when I run CheckString() I get errors:
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
CheckString()
File "/Users/georgeszpiro/Dropbox/metamath/GApl/drulewrapper.py", line 3, in CheckString
call("./drule","D11")
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 267, in call
with Popen(*popenargs, **kwargs) as p:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 609, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integerT
Can anybody help?

xlrd python does not work - "syntax error"

I'm trying to get familiar with xlrd so I copied an example into my IDE (spyder). I'm using python(x,y) 2.7.6.1
This is my example
import xlrd
import os
filename=os.path.join("C:/","Desktop/myfile"):
book = xlrd.open_workbook(filename)
print "The number of worksheets is", book.nsheets
print "Worksheet name(s):", book.sheet_names()
sh = book.sheet_by_index(0)
print sh.name, sh.nrows, sh.ncols
print "Cell D30 is", sh.cell_value(rowx=29, colx=3)
for rx in range(sh.nrows):
print sh.row(rx)
As you can see, I listened to advice on SE here but it still does not work (syntax error). As it is advised here I have written stuff in os.path.join() in manner given in the accepted answer.
This is error log:
runfile('C:/Users/PC/.spyder2/.temp.py', wdir=r'C:/Users/PC/.spyder2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "C:/Users/PC/.spyder2/.temp.py", line 12
filename=os.path.join("C:/","/Users/PC/Desktop/myfile"):
^
SyntaxError: invalid syntax
UPDATE
Now, when I removed colon from the end of the line with "join" i got another syntax error. This is it:
runfile('C:/Users/PC/.spyder2/.temp.py', wdir=r'C:/Users/PC/.spyder2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "C:/Users/PC/.spyder2/.temp.py", line 13, in <module>
book = xlrd.open_workbook(filename)
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 394, in open_workbook
f = open(filename, "rb")
IOError: [Errno 2] No such file or directory: 'C:/Users/PC/Desktop/myfile'
What am I doing wrong? What should I do instead?
It's the colon at the end of the join line. It shouldn't be there.
The syntax error is that there should be no : at the end of the first line.
The "no such file or directory" error is because the desktop is not a directory located at "C:/Desktop". There's actually more than one directory whose contents show on the desktop, but probably what you want is "C:/Users/USERNAME/Desktop/", where USERNAME is of course your username on the machine.
If you want to access the home directory in general (i.e. not just yours, you want the home directory of whoever is running the script) then you can access the HOMEDRIVE and HOMEPATH environment variables.
As suggested by demented hedgehog and Steve Jessop i had to convert following line
filename=os.path.join("C:/","Desktop/myfile"):
into
filename=os.path.join("C:/","/Users/PC/Desktop/myfile.xls")
So I just had to remove colon, write correct directory and write myfile.xls instead of just myfile.

Python - causes of ther error "OSError: [Errno 9] Bad file descriptor"

I get the error:
OSError: [Errno 9] Bad file descriptor
when my program calls up the line
with open(cs, 'r') as f :
list = [line.strip() for line in f]
This refers to it opening the file 'cs' and reading it. The list is then created out of every line in the text file
I cant seem to work out what is going wrong, some guidance will be helpful
Thanks
EDIT:
The traceback doesnt seem to provide much:
Traceback (most recent call last):
File "C:\Users\Sammy\Desktop\project 1\project1.py", line 85, in <module>
main() #run mainline
File "C:\Users\Sammy\Desktop\project 1\project1.py", line 83, in main
printCount(countVotes(candidateNumbers,getPapers(voteTXT,candidateNumbers)))
File "C:\Users\Wen\Desktop\project 1\project1.py", line 53, in countVotes
with open(cs, 'r') as `f :

Python: AssertionError when running nose tests with coverage

I'm fairly green with python testing, so this might be something I'm doing wrong..
When I run my tests, the test runners works fine and coverage too.. but between the two I get an assertion error:
Traceback (most recent call last):
File "/usr/local/bin/coverage", line 9, in <module>
load_entry_point('coverage==3.5.1', 'console_scripts', 'coverage')()
File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 657, in main
status = CoverageScript().command_line(argv)
File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 526, in command_line
self.coverage.stop()
File "/usr/local/lib/python2.7/dist-packages/coverage/control.py", line 389, in stop
self.collector.stop()
File "/usr/local/lib/python2.7/dist-packages/coverage/collector.py", line 262, in stop
assert self._collectors[-1] is self
AssertionError
To make thing more difficult, I'm trying to test a command line utility. Which means I had to tell coverage to cover subprocess calls.
I think I got this part working since coverage is now reporting a % of cover for the script that is being run. But since I got coverage working I can't get rid of the AssertionError.
Some help understanding what's wrong would be really appreciated. All my code is available on github:
repo
setup.py
run_tests
subprocess call
Quick run:
cd /tmp/ && git clone git://github.com/h3/django-duke-client.git
cd django-duke-client && chmod a+x run_tests && ./run_tests
Thanks
Update
I've run the test on a different computer and I got the same AssertionError .. Plus a new TypeError. Again the tests runs correctly and coverage also seems to work properly even with those errors..
...
Ran 9 tests in 1.324s
OK
Traceback (most recent call last):
File "/usr/local/bin/coverage", line 9, in <module>
load_entry_point('coverage==3.5.1', 'console_scripts', 'coverage')()
File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 657, in main
status = CoverageScript().command_line(argv)
File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 526, in command_line
self.coverage.stop()
File "/usr/local/lib/python2.7/dist-packages/coverage/control.py", line 389, in stop
self.collector.stop()
File "/usr/local/lib/python2.7/dist-packages/coverage/collector.py", line 262, in stop
assert self._collectors[-1] is self
AssertionError
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.7/multiprocessing/util.py", line 284, in _exit_function
info('process shutting down')
TypeError: 'NoneType' object is not callable
Error in sys.exitfunc:
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.7/multiprocessing/util.py", line 284, in _exit_function
info('process shutting down')
TypeError: 'NoneType' object is not callable
Name Stmts Miss Branch BrPart Cover Missing
------------------------------------------------------------------------------
dukeclient/__init__ 53 53 2 0 4% 1-93
dukeclient/commands/__init__ 41 33 6 2 26% 1-9, 12, 14-15, 17, 24-28, 34-43, 46-63
...
Regarding the NoneType is not callable error, please find below some elements that may help you.
In your module plugintest.py from nose-1.1.2-py2.7.egg/nose/plugins/, line 174, one can read the following line :
from multiprocessing import Manager
That leads the multiprocessing.util package to be imported, and with it an exit function to be registered :
atexit.register(_exit_function)
The problem seems to be that multiprocessing.util which is loaded in plugintest is then unloaded before the _exit_function gets called, and it's function definitions by the way.
Thus, if you import it in your setup.py file :
from multiprocessing import util
The error disappear.
By the way, I had to comment some parts in the tests that were failing or change some lines of code :
the -m command does not seems to be valid ;
I had to rename duke_conf.yml to duke_conf.yml ;
the tests that checks if README.rst and LICENSE files exists are failing (don't had the time to check why) ;
Hope it helps,

Categories