From: Fredrik Tolf Date: Wed, 18 Sep 2013 04:38:31 +0000 (+0200) Subject: Merge branch 'master' into python3 X-Git-Url: http://www.dolda2000.com/gitweb/?a=commitdiff_plain;h=199cdc4055921d7497e5a6b9a132ac44d04e9894;hp=-c;p=wrw.git Merge branch 'master' into python3 --- 199cdc4055921d7497e5a6b9a132ac44d04e9894 diff --combined wrw/proto.py index 96a8878,1af091f..0e49379 --- a/wrw/proto.py +++ b/wrw/proto.py @@@ -98,15 -98,16 +98,17 @@@ def simpleerror(env, startreq, code, ti """ % (title, title, htmlq(msg)) + buf = buf.encode("us-ascii") startreq("%i %s" % (code, title), [("Content-Type", "text/html"), ("Content-Length", str(len(buf)))]) return [buf] def urlq(url): + if isinstance(url, unicode): + url = url.encode("utf-8") ret = "" invalid = "&=#?/\"'" for c in url: - if c in invalid or (ord(c) <= 32): + if c in invalid or (ord(c) <= 32) or (ord(c) >= 128): ret += "%%%02X" % ord(c) else: ret += c diff --combined wrw/sp/util.py index ad99e8b,e15b947..0728a7e --- a/wrw/sp/util.py +++ b/wrw/sp/util.py @@@ -1,6 -1,6 +1,6 @@@ -import StringIO -from wrw import dispatch -import cons +import io +from .. import dispatch +from . import cons def findnsnames(el): names = {} @@@ -8,7 -8,7 +8,7 @@@ def proc(el): if isinstance(el, cons.element): if el.ns not in names: - names[el.ns] = u"n" + unicode(nid[0]) + names[el.ns] = "n" + str(nid[0]) nid[:] = [nid[0] + 1] for ch in el.children: proc(ch) @@@ -34,12 -34,12 +34,12 @@@ class formatter(object) def quotewrite(self, buf): for ch in buf: - if ch == u'&': - self.write(u"&") - elif ch == u'<': - self.write(u"<") - elif ch == u'>': - self.write(u">") + if ch == '&': + self.write("&") + elif ch == '<': + self.write("<") + elif ch == '>': + self.write(">") else: self.write(ch) @@@ -50,15 -50,15 +50,15 @@@ self.write(el) def attrval(self, buf): - qc, qt = (u"'", u"'") if u'"' in buf else (u'"', u""") + qc, qt = ("'", "'") if '"' in buf else ('"', """) self.write(qc) for ch in buf: - if ch == u'&': - self.write(u"&") - elif ch == u'<': - self.write(u"<") - elif ch == u'>': - self.write(u">") + if ch == '&': + self.write("&") + elif ch == '<': + self.write("<") + elif ch == '>': + self.write(">") elif ch == qc: self.write(qt) else: @@@ -67,38 -67,38 +67,38 @@@ def attr(self, k, v): self.write(k) - self.write(u'=') + self.write('=') self.attrval(v) def shorttag(self, el, **extra): - self.write(u'<' + self.elname(el)) - for k, v in el.attrs.iteritems(): - self.write(u' ') + self.write('<' + self.elname(el)) + for k, v in el.attrs.items(): + self.write(' ') self.attr(k, v) - for k, v in extra.iteritems(): - self.write(u' ') + for k, v in extra.items(): + self.write(' ') self.attr(k, v) - self.write(u" />") + self.write(" />") def elname(self, el): ns = self.nsnames[el.ns] if ns is None: return el.name else: - return ns + u':' + el.name + return ns + ':' + el.name def starttag(self, el, **extra): - self.write(u'<' + self.elname(el)) - for k, v in el.attrs.iteritems(): - self.write(u' ') + self.write('<' + self.elname(el)) + for k, v in el.attrs.items(): + self.write(' ') self.attr(k, v) - for k, v in extra.iteritems(): - self.write(u' ') + for k, v in extra.items(): + self.write(' ') self.attr(k, v) - self.write(u'>') + self.write('>') def endtag(self, el): - self.write(u'') + self.write('') def longtag(self, el, **extra): self.starttag(el, **extra) @@@ -123,19 -123,19 +123,19 @@@ raise Exception("Unknown object in element tree: " + el) def start(self): - self.write(u'\n') + self.write('\n') if self.doctype: - self.write(u'\n' % (self.root.name, - self.doctype[0], - self.doctype[1])) + self.write('\n' % (self.root.name, + self.doctype[0], + self.doctype[1])) extra = {} - for uri, nm in self.nsnames.iteritems(): + for uri, nm in self.nsnames.items(): if uri is None: continue if nm is None: - extra[u"xmlns"] = uri + extra["xmlns"] = uri else: - extra[u"xmlns:" + nm] = uri + extra["xmlns:" + nm] = uri self.element(self.root, **extra) @classmethod @@@ -148,7 -148,7 +148,7 @@@ @classmethod def format(cls, el, *args, **kw): - buf = StringIO.StringIO() + buf = io.BytesIO() cls.output(buf, el, *args, **kw) return buf.getvalue() @@@ -165,9 -165,8 +165,9 @@@ class iwriter(object) self.col = 0 def write(self, buf): - for c in buf: - if c == '\n': + for i in range(len(buf)): + c = buf[i:i + 1] + if c == b'\n': self.col = 0 else: self.col += 1 @@@ -178,16 -177,16 +178,16 @@@ if self.atbol: return if self.col != 0: - self.write('\n') + self.write(b'\n') self.write(indent) self.atbol = True class indenter(formatter): - def __init__(self, indent=u" ", *args, **kw): + def __init__(self, indent=" ", *args, **kw): super(indenter, self).__init__(*args, **kw) self.out = iwriter(self.out) self.indent = indent - self.curind = u"" + self.curind = "" def simple(self, el): for ch in el.children: @@@ -227,7 -226,7 +227,7 @@@ class response(dispatch.restart) formatter = indenter def __init__(self, root): - super(response, self).__init__() + super().__init__() self.root = root @property @@@ -235,5 -234,7 +235,7 @@@ raise Exception("a subclass of wrw.sp.util.response must override ctype") def handle(self, req): + ret = self.formatter.format(self.root, doctype=self.doctype, charset=self.charset) req.ohead["Content-Type"] = self.ctype - return [self.formatter.format(self.root, doctype=self.doctype, charset=self.charset)] + req.ohead["Content-Length"] = len(ret) + return [ret]