Load module from memory, not from file

65 views
Skip to first unread message

Bystroushaak

unread,
Jan 25, 2013, 6:47:19 PM1/25/13
to bry...@googlegroups.com
Hello.

Is it possible to load module from string? I mean override import
statement, so it wouldn't try to load module from web, but just from memory.

It would be handy, due to SOP (Same Origin Policy -
http://en.wikipedia.org/wiki/Same_origin_policy), because you can't make
ajax request to other servers, but there is nothing that prevents you
dynamically add another <script> into DOM.

Olemis Lang

unread,
Jan 25, 2013, 11:06:55 PM1/25/13
to bry...@googlegroups.com
On 1/25/13, Bystroushaak <bystr...@kitakitsune.org> wrote:
> Hello.
>

:)
If I interpreted the recent changes applied by Pierre correctly then
yes , this should be possible ... but /me not sure if there's a need
to hack the source code somehow .

--
Regards,

Olemis.

Blog ES: http://simelo-es.blogspot.com/
Blog EN: http://simelo-en.blogspot.com/

Featured article:

Pedro

unread,
Jan 26, 2013, 10:41:44 AM1/26/13
to bry...@googlegroups.com

You can use eval(text), but this will bring code and not exactly a module.
Be careful since brython is not mature (Pierre is re-writing part of it),
you could face some issues like:

a = """
print('hello world')
"""
eval(a)

working fine, but not:
a = """
print("hello world")
"""
eval(a)

(notice the quotes change in print)


You could also have a look at import code:
     http://code.google.com/p/brython/source/browse/trunk/py_import.js

more precisely at function '$import_py()' the code starting at 'var stack = $py2js(res,module)'
just seems to require the data (res) and a module name (module) to perform the task.

A new import mechanism could be hooked by just creating a $import_xxx() method
and adding it to the list of importers ( $import_funcs = [$import_js,$import_py,$import_xxx].
This new method will have to retrieve the text of the module from a module name.

Again be aware that issues are reported on importing, and the same issues for eval() may apply.


Here is a simple example illustring the topic:

<html>
    <head>
        <title>Brython test string import</title>
        <script src="brython.js"></script>
        <meta charset="utf-8"/>
    </head>

<script>
function import_from_string(res, module){
    var stack = $py2js(res,module)
    // insert module name as a JS object
    stack.list.splice(0,0,['code',module+'= new object()'],['newline','\n'])
    // search for module-level names
    // functions
    var $pos=0          
    while(true){
        var $mlname_pos = stack.find_next_at_same_level($pos,"keyword","function")
        if($mlname_pos===null){break}
        var $func_name = stack.list[$mlname_pos+1][1]
        stack.list.splice($mlname_pos,2,['code',module+'.'+$func_name+"=function"])
        // modify declaration at the end of function
        var br_pos = stack.find_next($mlname_pos,'bracket','{')
        var br_end = stack.find_next_matching(br_pos)
        var $fend = stack.find_next(br_end,"func_end")
        var $fend_code = stack.list[$fend][1]
        $fend_code = module+'.'+$fend_code.substr(1)
        $pv_pos = $fend_code.search(';')
        $fend_code = ";"+$fend_code.substr(0,$pv_pos)
        stack.list[$fend][1] = $fend_code
        $pos = $mlname_pos+1
    }
    // variables
    var $pos=0          
    while(true){
        var $mlname_pos = stack.find_next_at_same_level($pos,"assign_id")
        if($mlname_pos===null){break}
        stack.list[$mlname_pos][1]=module+'.'+stack.list[$mlname_pos][1]
        $pos = $mlname_pos+1
    }
    try{
        eval(stack.to_js())
        // add class and __str__
        eval(module+'.__class__ = $type')
        eval(module+'.__str__ = function(){return "<module \''+module+"'>\"}")
    }catch(err){
        $raise(err.name,err.message)
    }
}
</script>

    <body onLoad="brython(1)">
        <script type="text/python">
        some_code = """
        def hello(n, text):
            for i in range(n):
                doc <= P(str(i) + ' Hello ' + text)
        """

        import_from_string(some_code, "my_module")
        my_module.hello(10, "world")

        </script>
    </body>
</html>

Pedro


Bystroushaak

unread,
Jan 28, 2013, 1:15:29 AM1/28/13
to bry...@googlegroups.com
Thank you.

I've been looking into sources, but I am not that good in javascript.

Can I use your example in my project? (ajax brython console
http://kitakitsune.org/bhole/brython/console.html)

On 26.1.2013 16:41, Pedro wrote:
>
>
> Le samedi 26 janvier 2013 00:47:19 UTC+1, Lishaak Bystroushaak a
> �crit :
>
> Hello.
>
> Is it possible to load module from string? I mean override import
> statement, so it wouldn't try to load module from web, but just from
> memory.
>
> It would be handy, due to SOP (Same Origin Policy -
> http://en.wikipedia.org/wiki/Same_origin_policy
> <http://en.wikipedia.org/wiki/Same_origin_policy>), because you can't
> -- You received this message because you are subscribed to the
> Google Groups "brython" group. To post to this group, send email to
> bry...@googlegroups.com. To unsubscribe from this group, send email
> to brython+u...@googlegroups.com. To view this discussion on
> the web visit
> https://groups.google.com/d/msg/brython/-/1OgfVx3jcc4J. For more
> options, visit https://groups.google.com/groups/opt_out.
>
>

Pedro

unread,
Jan 28, 2013, 4:39:51 PM1/28/13
to bry...@googlegroups.com
Le lundi 28 janvier 2013 07:15:29 UTC+1, Lishaak Bystroushaak a écrit :
Thank you.

I've been looking into sources, but I am not that good in javascript.

Can I use your example in my project? (ajax brython console
http://kitakitsune.org/bhole/brython/console.html)

On 26.1.2013 16:41, Pedro wrote:


You should ask Pierre, after all, it's just part of his Brython's code
needing some factorization to provide a base for future expansion.

Pedro
Reply all
Reply to author
Forward
0 new messages