Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > wxpython

Reply
Thread Tools

wxpython

 
 
joe jacob
Guest
Posts: n/a
 
      01-23-2008
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?
 
Reply With Quote
 
 
 
 
Tim Golden
Guest
Posts: n/a
 
      01-23-2008
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
 
Reply With Quote
 
 
 
 
Tim Golden
Guest
Posts: n/a
 
      01-24-2008
[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
 
Reply With Quote
 
joe jacob
Guest
Posts: n/a
 
      01-24-2008
On Jan 24, 4:20 pm, Tim Golden <(E-Mail Removed)> 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.
 
Reply With Quote
 
Tim Golden
Guest
Posts: n/a
 
      01-24-2008
[... 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
 
Reply With Quote
 
Mike Driscoll
Guest
Posts: n/a
 
      01-24-2008
On Jan 24, 5:37 am, Tim Golden <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[wxPython-users] ANNOUNCE: wxPython 2.6.3.0 Robin Dunn Python 0 03-28-2006 06:03 PM
[wxPython-users] Web based applications are possible with wxPython? Ruben Charles Python 6 10-25-2005 09:41 PM
wxPython - wx package (new style wxPython?) Logan Python 5 12-11-2003 04:12 PM
[PY GUI] interest function in python GUI(wxpython,pyqt) program.wxpython,pyqt ulysses Python 4 10-22-2003 03:28 PM
wxPython looses function "wxPython.wx.miscc" Anand Python 1 07-23-2003 01:59 AM



Advertisments