Discussion:
[omniORB] Return Struct o None
Antonio Beamud Montero
2009-03-26 22:11:21 UTC
Permalink
Hi all:
I've defined a struct, and a method that returns this struct type. The
problem is when I try to return a null value instead this struct. For
example:

idl:
...
struct SInfo {
wstring id;
wstring address;
wstring tlf;
}

SInfo get_personal_info(in wstring name);

in python:
..
def get_personal_info(self, name):
if name in self.personal_dict:
return SInfo(self.personal_dict[name]['id'],
self.personal_dict[name]['address'],
self.personal_dict[name]['tlf'])
else:
return None

If the name exists, all works well, but if not exists, the next error
appears:
omniORB.CORBA.BAD_PARAM:
ORBA.BAD_PARAM(omniORB.BAD_PARAM_WrongPythonType, CORBA.COMPLETED_MAYBE)

I've tried with None, Corba.types.NoneType and CORBA.Object._nil
, but the same error.

Thanks.
Michael
2009-03-26 22:42:43 UTC
Permalink
Hi Antonio,

there is no conecpt of None/Nil/Null whatever when return a
struct/string/int or any datatype. That is because you're dealing with
pods and not pointers/references. This becomes more apparent when you're
imagening the following construct in C++:

int x = myinft->get_whatever();
So when your servant returned None, what should this do in this code?

You can only return None when returning an object reference (because it
is.. a reference :) - that will map to a nil reference in other
languages). So for what you want to accomplish it might be best to
return a sequence and make that empty in case there is no return value,
so in your IDL:

typedef sequence<SInfo> SInfoSeq;

SInfoSeq get_personal_info(in wstring name);

in python:
def get_personal_info(self, name):
if name in self.personal_dict:
return [SInfo(self.personal_dict[name]['id'],
self.personal_dict[name]['address'],
self.personal_dict[name]['tlf'])]
else:
return []

As an alternative, if that interface should usually return something (so
a miss is an exceptional event) you could throw a user exception instead
(never throw CORBA::OBJECT_NOT_EXIST in such a case, because it tells
the orb that the object reference you called get_personal_info on does
not exist - in case of location forwards and other scenarios that will
lead to disaster). Bottomline, in this case alter your idl:

exception PersonalInfoNotFoundException {
string message;
};

SInfo get_personal_info(in wstring name) raises
(PersonalInfoNotFoundException);

And in python:
...
else:
raise PersonalInfoNotFoundExceptino("Nothing there")

br
michael
Post by Antonio Beamud Montero
I've defined a struct, and a method that returns this struct type. The
problem is when I try to return a null value instead this struct. For
...
struct SInfo {
wstring id;
wstring address;
wstring tlf;
}
SInfo get_personal_info(in wstring name);
..
return SInfo(self.personal_dict[name]['id'],
self.personal_dict[name]['address'],
self.personal_dict[name]['tlf'])
return None
If the name exists, all works well, but if not exists, the next error
ORBA.BAD_PARAM(omniORB.BAD_PARAM_WrongPythonType, CORBA.COMPLETED_MAYBE)
I've tried with None, Corba.types.NoneType and CORBA.Object._nil
, but the same error.
Thanks.
_______________________________________________
omniORB-list mailing list
http://www.omniorb-support.com/mailman/listinfo/omniorb-list
William Bauder
2009-03-26 22:44:12 UTC
Permalink
CORBA doesn't allow nulls - try a discriminator union:

idl:
...
struct SInfo {
wstring id;
wstring address;
wstring tlf;
}

union SInfoDiscriminator switch(boolean)
{
case TRUE: SInfo value;
};

I don't know python, so this is pseudocode:

def get_personal_info(self, name):
if name in self.personal_dict:
return SInfoDiscrimitor().value(SInfo(self.personal_dict[name]['id'],
self.personal_dict[name]['address'],
self.personal_dict[name]['tlf']))
else:
return SInfoDiscrimitor()._default()

On the client side:
sInfoDiscriminator=get_personal_info("...")
if sInfoDiscriminator._d()==TRUE
sInfo=sInfoDiscriminator.value()
else
sInfo=NULL



-----Original Message-----
From: omniorb-list-***@omniorb-support.com
[mailto:omniorb-list-***@omniorb-support.com] On Behalf Of Antonio
Beamud Montero
Sent: Thursday, March 26, 2009 1:11 PM
To: omniorb-***@omniorb-support.com
Subject: [omniORB] Return Struct o None


Hi all:
I've defined a struct, and a method that returns this struct type. The
problem is when I try to return a null value instead this struct. For
example:

idl:
...
struct SInfo {
wstring id;
wstring address;
wstring tlf;
}

SInfo get_personal_info(in wstring name);

in python:
..
def get_personal_info(self, name):
if name in self.personal_dict:
return SInfo(self.personal_dict[name]['id'],
self.personal_dict[name]['address'],
self.personal_dict[name]['tlf'])
else:
return None

If the name exists, all works well, but if not exists, the next error
appears:
omniORB.CORBA.BAD_PARAM: ORBA.BAD_PARAM(omniORB.BAD_PARAM_WrongPythonType,
CORBA.COMPLETED_MAYBE)

I've tried with None, Corba.types.NoneType and CORBA.Object._nil
, but the same error.

Thanks.
Antonio Beamud Montero
2009-03-30 21:57:09 UTC
Permalink
Post by Antonio Beamud Montero
...
struct SInfo {
wstring id;
wstring address;
wstring tlf;
}
union SInfoDiscriminator switch(boolean)
{
case TRUE: SInfo value;
};
return SInfoDiscrimitor().value(SInfo(self.personal_dict[name]['id'],
self.personal_dict[name]['address'],
self.personal_dict[name]['tlf']))
return SInfoDiscrimitor()._default()
sInfoDiscriminator=get_personal_info("...")
if sInfoDiscriminator._d()==TRUE
sInfo=sInfoDiscriminator.value()
else
sInfo=NULL
Thanks for all your replies. For simplicity I've used the raise
exception approach, but the union seems very interesting...

A lot of thanks.
Duncan Grisby
2009-04-01 17:40:07 UTC
Permalink
Post by Antonio Beamud Montero
I've defined a struct, and a method that returns this struct type. The
problem is when I try to return a null value instead this struct. For
As others have said, you can't return null where the IDL requires a
struct.

An approach that nobody has suggested yet is to use a valuetype. Unlike
structs, those can be null (represented as None in Python).

valuetype SInfo {
public wstring id;
public wstring address;
public wstring tlf;
};

Cheers,

Duncan.
--
-- Duncan Grisby --
-- ***@grisby.org --
-- http://www.grisby.org --
Loading...