Skip to content

Commit

Permalink
Merge pull request #43 from zopefoundation/refactor
Browse files Browse the repository at this point in the history
Refactoring: use u"" literals instead of _u("...")
  • Loading branch information
mgedmin committed Jul 9, 2019
2 parents 4e990b5 + 75adbe5 commit 658a024
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 157 deletions.
13 changes: 6 additions & 7 deletions src/zope/publisher/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,17 +877,16 @@ def handleException(self, exc_info):

def internalError(self):
'See IPublisherResponse'
self.setStatus(500, _u("The engines can't take any more, Jim!"))
self.setStatus(500, u"The engines can't take any more, Jim!")

def _html(self, title, content):
t = escape(title)
return (
_u("<html><head><title>%s</title></head>\n"
"<body><h2>%s</h2>\n"
"%s\n"
"</body></html>\n") %
(t, t, content)
)
u"<html><head><title>%s</title></head>\n"
u"<body><h2>%s</h2>\n"
u"%s\n"
u"</body></html>\n"
) % (t, t, content)

def retry(self):
"""
Expand Down
5 changes: 2 additions & 3 deletions src/zope/publisher/interfaces/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from zope.publisher.interfaces import IResponse
from zope.publisher.interfaces import IView

from .._compat import _u

class IVirtualHostRequest(Interface):
"""The support for virtual hosts in Zope is very important.
Expand Down Expand Up @@ -481,8 +480,8 @@ class IHTTPVirtualHostChangedEvent(Interface):
The request referred to in this event implements at least the
IHTTPAppliationRequest interface.
"""
request = Attribute(_u("The application request whose virtual host info has "
"been altered"))
request = Attribute("The application request whose virtual host info has "
"been altered")

class IHTTPException(Interface):
"""Marker interface for http exceptions views
Expand Down
188 changes: 94 additions & 94 deletions src/zope/publisher/tests/test_browserrequest.py

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/zope/publisher/tests/test_browserresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from zope.publisher.interfaces.http import IHTTPResponse
from zope.publisher.interfaces.http import IHTTPApplicationResponse
from zope.publisher.interfaces import IResponse
from .._compat import _u


# TODO: Waaa need more tests
Expand Down Expand Up @@ -112,7 +111,7 @@ def testInsertBase(self):
insertBase(b'<html><head></head><body>Page</body></html>'))

# Ensure that unicode bases work as well
response.setBase(_u('http://localhost/folder/'))
response.setBase(u'http://localhost/folder/')
body = insertBase(b'<html><head></head><body>Page</body></html>')
self.assertTrue(isinstance(body, bytes))
self.assertTrue(b'<base href="http://localhost/folder/" />' in body)
Expand Down
55 changes: 27 additions & 28 deletions src/zope/publisher/tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
from zope.publisher.interfaces.http import IHTTPApplicationResponse
from zope.publisher.interfaces import IResponse
from zope.publisher.tests.publication import TestPublication
from .._compat import _u

from zope.publisher.tests.basetestipublicationrequest \
import BaseTestIPublicationRequest
Expand Down Expand Up @@ -463,14 +462,14 @@ def testCookies(self):
}
req = self._createRequest(extra_env=cookies)

self.assertEqual(req.cookies[_u('foo')], _u('bar'))
self.assertEqual(req[_u('foo')], _u('bar'))
self.assertEqual(req.cookies[u'foo'], u'bar')
self.assertEqual(req[u'foo'], u'bar')

self.assertEqual(req.cookies[_u('spam')], _u('eggs'))
self.assertEqual(req[_u('spam')], _u('eggs'))
self.assertEqual(req.cookies[u'spam'], u'eggs')
self.assertEqual(req[u'spam'], u'eggs')

self.assertEqual(req.cookies[_u('this')], _u('Should be accepted'))
self.assertEqual(req[_u('this')], _u('Should be accepted'))
self.assertEqual(req.cookies[u'this'], u'Should be accepted')
self.assertEqual(req[u'this'], u'Should be accepted')

# Reserved key
self.assertFalse(req.cookies.has_key('path'))
Expand Down Expand Up @@ -509,18 +508,18 @@ def testCookiesUnicode(self):
# Cookie values are assumed to be UTF-8 encoded
cookies = {'HTTP_COOKIE': r'key="\342\230\243";'}
req = self._createRequest(extra_env=cookies)
self.assertEqual(req.cookies[_u('key')], _u('\N{BIOHAZARD SIGN}'))
self.assertEqual(req.cookies[u'key'], u'\N{BIOHAZARD SIGN}')

def testHeaders(self):
headers = {
'TEST_HEADER': 'test',
'Another-Test': 'another',
}
req = self._createRequest(extra_env=headers)
self.assertEqual(req.headers[_u('TEST_HEADER')], _u('test'))
self.assertEqual(req.headers[_u('TEST-HEADER')], _u('test'))
self.assertEqual(req.headers[_u('test_header')], _u('test'))
self.assertEqual(req.getHeader('TEST_HEADER', literal=True), _u('test'))
self.assertEqual(req.headers[u'TEST_HEADER'], u'test')
self.assertEqual(req.headers[u'TEST-HEADER'], u'test')
self.assertEqual(req.headers[u'test_header'], u'test')
self.assertEqual(req.getHeader('TEST_HEADER', literal=True), u'test')
self.assertEqual(req.getHeader('TEST-HEADER', literal=True), None)
self.assertEqual(req.getHeader('test_header', literal=True), None)
self.assertEqual(req.getHeader('Another-Test', literal=True),
Expand Down Expand Up @@ -682,10 +681,10 @@ def testUnicodeURLs(self):
req = self._createRequest(
{'PATH_INFO': '/\xc3\xa4\xc3\xb6/\xc3\xbc\xc3\x9f/foo/bar.html'})
self.assertEqual(req._traversal_stack,
[_u('bar.html'), _u('foo'), _u('\u00fc\u00df'), _u('\u00e4\u00f6')])
[u'bar.html', u'foo', u'\u00fc\u00df', u'\u00e4\u00f6'])
# the request should have converted PATH_INFO to unicode
self.assertEqual(req['PATH_INFO'],
_u('/\u00e4\u00f6/\u00fc\u00df/foo/bar.html'))
u'/\u00e4\u00f6/\u00fc\u00df/foo/bar.html')

def testResponseWriteFaile(self):
self.assertRaises(TypeError,
Expand All @@ -700,7 +699,7 @@ def test_PathTrailingWhitespace(self):
def test_unacceptable_charset(self):
# Regression test for https://bugs.launchpad.net/zope3/+bug/98337
request = self._createRequest({'HTTP_ACCEPT_CHARSET': 'ISO-8859-1'})
result = _u("Latin a with ogonek\u0105 Cyrillic ya \u044f")
result = u"Latin a with ogonek\u0105 Cyrillic ya \u044f"
provideAdapter(HTTPCharsets)
request.response.setHeader('Content-Type', 'text/plain')

Expand Down Expand Up @@ -761,7 +760,7 @@ def getPreferredCharsets(self):
# set the response on a result
request = self._createRequest()
request.response.setHeader('Content-Type', 'text/plain')
result = _u("Latin a with ogonek\u0105 Cyrillic ya \u044f")
result = u"Latin a with ogonek\u0105 Cyrillic ya \u044f"
request.response.setResult(result)

body = request.response.consumeBody()
Expand Down Expand Up @@ -827,7 +826,7 @@ def testContentLength(self):
eq(b"test", body)

headers, body = self._getResultFromResponse(
_u('\u0442\u0435\u0441\u0442'), "utf-8",
u'\u0442\u0435\u0441\u0442', "utf-8",
{"content-type": "text/plain"})
eq("8", headers["Content-Length"])
eq(b'\xd1\x82\xd0\xb5\xd1\x81\xd1\x82', body)
Expand All @@ -839,48 +838,48 @@ def testContentType(self):
eq("", headers.get("Content-Type", ""))
eq(b"test", body)

headers, body = self._getResultFromResponse(_u("test"),
headers, body = self._getResultFromResponse(u"test",
headers={"content-type": "text/plain"})
eq("text/plain;charset=utf-8", headers["Content-Type"])
eq(b"test", body)

headers, body = self._getResultFromResponse(_u("test"), "utf-8",
headers, body = self._getResultFromResponse(u"test", "utf-8",
{"content-type": "text/html"})
eq("text/html;charset=utf-8", headers["Content-Type"])
eq(b"test", body)

headers, body = self._getResultFromResponse(_u("test"), "utf-8",
headers, body = self._getResultFromResponse(u"test", "utf-8",
{"content-type": "text/plain;charset=cp1251"})
eq("text/plain;charset=cp1251", headers["Content-Type"])
eq(b"test", body)

# see https://bugs.launchpad.net/zope.publisher/+bug/98395
# RFC 3023 types and */*+xml output as unicode

headers, body = self._getResultFromResponse(_u("test"), "utf-8",
headers, body = self._getResultFromResponse(u"test", "utf-8",
{"content-type": "text/xml"})
eq("text/xml;charset=utf-8", headers["Content-Type"])
eq(b"test", body)

headers, body = self._getResultFromResponse(_u("test"), "utf-8",
headers, body = self._getResultFromResponse(u"test", "utf-8",
{"content-type": "application/xml"})
eq("application/xml;charset=utf-8", headers["Content-Type"])
eq(b"test", body)

headers, body = self._getResultFromResponse(_u("test"), "utf-8",
headers, body = self._getResultFromResponse(u"test", "utf-8",
{"content-type": "text/xml-external-parsed-entity"})
eq("text/xml-external-parsed-entity;charset=utf-8",
headers["Content-Type"])
eq(b"test", body)

headers, body = self._getResultFromResponse(_u("test"), "utf-8",
headers, body = self._getResultFromResponse(u"test", "utf-8",
{"content-type": "application/xml-external-parsed-entity"})
eq("application/xml-external-parsed-entity;charset=utf-8",
headers["Content-Type"])
eq(b"test", body)

# Mozilla XUL
headers, body = self._getResultFromResponse(_u("test"), "utf-8",
headers, body = self._getResultFromResponse(u"test", "utf-8",
{"content-type": "application/vnd+xml"})
eq("application/vnd+xml;charset=utf-8", headers["Content-Type"])
eq(b"test", body)
Expand All @@ -892,7 +891,7 @@ def testContentType(self):
eq("image/gif", headers["Content-Type"])
eq(b"test", body)

headers, body = self._getResultFromResponse(_u("test"), "utf-8",
headers, body = self._getResultFromResponse(u"test", "utf-8",
{"content-type": "application/json"})
eq("application/json", headers["Content-Type"])
eq(b"test", body)
Expand Down Expand Up @@ -923,7 +922,7 @@ def testSetCookie(self):
self.assertTrue('alpha=beta;' in c or 'alpha=beta' in c)

c = self._getCookieFromResponse([
('sign', _u('\N{BIOHAZARD SIGN}'), {}),
('sign', u'\N{BIOHAZARD SIGN}', {}),
])
self.assertTrue((r'sign="\342\230\243";' in c) or
(r'sign="\342\230\243"' in c))
Expand All @@ -940,7 +939,7 @@ def testSetCookie(self):
'domain': 'example.com',
'pAth': '/froboz',
'max_age': 3600,
'comment': _u('blah;\N{BIOHAZARD SIGN}?'),
'comment': u'blah;\N{BIOHAZARD SIGN}?',
'seCure': True,
}),
])[0]
Expand Down
15 changes: 6 additions & 9 deletions src/zope/publisher/tests/test_paste.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import unittest
import doctest

from .._compat import _u


class SamplePublication(object):

Expand All @@ -38,13 +36,12 @@ def afterTraversal(self, request, ob):
pass

def callObject(self, request, ob):
return (_u('<html><body>Thanks for your request:<br />\n'
'<h1>%s</h1>\n<pre>\n%s\n</pre>\n'
'<h1>Publication arguments:</h1>\n'
'Globals: %r<br />\nOptions: %r\n</body></html>')
% (request.__class__.__name__, request,
self.args[0], self.args[1])
)
return (
u'<html><body>Thanks for your request:<br />\n'
u'<h1>%s</h1>\n<pre>\n%s\n</pre>\n'
u'<h1>Publication arguments:</h1>\n'
u'Globals: %r<br />\nOptions: %r\n</body></html>'
) % (request.__class__.__name__, request, self.args[0], self.args[1])

def afterCall(self, request, ob):
pass
Expand Down
4 changes: 2 additions & 2 deletions src/zope/publisher/tests/test_principallogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"""
import unittest
from zope.interface.verify import verifyObject
from .._compat import _u


class PrincipalStub(object):

id = _u('\xfc principal')
id = u'\xfc principal'


class TestPrincipalLogging(unittest.TestCase):
Expand Down
23 changes: 11 additions & 12 deletions src/zope/publisher/zcml.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@
from zope.publisher.interfaces.browser import IBrowserRequest
from zope.publisher.interfaces.browser import IBrowserSkinType
from zope.publisher.interfaces.browser import IDefaultSkin
from ._compat import _u
from zope.schema import TextLine


class IDefaultSkinDirective(Interface):
"""Sets the default browser skin"""

name = TextLine(
title=_u("Default skin name"),
description=_u("Default skin name"),
title=u"Default skin name",
description=u"Default skin name",
required=True
)

Expand All @@ -46,28 +45,28 @@ class IDefaultViewDirective(Interface):
"""

name = TextLine(
title=_u("The name of the view that should be the default."),
description=_u("""
title=u"The name of the view that should be the default.",
description=u"""
This name refers to view that should be the view used by
default (if no view name is supplied explicitly)."""),
default (if no view name is supplied explicitly).""",
required=True
)

for_ = GlobalObject(
title=_u("The interface this view is the default for."),
description=_u("""
title=u"The interface this view is the default for.",
description=u"""
Specifies the interface for which the view is registered.
All objects implementing this interface can make use of
this view. If this attribute is not specified, the view is available
for all objects."""),
for all objects.""",
required=False
)

layer = GlobalInterface(
title=_u("The layer the default view is declared for"),
description=_u("The default layer for which the default view is "
"applicable. By default it is applied to all layers."),
title=u"The layer the default view is declared for",
description=u"The default layer for which the default view is "
u"applicable. By default it is applied to all layers.",
required=False
)

Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ commands =
deps =
{[testenv]deps}
coverage
parallel_show_output = true


[testenv:docs]
Expand Down

0 comments on commit 658a024

Please sign in to comment.