Skip to content

Conversation

@marcelometal
Copy link
Contributor

@marcelometal marcelometal commented May 14, 2025

Fixes a TypeError when checking if input is gzipped using bytestring.startswith(b"\x1f\x8b").

File ".../site-packages/cairosvg/parser.py", line 447, in __init__
    if bytestring.startswith(b"\x1f\x8b"):
TypeError: startswith first arg must be str or a tuple of str, not bytes

This patch ensures bytestring is properly encoded as bytes before gzip header detection.

Related error:
TypeError: startswith first arg must be str or a tuple of str, not bytes
@marcelometal
Copy link
Contributor Author

Hi @liZe ,

First of all, thanks for maintaining this great library.

This used to work in 2.7.1 but now breaks downstream code, for example in projects like Thumbor.

Would appreciate a review when possible. Thanks!

@liZe
Copy link
Member

liZe commented May 14, 2025

Hi!

This is probably already fixed in 2.8.1, did you try the latest version?

@marcelometal
Copy link
Contributor Author

Hi!

This is probably already fixed in 2.8.1, did you try the latest version?

Yes, I tested it with the latest version (2.8.1), and the issue still occurs.

@liZe
Copy link
Member

liZe commented May 14, 2025

Yes, I tested it with the latest version (2.8.1), and the issue still occurs.

Could you please share a code sample that shows your problem?

@marcelometal
Copy link
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

@liZe
Copy link
Member

liZe commented May 15, 2025

I was able to reproduce it with this small example

Thanks. The bytestring argument was only supposed to accept… bytes, and not Unicode strings 😄.

Let’s fix that to avoid breaking other libraries!

@liZe liZe merged commit b4670ec into Kozea:main May 15, 2025
2 checks passed
@marcelometal
Copy link
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
marcelometal added a commit to thumbor/thumbor that referenced this pull request Jun 11, 2025
marcelometal added a commit to thumbor/thumbor that referenced this pull request Jun 11, 2025
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 this pull request may close these issues.

2 participants