`open` does not open files from a virtual file system
See original GitHub issueTest file system
__BRYTHON__.add_files({
"hello.txt": {
"content": "aGVsbG8gaXMgdGhlIHdheSB0byBnbwo=\n",
"ctime": 1634418826.57,
"mtime": 1634418828.0
}
})
Test code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="brython.js"></script>
<script type="text/javascript" src="hello.vfs.js"></script>
</head>
<body onload="brython(1)">
<script type="text/python">
hfile = open('hello.txt', 'r')
print(hfile)
hdata = hfile.read()
print(hdata)
</script>
</body>
</html>
And the output in the console is:
Javascript error TypeError: Cannot read properties of undefined (reading 'length')
at $url_open (py_builtin_functions.js:2993)
at eval (eval at $B.loop (loaders.js:378), <anonymous>:15:51)
at Object.$B.loop (loaders.js:378)
at $B.run_script (py2js.js:12939)
at Object.$B.loop (loaders.js:398)
at $B.parser._run_scripts (py2js.js:13133)
at $B.parser.brython (py2js.js:12878)
at onload ((index):48)
py_exceptions.js:697 test is js exc is recursion error TypeError: Cannot read properties of undefined (reading 'length')
at $url_open (py_builtin_functions.js:2993)
at eval (eval at $B.loop (loaders.js:378), <anonymous>:15:51)
at Object.$B.loop (loaders.js:378)
at $B.run_script (py2js.js:12939)
at Object.$B.loop (loaders.js:398)
at $B.parser._run_scripts (py2js.js:13133)
at $B.parser.brython (py2js.js:12878)
at onload ((index):48) TypeError: Cannot read properties of undefined (reading 'length')
py_exceptions.js:160 print stack ok []
py_utils.js:1106 Traceback (most recent call last):
RuntimeError: TypeError: Cannot read properties of undefined (reading 'length')
The problem, a long guess, seems to be code that wasn’t udpated for a file found in $B.files
here:
https://github.com/brython-dev/brython/blob/33aeaab551f1b73209326c5a0aecf98642d4c126/www/src/py_builtin_functions.js#L2909-L2919
because the other cases put the result in result.content
, which is not happening here
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Virtual files won't open on Ubuntu 20.04.2 LTS - Desktop
I selected virtual file system at install. It synced and created folders with meta data .owncloud files. They are not linked to ownCloud....
Read more >Powering on a virtual machine fails with the error
The error Failed to lock the file means that another process is opening the file and the process you are using cannot open...
Read more >Unable to open files in virtual drive [Windows] #1056 - GitHub
The virtual drive does not seem to work properly. I can navigate in the server tree, but I can not open any file....
Read more >Fix File Explorer if it won't open or start - Microsoft Support
Under Virtual memory, select Change and make sure that Automatically manage paging file sizes for all drives is selected. Select OK, then select...
Read more >Opening a file with Python in the browser [duplicate]
The file /home/pyodide/file.json is located within the browser virtual file system. That file system is not part of your local disk storage.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think the feature is useful. And a small extension would make it more useful
The current logic (simpflied)
$B.files
if an entry is available therefile
=> launch anXMLHttpRequest
As a user I may want to
open
a file without going to the network. If the file is not available inside$B.files
I may want to simply stop there.For that use case I see two potential implementation options
Extra keyword-argument for
open
. Something like:_only_local
which is aboolean
with default valueFalse
to avoid altering the current behavior If the file is not in$B.files
eitherNone
can be returned or anException
(FileNotFound
) be raisedExpose a function which allows testing for the presence of a an entry in
$B.files
Maybe add the ability to specify the workings of it- such as if the programmer wanted to set up their own virtual file system, they could provide brython with a custom
open
class.