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

mimetype guessing no longer working in python3.12 #40

Open
debbiedub opened this issue Jan 12, 2025 · 2 comments
Open

mimetype guessing no longer working in python3.12 #40

debbiedub opened this issue Jan 12, 2025 · 2 comments

Comments

@debbiedub
Copy link
Contributor

debbiedub commented Jan 12, 2025

The current mimetype guessing relies on mimetypes.guess_type throwing TypeError on bytes. In Python 3.12, it no longer does, it just returns that there is no match. Easily illustrated by the following command:

for p in 3.10 3.11 3.12; do docker run --rm python:$p python -c 'import mimetypes; print(mimetypes.guess_type(b"file.html", False))'; done

For pyFreenet3 this means that no files will get any default mime types set since guess_type is always called with a bytes argument first, in fcp3/node.py in the guessMimeType. The different result can be seen like this:

$ for p in 3.10 3.11 3.12; do docker run --rm -v $PWD/fcp3:/fcp3 -v $PWD/freenet3:/freenet3 python:$p python -c 'import fcp3.node; print(fcp3.node.guessMimetype(b"file.html"))'; done
text/html
text/html
application/octet-stream
$ 
@debbiedub
Copy link
Contributor Author

In python3.13 it looks like mimetypes.guess_type doesn't throw a TypeError but can handle bytes as opposed to strings so it is probably only 3.12 that is the problem.

@debbiedub
Copy link
Contributor Author

More specifically, the change to not throw TypeError seems to appear in python 3.12.3.

$ for p in 3.12.1 3.12.2 3.12.3 3.12.4 3.12.5 3.12.6 3.12.7 3.12.8 3.13.0 3.13.1; do docker run --rm -it python:$p python -c 'import mimetypes; import sys; print(sys.version, ":"); print(mimetypes.guess_type(b"file.html", False))'; done
3.12.1 (main, Feb  1 2024, 04:22:19) [GCC 12.2.0] :
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.12/mimetypes.py", line 304, in guess_type
    return _db.guess_type(url, strict)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/mimetypes.py", line 123, in guess_type
    scheme, url = urllib.parse._splittype(url)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/urllib/parse.py", line 1079, in _splittype
    match = _typeprog.match(url)
            ^^^^^^^^^^^^^^^^^^^^
TypeError: cannot use a string pattern on a bytes-like object
3.12.2 (main, Mar 12 2024, 11:02:14) [GCC 12.2.0] :
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.12/mimetypes.py", line 304, in guess_type
    return _db.guess_type(url, strict)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/mimetypes.py", line 123, in guess_type
    scheme, url = urllib.parse._splittype(url)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/urllib/parse.py", line 1079, in _splittype
    match = _typeprog.match(url)
            ^^^^^^^^^^^^^^^^^^^^
TypeError: cannot use a string pattern on a bytes-like object
3.12.3 (main, May 14 2024, 07:23:41) [GCC 12.2.0] :
(None, None)
3.12.4 (main, Aug  2 2024, 14:41:30) [GCC 12.2.0] :
(None, None)
3.12.5 (main, Sep  5 2024, 00:16:34) [GCC 12.2.0] :
(None, None)
3.12.6 (main, Sep 27 2024, 06:10:24) [GCC 12.2.0] :
(None, None)
3.12.7 (main, Dec  3 2024, 05:24:11) [GCC 12.2.0] :
(None, None)
3.12.8 (main, Dec 25 2024, 01:34:33) [GCC 12.2.0] :
(None, None)
3.13.0 (main, Dec  3 2024, 05:24:36) [GCC 12.2.0] :
('text/html', None)
3.13.1 (main, Dec 25 2024, 01:33:28) [GCC 12.2.0] :
('text/html', None)
$ 

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