Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

open_resource() does not allow mode='rt' #3163

Closed
goodmami opened this issue Apr 25, 2019 · 2 comments
Closed

open_resource() does not allow mode='rt' #3163

goodmami opened this issue Apr 25, 2019 · 2 comments
Milestone

Comments

@goodmami
Copy link

@goodmami goodmami commented Apr 25, 2019

In Python 3, mode='rt' is synonymous with mode='r' but Flask disallows it thinking it is not a valid mode for reading. While this is true in Python 2, Flask could (at least) provide a more meaningful error message, if not just allow it.

A simple solution might change these lines in flask/helpers.py:

    def open_resource(self, resource, mode='rb'):
        """..."""  # truncated for example
        if mode not in ('r', 'rb'):
            raise ValueError('Resources can only be opened for reading')
        return open(os.path.join(self.root_path, resource), mode)

...to...

    def open_resource(self, resource, mode='rb'):
        """..."""
        if mode == 'rt':
            mode = 'r'
        if mode not in ('r', 'rb'):
            raise ValueError('Resources can only be opened for reading')
        return open(os.path.join(self.root_path, resource), mode)

A heavier solution might use io.open() (and also allow encoding, but see #1740):

    def open_resource(self, resource, mode='rb', encoding=None):
        """..."""
        if mode not in ('r', 'rb', 'rt'):
            raise ValueError('Resources can only be opened for reading')
        return io.open(os.path.join(self.root_path, resource), mode=mode, encoding=encoding)

Expected Behavior

Calling open_resource() with mode='rt' should open a file in text mode (at least in Python 3).

from flask import Flask

app = Flask(__name__)
with app.open_resource('README.md', mode='rt') as f:
    print(f.read())

Actual Behavior

A misleading exception is raised.

Traceback (most recent call last):
  File "mwe.py", line 5, in <module>
    with app.open_resource('README.md', mode='rt') as f:
  File "/home/goodmami/repos/OMW/py3env/lib/python3.6/site-packages/flask/helpers.py", line 1002, in open_resource
    raise ValueError('Resources can only be opened for reading')
ValueError: Resources can only be opened for reading

Environment

  • Python version: 3.6.7
  • Flask version: 1.0.2
  • Werkzeug version: 0.14.1
@seawolf42
Copy link
Contributor

@seawolf42 seawolf42 commented May 6, 2019

Starting work on this now.

Loading

@icreatedanaccount
Copy link
Contributor

@icreatedanaccount icreatedanaccount commented May 6, 2019

Loading

@davidism davidism added this to the 1.1.0 milestone May 17, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

4 participants