question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`open` does not open files from a virtual file system

See original GitHub issue

Test 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:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
anpylarcommented, Oct 19, 2021

I think the feature is useful. And a small extension would make it more useful

The current logic (simpflied)

  • Open from $B.files if an entry is available there
  • if not and the protocol is not file => launch an XMLHttpRequest

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 a boolean with default value False to avoid altering the current behavior If the file is not in $B.files either None can be returned or an Exception (FileNotFound) be raised

  • Expose a function which allows testing for the presence of a an entry in $B.files

0reactions
Froggo8311commented, Mar 7, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found