4
votes

i've implemented IronPython in my C#-Application successful. I store all my scripts in a database and load them when they are needed. Now i want to debug my Python-Code with the PTVS. But always when i try to connect with the remote debugger to my application the visual studio say that i should use ptvsd.enable_attach().

  1. i thought if i enable the Debug-Mode for my Python-Engine it would be enought
  2. If i need to import ptvsd, how i can import the scripts(ini, main, ...) should i put them also in my database?

I can't figure this points out and have tried a lot, but nothing realy work.

EDIT: I could figure out how to use ptvsd, i have to "include" the ptvsd-module:

//copied from: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0

string dir = Path.GetDirectoryName("C:\\Support\\Modules\\ptvsd");
ICollection<string> paths = myScriptEngine.GetSearchPaths();

if (dir != null && dir != "")
{
    paths.Add(dir);
}
else
{
    paths.Add(Environment.CurrentDirectory);
}

But now i get an error in os.py:

global name 'statvfs_result' is not defined

in the lines:

_copy_reg.pickle(statvfs_result, _pickle_statvfs_result,
    _make_statvfs_result)

EDIT 2: It seems that i can ignore the error message with the global name. But now i get the following message:

IronPython must be started with -X:Tracing and -X:Frames options to support PTVS remote debugging.

EDIT 3: I solved the error with Tracing and Frames by using the following code:

        Dictionary<string, object> options = new Dictionary<string, object>();
        options["Debug"] = true;
        options["Tracing"] = true;
        options["Frames"] = true;
        myScriptEngine = Python.CreateEngine(options);

But now i have the next problem, i can't attach visual studio to my application, i get always the following error-message:

Could not connect to remote Python process at 'localhost:5678'. Make sure that the process is running, and has called ptvsd.enable_attach()-

EDIT 4: My python code:

# -----------------------------------------------
# Framework-Root-Script
# This script is the main-framework script
#  Autor: BE
#  Date: 07.10.2013
# -----------------------------------------------

# --------------------------------------------
import sys
#import atexit
import ptvsd

ptvsd.enable_attach(None)
#ptvsd.wait_for_attach()

#
from System import *
from System.Windows import MessageBox
from System.Windows.Controls import Grid, MenuItem
from ESS.MS.Base import GlobalSettings
from ESS.MS.Framework.Core.TaskbarNotification import TaskbarNotificationManager
from ESS.MS.Framework.UIG.Mask import DynamicMaskManager
# --------------------------------------------

# --------------------------------------------
#<summary>
#Eine Instanz dieser Klasse wird automatisch mit
#dem Start des DocCenter Studios erstellt.
#</summary>
class StudioInstance:

    # --------------------------------------------
    # Declarations

    # --------------------------------------------

    # --------------------------------------------
    # Constructor
    def __init__(self): 
        pass 
    # --------------------------------------------

    # --------------------------------------------
    # Will be called before the Login-Window open
    def BeforeUserLogin(self):
        try:
            pass
        except:
            pass
    # --------------------------------------------

    # --------------------------------------------
    #<summary>
    #Wird ausgeführt, wenn der Login für einen Benutzer
    # Fehlschlägt
    #</summary>
    #<param Name="InputUserName">Eingegeber Benutzername</param>
    #<param Name="InputDomain">Eingegebene Domain<param>
    def LoginFailed(self, InputUserName, InputDomain):
        try:
            pass
        except:
            pass
    # --------------------------------------------

    # --------------------------------------------
    # Will be called if the Login-Process is complete
    def LoginComplete(self, UserName, Domain):
        try:
            # -------------------------------------------------------------------
            # Control auf das Tray-Icon setzten (Linksklick)
            # Mask = DynamicMaskManager.Singleton.GetMaskInstance("Win_DCC_Bediener", False)

            # grid = Grid()
            # grid.Children.Add(Mask.VisualElement)

            # TaskbarNotificationManager.Singleton.AddTrayPopupControl(grid)
            # -------------------------------------------------------------------

            # -------------------------------------------------------------------
            # Context-Menu einttrag auf das Tray-Icon setzten
            # test = MenuItem()
            # test.Header = "Hallo Welt"
            # TaskbarNotificationManager.Singleton.AddContextMenuItem(test)
            # -------------------------------------------------------------------
            pass
        except Exception, e:
            MessageBox.Show(e.ToString())
    # --------------------------------------------

    # --------------------------------------------
    # Will be called synchron with the UI (same thread)
    def SyncUpdate(self):
        try:
            pass
        except Exception, e:
            MessageBox.Show(e.ToString())
    # --------------------------------------------

    # --------------------------------------------
    # Will be called in a custom thread
    def AsyncUpdate(self):
        try:
            pass
        except:
            pass
    # --------------------------------------------

# --------------------------------------------

EDIT 5 I think i can attach now to the process. But when i click on the refresh-button in the visual studio debugger window, visual studio hang up and the program doesn't react anymore.

Refresh-Button: Scrennshot

Maybe some one can halp me, thank you!

2
Can you show the Python code that you've used to load and enable ptvsd in your script? - Pavel Minaev
Ok, i added my cod on EDIT 5 - BendEg
What is the "refresh button" that you've mentioned in your last edit? - Pavel Minaev
Thank you for your comment. I have added a screenshot with the refresh-button. - BendEg
This doesn't look like you've actually attached to anything. If the connection to your process would've been successful, you should see it in the "Available Processes" list, and be able to click "Attach" at the bottom of the dialog. If you don't see it in the list then it wasn't detected. All that "Refresh" does is re-query the list of processes by pinging the server again. - Pavel Minaev

2 Answers

1
votes

Assuming that the process is running on localhost and you've called ptvsd.enable_attach(), it's probably a firewall issue. You might have to tweak the Windows firewall to allow connections to that port (I thought that localhost connections were always allowed but I'm not sure).

0
votes

Man, You should read attach_server.py and insert some logging output in in, especially server_thread_func(). Look into it and put debug output on some points from starting connection to attach. Find where it fails, You'll understand a reason. Now You can fix it.

Also add debug output to visualstudio_py_util.py::write_bytes() etc, and You'll understand what send and received to/from debug socket.