Menu

About unicode

liyinghui
2004-04-08
2004-04-12
  • liyinghui

    liyinghui - 2004-04-08

    Yesterday I tried the unicode version of wxWindows. First, I tried to install the unicode version 2.4.2.4 in win98, but the wxWindows' demo crashed. I searched the mailinglist, it seems to be a bug about unicode in win98. Today, I tried the unicode version in win2000, it was ok. When I inputted some chinese word in DrPython, it showed right. As I saved text to a file, the file was created, but it was nothing in it. So I openned the dos command line, and rerunned the program in the command line. When I saved the file again, I found the error said that:
    Traceback (most recent call last):
       File "drpython.py", line 2318, in OnSave
         self.OnSaveAs(event)
       File "drpython.py", line 2336, in OnSaveAs
         self.SaveFile(not (old == self.filename))
       File "drpython.py", line 2862, in SaveFile
         cfile.write(self.txtDocument.GetText())
    UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-17: ordinal not in range(128)

    So I modified the code:
    cfile.write(self.txtDocument.GetText().encode('utf-8')), every thing was ok. I think that the wxStyledTextCtrl uses unicode encode internally. When you try to save the text to a file, It'll convert unicode to ascii in default mode. So python complains the error.

    Could you add a wxChoice widget in save file form, so that one can choice the encode of the saved file.

    BTW, the lastest version of wxWindows is 2.5.1.5, does  DrPython support it?

     
    • Daniel Pozmanter

      Oh you blessed blessed man!

      So THAT's how you do unicode!
      I will try my best to add unicode support to 2.4.0,
      and I will also add you to the credits (email me your full name, if you wish me to use that instead of your sourceforge login).

      I will probably add unicode thusly:  As a default option in preferences for how to encode files, and as an option under the Edit menu (Edit:Encoding).  (Messing with the save file form is a bit tricky, as it is built in.)

      In terms of wxWindows 2.5.1, the answer is yes and no.

      DrPython (2.4.0 CVS) has been changed to run properly on 2.5.1

      However 2.5.1 has not yet fully implemented the wxExecute function, which is essential to DrPython's function (running programs/python, etc).

      Once this has been addressed, DrPython should run fine on 2.5.1.

       
    • liyinghui

      liyinghui - 2004-04-08

      Oh, I'm very glade to hear that you will add me to the credits. My full name is liyinghui, you can use my nick name limodou, that's ok.

       
    • Daniel Pozmanter

      Which do you prefer I use?

      is liyinghui one word?

       
    • liyinghui

      liyinghui - 2004-04-08

      limodou is ok! Thanks.

       
    • Daniel Pozmanter

      Some unicode stuff is in cvs now.  Is there any harm in only saving with utf-8 encoding?

       
    • liyinghui

      liyinghui - 2004-04-10

      I think in the most cases, people is used to local encoding rather than utf-8 encoding. So if someone doesn't specify the encoding of the saved file, DrPython could use the default encoding(local encoding). If he specifies the encoding, DrPython could use that encoding to encode the file.

      And I have no ideas about how to find a local encoding.

       
    • liyinghui

      liyinghui - 2004-04-10

      I'v tried 2.4.0, and I found that if I used non-unicode version of wxPython, when I saved the file which had unicode chars in it, an exception was rosed:
      Traceback (most recent call last):
        File "drpython.py", line 2389, in OnSave
          self.OnSaveAs(event)
        File "drpython.py", line 2407, in OnSaveAs
          self.SaveFile(not (old == self.filename))
        File "drpython.py", line 2972, in SaveFile
          cfile.write(self.txtDocument.GetText().encode('utf-8'))
      UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 0: ordinal
      not in range(128)

      So I think when saving a file, you'd better judge which encoding is used in wxStyledTextContral, acording to the encoding type, use different method to encode the file. I modify the code and test it.

      if self.txtDocument.GetCodePage()==0:
          cfile.write(self.txtDocument.GetText())
      else:
          cfile.write(self.txtDocument.GetText().encode('utf-8'))

      The first line is to test which encoding type is used in wxStyledTextContral. If 0, ascii encoding is used. In this case doesn't do anything. Or you want to save the file in other encoding, you can convert the text to unicode first, then conver the text to specfied encoding. But you need to know which local encoding is used. For example, I typed something like "中国" with 'gb2312' encoding, I can convert them to 'utf-8' encoding.

      a="中国"
      b=unicode(a, 'gb2312')
      b.encode('utf-8')

      The result is '\xe4\xb8\xad\xe5\x9b\xbd'。It's ok。The previous code what I supplied is simple. If DrPython can permit one choice the encoding which he want to save file, the code could be more complex。But It may be like that:

      localencoding=self.getLocalEncoding() #get local encoding
      savedencoding=self.getSavedEncoding() #get saved encoding
      if self.txtDocument.GetCodePage()==0:
          cfile.write(unicode(self.txtDocument.GetText(), localencoding).encode(savedencoding))
      else:
          cfile.write(self.txtDocument.GetText().encode(savedencoding))

      I hope these will help you.

       
    • liyinghui

      liyinghui - 2004-04-10

      I found that you can use locale module to judge the default encoding.

      import locale
      localencoding=locale.getdefaultlocale()[1]

       
    • Daniel Pozmanter

      Thanks, I will add to prefs and the menu.
      Under prefs, "Default Locale"  will be the default option,
      with the ability to write in the encoding you wish
      ("ascii", "utf-8"), since the function seems to take encoding as a string.  I will use a try clause to protect the save as function, and give feedback.

      Now about using Chinese characters in the prompt.

      Does this work?  Does it crash DrPython?

      Thanks again for the help.
      Do you want to be added as a Developer to the project?

       
    • liyinghui

      liyinghui - 2004-04-11

      I don't know how to use chinese characters in the prompt. Could you tell me some ways how to test it? And my os is win98, so it'll be until Monday I can test it in win2000 as I'm at work.

      I know python but I'm not familar with wxPython。I'm very glad to be a member of developer. And I'll have chance to obtain your kindly help. Thanks.

       
    • Daniel Pozmanter

      Well. there are several ways.  You could copy and paste them from the main document, or you could open the Dynamic DrScript window, and type:

      DrPrompt.SetText('Python \xe6\x98\xaf\xe6\x9c\x80\xe5\xa5\xbd\xe7\x9a\x84\xe7\xb7\xa8\xe7\xa8\x8b\xe8\xaa\x9e\xe8\xa8\x80\xef\xbc\x81'.encode('utf-8'))

      You have been added as a developer.

       
    • liyinghui

      liyinghui - 2004-04-12

      Thanks, I'v seen that.

      I'v test the lastest code, and the chinese characters in DrPrompt show right. And I also found that the encoding of the wxStyledTextContral is utf-8.

      But when I saved the file, it's an error "Trying without any encoding...". Whether you have not add it?

      BTW, could tell me if there are some rules that I shoud abide as I check in my modification to the cvs.

       
    • Daniel Pozmanter

      Yes.  Please only work on stuff listed in the to do list as slated for 2.4.0.

      If you choose to work on anything else, please clearly mark it so that the code can be safely removed for the next stable release.

      Also please be sure the code you upload works, and does not break anything else in DrPython.

      Also put in the Changlog:

      limodou:  Change (*Notes to be removed when the feature is complete*)

       
    • Daniel Pozmanter

      I made the necessary format changes for the fix you plopped in there.  Thanks.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.