Skip to content
This repository

json/jsonapi cleanup #1040

Closed
wants to merge 1 commit into from

2 participants

Min RK Fernando Perez
Min RK
Owner
  • never use zmq.utils.jsonapi when talking to files
  • catch decimal.Decimal returned by jsonlib in the parallel scheduler
  • remove IPython.zmq.jsonapi shim

See discussion in #1037 for details.

Min RK json/jsonapi cleanup
* never use zmq.utils.jsonapi when talking to files
* catch decimal.Decimal returned by jsonlib in the parallel scheduler
* remove IPython.zmq.jsonapi shim
584513b
Fernando Perez
Owner

This looks good, verified that the test suite now completes with jsonlib installed. I'd much rather take this approach than having in-house little hacks, so I'm merging it now (rebased to avoid recursive merge for just a single commit).

Fernando Perez
Owner

Pushed as 3892cf3, closing. Thanks!

Fernando Perez fperez closed this November 25, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Nov 25, 2011
Min RK json/jsonapi cleanup
* never use zmq.utils.jsonapi when talking to files
* catch decimal.Decimal returned by jsonlib in the parallel scheduler
* remove IPython.zmq.jsonapi shim
584513b
This page is out of date. Refresh to see the latest.
8  IPython/parallel/apps/ipcontrollerapp.py
@@ -23,6 +23,7 @@
23 23
 
24 24
 from __future__ import with_statement
25 25
 
  26
+import json
26 27
 import os
27 28
 import socket
28 29
 import stat
@@ -34,11 +35,6 @@
34 35
 from zmq.devices import ProcessMonitoredQueue
35 36
 from zmq.log.handlers import PUBHandler
36 37
 
37  
-# Note: use our own import to work around jsonlib api mismatch.  When these
38  
-# changes propagate to zmq, revert back to the following line instead:
39  
-#from zmq.utils import jsonapi as json
40  
-from IPython.zmq import jsonapi as json
41  
-
42 38
 from IPython.core.profiledir import ProfileDir
43 39
 
44 40
 from IPython.parallel.apps.baseapp import (
@@ -214,7 +210,7 @@ def save_connection_dict(self, fname, cdict):
214 210
                     location = '127.0.0.1'
215 211
             cdict['location'] = location
216 212
         fname = os.path.join(self.profile_dir.security_dir, fname)
217  
-        with open(fname, 'wb') as f:
  213
+        with open(fname, 'w') as f:
218 214
             f.write(json.dumps(cdict, indent=2))
219 215
         os.chmod(fname, stat.S_IRUSR|stat.S_IWUSR)
220 216
     
4  IPython/parallel/controller/scheduler.py
@@ -359,7 +359,9 @@ def dispatch_submission(self, raw_msg):
359 359
         # turn timeouts into datetime objects:
360 360
         timeout = header.get('timeout', None)
361 361
         if timeout:
362  
-            timeout = datetime.now() + timedelta(0,timeout,0)
  362
+            # cast to float, because jsonlib returns floats as decimal.Decimal,
  363
+            # which timedelta does not accept
  364
+            timeout = datetime.now() + timedelta(0,float(timeout),0)
363 365
 
364 366
         args = [raw_msg, targets, after, follow, timeout]
365 367
 
8  IPython/zmq/entry_point.py
@@ -4,6 +4,7 @@
4 4
 
5 5
 # Standard library imports.
6 6
 import atexit
  7
+import json
7 8
 import os
8 9
 import socket
9 10
 from subprocess import Popen, PIPE
@@ -12,11 +13,6 @@
12 13
 
13 14
 # System library imports
14 15
 
15  
-# Note: use our own import to work around jsonlib api mismatch.  When these
16  
-# changes propagate to zmq, revert back to the following line instead:
17  
-#from zmq.utils import jsonapi as json
18  
-from IPython.zmq import jsonapi as json
19  
-
20 16
 # IPython imports
21 17
 from IPython.utils.localinterfaces import LOCALHOST
22 18
 from IPython.utils.py3compat import bytes_to_str
@@ -86,7 +82,7 @@ def write_connection_file(fname=None, shell_port=0, iopub_port=0, stdin_port=0,
86 82
     cfg['ip'] = ip
87 83
     cfg['key'] = bytes_to_str(key)
88 84
     
89  
-    with open(fname, 'wb') as f:
  85
+    with open(fname, 'w') as f:
90 86
         f.write(json.dumps(cfg, indent=2))
91 87
     
92 88
     return fname, cfg
36  IPython/zmq/jsonapi.py
... ...
@@ -1,36 +0,0 @@
1  
-"""Wrap zmq's jsonapi and work around api incompatibilities.
2  
-
3  
-This file is effectively a replacement for zmq.utils.jsonapi, that works around
4  
-incompatibilities between jsonlib and the stdlib json, such as the
5  
-interpretation of the 'indent' keyword in dumps().
6  
-"""
7  
-#-----------------------------------------------------------------------------
8  
-#  Copyright (C) 2011  The IPython Development Team
9  
-#
10  
-#  Distributed under the terms of the BSD License.  The full license is in
11  
-#  the file COPYING, distributed as part of this software.
12  
-#-----------------------------------------------------------------------------
13  
-
14  
-#-----------------------------------------------------------------------------
15  
-# Imports
16  
-#-----------------------------------------------------------------------------
17  
-
18  
-from zmq.utils import jsonapi as _json
19  
-from zmq.utils.jsonapi import *
20  
-
21  
-#-----------------------------------------------------------------------------
22  
-# Function definitions
23  
-#-----------------------------------------------------------------------------
24  
-try:
25  
-    _json.dumps(1, indent=2)
26  
-except TypeError:
27  
-    # This happens with jsonlib, which takes indent as a string instead of as
28  
-    # an int.
29  
-    def dumps(o, **kw):
30  
-        if 'indent' in kw:
31  
-            indent = kw.pop('indent')
32  
-            if isinstance(indent, int):
33  
-                indent = ' ' * indent
34  
-            kw['indent'] = indent
35  
-            
36  
-        return _json.dumps(o, **kw)
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.