##// END OF EJS Templates
Work to address the review comments on Fernando's branch....
Work to address the review comments on Fernando's branch. * Added comment about Magic(object) (r1224) * Moved InteractiveTB.set_mode from IPythonApp -> InteractiveShell (r1229) * Moved pylabtools.py to IPython/lib (r1229) * Cleaned up comments and copyrights in testing (r1233) * Added comment about ip.shell._ofind (r1237) * Removed "Bye." from quitter (r1240). * Refactored and removed :mod:`IPython.utils.genutils` and :mod:`IPython.utils.platutils`. These modules have been replaced by topical focused modules in :mod:`IPython.utils`. * Refactored tests in :mod:`IPython.utils.tests`. * Moved :func:`IPython.testing.tools.temp_pyfile` to :mod:`IPython.utils.io`. * Moved :func:`IPython.testing.tools.cmd2argv` to :func:`IPython.testing.tools.pycmd2argv` and documented the fact that this only works with Python based command line programs. * Created a new :func:`IPython.utils.path.get_ipython_module_path` to use in finding paths to IPython modules.

File last commit:

r2498:3eae1372
r2498:3eae1372
Show More
pbutil.py
91 lines | 3.1 KiB | text/x-python | PythonLexer
# encoding: utf-8
"""Utilities for PB using modules."""
__docformat__ = "restructuredtext en"
#-------------------------------------------------------------------------------
# Copyright (C) 2008 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
#-------------------------------------------------------------------------------
import cPickle as pickle
from twisted.python.failure import Failure
from twisted.python import failure
from IPython.kernel import pbconfig
from IPython.kernel.error import PBMessageSizeError, UnpickleableException
#-------------------------------------------------------------------------------
# The actual utilities
#-------------------------------------------------------------------------------
def packageFailure(f):
"""Clean and pickle a failure preappending the string FAILURE:"""
f.cleanFailure()
# This is sometimes helpful in debugging
#f.raiseException()
try:
pString = pickle.dumps(f, 2)
except pickle.PicklingError:
# Certain types of exceptions are not pickleable, for instance ones
# from Boost.Python. We try to wrap them in something that is
f.type = UnpickleableException
f.value = UnpickleableException(str(f.type) + ": " + str(f.value))
pString = pickle.dumps(f, 2)
return 'FAILURE:' + pString
def unpackageFailure(r):
"""
See if a returned value is a pickled Failure object.
To distinguish between general pickled objects and pickled Failures, the
other side should prepend the string FAILURE: to any pickled Failure.
"""
if isinstance(r, str):
if r.startswith('FAILURE:'):
try:
result = pickle.loads(r[8:])
except pickle.PickleError:
return failure.Failure( \
UnpickleableException("Could not unpickle failure."))
else:
return result
return r
def checkMessageSize(m, info):
"""Check string m to see if it violates banana.SIZE_LIMIT.
This should be used on the client side of things for push, scatter
and push_serialized and on the other end for pull, gather and pull_serialized.
:Parameters:
`m` : string
Message whose size will be checked.
`info` : string
String describing what object the message refers to.
:Exceptions:
- `PBMessageSizeError`: Raised in the message is > banana.SIZE_LIMIT
:returns: The original message or a Failure wrapping a PBMessageSizeError
"""
if len(m) > pbconfig.banana.SIZE_LIMIT:
s = """Objects too big to transfer:
Names: %s
Actual Size (kB): %d
SIZE_LIMIT (kB): %d
* SIZE_LIMIT can be set in kernel.pbconfig""" \
% (info, len(m)/1024, pbconfig.banana.SIZE_LIMIT/1024)
return Failure(PBMessageSizeError(s))
else:
return m