Merge lp:~cjwatson/lazr.restful/py3-make-entry into lp:lazr.restful

Proposed by Colin Watson
Status: Merged
Merged at revision: 263
Proposed branch: lp:~cjwatson/lazr.restful/py3-make-entry
Merge into: lp:lazr.restful
Diff against target: 44 lines (+6/-14)
1 file modified
src/lazr/restful/tests/test_webservice.py (+6/-14)
To merge this branch: bzr merge lp:~cjwatson/lazr.restful/py3-make-entry
Reviewer Review Type Date Requested Status
Thiago F. Pappacena (community) Approve
Review via email: mp+390264@code.launchpad.net

Commit message

Port lazr.restful.tests.test_webservice.make_entry to Python 3.

Description of the change

Python 3 has no exec statement. We could have switched to the "exec(code)" form, which works in both Python 2 and 3; but this is a good opportunity to switch to constructing the class dynamically without "exec", which seems more elegant.

To post a comment you must log in.
Revision history for this message
Thiago F. Pappacena (pappacena) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/lazr/restful/tests/test_webservice.py'
2--- src/lazr/restful/tests/test_webservice.py 2020-09-02 22:26:24 +0000
3+++ src/lazr/restful/tests/test_webservice.py 2020-09-03 18:39:07 +0000
4@@ -13,7 +13,6 @@
5 )
6 from lxml import etree
7 from operator import attrgetter
8-from textwrap import dedent
9 import collections
10 import logging
11 import random
12@@ -28,6 +27,7 @@
13 getUtility,
14 )
15 from zope.interface import implementer, Interface
16+from zope.interface.interface import InterfaceClass
17 from zope.publisher.browser import TestRequest
18 from zope.schema import Choice, Date, Datetime, TextLine
19 from zope.schema.interfaces import ITextLine
20@@ -773,19 +773,11 @@
21
22 def make_entry(name):
23 """Make an entity with some attibutes to expose as a web service."""
24- code = """
25- @exported_as_webservice_entry(singular_name='%(name)s')
26- class %(name)s(Interface):
27- pass
28- """ % locals()
29-
30- for letter in 'rstuvwxyz':
31- code += """
32- %(letter)s_field = exported(TextLine(title=u'Field %(letter)s'))
33- """ % locals()
34-
35- exec dedent(code)
36- return locals()[name]
37+ fields = {
38+ '%s_field' % letter: exported(TextLine(title=u'Field %s' % letter))
39+ for letter in 'rstuvwxyz'}
40+ cls = InterfaceClass(name, bases=(Interface,), attrs=fields)
41+ return exported_as_webservice_entry(singular_name=name)(cls)
42
43
44 class TestWadlDeterminism(WebServiceTestCase):

Subscribers

People subscribed via source and target branches