wxpython

Discussion in 'Python' started by joe jacob, Jan 23, 2008.

  1. joe jacob

    joe jacob Guest

    I am trying to open a file containing non displayable characters like
    contents an exe file. The is is with the below mentioned code:

    self.text_ctrl_1.SetValue(file_content)

    If the file_content contains non displayable characters I am getting
    an error like this:

    Traceback (most recent call last):
    File "C:\Documents and Settings\joe_jacob\Desktop\notepad.py", line
    102, in open_file
    self.text_ctrl_1.SetValue(file_content)
    File "D:\softwares\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx
    \_controls.py", line 1708, in SetValue
    return _controls_.TextCtrl_SetValue(*args, **kwargs)
    File "D:\softwares\Python25\lib\encodings\cp1252.py", line 15, in
    decode
    return codecs.charmap_decode(input,errors,decoding_table)
    UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position
    2: character maps to <undefined>

    I am trying to create an encryption program so if I open any file even
    if it is an exe file it should display in text ctrl.

    What is this problem with this ? How can I rectify this?
     
    joe jacob, Jan 23, 2008
    #1
    1. Advertising

  2. joe jacob

    Tim Golden Guest

    joe jacob wrote:
    > I am trying to open a file containing non displayable characters like
    > contents an exe file. The is is with the below mentioned code:
    >
    > self.text_ctrl_1.SetValue(file_content)
    >
    > If the file_content contains non displayable characters I am getting
    > an error like this:
    >
    > Traceback (most recent call last):
    > File "C:\Documents and Settings\joe_jacob\Desktop\notepad.py", line
    > 102, in open_file
    > self.text_ctrl_1.SetValue(file_content)
    > File "D:\softwares\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx
    > \_controls.py", line 1708, in SetValue
    > return _controls_.TextCtrl_SetValue(*args, **kwargs)
    > File "D:\softwares\Python25\lib\encodings\cp1252.py", line 15, in
    > decode
    > return codecs.charmap_decode(input,errors,decoding_table)
    > UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position
    > 2: character maps to <undefined>
    >
    > I am trying to create an encryption program so if I open any file even
    > if it is an exe file it should display in text ctrl.
    >
    > What is this problem with this ? How can I rectify this?


    If I may be permitted a bit of levity at your expense: you're asking
    how to display "non displayable" characters! The most serious answer
    I can give is: how do you *want* to display those characters? What
    do you *expect* to appear in the text control.

    wxPython is trying to interpret your byte stream as a Unicode
    text stream encoded as cp1252. But it's not, so it gives up
    in a heap. One solution is to pass the repr of file_content.
    Another solution is for you to prefilter the text, replacing
    non-printables by their hex value or by some marker. Not much
    in it, really.

    <code>
    import random
    file_content = "".join (
    chr (random.randint (0, 255)) for i in range (1000)
    )
    munged_text = "".join (
    c if 32 <= ord (c) <= 126 else hex (ord (c)) for c in file_content
    )

    print repr (file_content)
    print munged_text
    </code>

    TJG
     
    Tim Golden, Jan 23, 2008
    #2
    1. Advertising

  3. joe jacob

    Tim Golden Guest

    [Tim Golden]
    >> wxPython is trying to interpret your byte stream as a Unicode
    >> text stream encoded as cp1252. But it's not, so it gives up
    >> in a heap. One solution is to pass the repr of file_content.
    >> Another solution is for you to prefilter the text, replacing
    >> non-printables by their hex value or by some marker. Not much
    >> in it, really.
    >>
    >> <code>
    >> import random
    >> file_content = "".join (
    >> chr (random.randint (0, 255)) for i in range (1000)
    >> )
    >> munged_text = "".join (
    >> c if 32 <= ord (c) <= 126 else hex (ord (c)) for c in file_content
    >> )
    >>
    >> print repr (file_content)
    >> print munged_text
    >> </code>
    >>
    >> TJG


    [joe jacob]
    > If I open an exe file in notepad, I can see some junk characters. I'm
    > trying to develop a program like an editor which can encrypt a file
    > opened by it. I need to open files like exe or jpeg etc in the editor
    > and after encryption the encrypted data should be displayed in the
    > editor. I developed the editor but when I tried to open an windows exe
    > file in it I am getting the above mentioned error as the characters
    > contained in it are non unicode.


    Hi, Joe. I've copied this back to the list (and I encourage you to
    do the same when replying) since the more people see the issue,
    the more chances you've got of a useful answer!

    To try to address what you're saying here: notepad.exe makes some
    choice or other when confronted by the byte stream in a file which
    you open. I don't know what that choice is, or how it tries to cope
    with encoded unicode text, but whatever it does by the choice of
    its developers. The "some junk characters" are one of several
    possible representations of the not-ordinary-characters which your
    file contains.

    If you're writing your own editor or file display, you have to make
    similar choices, which includes understanding what possibilities are
    offered by the toolset you're employing -- in this case, wxPython.

    <slight guesswork - I haven't checked the source>
    The wxPython wx.TextCtrl expects to be fed with Unicode text. If
    you pass it a string instead, it tries to decode it according to
    the system's default encoding, here cp1252. If it can't, it doesn't
    display "junk characters": it just gives an error.
    </slight guesswork>

    If you want junk characters, then you'll either have to explicitly
    pass in the characters you want displayed or do the kind of thing
    I suggested above to indicate their hex value, or find another
    control (or an option of the wx.TextCtrl) which will attempt to do
    the work for you when displaying raw bytes.

    I'm afraid I'm not a wxPython expert so maybe someone else has
    suggestions. But the most important thing is that you understand
    what's happening here and why you can't just say "I want it to
    do what Notepad does".

    TJG
     
    Tim Golden, Jan 24, 2008
    #3
  4. joe jacob

    joe jacob Guest

    On Jan 24, 4:20 pm, Tim Golden <> wrote:
    > [Tim Golden]
    >
    >
    >
    > >> wxPython is trying to interpret your byte stream as a Unicode
    > >> text stream encoded as cp1252. But it's not, so it gives up
    > >> in a heap. One solution is to pass the repr of file_content.
    > >> Another solution is for you to prefilter the text, replacing
    > >> non-printables by their hex value or by some marker. Not much
    > >> in it, really.

    >
    > >> <code>
    > >> import random
    > >> file_content = "".join (
    > >> chr (random.randint (0, 255)) for i in range (1000)
    > >> )
    > >> munged_text = "".join (
    > >> c if 32 <= ord (c) <= 126 else hex (ord (c)) for c in file_content
    > >> )

    >
    > >> print repr (file_content)
    > >> print munged_text
    > >> </code>

    >
    > >> TJG

    >
    > [joe jacob]
    >
    > > If I open an exe file in notepad, I can see some junk characters. I'm
    > > trying to develop a program like an editor which can encrypt a file
    > > opened by it. I need to open files like exe or jpeg etc in the editor
    > > and after encryption the encrypted data should be displayed in the
    > > editor. I developed the editor but when I tried to open an windows exe
    > > file in it I am getting the above mentioned error as the characters
    > > contained in it are non unicode.

    >
    > Hi, Joe. I've copied this back to the list (and I encourage you to
    > do the same when replying) since the more people see the issue,
    > the more chances you've got of a useful answer!
    >
    > To try to address what you're saying here: notepad.exe makes some
    > choice or other when confronted by the byte stream in a file which
    > you open. I don't know what that choice is, or how it tries to cope
    > with encoded unicode text, but whatever it does by the choice of
    > its developers. The "some junk characters" are one of several
    > possible representations of the not-ordinary-characters which your
    > file contains.
    >
    > If you're writing your own editor or file display, you have to make
    > similar choices, which includes understanding what possibilities are
    > offered by the toolset you're employing -- in this case, wxPython.
    >
    > <slight guesswork - I haven't checked the source>
    > The wxPython wx.TextCtrl expects to be fed with Unicode text. If
    > you pass it a string instead, it tries to decode it according to
    > the system's default encoding, here cp1252. If it can't, it doesn't
    > display "junk characters": it just gives an error.
    > </slight guesswork>
    >
    > If you want junk characters, then you'll either have to explicitly
    > pass in the characters you want displayed or do the kind of thing
    > I suggested above to indicate their hex value, or find another
    > control (or an option of the wx.TextCtrl) which will attempt to do
    > the work for you when displaying raw bytes.
    >
    > I'm afraid I'm not a wxPython expert so maybe someone else has
    > suggestions. But the most important thing is that you understand
    > what's happening here and why you can't just say "I want it to
    > do what Notepad does".
    >
    > TJG


    Thanks for the information. I'll try to manage it some how.
     
    joe jacob, Jan 24, 2008
    #4
  5. joe jacob

    Tim Golden Guest

    [... snip stuff from TJG ...]

    joe jacob wrote:
    > Thanks for the information. I'll try to manage it some how.


    If you haven't already, try posting to a wxPython or wxWidgets
    mailing list; maybe someone's already done this kind of thing?

    TJG
     
    Tim Golden, Jan 24, 2008
    #5
  6. On Jan 24, 5:37 am, Tim Golden <> wrote:
    > [... snip stuff from TJG ...]
    >
    > joe jacob wrote:
    > > Thanks for the information. I'll try to manage it some how.

    >
    > If you haven't already, try posting to a wxPython or wxWidgets
    > mailing list; maybe someone's already done this kind of thing?
    >
    > TJG


    I'm not able to think of anything offhand that's in wxPython, but I
    seem to recall someone trying to display a language even if the
    language wasn't installed on the PC. The OP might find the
    internationalization techniques useful for this application.

    Here's some wiki entries on that topic:

    http://wiki.wxpython.org/RecipesI18n
    http://wiki.wxpython.org/Internationalization


    The newest version of wxPython comes with Editra, which is a fairly
    advanced text editor written with wxPython. It won't open exes or
    pictures, but most files that are in plain text it opens with no
    problem. Editra has its own site: http://editra.org/

    The mailing list for wxPython can be found here: http://wxpython.org/maillist.php

    Mike
     
    Mike Driscoll, Jan 24, 2008
    #6
    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. Anand
    Replies:
    1
    Views:
    357
    Cliff Wells
    Jul 23, 2003
  2. ulysses
    Replies:
    4
    Views:
    768
    Werner Schiendl
    Oct 22, 2003
  3. Logan
    Replies:
    5
    Views:
    765
    David Bolen
    Dec 11, 2003
  4. Ruben Charles
    Replies:
    6
    Views:
    501
    =?ISO-8859-1?Q?Michael_Str=F6der?=
    Oct 25, 2005
  5. Robin Dunn
    Replies:
    0
    Views:
    519
    Robin Dunn
    Mar 28, 2006
Loading...

Share This Page