[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

joe jacob

1/23/2008 11:31:00 AM

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?
6 Answers

Tim Golden

1/23/2008 11:50:00 AM

0

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

1/24/2008 11:21:00 AM

0

[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

joe jacob

1/24/2008 11:30:00 AM

0

On Jan 24, 4:20 pm, Tim Golden <m...@timgolden.me.uk> 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.

Tim Golden

1/24/2008 11:38:00 AM

0

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

Mike Driscoll

1/24/2008 2:15:00 PM

0

On Jan 24, 5:37 am, Tim Golden <m...@timgolden.me.uk> 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/R...
http://wiki.wxpython.org/Internatio...


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://e...

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

Mike

The Revd

2/20/2014 3:23:00 AM

0

On Wed, 19 Feb 2014 11:59:02 -0800 (PST), NEMO
<brianlambsbigtoe@excite.com> wrote:

>Rere.... It not the first time the Finns kicked the Russkies in the arse....

Bumsky, the Russians have been rearing the Finns for YEARS!


>
>On Wednesday, February 19, 2014 2:44:49 PM UTC-5, Rere wrote:
>> Bwaa hah hah haa!
>>
>> http://www.telegraph.co.uk/sport/othersports/winter-olympics/10649343/Sochi-2014-Dismay-for-Vladimir-Putin-as-Russia-crash-out-of-Winter-Olympics-ice-hockey-tournament-to-Fi...
>>
>> Putin must be furious now, LOL!