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

allow setting encoding in open_resource() #5504

Closed
liffiton opened this issue Jun 20, 2024 · 0 comments · Fixed by #5526
Closed

allow setting encoding in open_resource() #5504

liffiton opened this issue Jun 20, 2024 · 0 comments · Fixed by #5526
Milestone

Comments

@liffiton
Copy link

This is a duplicate of #1740 — that may have been closed for lack of a clear rationale, however, and I'd like to suggest it again with the following reasoning.

The documentation currently gives this example for using open_resource():

with app.open_resource("schema.sql") as f:
    conn.executescript(f.read())

On Windows, however, this can fail to open a file encoded in UTF-8, which most are these days, and safer code looks like this:

with app.open_resource("schema.sql", mode="rb") as f:
   conn.executescript(f.read().decode("utf-8"))  # type: ignore [attr-defined]

(The type comment is needed to prevent mypy from complaining about f.read() possibly being a string with no .decode() method, as it can't tell that the file was opened in 'rb' mode.)

It would be cleaner and more flexible to be able to write:

with app.open_resource("schema.sql", encoding="utf-8") as f:
   conn.executescript(f.read())
@davidism davidism changed the title Request: allow setting encoding in open_resource() allow setting encoding in open_resource() Jun 20, 2024
@davidism davidism added this to the 3.1.0 milestone Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants