Skip to content

Commit

Permalink
Get rid of _u()
Browse files Browse the repository at this point in the history
Where we know we have bytes, we can call bytes.decode() directly.

The one place where we don't know what we have, we'll have to keep
calling unicode().  (That place is handling string exceptions AFAICT,
which are not allowed on Python 3.)
  • Loading branch information
mgedmin committed Jul 10, 2019
1 parent 658a024 commit 696e59a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
10 changes: 4 additions & 6 deletions src/zope/publisher/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
PYTHON3 = sys.version_info[0] == 3

if PYTHON2:
def _u(s, encoding='unicode_escape'):
return unicode(s, encoding)
def to_unicode(s):
return unicode(s, 'unicode_escape')
from xmlrpclib import *
import types
CLASS_TYPES = (type, types.ClassType)
else:
def _u(s, encoding=None):
if encoding is None:
return s
return str(s, encoding)
def to_unicode(s):
return s
CLASS_TYPES = (type,)
from xmlrpc.client import *
6 changes: 3 additions & 3 deletions src/zope/publisher/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
from zope.publisher.skinnable import applySkin #BBB import
from zope.publisher.skinnable import SkinChangedEvent #BBB import

from zope.publisher._compat import PYTHON2, _u
from zope.publisher._compat import PYTHON2


__ArrayTypes = (list, tuple)
Expand Down Expand Up @@ -259,7 +259,7 @@ def _decode(self, text):
self.charsets = [c for c in self.charsets if c != '*']
for charset in self.charsets:
try:
text = _u(text, charset)
text = text.decode(charset)
break
except UnicodeError:
pass
Expand Down Expand Up @@ -651,7 +651,7 @@ def __init__(self, aFieldStorage):
self.headers = aFieldStorage.headers
filename = aFieldStorage.filename
if isinstance(aFieldStorage.filename, bytes):
filename = _u(aFieldStorage.filename, 'UTF-8')
filename = aFieldStorage.filename.decode('UTF-8')
# fix for IE full paths
filename = filename[filename.rfind('\\')+1:].strip()
self.filename = filename
Expand Down
17 changes: 10 additions & 7 deletions src/zope/publisher/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import zope.event
import zope.interface

from zope.publisher._compat import PYTHON2, CLASS_TYPES, _u
from zope.publisher._compat import PYTHON2, CLASS_TYPES, to_unicode

if PYTHON2:
import Cookie as cookies
Expand Down Expand Up @@ -421,12 +421,15 @@ def _parseCookies(self, text, result=None):
eventlog.warning(e)
return result

for k,v in c.items():
for k, v in c.items():
# recode cookie value to ENCODING (UTF-8)
rk = _u(k if type(k) == bytes
else k.encode('latin1'), ENCODING)
rv = _u(v.value if type(v.value) == bytes
else v.value.encode('latin1'), ENCODING)
if not isinstance(k, bytes):
k = k.encode('latin1')
rk = k.decode(ENCODING)
v = v.value
if not isinstance(v, bytes):
v = v.encode('latin1')
rv = v.decode(ENCODING)
result[rk] = rv

return result
Expand Down Expand Up @@ -865,7 +868,7 @@ def handleException(self, exc_info):
return
title = tname = t.__name__
else:
title = tname = _u(t)
title = tname = to_unicode(t)

# Throwing non-protocol-specific exceptions is a good way
# for apps to control the status code.
Expand Down

0 comments on commit 696e59a

Please sign in to comment.