4

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!

BendEg
  • 20,098
  • 17
  • 57
  • 131
  • Can you show the Python code that you've used to load and enable ptvsd in your script? – Pavel Minaev Mar 17 '14 at 16:29
  • Ok, i added my cod on __EDIT 5__ – BendEg Mar 17 '14 at 20:41
  • What is the "refresh button" that you've mentioned in your last edit? – Pavel Minaev Mar 21 '14 at 07:14
  • Thank you for your comment. I have added a screenshot with the refresh-button. – BendEg Mar 21 '14 at 09:27
  • 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 Mar 21 '14 at 18:43

2 Answers2

1

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).

Jeff Hardy
  • 7,632
  • 24
  • 24
  • Thank you for your reply, but deactivating the firewall didn't help. Maybe the problem is, that i don't have any python files, i only copied my Scripts out of the database in a Visual Studio project (Python-Project). – BendEg Mar 12 '14 at 14:12
  • I would suggest running netstat -a -n | findstr LISTEN from a command prompt and make sure that the port is opened on the machine running IronPython. – Dino Viehland Mar 17 '14 at 19:58
  • If i execute: __netstat -a -n | findstr LISTEN__ i get no result. If i execute: __netstat -a -n__ i could find the following line: __TCP 0.0.0.0:5678__. – BendEg Mar 17 '14 at 21:19
  • Ok, i don't know how, but now "i could attach to the process", but my visual studio didn't give any "feedback". Now __netstat -a -n__ gibe the following result: __TCP 127.0.0.1:5678 127.0.0.1:63744 CONNECTED__ – BendEg Mar 17 '14 at 21:26
  • When you say that VS doesn't give any feedback, what do you mean by this? All that attach should do by itself is switch your VS to debug view, i.e. you should be seeing the Auto and Call Stack windows. But since the code is still running it won't show anything in those windows - you'll need to set a breakpoint somewhere and hit it, or use the "Pause" button. – Pavel Minaev Mar 21 '14 at 18:45
  • I mean, if i press the refresh-button in the vs window (see the image in my question), visual studio freezes and i have to kill the process over the task-manager. – BendEg Mar 22 '14 at 08:46
0

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.

NoAngel
  • 1,072
  • 2
  • 18
  • 27