minidom's setAttribute + UnicodeDecodeError

Discussion in 'Python' started by Ruslan, Sep 7, 2004.

  1. Ruslan

    Ruslan Guest

    Hi, everybody.
    In this excerpt of code

    enc = 'some_type_of_encoding'

    def _encode(v):
    if isinstance(v, UnicodeType):
    v = v.encode(v)
    return v

    .....
    node.setAttribute('style:name', _encode(value))
    .....

    i get UnicodeDecodeError:
    ------------------------------------------------------------
    Traceback (most recent call last):
    File "stnreplace.py", line 107, in ?
    StylesHelper(fname).replace(trdict)
    File "stnreplace.py", line 63, in replace
    node.setAttribute('style:name', _encode(uval))
    File "/usr/local/lib/python2.3/site-packages/_xmlplus/dom/minidom.py",
    line 704, in setAttribute
    elif value != attr.value:
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 14:
    ordinal not in range(128)
    ------------------------------------------------------------

    The value passed to setAttribute is utf8 string, i tried with different
    encodings, but no luck.
    Could somebody give any suggestion how to solve that? Seems problem is
    in 'ascii' codec in minidom, but how to make it handle not just ascii?
    TIA

    P.S. Tested with python2.3.3 & python2.3.4

    Best regards,
    Ruslan
     
    Ruslan, Sep 7, 2004
    #1
    1. Advertising

  2. Ruslan wrote:
    > def _encode(v):
    > if isinstance(v, UnicodeType):
    > v = v.encode(v)
    > return v
    >
    > ....
    > node.setAttribute('style:name', _encode(value))
    > ....

    [...]
    > Could somebody give any suggestion how to solve that? Seems problem is
    > in 'ascii' codec in minidom, but how to make it handle not just ascii?


    The problem is in your code. node.setAttribute requires both the
    attribute name and the attribute value to be Unicode objects, as
    per the DOM spec.

    For backwards-compatibility, ease-of-use, and performance reasons,
    it does not actually check that these are Unicode objects, and it
    will work with byte strings just fine as long as they are ASCII.
    But this would still be an error in the application, which really
    needs to pass Unicode objects.

    IOW: just remove the _encode call, and all will be fine.

    Regards,
    Martin
     
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Sep 7, 2004
    #2
    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:
    2,898
  2. Joerg Gippert

    ServletRequest.setAttribute question

    Joerg Gippert, Jul 30, 2003, in forum: Java
    Replies:
    2
    Views:
    3,148
    Joerg Gippert
    Jul 31, 2003
  3. Steve Peake

    Problem with get/setAttribute

    Steve Peake, May 5, 2004, in forum: Java
    Replies:
    3
    Views:
    462
    Arvind
    May 9, 2004
  4. Matt
    Replies:
    7
    Views:
    14,829
    John C. Bollinger
    Nov 8, 2004
  5. Replies:
    3
    Views:
    552
    Stefan Behnel
    Aug 3, 2007
Loading...

Share This Page