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(filename, "rt") in textual.css.stylesheet.Stylesheet.read should be open(filename, "rt", encoding="utf-8") #5317

Open
python-and-fiction opened this issue Dec 2, 2024 · 1 comment

Comments

@python-and-fiction
Copy link

After I write chinese comments in CSS file which is encoded in utf-8, my program won't work.
It outputs:
image
I noticed that open(filename, "rt") in textual.css.stylesheet.Stylesheet.read without encoding, and it use cp936 as encoding by default.
So I write a patch in my program:

import os
from textual.css.stylesheet import Stylesheet, CssSource
from pathlib import PurePath
from textual.css.errors import StylesheetError
def read(self, filename: str | PurePath) -> None:
    """Read Textual CSS file.

    Args:
        filename: Filename of CSS.

    Raises:
        StylesheetError: If the CSS could not be read.
        StylesheetParseError: If the CSS is invalid.
    """
    filename = os.path.expanduser(filename)
    try:
        with open(filename, "rt", encoding="utf-8") as css_file:
            css = css_file.read()
        path = os.path.abspath(filename)
    except Exception:
        raise StylesheetError(f"unable to read CSS file {
                              filename!r}") from None
    self.source[(str(path), "")] = CssSource(css, False, 0)
    self._require_parse = True
Stylesheet.read = read

Maybe maintainer can change it.
Is there any problem?

Copy link

github-actions bot commented Dec 2, 2024

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

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

No branches or pull requests

1 participant