Efficient ways for Python debugging when called from within C++ code - python

I have a C++ application (on Windows XP) that invokes some Python code. I currently use Winpdb as my python debugger.
Using winpdb as the debugger has some disadvantages since firstly, I need to add a pdb statement, and there is little control afforded to me during the execution since I add dynamic breakpoints.
Does anyone know of tools that can be used that work in a similar fashion to Visual Studio 2010 (or Visual Studio in general)? Most important for me is to be able to set up and remove breakpoints while the application is executing, much like we can do using Visual Studio.
I have looked at ActiveState Komodo IDE6 and Eclipse Python PyDev, but I do not think either one actually fits the bill. This is because I have various python modules that are initialized
using the C++ function call "Py_InitModule" with a name that might be different from the name of the .py file. There modules are not recognized by Komodo and Eclipse Python PyDev.
Please let me know if you have any suggestions. Thank you for your help.

Well, for me it seems that it'd work in PyDev... The only thing is that you have to attach the tracing to the debugger -- use pydevd.settrace(suspend=False) on the thread you want to trace and after that it should 'synchronize' the current breakpoints and their additions/removals ( more references: http://pydev.org/manual_adv_remote_debugger.html ) -- note that you could use pydevd.settrace(suspend=True) to work as a breakpoint as in in pdb.
I didn't really understand why you said it wouldn't work. Can you post your specific example of what's not working to see how that would be solvable? (some things may be customized in pydevd_file_utils.py to help in translating breakpoint paths).

Related

Control Visual Studio using python

I am building an application in which I would like to control Visual Studio, or at least have access to its compilation and build functionalities using (hopefully) python. I know Visual Studio macros are possible and available but it appears they are in .NET (see doc). Since I would like to automate what I want to do with VS in python I'm looking more for something along the lines of:
import vs2016 as controller
controller.build()
controller.compile()
Please note I am not looking to run python code in Visual Studio. This is an altogether different problem that is already widely documented by Microsoft.
Is it possible to control Visual Studio within Python?
if so, how?
if not, what is a viable alternative? let's say I accept that it can only be done with dotnet. In this case how can I relay back to python while keeping it debuggable from a python script?
Thanks!
This sounds like a pretty big undertaking, but I would look in to a win32com.client.dispatch or comtypesto plug in to the COM object(there are several from a quick search and I dont know what any of them do)

Debugging Subprocesses in PyDev

We are trying to debug C++ code called by a Python script (using subprocess.call). Is there a way to do this with PyDev? I am a Java developer and we are developing an Eclipse RCP application, and can write custom code as needed to make this work.
If you want to debug c++ code, you need a c++ debugger (and PyDev won't really help you there).
My suggestion is looking for a c++ debugger in this case (you can use gdb in windows and linux -- on windows you can also use visual c++ if you want -- you should be able to attach to a running process on both cases -- I think that CDT also has some gdb integration which you can use if you set it up properly).

Visual Studio Python WPF debugging

So I spend almost all of my programming time on Linux, but recently, for a project, I had to try out PTVS and WPF. As it is a very good framework, I would like to learn it further, but the issue in question is a huge disadvantage when compared to running python programs on Linux.
So say I have a little WPF project on IronPython2.7, with some buttons and some functions handling those buttons' click events. The problem is that not every exception/error present in that code is shown in debugger: sometimes it just says "the process exited with code 0x01". That, for example, happens if I try to access an array element outside of array bounds.
The question is: is there a way to fix this and be able to see all the errors and exceptions while debugging a WPF program?
By default, IronPython projects in Visual Studio run using the managed debugger. This makes it really easy to debug mixed Python/.NET code, however, you don't get quite as nice an experience when you only have Python code.
To change this, right-click your project in Solution Explorer and select Properties. On the Debug tab, there's a dropdown for the launcher - you can see a screenshot here.
It's probably got IronPython (.NET) launcher selected, but changing it to Standard Python launcher should give you better pure-Python debugging. The downside is that you won't be able to step into .NET code anymore, and it's a little bit more invasive. In general though, the improvements are worth it if you are not using IronPython to extend a .NET application.

Is it possible to implement automatic error highlighting for Python?

Are there any IDEs for Python that support automatic error highlighting (like the Eclipse IDE for Java?) I think it would be a useful feature for a Python IDE, since it would make it easier to find syntax errors. Even if such an editor did not exist, it still might be possible to implement this by automatically running the Python script every few seconds, and then parsing the console output for error messages.
eclipse+pydev
pycharm
many others ....
If you use VIM or don't have a problem with it, try this extension. https://github.com/klen/python-mode
This is for Emacs as well: https://github.com/gabrielelanaro/emacs-for-python
Also pycharm and eclipse with pydev work fine.
If I don't use vim I really enjoy spyder. It is easy to use and has some really nice features, like integrated debugging and profiling, graphical variable explorer and object inspector. The latter shows, e.g., the integrated documentation for every function of class you use.
I built an extension to Eclipse and PyDev that does what you describe, it runs the Python code as you're typing, and displays all the variable values and any exceptions that occur. It's called Live Coding in Python, and the web site has a tutorial and a demo video.
PyDev can highlight some problems in your code by analysing it, and Live Coding in Python can show you problems that happen when you run it.

Debug crashing C Library used in Python project

complete Python noob here (and rusty in C).
I am using a Mac with Lion OS. Trying to use NFCpy, which uses USBpy, which uses libUSB. libUSB is crashing due to a null pointer but I have no idea how to debug that since there are so many parts involved.
Right now I am using xcode to view the code highlighted but I run everything from bash. I can switch to Windows or Linux if this is going to be somehow easier with a different environment.
Any suggestions on how to debug this would be much appreciated ;-)
PS: It would be just fine if I could see the prints I put in C in the bash where I run the Python script
You should see your printf() you put in C in your terminal, something is already wrong here. Are you sure that you're using the latest compiled library? To be sure, instead of print, you can use use assert(0) (you need to include assert.h).
Anyway, you can debug your software using gdb:
gdb --args python yourfile.py
# type "run" to start the program
# if you put assert() in your code, gdb will stop at the assert, or you can put
# manual breakpoint by using "b filename:lineno" before "run"
Enable core dumps (ulimit -Sc unlimited) and crash the program to produce a core file. Examine the core file with gdb to learn more about the conditions leading up to the crash. Inspect the functions and local variables on the call stack for clues.
Or run the program under gdb to begin with and inspect the live process after it crashes and gdb intercepts the signal (SIGSEGV, SIGBUS, whatever).
Both of these approaches will be easier if you make sure all relevant native code (Python, libUSB, etc) have debugging symbols available.
Isolating the problem in a program which is as small as you can manage to make it, as Tio suggested, will also make this process easier.
PS: It would be just fine if I could see the prints I put in C in the bash where I run the Python script
You didn't mention anything about adding prints "in C" elsewhere in your question. Did you modify libUSB to add debugging prints? If so, did you rebuild it? What steps did you take to ensure that your new build would be used instead of the previously available libUSB? You may need to adjust your dylib-related environment variables to get the dynamic linker to prefer your version over the system version. If you did something else, explain what. :)

Categories

Resources