From 3892cf3208339cdd2354306ba8f0cff13946fe69 2011-11-26 00:12:52 From: MinRK Date: 2011-11-26 00:12:52 Subject: [PATCH] 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 --- diff --git a/IPython/parallel/apps/ipcontrollerapp.py b/IPython/parallel/apps/ipcontrollerapp.py index c0600c2..4ef765d 100755 --- a/IPython/parallel/apps/ipcontrollerapp.py +++ b/IPython/parallel/apps/ipcontrollerapp.py @@ -23,6 +23,7 @@ Authors: from __future__ import with_statement +import json import os import socket import stat @@ -34,11 +35,6 @@ import zmq from zmq.devices import ProcessMonitoredQueue from zmq.log.handlers import PUBHandler -# Note: use our own import to work around jsonlib api mismatch. When these -# changes propagate to zmq, revert back to the following line instead: -#from zmq.utils import jsonapi as json -from IPython.zmq import jsonapi as json - from IPython.core.profiledir import ProfileDir from IPython.parallel.apps.baseapp import ( @@ -214,7 +210,7 @@ class IPControllerApp(BaseParallelApplication): location = '127.0.0.1' cdict['location'] = location fname = os.path.join(self.profile_dir.security_dir, fname) - with open(fname, 'wb') as f: + with open(fname, 'w') as f: f.write(json.dumps(cdict, indent=2)) os.chmod(fname, stat.S_IRUSR|stat.S_IWUSR) diff --git a/IPython/parallel/controller/scheduler.py b/IPython/parallel/controller/scheduler.py index 3d723e8..90e913e 100644 --- a/IPython/parallel/controller/scheduler.py +++ b/IPython/parallel/controller/scheduler.py @@ -359,7 +359,9 @@ class TaskScheduler(SessionFactory): # turn timeouts into datetime objects: timeout = header.get('timeout', None) if timeout: - timeout = datetime.now() + timedelta(0,timeout,0) + # cast to float, because jsonlib returns floats as decimal.Decimal, + # which timedelta does not accept + timeout = datetime.now() + timedelta(0,float(timeout),0) args = [raw_msg, targets, after, follow, timeout] diff --git a/IPython/zmq/entry_point.py b/IPython/zmq/entry_point.py index 75d7803..bff59b3 100644 --- a/IPython/zmq/entry_point.py +++ b/IPython/zmq/entry_point.py @@ -4,6 +4,7 @@ launchers. # Standard library imports. import atexit +import json import os import socket from subprocess import Popen, PIPE @@ -12,11 +13,6 @@ import tempfile # System library imports -# Note: use our own import to work around jsonlib api mismatch. When these -# changes propagate to zmq, revert back to the following line instead: -#from zmq.utils import jsonapi as json -from IPython.zmq import jsonapi as json - # IPython imports from IPython.utils.localinterfaces import LOCALHOST 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, cfg['ip'] = ip cfg['key'] = bytes_to_str(key) - with open(fname, 'wb') as f: + with open(fname, 'w') as f: f.write(json.dumps(cfg, indent=2)) return fname, cfg diff --git a/IPython/zmq/jsonapi.py b/IPython/zmq/jsonapi.py deleted file mode 100644 index cb2271c..0000000 --- a/IPython/zmq/jsonapi.py +++ /dev/null @@ -1,36 +0,0 @@ -"""Wrap zmq's jsonapi and work around api incompatibilities. - -This file is effectively a replacement for zmq.utils.jsonapi, that works around -incompatibilities between jsonlib and the stdlib json, such as the -interpretation of the 'indent' keyword in dumps(). -""" -#----------------------------------------------------------------------------- -# Copyright (C) 2011 The IPython Development Team -# -# Distributed under the terms of the BSD License. The full license is in -# the file COPYING, distributed as part of this software. -#----------------------------------------------------------------------------- - -#----------------------------------------------------------------------------- -# Imports -#----------------------------------------------------------------------------- - -from zmq.utils import jsonapi as _json -from zmq.utils.jsonapi import * - -#----------------------------------------------------------------------------- -# Function definitions -#----------------------------------------------------------------------------- -try: - _json.dumps(1, indent=2) -except TypeError: - # This happens with jsonlib, which takes indent as a string instead of as - # an int. - def dumps(o, **kw): - if 'indent' in kw: - indent = kw.pop('indent') - if isinstance(indent, int): - indent = ' ' * indent - kw['indent'] = indent - - return _json.dumps(o, **kw)