#279 Lua wrapper: require should return module t

general (37)

There is a long standing request in Debian to make SWIG wrapped Lua modules return the module table when importing the module:


As it turns out, this is not too hard to fix. While looking at it, I also noticed that loading a SWIG-generated Lua module will also clobber a global variable of the same name (often not a problem, but who knows).

The attached patch adds a new option -nomoduleglobal to disable installing the module table into the global namespace _G.

There is a slight change in behaviour: In any case, the require call to load a module will now return the module table. Originally, the name of the module was returned (as a string), which does not seem very useful for me.

I included a unit test with the patch.

Please consider applying the patch,



  • Torsten Landschoff

    Make require always return the module's table and optionally skip the global registration

  • William Fulton

    William Fulton - 2011-08-20

    Some small tweaks to the original patch adding in html documentation and Makefile changes and warning fixes

  • William Fulton

    William Fulton - 2011-08-20

    I've put in a slightly modified patch for cosmetic type changes. However, my main concern is that if the test is run as a C++ test, there is an assertion...

    ~/swig/trunk-2/Examples/test-suite/lua$ make -s lua_no_module_global.cpptest
    checking testcase lua_no_module_global (with run test) under lua
    /usr/bin/lua: ./lua_no_module_global_runme.lua:17: assertion failed!
    stack traceback:
    [C]: in function 'assert'
    ./lua_no_module_global_runme.lua:17: in main chunk
    [C]: ?
    make: *** [lua_no_module_global.cpptest] Error 1

    Can you fix it for C++?

  • Torsten Landschoff

    The C++ test is trivial to fix - SWIG is not invoked with the correct options. I missed that the C tests can also run as C++ tests. This is fixed by

    --- a/Examples/test-suite/lua/Makefile.in
    +++ b/Examples/test-suite/lua/Makefile.in
    @@ -24,7 +24,7 @@ include $(srcdir)/../common.mk
    LIBS = -L.

    # Custom tests - tests with additional commandline options
    -lua_no_module_global.ctest: SWIGOPT += -nomoduleglobal
    +lua_no_module_global.%: SWIGOPT += -nomoduleglobal

    # Rules for the different types of tests

    I will combine this patch with your changes and attach the updated patch.

  • William Fulton

    William Fulton - 2011-08-22

    Applied to svn for swig-2.0.5. Thanks.

  • William Fulton

    William Fulton - 2011-08-22
    • status: open --> closed
  • William Fulton

    William Fulton - 2011-08-22
    • assigned_to: nobody --> wsfulton
    • status: closed --> closed-accepted

Log in to post a comment.