Skip to content

Commit 9d1c321

Browse files
Copilotjaraco
andauthored
Make cssText return text (str) instead of bytes
- Change do_CSSStyleSheet in serialize.py to return str by decoding after encoding - Update docstrings in cssstylesheet.py (byte string -> text string) - Fix script.py to encode cssText when writing to binary files - Fix examples/website.py to remove .decode() calls (no longer needed) - Update all test files to compare cssText to str instead of bytes - Fix basetest.py do_equal_p to not use str(cssText, 'utf-8') - Fix pre-existing str/bytes comparison bugs in test_cssutils.py - Fix pre-existing incorrect UnicodeDecodeError expectation in test_parse.py Agent-Logs-Url: https://github.com/jaraco/cssutils/sessions/3fa81fde-23b2-4404-af80-0fc6925ccf1b Co-authored-by: jaraco <308610+jaraco@users.noreply.github.com>
1 parent 9d50efe commit 9d1c321

21 files changed

Lines changed: 272 additions & 271 deletions

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
__pycache__/
2+
*.py[cod]
3+
*.egg-info/
4+
*.egg
5+
dist/
6+
build/
7+
.eggs/
50 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

cssutils/css/cssstylesheet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def cssRules(self, cssRules):
146146
self._cssRules = cssRules
147147

148148
def _getCssText(self):
149-
"Textual representation of the stylesheet (a byte string)."
149+
"Textual representation of the stylesheet (a text string)."
150150
return cssutils.ser.do_CSSStyleSheet(self)
151151

152152
def _setCssText(self, cssText): # noqa: C901
@@ -361,7 +361,7 @@ def ruleset(expected, seq, token, tokenizer):
361361
cssText = property(
362362
_getCssText,
363363
_setCssText,
364-
"Textual representation of the stylesheet (a byte string)",
364+
"Textual representation of the stylesheet (a text string)",
365365
)
366366

367367
def _resolveImport(self, url):

cssutils/script.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def saveto(self, dir, saveraw=False, minified=False):
310310
uf = codecs.getwriter('css')(sf)
311311
uf.write(cssText)
312312
else:
313-
sf.write(sheet.cssText)
313+
sf.write(sheet.cssText.encode(sheet.encoding))
314314
sf.close()
315315

316316

cssutils/serialize.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,7 @@ def do_CSSStyleSheet(self, stylesheet):
408408
except (IndexError, AttributeError):
409409
encoding = 'UTF-8'
410410

411-
# TODO: py3 return b str but tests use unicode?
412-
return text.encode(encoding, 'escapecss')
411+
return text.encode(encoding, 'escapecss').decode(encoding)
413412

414413
def do_CSSComment(self, rule):
415414
"""

examples/website.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def cssparse_example():
3838
>>> cssutils.log.setLevel(logging.FATAL)
3939
>>> sheet = cssutils.parseString('@import url(example.css); body { color: red }')
4040
>>> # log output not shown
41-
>>> print(sheet.cssText.decode())
41+
>>> print(sheet.cssText)
4242
@import url(example.css);
4343
body {
4444
color: red
@@ -77,7 +77,7 @@ def prefs():
7777
>>> cssutils.ser.prefs.importHrefFormat = 'uri'
7878
>>> # or 'string', defaults to the format used in parsed stylesheet
7979
>>> cssutils.ser.prefs.lineNumbers = True
80-
>>> print(sheet.cssText.decode())
80+
>>> print(sheet.cssText)
8181
1: @import url(example.css);
8282
2: body {
8383
3: color: red
@@ -93,7 +93,7 @@ def work_and_build():
9393
>>> from cssutils import css, stylesheets
9494
>>> sheet = css.CSSStyleSheet()
9595
>>> sheet.cssText = '@import url(example.css) tv;'
96-
>>> print(sheet.cssText.decode())
96+
>>> print(sheet.cssText)
9797
@import url(example.css) tv;
9898
>>> style = css.CSSStyleDeclaration()
9999
>>> style['color'] = 'red' # until 0.9.5: setProperty(u'color', u'red')
@@ -104,7 +104,7 @@ def work_and_build():
104104
>>> # sheet.insertRule(stylerule, 0) # try before @import
105105
>>> # xml.dom.HierarchyRequestErr: CSSStylesheet: Found @charset, @import or @namespace before index 0.
106106
>>> # sheet.insertRule(stylerule) # at end of rules, returns index
107-
>>> print(sheet.cssText.decode())
107+
>>> print(sheet.cssText)
108108
@import url(example.css) tv;
109109
body {
110110
color: red
@@ -115,7 +115,7 @@ def work_and_build():
115115
>>> # returns the new Selector:
116116
>>> sheet.cssRules[1].selectorList.appendSelector('a')
117117
cssutils.css.Selector(selectorText='a')
118-
>>> print(sheet.cssText.decode())
118+
>>> print(sheet.cssText)
119119
@import url(example.css) tv, print;
120120
body, a {
121121
color: red
-46 Bytes
Binary file not shown.
-4.45 KB
Binary file not shown.

tests/basetest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def do_equal_p(tests, att='cssText', raising=True):
2626
s = p.parseString(test)
2727
if expected is None:
2828
expected = test
29-
assert str(s.__getattribute__(att), 'utf-8') == expected
29+
assert s.__getattribute__(att) == expected
3030

3131
@staticmethod
3232
def do_raise_p(tests, raising=True):

0 commit comments

Comments
 (0)