Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
Drop Python 2 support leftovers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Howitz committed Aug 5, 2022
1 parent 10a736a commit dd2e480
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 51 deletions.
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def read(*rnames):
'grokcore.content',
'grokcore.view[security_publication]',
'grokcore.view[test]',
'six',
'zope.app.appsetup',
'zope.app.wsgi[test]',
'zope.errorview >= 1.2.0',
Expand Down
2 changes: 1 addition & 1 deletion src/grokcore/rest/publication.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ class GrokHTTPFactory(HTTPFactory):
"""

def __call__(self):
request, publication = super(GrokHTTPFactory, self).__call__()
request, publication = super().__call__()
return request, GrokHTTPPublication
10 changes: 0 additions & 10 deletions src/grokcore/rest/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,3 @@ def grok(module_name):
zcml.do_grok('grokcore.rest.meta', config)
zcml.do_grok(module_name, config)
config.execute_actions()


def bprint(data):
"""Python 2 and 3 doctest compatible print.
http://python3porting.com/problems.html#string-representation
"""
if not isinstance(data, str):
data = data.decode()
print(data.strip())
60 changes: 30 additions & 30 deletions src/grokcore/rest/tests/functional/rest/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,40 @@
Issue a GET request::
>>> response = http_call(wsgi_app(), 'GET', 'http://localhost/++rest++a/app')
>>> bprint(response.getBody())
GET
>>> print(response.getBody())
b'GET'
Issue a POST request::
>>> response = http_call(wsgi_app(), 'POST', 'http://localhost/++rest++a/app')
>>> bprint(response.getBody())
POST
>>> print(response.getBody())
b'POST'
Issue a PUT request::
>>> response = http_call(wsgi_app(), 'PUT', 'http://localhost/++rest++a/app')
>>> bprint(response.getBody())
PUT
>>> print(response.getBody())
b'PUT'
Issue a DELETE request::
>>> response = http_call(wsgi_app(), 'DELETE', 'http://localhost/++rest++a/app')
>>> bprint(response.getBody())
DELETE
>>> print(response.getBody())
b'DELETE'
Let's examine a rest protocol b which has no POST or DELETE request defined::
The GET request works as expected::
>>> response = http_call(wsgi_app(), 'GET', 'http://localhost/++rest++b/app')
>>> bprint(response.getBody())
GET
>>> print(response.getBody())
b'GET'
So does the PUT request::
>>> response = http_call(wsgi_app(), 'PUT', 'http://localhost/++rest++b/app')
>>> bprint(response.getBody())
PUT
>>> print(response.getBody())
b'PUT'
POST is not defined, however, and we should get a 405 (Method not
allowed) error::
Expand Down Expand Up @@ -125,8 +125,8 @@
the default rest layer::
>>> response = http_call(wsgi_app(), 'GET', 'http://localhost/++rest++d/app/alpha')
>>> bprint(response.getBody())
GET2
>>> print(response.getBody())
b'GET2'
But not for POST::
Expand Down Expand Up @@ -271,38 +271,38 @@
We should get a different result for the GET request::
>>> response = http_call(wsgi_app(), 'GET', 'http://localhost/++rest++g/app/one')
>>> bprint(response.getBody())
GET interface registered
>>> print(response.getBody())
b'GET interface registered'
>>> response = http_call(wsgi_app(), 'GET', 'http://localhost/++rest++g/app/two')
>>> bprint(response.getBody())
GET directly registered
>>> print(response.getBody())
b'GET directly registered'
We should also get a different result for the PUT request::
>>> response = http_call(wsgi_app(), 'PUT', 'http://localhost/++rest++g/app/one')
>>> bprint(response.getBody())
PUT interface registered
>>> print(response.getBody())
b'PUT interface registered'
>>> response = http_call(wsgi_app(), 'PUT', 'http://localhost/++rest++g/app/two')
>>> bprint(response.getBody())
PUT directly registered
>>> print(response.getBody())
b'PUT directly registered'
We expect POST and DELETE to be the same on both. For the directly
registered object (two) it should fall back to the interface as there
is none more specifically declared::
>>> response = http_call(wsgi_app(), 'POST', 'http://localhost/++rest++g/app/one')
>>> bprint(response.getBody())
POST interface registered
>>> print(response.getBody())
b'POST interface registered'
>>> response = http_call(wsgi_app(), 'POST', 'http://localhost/++rest++g/app/two')
>>> bprint(response.getBody())
POST interface registered
>>> print(response.getBody())
b'POST interface registered'
>>> response = http_call(wsgi_app(), 'DELETE', 'http://localhost/++rest++g/app/one')
>>> bprint(response.getBody())
DELETE interface registered
>>> print(response.getBody())
b'DELETE interface registered'
>>> response = http_call(wsgi_app(), 'DELETE', 'http://localhost/++rest++g/app/two')
>>> bprint(response.getBody())
DELETE interface registered
>>> print(response.getBody())
b'DELETE interface registered'
Todo:
Expand Down
15 changes: 6 additions & 9 deletions src/grokcore/rest/tests/test_functional.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import doctest
import unittest

import six
from pkg_resources import resource_listdir

import zope.app.wsgi.testlayer
Expand Down Expand Up @@ -31,16 +30,15 @@ def http_call(app, method, path, data=None, handle_errors=False, **kw):
"""
if path.startswith('http://localhost'):
path = path[len('http://localhost'):]
request_string = '{} {} HTTP/1.1\n'.format(method, path)
request_string = f'{method} {path} HTTP/1.1\n'
for key, value in kw.items():
request_string += '{}: {}\n'.format(key, value)
request_string += f'{key}: {value}\n'
if data is not None:
request_string += 'Content-Length:{}\n'.format(len(data))
request_string += f'Content-Length:{len(data)}\n'
request_string += '\r\n'
request_string += data

if six.PY3: # pragma: PY3
request_string = request_string.encode()
request_string = request_string.encode()

result = http(app, request_string, handle_errors=handle_errors)
return result
Expand All @@ -54,20 +52,19 @@ def str_http_call(*args, **kw):

def suiteFromPackage(name):
layer_dir = 'functional'
files = resource_listdir(__name__, '{}/{}'.format(layer_dir, name))
files = resource_listdir(__name__, f'{layer_dir}/{name}')
suite = unittest.TestSuite()
for filename in files:
if not filename.endswith('.py'):
continue
if filename == '__init__.py':
continue

dottedname = 'grokcore.rest.tests.%s.%s.%s' % (
dottedname = 'grokcore.rest.tests.{}.{}.{}'.format(
layer_dir, name, filename[:-3])
test = doctest.DocTestSuite(
dottedname,
extraglobs=dict(
bprint=grokcore.rest.testing.bprint,
getRootFolder=layer.getRootFolder,
http_call=http_call,
str_http_call=str_http_call,
Expand Down

0 comments on commit dd2e480

Please sign in to comment.