[IronPython] Problems with 'unbound-class-instance' method

Okko Willeboordse okko.willeboordse at gmail.com
Tue May 18 09:42:35 CEST 2010


Thanks,

I tried the typedproxy but failed.

Maybe I got it wrong;

I do;

EventManager = typedproxy(RemoteServerApi.EventManager,
Bosch.Vms.SDK.IEventManager)

Then;

EventManager.GetAllEventTypes()

This results in exact the same error as;

Bosch.Vms.SDK.IEventManager.GetAllEventTypes(RemoteServerApi.EventManager)

Namely;

TypeError: expected IEventManager, got MarshalByRefObject

Se below

Kind regards,

import traceback
import clr
clr.AddReference("Bosch.Vms.SDK")

import Bosch.Vms.SDK

class typedproxy(object):
  __slots__ = ['obj', 'proxyType']
  def __init__(self, obj, proxyType):
    self.obj = obj
    self.proxyType = proxyType
  def __getattribute__(self, attr):
    proxyType = object.__getattribute__(self, 'proxyType')
    obj = object.__getattribute__(self, 'obj')
    return getattr(proxyType, attr).__get__(obj, proxyType)

# RemoteServerApi constructor instantiates an object implementing a
Bosch.Vms.SDK.IEventManager
# interface
RemoteServerApi = Bosch.Vms.SDK.RemoteServerApi("10.119.33.241:5390",
"Admin", "")
# RemoteServerApi holds a reference to the object implementing a
Bosch.Vms.SDK.IEventManager
# interface. This reference is of type MarshalByRefObject.
print type(RemoteServerApi.EventManager)
# How to call a method of EventManager through MarshalByRefObject?
print Bosch.Vms.SDK.IEventManager.GetAllEventTypes

try:
  # Try the naive approach.
  RemoteServerApi.EventManager.GetAllEventTypes()
except:
  # Obviously fails because MarshalByRefObject instances do not support
reflection.
  # AttributeError: 'MarshalByRefObject' object has no attribute
'GetAllEventTypes'
  print traceback.format_exc()

try:
  # Try the 'unbound-class-instance' method
  Bosch.Vms.SDK.IEventManager.GetAllEventTypes(RemoteServerApi.EventManager)
except:
  # TypeError: expected IEventManager, got MarshalByRefObject
  print traceback.format_exc()

try:
  # Okay I need a IEventManager instance
  Bosch.Vms.SDK.IEventManager(RemoteServerApi.EventManager)
except:
  # TypeError: Cannot create instances of IEventManager because it is
abstract
  print traceback.format_exc()

EventManager = typedproxy(RemoteServerApi.EventManager,
Bosch.Vms.SDK.IEventManager)
try:
  print EventManager.GetAllEventTypes()
except:
  print traceback.format_exc()
  # Traceback (most recent call last):
  #   File "bvms.py", line 50, in <module>
  #     print EventManager.GetAllEventTypes()
  #   File "bvms.py", line 15, in __getattribute__
  #     return getattr(proxyType, attr).__get__(obj, proxyType)
  # TypeError: expected IEventManager, got MarshalByRefObject


On Mon, May 17, 2010 at 10:03 PM, Dino Viehland <dinov at microsoft.com> wrote:

>  Can you try the typedproxy class which is on this bug:
> http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=470 ?
>
>
>
> It should work to wrap the object and let you access the various attributes
> that you want to by calling through the type (in this case the interface
> type – you’d pass IEventManager for the 2nd argument to the typedproxy).
>
>
>
> *From:* users-bounces at lists.ironpython.com [mailto:
> users-bounces at lists.ironpython.com] *On Behalf Of *Okko Willeboordse
> *Sent:* Monday, May 17, 2010 9:36 AM
> *To:* users at lists.ironpython.com
> *Subject:* [IronPython] Problems with 'unbound-class-instance' method
>
>
>
> Hello,
>
> I have some problems accessing an object.
>
> This object is instantiated by another object.
>
> This other object holds a reference to the object.
>
> This reference is of type MarshalByRefObject
>
> Obviously I cannot access the object through MarshalByRefObject.
>
> This must be done using the 'unbound-class-instance' method (please correct
> me if I am wrong)
>
> For this method you need to instantiate the interface type object.
>
> However the interface is abstract!
>
> How to cope with this.
>
>
> Please find sample code below;
>
>
> import traceback
> import clr
> clr.AddReference("Bosch.Vms.SDK")
>
> import Bosch.Vms.SDK
>
> # RemoteServerApi constructor instantiates an object implementing a
> Bosch.Vms.SDK.IEventManager
> # interface
> RemoteServerApi = Bosch.Vms.SDK.RemoteServerApi("xxxx", "xxxx", "xxxx")
> # RemoteServerApi holds a reference to the object implementing a
> Bosch.Vms.SDK.IEventManager
> # interface. This reference is of type MarshalByRefObject.
> print type(RemoteServerApi.EventManager)
> # How to call a method of EventManager through MarshalByRefObject?
> print Bosch.Vms.SDK.IEventManager.GetAllEventTypes
>
> try:
>   # Try the naive approach.
>   RemoteServerApi.EventManager.GetAllEventTypes()
> except:
>   # Obviously fails because MarshalByRefObject instances do not support
> reflection.
>   # AttributeError: 'MarshalByRefObject' object has no attribute
> 'GetAllEventTypes'
>   print traceback.format_exc()
>
> try:
>   # Try the 'unbound-class-instance' method
>
> Bosch.Vms.SDK.IEventManager.GetAllEventTypes(RemoteServerApi.EventManager)
> except:
>   # TypeError: expected IEventManager, got MarshalByRefObject
>   print traceback.format_exc()
>
> try:
>   # Okay I need an IEventManager instance
>   Bosch.Vms.SDK.IEventManager(RemoteServerApi.EventManager)
> except:
>   # TypeError: Cannot create instances of IEventManager because it is
> abstract
>   print traceback.format_exc()
>
> Okko Willeboordse
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100518/5381efd7/attachment.html>


More information about the Ironpython-users mailing list