How to prepend python code to swig-generated proxy module?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to prepend python code to swig-generated proxy module?

vitaly numenta
My swig-based build generates several python extensions, including the corresponding python proxy modules. I need to execute some boilerplate python code in the generated python module before the generated python module imports the extensions shared library. However, Swig doesn't want to cooperate, and my boilerplate code always ends up getting inserted later than what I need.

The Swig-generated python module begins with the following preamble inserted by Swig:

```
from sys import version_info
if version_info >= (2,6,0):
    def swig_import_helper():
        from os.path import dirname
        import imp
        fp = None
        try:
            fp, pathname, description = imp.find_module('_algorithms', [dirname(__file__)])
        except ImportError:
            import _algorithms
            return _algorithms
        if fp is not None:
            try:
                _mod = imp.load_module('_algorithms', fp, pathname, description)
            finally:
                fp.close()
            return _mod
    _algorithms = swig_import_helper()
    del swig_import_helper
```

I need to pre-load a shared library (via ctypes.CDLL) before the preamble's `swig_import_helper` runs.

I tried adding my own boilerplate code that loads the shared library at the very beginning of the .i file that defines the extension via `%pythoncode`, but my code always shows up after Swig's own preamble, so that it executes too late.

Can someone suggest how I can coerce Swig to add my own boilerplate preamble ahead of Swig's own preamble? I need my code inserted ahead of Swig's swig_import_helper code block.


Many thanks!

------------------------------------------------------------------------------

_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: How to prepend python code to swig-generated proxy module?

Mark Pallone
I couldn't find an "official" way to do this, so I just have the following in my build script. 

If I give swig mymodule.i and it generates mymodule.py, I then run: 

    cat top_of_mymodule_dot_py.py mymodule.py > temp_mymodule.py && mv temp_mymodule.py mymodule.py 

where top_of_mymodule_dot_py.py contains whatever Python code that I want to execute every time mymodule.py is imported. 

I wonder if there's a better way, but this works so it probably doesn't matter. 

-Mark 


On Fri, Aug 5, 2016 at 12:40 AM vitaly numenta <[hidden email]> wrote:
My swig-based build generates several python extensions, including the corresponding python proxy modules. I need to execute some boilerplate python code in the generated python module before the generated python module imports the extensions shared library. However, Swig doesn't want to cooperate, and my boilerplate code always ends up getting inserted later than what I need.

The Swig-generated python module begins with the following preamble inserted by Swig:

```
from sys import version_info
if version_info >= (2,6,0):
    def swig_import_helper():
        from os.path import dirname
        import imp
        fp = None
        try:
            fp, pathname, description = imp.find_module('_algorithms', [dirname(__file__)])
        except ImportError:
            import _algorithms
            return _algorithms
        if fp is not None:
            try:
                _mod = imp.load_module('_algorithms', fp, pathname, description)
            finally:
                fp.close()
            return _mod
    _algorithms = swig_import_helper()
    del swig_import_helper
```

I need to pre-load a shared library (via ctypes.CDLL) before the preamble's `swig_import_helper` runs.

I tried adding my own boilerplate code that loads the shared library at the very beginning of the .i file that defines the extension via `%pythoncode`, but my code always shows up after Swig's own preamble, so that it executes too late.

Can someone suggest how I can coerce Swig to add my own boilerplate preamble ahead of Swig's own preamble? I need my code inserted ahead of Swig's swig_import_helper code block.


Many thanks!
------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user

------------------------------------------------------------------------------

_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: How to prepend python code to swig-generated proxy module?

RAVI NANJUNDAPPA
In reply to this post by vitaly numenta
Hi,

I feel you can use "%pythoncode". For more informaiton on how to use pythoncode please refer to section "36.6.2 Adding additional Python code" from SWIG-3.0 Documention.pdf


Thanks and Best Regards,
N Ravi 
 
--------- Original Message ---------
Sender : Mark Pallone <[hidden email]>
Date   : 2016-08-05 10:32 (GMT+5:30)
Title  : Re: [Swig-user] How to prepend python code to swig-generated proxy module?
 
I couldn't find an "official" way to do this, so I just have the following in my build script. 

If I give swig mymodule.i and it generates mymodule.py, I then run: 

    cat top_of_mymodule_dot_py.py mymodule.py > temp_mymodule.py && mv temp_mymodule.py mymodule.py 

where top_of_mymodule_dot_py.py contains whatever Python code that I want to execute every time mymodule.py is imported. 

I wonder if there's a better way, but this works so it probably doesn't matter. 

-Mark 


On Fri, Aug 5, 2016 at 12:40 AM vitaly numenta <[hidden email]> wrote:
My swig-based build generates several python extensions, including the corresponding python proxy modules. I need to execute some boilerplate python code in the generated python module before the generated python module imports the extensions shared library. However, Swig doesn't want to cooperate, and my boilerplate code always ends up getting inserted later than what I need.

The Swig-generated python module begins with the following preamble inserted by Swig:

```
from sys import version_info
if version_info >= (2,6,0):
    def swig_import_helper():
        from os.path import dirname
        import imp
        fp = None
        try:
            fp, pathname, description = imp.find_module('_algorithms', [dirname(__file__)])
        except ImportError:
            import _algorithms
            return _algorithms
        if fp is not None:
            try:
                _mod = imp.load_module('_algorithms', fp, pathname, description)
            finally:
                fp.close()
            return _mod
    _algorithms = swig_import_helper()
    del swig_import_helper
```

I need to pre-load a shared library (via ctypes.CDLL) before the preamble's `swig_import_helper` runs.

I tried adding my own boilerplate code that loads the shared library at the very beginning of the .i file that defines the extension via `%pythoncode`, but my code always shows up after Swig's own preamble, so that it executes too late.

Can someone suggest how I can coerce Swig to add my own boilerplate preamble ahead of Swig's own preamble? I need my code inserted ahead of Swig's swig_import_helper code block.

If it helps, the relevant Swig commands in my cmake build are here: https://github.com/numenta/nupic.core/blob/f7bdb0a369e51a9488a9c383bf3778c79b8d1ba5/src/CMakeLists.txt#L636-L798

Many thanks!

------------------------------------------------------------------------------

_______________________________________________

Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
------------------------------------------------------------------------------
 
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
 
------------------------------------------------------------------------------

_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: How to prepend python code to swig-generated proxy module?

William S Fulton
In reply to this post by Mark Pallone
Use %pythonbegin http://swig.org/Doc3.0/Python.html#Python_nn42.

William

On 5 August 2016 at 06:00, Mark Pallone <[hidden email]> wrote:

> I couldn't find an "official" way to do this, so I just have the following
> in my build script.
>
> If I give swig mymodule.i and it generates mymodule.py, I then run:
>
>     cat top_of_mymodule_dot_py.py mymodule.py > temp_mymodule.py && mv
> temp_mymodule.py mymodule.py
>
> where top_of_mymodule_dot_py.py contains whatever Python code that I want to
> execute every time mymodule.py is imported.
>
> I wonder if there's a better way, but this works so it probably doesn't
> matter.
>
> -Mark
>
>
> On Fri, Aug 5, 2016 at 12:40 AM vitaly numenta
> <[hidden email]> wrote:
>>
>> My swig-based build generates several python extensions, including the
>> corresponding python proxy modules. I need to execute some boilerplate
>> python code in the generated python module before the generated python
>> module imports the extensions shared library. However, Swig doesn't want to
>> cooperate, and my boilerplate code always ends up getting inserted later
>> than what I need.
>>
>> The Swig-generated python module begins with the following preamble
>> inserted by Swig:
>>
>> ```
>> from sys import version_info
>> if version_info >= (2,6,0):
>>     def swig_import_helper():
>>         from os.path import dirname
>>         import imp
>>         fp = None
>>         try:
>>             fp, pathname, description = imp.find_module('_algorithms',
>> [dirname(__file__)])
>>         except ImportError:
>>             import _algorithms
>>             return _algorithms
>>         if fp is not None:
>>             try:
>>                 _mod = imp.load_module('_algorithms', fp, pathname,
>> description)
>>             finally:
>>                 fp.close()
>>             return _mod
>>     _algorithms = swig_import_helper()
>>     del swig_import_helper
>> ```
>>
>> I need to pre-load a shared library (via ctypes.CDLL) before the
>> preamble's `swig_import_helper` runs.
>>
>> I tried adding my own boilerplate code that loads the shared library at
>> the very beginning of the .i file that defines the extension via
>> `%pythoncode`, but my code always shows up after Swig's own preamble, so
>> that it executes too late.
>>
>> Can someone suggest how I can coerce Swig to add my own boilerplate
>> preamble ahead of Swig's own preamble? I need my code inserted ahead of
>> Swig's swig_import_helper code block.
>>
>> If it helps, the relevant Swig commands in my cmake build are here:
>> https://github.com/numenta/nupic.core/blob/f7bdb0a369e51a9488a9c383bf3778c79b8d1ba5/src/CMakeLists.txt#L636-L798
>>
>> Many thanks!
>>
>> ------------------------------------------------------------------------------
>> _______________________________________________
>> Swig-user mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/swig-user
>
>
> ------------------------------------------------------------------------------
>
> _______________________________________________
> Swig-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/swig-user
>

------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: How to prepend python code to swig-generated proxy module?

vitaly numenta
In reply to this post by vitaly numenta

Thank you, William! `pythonbegin` is exactly what I was looking for, but

had somehow missed it in all the wealth of documentation.


Best,

Vitaly


------------------------------------------------------------------------------

_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: How to prepend python code to swig-generated proxy module?

Mark Pallone
Same! I can't believe I missed it. 

Thanks!
Mark

On Tue, Aug 16, 2016 at 12:50 PM vitaly numenta <[hidden email]> wrote:

Thank you, William! `pythonbegin` is exactly what I was looking for, but

had somehow missed it in all the wealth of documentation.


Best,

Vitaly

------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user

------------------------------------------------------------------------------

_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user