Mapping of IDL definitions to Python language.

Introduction

SALOME is a distributed client/server application using the Common Object Request Broker Architecture (CORBA). CORBA architecture uses the Interface Definition Language (IDL), which specifies interfaces between CORBA objects. So with help of IDL CORBA's language independence is ensured . Because interfaces described in IDL can be mapped to the most of currently used programming languages, CORBA applications and components are thus independent of the language(s) used to implement them. In other words, a client written in C++ can communicate with a server written in Java, which in turn can communicate with another server written in COBOL, and so forth.

One important thing to remember about IDL is that it is not an implementation language. That is, applications can't be written in IDL. The sole purpose of IDL is to define interfaces; providing implementations for these interfaces is performed using some other language.

This page contains an abridged reference manual for mapping of IDL definitions to Python language. It will be useful for Python programmers who are not familiar with IDL language. All examples are taken from SALOME source files. The complete version of Python Language Mapping Specification can be found here.
 

CONTENTS:


Using Scoped Names

Python implements a module concept that is similar to the IDL scoping mechanisms, except that it does not allow for nested modules. In addition, Python requires each object to be implemented in a module; globally visible objects are not supported.

Because of these constraints, scoped names are translated into Python using the following rules:

• An IDL module mapped into a Python module. Modules containing modules are mapped to packages (i.e., directories with an __init__ module containing all definitions excluding the nested modules). An implementation can chose to map toplevel definitions (including the module CORBA) to modules in an implementationdefined package, to allow concurrent installations of different CORBA runtime libraries. In that case, the implementation must provide additional modules so that toplevel modules can be used without importing them from a package.

• For all other scopes, a Python class is introduced that contains all the definitions inside this scope.

• Other global definitions (except modules) appear in a module whose name is implementation dependent. Implementations are encouraged to use the name of the IDL file when defining the name of that module.

For instance,

module SALOMEDS {
 interface StudyManager {
  void  Close(in Study aStudy);
 };
};
would introduce a module SALOMEDS.py, which contains the following definitions:
# module SALOMEDS.py
class StudyManager:
  def _Close(self,aStudy):
   pass #interfaces are discussed later
To avoid conflicts, IDL names that are also Python identifiers are prefixed with an underscore (‘_’).

Back to the contents

Mapping for Template and Array Types

Both the bounded and the unbounded string type of IDL are mapped to the Python string type. Wide strings are represented by an implementation-defined type with the following properties:

• For the wide string X and the integer n, X[n] returns the nth character, which is a wide string of length 1.

• len(X) returns the number of characters of wide string X.

• CORBA.wstr(c) returns a wide character with the code point c in an implementation-defined encoding.

• X+Y returns the concatenation of wide strings X and Y.

• CORBA.word(CORBA.wstr(c)) == c

The sequence template is mapped to sequence objects (e.g., tuples or lists). Applications should not assume that values of a sequence type are mutable. Sequences and arrays of octets and characters are mapped to the string type for efficiency reasons.

For example, given the IDL definitions

module SALOMEDS {
  typedef sequence <string> StringSeq;
   
   interface AttributeTableOfInteger : GenericAttribute {

    void SetRowTitles(in StringSeq theTitles) raises(IncorrectArgumentLength);
 };
};
a client could invoke the operation
print My_AttributeTableOfInteger.SetRowTitles(["X","F"])
Array types are mapped like sequence templates. The application in this example also expects an IncorrectArgumentLength exception if it passes sequences that violate the bounds constraint or arrays of wrong size.

Another example with arrays. The following IDL definition

module SALOMEDS {
 typedef sequence<GenericAttribute> ListOfAttributes;
 interface SObject {
  ListOfAttributes     GetAllAttributes();
 };
};
is equal to
import SALOMEDS

attributes=[]
 
attributes = My_SObject.GetAllAttributes()

length = len(attributes)

print "Attributes number = ", length
print attributes
Back to the contents

Mapping for Objects and Operations

A CORBA object reference is represented as a Python object at run-time. This object provides all the operations that are available on the interface of the object. Although this specification does not mandate the use of classes for stub objects, the following discussion uses classes to indicate the interface.

The nil object is represented by None.

If an operation expects parameters of the IDL Object type, any Python object representing an object reference might be passed as actual argument.

If an operation expects a parameter of an abstract interface, either an object implementing that interface, or a value supporting this interface may be passed as actual argument. The semantics of abstract values then define whether the argument is passed by value or by reference.

Operations of an interface map to methods available on the object references. Parameters with a parameter attribute of in or inout are passed from left to right tothe method, skipping out parameters. The return value of a method depends on the number of out parameters and the return type. If the operation returns a value, this value forms the first result value. All inout or out parameters form consecutive result values. The method result depends then on the number of result values:

• If there is no result value, the method returns None.

• If there is exactly one result value, it is returned as a single value.

• If there is more than one result value, all of them are packed into a tuple, and this tuple is returned.

Assuming the IDL definition

module SALOMEDS{
 interface StudyBuilder{
  boolean FindAttribute  ( in SObject anObject, 
                           out GenericAttribute anAttribute, 
                           in string aTypeOfAttribute );
 };
};
a client could write
from SALOMEDS import StudyBuilder;
my_StudyBuilder=...
  
  res,A=my_StudyBuilder.FindAttribute(Sobj, "AttributeSequenceOfReal")
In this example A corresponds to the return value anAttribute and res to the boolean return value.

If an interface defines an attribute name, for example, the attribute is mapped into an operation _get_name. If the attribute is not readonly, there is an additional operation _set_name.

The IDL definition

module SALOMEDS{
 interface Study{
  attribute string Name;
 };
};
is equal to the following
from SALOMEDS import Study
My_Study=...
  Name=My_Study._get_name();
  Name=My_Study._set_name();
Back to the contents

Narrowing Object References

Python objects returned from CORBA operations or pseudo-operations (such as string_to_object) might have a dynamic type, which is more specific than the static type as defined in the operation signature.

Since there is no efficient and reliable way of automatically creating the most specific type, explicit narrowing is necessary. To narrow an object reference A to an interface class AttributeSequenceOfReal, the client can use the following operation

A = A._narrow(SALOMEDS.AttributeSequenceOfReal)
Back to the contents

Mapping for Exceptions

An IDL exception is translated into a Python class derived from CORBA.UserException. System exceptions are derived from CORBA.SystemException. Both base classes are derived from CORBA.Exception. The parameters of the exception are mapped in the same way as the fields of a struct definition. When raising an exception, a new instance of the class is created; the constructor expects the exception parameters. For example, the definition
module SALOMEDS{
 interface StudyBuilder{
  exception LockProtection {};
  void CommitCommand() raises(LockProtection);
 };
};
could be used caught as
from SALOMEDS import StudyBuilder;
my_StudyBuilder=...
try:
  my_StudyBuilder.CommitCommand();
except StudyBuilder.LockProtection,value:
  print "Error! Study is locked for modifications"


Back to the contents

Mapping for Enumeration Types

An enumeration is mapped into a number of constant objects in the name space where the enumeration is defined. An application may only test for equivalence of two enumeration values, and not assume that they behave like numbers. For example, the definition
module VISU {
 interface PrsObject{
 
  enum PrsObjType{ TCURVE, TTABLE, TMESH, TCONTAINER,
                   TSCALARMAP, TISOSURFACE, TDEFORMEDSHAPE,
                   TCUTPLANES, TVECTORS };
 };
};
introduces the objects
from VISU import PrsObject
VISU.PrsObjType.TCURVE,VISU.PrsObjType.TTABLE,VISU.PrsObjType.TMESH,VISU.PrsObjType.TCONTAINER,
VISU.PrsObjType.TSCALARMAP,VISU.PrsObjType.TISOSURFACE,VISU.PrsObjType.TDEFORMEDSHAPE,VISU.PrsObjType.TCUTPLANES,
VISU.PrsObjType.TVECTORS
Back to the contents

Mapping for Structured Types

An IDL struct definition is mapped into a Python class or type. For each field in the struct, there is a corresponding attribute in the class with the same name as the field. The constructor of the class expects the field values, from left to right. For example, the IDL definition
struct SDate {
               short Second;
               short Minute;
               short Hour;
               short Day;
               short Month;
               short Year;
             };
could be used in the Python statements
Date=SDate(30, 12, 15, 26, 1, 79)
print Date.Second,Date.Minute,Date.Hour,Date.Day,Date.Month,Date.Year
Back to the contents