IronLanguages / IronLanguages/ironpython3

ipy throws StandardError on EventHandler

Open
#1,492 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

confirmed
Dominant language
C#
Stars
2.8k
Forks
316
Avg merge
1d 9h
Merged PRs (30d)
1

Description

Concerns IronPython 2.7.12 2.7.12.0 on .NET 4.0.30319.42000 and IronPython 3.4.0b1. Maybe related to IronLanguages/ironpython2#594

Description

Assigning an EventHandler throws a StandardError in ipy with external dll. It works in the C# project but fails with ipy. dll is an external dll from ABB's PC SDK . It works with an older version of the dll.

Steps to Reproduce

Works in C# (full code at end of issue)

ctrl.EventLog.MessageWritten += new EventHandler<MessageWrittenEventArgs>(ctrl_EventMessageWritten);

In ipy:

delegate = EventHandler[EventLogDomain.MessageWrittenEventArgs](message)
c.EventLog.MessageWritten += delegate

I get a StandardError: Exception has been thrown by the target of an invocation. with a newer dll from ABB.

All dll versions work in C#. Works in ipy only with ABB dll version up to (and including) 6.8.8307.1040 . Starting with the next version PC SDK 2019.3 aka 7.8.8617.559 the error is thrown.

How can I debug what the underlying problem is? I tried -v -u -X:Debug -X:ExceptionDetail -X:FullFrames -X:ShowClrExceptions -X:Tracing -X:PassExceptions but did not get any deeper insights. Is this more related to ipy or to ABB? (All their dlls work in C#)

Full code

Full Python code:

import clr
import os
from encodings import utf_8  # Preload so that module is not loaded when exeception occurrs

from System import EventHandler

clr.AddReferenceToFileAndPath(os.path.abspath("ABB.Robotics.Controllers.PC.dll"))

def message(sender, event_args):
    print('message')
    return None

from ABB.Robotics.Controllers import *

try:
    netScanner = Discovery.NetworkScanner()
except SystemError as se:
    print('Cannot create NetworkScanner. Are RobotStudio.Services.RobApi.Desktop.dll and RobotStudio.Services.RobApi.dll present?')

controllers = netScanner.GetControllers()

for cinfo in controllers:
    print("cinfo", cinfo.Id)

if len(controllers) > 0:
    cinfo = controllers[0]
    # c = Controller.Connect(cinfo, ConnectionType.Standalone)  # New variant, replaced CreateFrom
    c = ControllerFactory.CreateFrom(cinfo)
    c.Logon(UserInfo.DefaultUser)

    delegate = EventHandler[EventLogDomain.MessageWrittenEventArgs](message)

    try:
        c.EventLog.MessageWritten += delegate
        # c.EventLog.MessageWritten += message  # Alternative
    except Exception as ex:
        import traceback
        traceback.print_exc()

Full code in C#, example project from ABB. SDK must be installed and ABB.Robotics.Controllers.PC.dll, RobotStudio.Services.RobApi.Desktop.dll and RobotStudio.Services.RobApi.dll must be referenced.

/* 

Copyright (c) 2011, ABB
All rights reserved.

Redistribution and use in source and binary forms, with
or without modification, are permitted provided that 
the following conditions are met:

  * Redistributions of source code must retain the 
    above copyright notice, this list of conditions 
    and the following disclaimer.
  * Redistributions in binary form must reproduce the 
    above copyright notice, this list of conditions 
    and the following disclaimer in the documentation 
    and/or other materials provided with the 
    distribution.
  * Neither the name of ABB nor the names of its 
    contributors may be used to endorse or promote 
    products derived from this software without 
    specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

using ABB.Robotics.Controllers;
using ABB.Robotics.Controllers.Discovery;
using ABB.Robotics.Controllers.EventLogDomain;

using System;

namespace ControllerAPI
{
	/// <summary>
	/// Listens for events on the Controller object
	/// </summary>
	class Listener
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[MTAThread]
		static void Main(string[] args)
		{
			Controller ctrl;
			while( ( ctrl = CreateController() ) == null )
			{
			}

			ctrl.Logon( UserInfo.DefaultUser );
            ctrl.OperatingModeChanged += new EventHandler<OperatingModeChangeEventArgs>(ctrl_OperatingModeChanged);
			ctrl.EventLog.MessageWritten += new EventHandler<MessageWrittenEventArgs>(ctrl_EventMessageWritten);

            Console.WriteLine("Press any key to terminate");
            Console.ReadKey();

			ctrl.Logoff();
		}

		static Controller CreateController()
		{
			NetworkScanner scanner = new NetworkScanner();
			ControllerInfo[] controllers = scanner.GetControllers( NetworkScannerSearchCriterias.Real);
			if( controllers.Length > 0 )
			{
				Controller dynamic = ControllerFactory.CreateFrom( controllers[0] );
				return dynamic;
			}
			return null;
		}

		private static void ctrl_OperatingModeChanged(object sender, OperatingModeChangeEventArgs e)
		{
			Console.WriteLine( "New Operating mode at: {0} new mode is: {1}", e.Time, e.NewMode );			
		}

		private static void ctrl_EventMessageWritten(object sender, MessageWrittenEventArgs e)
        {
			Console.WriteLine("New Event at: {0} event is {1}", e.Time, e.Message.Title);
        }
	}
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Python event-subscription example using EventHandler and the ABB.Robotics.Controllers.PC.dll versions identified in the issue, then compare it with the supplied C# example. Use the listed IronPython exception and tracing options while isolating the first failing DLL version. Done means identifying whether the failure is in IronPython or the ABB DLL and exposing a useful underlying exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.