wxpython

J

joe jacob

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?
 
T

Tim Golden

joe said:
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
 
T

Tim Golden

[Tim Golden]
[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
 
J

joe jacob

[Tim Golden]



[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.
 
T

Tim Golden

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

joe said:
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
 
M

Mike Driscoll

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

joe said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top