mimetypes oddity

Discussion in 'Python' started by Sion Arrowsmith, Jan 15, 2009.

  1. >>> ge = mimetypes.guess_extension
    >>> ge('image/jpeg')

    '.jpe'
    >>> ge('image/jpeg')

    '.jpeg'
    >>>


    I actually discovered this through explicitly calling mimetypes.init
    to reload an edited mime.types file between calls to guess_extension,
    but I think the above scenario makes for more disturbing reading :cool:

    The problem is that mimetools initialises its type mapping dict
    before adding new type mappings from a file by iterating through the
    existing type mapping dict. This process is bootstrapped by a hard-
    coded dict which gets shadowed by the first file read, so the second
    file read uses a dict initialsed from the dict created by the first
    file read (got that?) which has a different iteration order to the
    hard-coded dict. So the mappings get added in a different order the
    second time around, and where there are multiple mappings like this
    you get a different answer.

    Is this a bug? If I want to be sure of a consistent result, do I
    need to use sorted(mimetools.guess_all_extensions(t))[0] (or [-1])?

    --
    \S -- -- http://www.chaos.org.uk/~sion/
    "Frankly I have no feelings towards penguins one way or the other"
    -- Arthur C. Clarke
    her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
    Sion Arrowsmith, Jan 15, 2009
    #1
    1. Advertising

  2. In article <>,
    <> wrote:
    > [mimetype weirdness reported]
    > Sion> Is this a bug?
    >Might be. Can you file a bug report in the Python issue tracker with a
    >small script that demonstrates the behavior?


    http://bugs.python.org/issue4963

    (It's tagged as being 2.4 and 2.5 because those are the versions I
    have to hand to confirm it on, but I'd be rather surprised if it's
    not still like this in 2.6.)

    --
    \S -- -- http://www.chaos.org.uk/~sion/
    "Frankly I have no feelings towards penguins one way or the other"
    -- Arthur C. Clarke
    her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
    Sion Arrowsmith, Jan 16, 2009
    #2
    1. Advertising

  3. Sion Arrowsmith

    Terry Reedy Guest

    Sion Arrowsmith wrote:
    > In article <>,
    > <> wrote:
    >> [mimetype weirdness reported]
    >> Sion> Is this a bug?
    >> Might be. Can you file a bug report in the Python issue tracker with a
    >> small script that demonstrates the behavior?

    >
    > http://bugs.python.org/issue4963
    >
    > (It's tagged as being 2.4 and 2.5 because those are the versions I
    > have to hand to confirm it on, but I'd be rather surprised if it's
    > not still like this in 2.6.)


    Trying to reproduce a bug, as this report requires, is easier if you
    give code that can be copy and pasted into the interpreter or even into
    an edit window (as with IDLE) to be run from there. (3.0 version)

    import mimetypes
    print(mimetypes.guess_extension('image/jpeg'))
    mimetypes.init()
    print(mimetypes.guess_extension('image/jpeg'))
    Terry Reedy, Jan 16, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    511
  2. GHUM
    Replies:
    0
    Views:
    251
  3. Aaron Gray

    navigator.mimeTypes

    Aaron Gray, Mar 9, 2007, in forum: Javascript
    Replies:
    3
    Views:
    189
    -Lost
    Mar 10, 2007
  4. Gelonida N
    Replies:
    0
    Views:
    258
    Gelonida N
    Sep 26, 2012
  5. Johannes Bauer
    Replies:
    10
    Views:
    97
    Asaf Las
    Feb 8, 2014
Loading...

Share This Page