Ensure bytestring is bytes before checking for gzip header#443
Merged
liZe merged 1 commit intoKozea:mainfrom May 15, 2025
Merged
Ensure bytestring is bytes before checking for gzip header#443liZe merged 1 commit intoKozea:mainfrom
liZe merged 1 commit intoKozea:mainfrom
Conversation
Related error: TypeError: startswith first arg must be str or a tuple of str, not bytes
Contributor
Author
Member
|
Hi! This is probably already fixed in 2.8.1, did you try the latest version? |
Contributor
Author
Yes, I tested it with the latest version (2.8.1), and the issue still occurs. |
Member
Could you please share a code sample that shows your problem? |
Contributor
Author
|
I was able to reproduce it with this small example import sys
import cairosvg
print(f"Python version: {sys.version}")
print(f"CairoSVG version: {cairosvg.__version__}")
svg_str = (
"<svg width='100' height='100'><rect width='100' height='100'/></svg>"
)
cairosvg.svg2png(bytestring=svg_str)Output: Python version: 3.11.9 (main, Apr 10 2024, 13:16:36) [GCC 13.2.0]
CairoSVG version: 2.8.1
Traceback (most recent call last):
File "test.py", line 12, in <module>
cairosvg.svg2png(bytestring=svg_str)
File "/home/metal/.virtualenvs/thumbor/lib/python3.11/site-packages/cairosvg/__init__.py", line 54, in svg2png
return surface.PNGSurface.convert(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/metal/.virtualenvs/thumbor/lib/python3.11/site-packages/cairosvg/surface.py", line 129, in convert
tree = Tree(
^^^^^
File "/home/metal/.virtualenvs/thumbor/lib/python3.11/site-packages/cairosvg/parser.py", line 390, in __init__
if bytestring.startswith(b'\x1f\x8b'):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: startswith first arg must be str or a tuple of str, not bytes |
Member
Thanks. The Let’s fix that to avoid breaking other libraries! |
Contributor
Author
|
Thank you @liZe ! On my side, I’ll make sure to fix it so that it only passes bytes — not Unicode strings. Thanks again! |
marcelometal
added a commit
to thumbor/thumbor
that referenced
this pull request
May 31, 2025
More info at: Kozea/CairoSVG#443
marcelometal
added a commit
to thumbor/thumbor
that referenced
this pull request
Jun 11, 2025
More info at: Kozea/CairoSVG#443
marcelometal
added a commit
to thumbor/thumbor
that referenced
this pull request
Jun 11, 2025
More info at: Kozea/CairoSVG#443
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes a TypeError when checking if input is gzipped using
bytestring.startswith(b"\x1f\x8b").This patch ensures
bytestringis properly encoded asbytesbefore gzip header detection.