~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to scripts/python-console

  • Committer: wagrant
  • Date: 2008-07-15 07:04:54 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:874
python-console: We are now a shiny UTF-8 console. Don't send anything
                else unless you want to be horribly confused. This also
                means that IVLE should have Unicode support pretty much
                everywhere!

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
        self.lineQ = lineQ
62
62
        self.remainder = ''
63
63
 
 
64
    def _trim_incomplete_final(self, stuff):
 
65
        '''Trim an incomplete UTF-8 character from the end of a string.
 
66
           Returns (trimmed_string, count_of_trimmed_bytes).
 
67
        '''
 
68
        tokill = incomplete_utf8_sequence(stuff)
 
69
        if tokill == 0:
 
70
            return (stuff, tokill)
 
71
        else:
 
72
            return (stuff[:-tokill], tokill)
 
73
 
64
74
    def write(self, stuff):
 
75
        # print will only give a non-file a unicode or str. There's no way
 
76
        # to convince it to encode unicodes, so we have to do it ourselves.
 
77
        # Yay for file special-cases (fileobject.c, PyFile_WriteObject).
 
78
        # If somebody wants to write some other object to here, they do it
 
79
        # at their own peril.
 
80
        if isinstance(stuff, unicode):
 
81
            stuff = stuff.encode('utf-8')
65
82
        self.remainder = self.remainder + stuff
66
83
 
67
84
        # if there's less than 128 bytes, buffer
70
87
 
71
88
        # if there's lots, then send it in 1/2K blocks
72
89
        while len(self.remainder) > 512:
73
 
            blk = self.remainder[0:512]
74
 
            self.cmdQ.put({"output":blk})
 
90
            # We send things as Unicode inside JSON, so we must only send
 
91
            # complete UTF-8 characters.
 
92
            (blk, count) = self._trim_incomplete_final(self.remainder[:512])
 
93
            self.cmdQ.put({"output":blk.decode('utf-8', 'replace')})
75
94
            expiry.ping()
76
95
            ln = self.lineQ.get()
77
 
            self.remainder = self.remainder[512:]
 
96
            self.remainder = self.remainder[512 - count:]
78
97
 
79
98
        # Finally, split the remainder up into lines, and ship all the
80
99
        # completed lines off to the server.
85
104
        if len(lines) > 0:
86
105
            lines.append('')
87
106
            text = "\n".join(lines)
88
 
            self.cmdQ.put({"output":text})
 
107
            self.cmdQ.put({"output":text.decode('utf-8', 'replace')})
89
108
            expiry.ping()
90
109
            ln = self.lineQ.get()
91
110
            if 'interrupt' in ln:
93
112
 
94
113
    def flush(self):
95
114
        if len(self.remainder) > 0:
96
 
            self.cmdQ.put({"output":self.remainder})
 
115
            (out, count) = self._trim_incomplete_final(self.remainder)
 
116
            self.cmdQ.put({"output":out.decode('utf-8', 'replace')})
97
117
            expiry.ping()
98
118
            ln = self.lineQ.get()
99
 
            self.remainder = ''
 
119
            # Leave incomplete characters in the buffer.
 
120
            # Yes, this does mean that an incomplete character will be left
 
121
            # off the end, but we discussed this and it was deemed best.
 
122
            self.remainder = self.remainder[len(self.remainder)-count:]
100
123
            if 'interrupt' in ln:
101
124
                raise Interrupt()
102
125
 
141
164
        except:
142
165
            tb = format_exc_start(start=1)
143
166
            self.webio.flush()
144
 
            self.cmdQ.put({"exc": ''.join(tb)})
 
167
            self.cmdQ.put({"exc": ''.join(tb).decode('utf-8', 'replace')})
145
168
            self.curr_cmd = ''
146
169
 
147
170
    def run(self):
167
190
                        self.execCmd(cmd)
168
191
                except:
169
192
                    tb = format_exc_start(start=3)
170
 
                    self.cmdQ.put({"exc": ''.join(tb)})
 
193
                    self.cmdQ.put({"exc": ''.join(tb).decode('utf-8', 'replace')})
171
194
                    self.webio.flush()
172
195
                    self.curr_cmd = ''
173
196
            if 'block' in ln:
178
201
                except:
179
202
                    tb = format_exc_start(start=1)
180
203
                    self.webio.flush()
181
 
                    self.cmdQ.put({"exc": ''.join(tb)})
 
204
                    self.cmdQ.put({"exc": ''.join(tb).decode('utf-8', 'replace')})
182
205
                    self.curr_cmd = ''
183
206
 
184
207
def daemonize():