Merge lp:~cjwatson/launchpadlib/stdlib-json into lp:launchpadlib

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 136
Merged at revision: 135
Proposed branch: lp:~cjwatson/launchpadlib/stdlib-json
Merge into: lp:launchpadlib
Diff against target: 142 lines (+37/-22)
5 files modified
setup.py (+14/-11)
src/launchpadlib/apps.py (+5/-2)
src/launchpadlib/credentials.py (+5/-2)
src/launchpadlib/docs/command-line.txt (+8/-6)
src/launchpadlib/tests/test_http.py (+5/-1)
To merge this branch: bzr merge lp:~cjwatson/launchpadlib/stdlib-json
Reviewer Review Type Date Requested Status
Colin Watson (community) Approve
Dimitri John Ledkov Approve
Review via email: mp+176031@code.launchpad.net

Commit message

Use json from the standard library if available.

Description of the change

simplejson was integrated into the Python standard library as json in 2.6. Use it if available rather than requiring an external dependency.

I have not managed to get the tests to work locally (buildout vs. setuptools hatred). Advice or running-the-tests-for-me appreciated.

To post a comment you must log in.
Revision history for this message
Barry Warsaw (barry) wrote :

On Jul 20, 2013, at 11:09 PM, Colin Watson wrote:

>simplejson was integrated into the Python standard library as json in 2.6.
>Use it if available rather than requiring an external dependency.

+1 for replacing any external dependencies with stdlib modules, making an
eventual <wink> port to Python 3 easier.

>I have not managed to get the tests to work locally (buildout vs. setuptools
>hatred). Advice or running-the-tests-for-me appreciated.

It's been ages since I tried to run these tests, so maybe Gary or someone else
can chime in on that. From a mechanical reading of the changes, they look
good to me.

Revision history for this message
Dimitri John Ledkov (xnox) wrote :

python-launchpadlib deban packaging does the following:

mkdir -p build/home; HOME=build/home python setup.py test

Running tests as per above -> everything passes with or without this branch merged.

review: Approve
136. By Colin Watson

Set JSONDecodeError = ValueError if using json from the standard library.

Revision history for this message
Colin Watson (cjwatson) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'setup.py'
2--- setup.py 2011-01-25 11:50:08 +0000
3+++ setup.py 2014-05-12 13:21:50 +0000
4@@ -41,6 +41,19 @@
5 sys.path.insert(0, 'src')
6 from launchpadlib import __version__
7
8+install_requires = [
9+ 'httplib2',
10+ 'keyring',
11+ 'lazr.restfulclient>=0.9.19',
12+ 'lazr.uri',
13+ 'oauth',
14+ 'setuptools',
15+ 'testresources',
16+ 'wadllib',
17+ ]
18+if sys.version < "2.6":
19+ install_requires.append('simplejson')
20+
21 setup(
22 name='launchpadlib',
23 version=__version__,
24@@ -58,17 +71,7 @@
25 'src/launchpadlib/README.txt',
26 'src/launchpadlib/NEWS.txt'),
27 license='LGPL v3',
28- install_requires=[
29- 'httplib2',
30- 'keyring',
31- 'lazr.restfulclient>=0.9.19',
32- 'lazr.uri',
33- 'oauth',
34- 'setuptools',
35- 'simplejson',
36- 'testresources',
37- 'wadllib',
38- ],
39+ install_requires=install_requires,
40 url='https://help.launchpad.net/API/launchpadlib',
41 classifiers=[
42 "Development Status :: 5 - Production/Stable",
43
44=== modified file 'src/launchpadlib/apps.py'
45--- src/launchpadlib/apps.py 2010-10-20 13:14:40 +0000
46+++ src/launchpadlib/apps.py 2014-05-12 13:21:50 +0000
47@@ -28,7 +28,10 @@
48 import getpass
49 import sys
50
51-import simplejson
52+try:
53+ import json
54+except ImportError:
55+ import simplejson as json
56
57 from launchpadlib.credentials import (
58 Credentials, RequestTokenAuthorizationEngine, TokenAuthorizationException)
59@@ -49,6 +52,6 @@
60 token = self.credentials.get_request_token(
61 self.context, self.web_root,
62 token_format=Credentials.DICT_TOKEN_FORMAT)
63- return simplejson.dumps(token)
64+ return json.dumps(token)
65
66
67
68=== modified file 'src/launchpadlib/credentials.py'
69--- src/launchpadlib/credentials.py 2012-06-18 15:39:53 +0000
70+++ src/launchpadlib/credentials.py 2014-05-12 13:21:50 +0000
71@@ -41,7 +41,10 @@
72 b64encode,
73 )
74
75-import simplejson
76+try:
77+ import json
78+except ImportError:
79+ import simplejson as json
80
81 from lazr.restfulclient.errors import HTTPError
82 from lazr.restfulclient.authorize.oauth import (
83@@ -162,7 +165,7 @@
84 headers['Accept'] = 'application/json'
85 response, content = _http_post(url, headers, params)
86 if token_format == self.DICT_TOKEN_FORMAT:
87- params = simplejson.loads(content)
88+ params = json.loads(content)
89 if context is not None:
90 params["lp.context"] = context
91 self._request_token = AccessToken.from_params(params)
92
93=== modified file 'src/launchpadlib/docs/command-line.txt'
94--- src/launchpadlib/docs/command-line.txt 2010-10-20 13:14:40 +0000
95+++ src/launchpadlib/docs/command-line.txt 2014-05-12 13:21:50 +0000
96@@ -17,21 +17,23 @@
97 Launchpad installation, and returns a JSON description of the request
98 token and the available access levels.
99
100- >>> import simplejson
101+ >>> try:
102+ ... import json
103+ ... except ImportError:
104+ ... import simplejson as json
105 >>> from launchpadlib.apps import RequestTokenApp
106
107 >>> web_root = "http://launchpad.dev:8085/"
108 >>> consumer_name = "consumer"
109 >>> token_app = RequestTokenApp(web_root, consumer_name, "context")
110- >>> json = simplejson.loads(token_app.run())
111+ >>> token_json = json.loads(token_app.run())
112
113- >>> sorted(json.keys())
114+ >>> sorted(token_json.keys())
115 ['access_levels', 'lp.context', 'oauth_token',
116 'oauth_token_consumer', 'oauth_token_secret']
117
118- >>> print json['lp.context']
119+ >>> print token_json['lp.context']
120 context
121
122- >>> print json['oauth_token_consumer']
123+ >>> print token_json['oauth_token_consumer']
124 consumer
125-
126
127=== modified file 'src/launchpadlib/tests/test_http.py'
128--- src/launchpadlib/tests/test_http.py 2011-02-28 16:07:08 +0000
129+++ src/launchpadlib/tests/test_http.py 2014-05-12 13:21:50 +0000
130@@ -20,7 +20,11 @@
131 import tempfile
132 import unittest
133
134-from simplejson import dumps, JSONDecodeError
135+try:
136+ from json import dumps
137+ JSONDecodeError = ValueError
138+except ImportError:
139+ from simplejson import dumps, JSONDecodeError
140
141 from launchpadlib.errors import Unauthorized
142 from launchpadlib.credentials import UnencryptedFileCredentialStore

Subscribers

People subscribed via source and target branches