sgzwiz / brython

Automatically exported from code.google.com/p/brython
BSD 3-Clause "New" or "Revised" License
0 stars 1 forks source link

local python module import fails (offline, firefox) #20

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Using firefox 17.0.1, put the following files in temporary folder
(see content below)
x.html
a.py
brython.js

x.html reports the following messages in the console:

not well-formed @ file:///tmp/zzz/a.py?foo=ot45ebsl:1
ImportError: ImportError: No module named 'a'
Line 1
            import a @ file:///tmp/zzz/brython.js:2839

My understanding of the issues is that
- there is no http request issued (local browsing),
  and the browser has to guess the type of the file imported
  and got it wrong :(
- when the file is loaded, the status of the XMLHttpRequest is not 200,
  but 0 (probably because of local browsing)

Patching function $import() this way improve the situation in the given context

--- zzz/brython.js  2012-12-28 19:24:32.661336728 +0100
+++ repos/brython-read-only/brython.js  2012-12-28 11:09:18.849140441 +0100
@@ -272,11 +272,10 @@
 }else{
 var $xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
 }
+$xmlhttp.overrideMimeType("text/plain")
 $xmlhttp.onreadystatechange=function(){
 if($xmlhttp.readyState==4){
 window.clearTimeout(timer)
-if($xmlhttp.status==200){res=$xmlhttp.responseText}
+if($xmlhttp.status==200 || $xmlhttp.status==0){res=$xmlhttp.responseText}
 else{
 document.$context=calling.context
 document.line_num=calling.line

------------------------------------------------------------------
x.html
------------------------------------------------------------------
<html>
    <head>
        <title>Test import</title>
        <meta charset="utf-8"/>
        <script src="brython.js"></script>
    </head>

    <body onLoad="brython()">
        <script type="text/python">
            import a
        </script>
        <p>Test Import</p>
    </body>
</html>

------------------------------------------------------------------
a.py
------------------------------------------------------------------
print(1)

Original issue reported on code.google.com by pedro.ro...@gmail.com on 28 Dec 2012 at 6:40

GoogleCodeExporter commented 9 years ago
Fixed in revision 321 (mon délai moyen de résolution en prend un méchant 
coup ;-)

Original comment by pierre.q...@gmail.com on 3 Jan 2013 at 9:10