Constant variable and API

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mg

    #1

    Constant variable and API

    Hi everybody...

    I am using Python API in order to create bindings.
    So, in the init function of my module, I create constants :

    PyMODINIT_FUNC initMyModule( void )
    {
    PyObject* module = Py_InitModule3( "MyModule", 0, "this is my
    module" ) ;
    if( ! module ) return ;

    PyObject* dict = PyModule_GetDic t( module ) ;
    if( ! dict ) return ;

    long value = 0 ; // for the example
    PyObject* py_value = PyInt_FromLong( value ) ;
    char* name = "NULL" ;
    PyDict_SetItemS tring( dict, name, py_value ) ;
    Py_DECREF( py_value ) ;
    }

    So, my first "problem" is that my module variable is mutable; I can
    write the following instruction in python :
    [color=blue][color=green][color=darkred]
    >>> import MyModule
    >>> null = MyModule.NULL
    >>> print null[/color][/color][/color]
    0[color=blue][color=green][color=darkred]
    >>> MyModule.NULL = 99
    >>> null = MyModule.NULL
    >>> print null[/color][/color][/color]
    99

    Then, my question is : how can I implement a constant variable from the
    API in order to the reaffectation (MyModule.NULL = 99) of my variable be
    impossible ?

    Thanks for your help.
    Mathieu
  • Alex Martelli

    #2
    Re: Constant variable and API

    mg <mg.mailing-list@laposte.ne t> wrote:
    ...[color=blue]
    > Then, my question is : how can I implement a constant variable from the
    > API in order to the reaffectation (MyModule.NULL = 99) of my variable be
    > impossible ?[/color]

    With a module (a direct instance of types.ModuleTyp e), you can't -- the
    module type just doesn't have any functionality to control attribute
    setting. You can subclass the type to do that, and install an instance
    of the subclass in sys.modules in the appropriate place...


    Alex

    Comment

    Working...