vinegar.py :  » Web-Services » RPyC » rpyc-3.0.7 » rpyc » core » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Services » RPyC 
RPyC » rpyc 3.0.7 » rpyc » core » vinegar.py
"""
vinegar ('when things go sour'): safe serialization of exceptions.

note that by changing the configuration parameters, this module can be
made non-secure
"""
import sys
import exceptions
import traceback
from types import InstanceType,ClassType
from rpyc.core import brine
from rpyc.core import consts


class GenericException(Exception):
    pass

_generic_exceptions_cache = {}

STOP_ITERATION_MAGIC = 0

def dump(typ, val, tb, include_local_traceback):
    if type(typ) is str:
        return typ
    if typ is StopIteration:
        return consts.EXC_STOP_ITERATION # optimization
    
    if include_local_traceback:
        tbtext = "".join(traceback.format_exception(typ, val, tb))
    else:
        tbtext = "<traceback denied>"
    attrs = []
    args = []
    for name in dir(val):
        if name == "args":
            for a in val.args:
                if brine.dumpable(a):
                    args.append(a)
                else:
                    args.append(repr(a))
        elif not name.startswith("_") or name == "_remote_tb":
            attrval = getattr(val, name)
            if not brine.dumpable(attrval):
                attrval = repr(attrval)
            attrs.append((name, attrval))
    return (typ.__module__, typ.__name__), tuple(args), tuple(attrs), tbtext

try:
    BaseException
except NameError:
    # python 2.4 compatible
    BaseException = Exception

def load(val, import_custom_exceptions, instantiate_custom_exceptions, instantiate_oldstyle_exceptions):
    if val == consts.EXC_STOP_ITERATION:
        return StopIteration # optimization
    if type(val) is str:
        return val # deprecated string exceptions
    
    (modname, clsname), args, attrs, tbtext = val
    if import_custom_exceptions and modname not in sys.modules:
        try:
            mod = __import__(modname, None, None, "*")
        except ImportError:
            pass
    if instantiate_custom_exceptions:
        cls = getattr(sys.modules[modname], clsname, None)
    elif modname == "exceptions":
        cls = getattr(exceptions, clsname, None)
    else:
        cls = None
    
    if not isinstance(cls, (type, ClassType)):
        cls = None
    elif issubclass(cls, ClassType) and not instantiate_oldstyle_exceptions:
        cls = None
    elif not issubclass(cls, BaseException):
        cls = None
    
    if cls is None:
        fullname = "%s.%s" % (modname, clsname)
        if fullname not in _generic_exceptions_cache:
            fakemodule = {"__module__" : "%s.%s" % (__name__, modname)}
            if isinstance(GenericException, ClassType):
                _generic_exceptions_cache[fullname] = ClassType(fullname, (GenericException,), fakemodule)
            else:
                _generic_exceptions_cache[fullname] = type(fullname, (GenericException,), fakemodule)
        cls = _generic_exceptions_cache[fullname]
    
    # support old-style exception classes
    if isinstance(cls, ClassType):
        exc = InstanceType(cls)
    else:
        exc = cls.__new__(cls)
    
    exc.args = args
    for name, attrval in attrs:
        setattr(exc, name, attrval)
    if hasattr(exc, "_remote_tb"):
        exc._remote_tb += (tbtext,)
    else:
        exc._remote_tb = (tbtext,)
    return exc


#===============================================================================
# customized except hook
#===============================================================================
if hasattr(sys, "excepthook"):
    _orig_excepthook = sys.excepthook
else:
    # ironpython forgot to implement excepthook, scheisse
    _orig_excepthook = None

def rpyc_excepthook(typ, val, tb):
    if hasattr(val, "_remote_tb"):
        sys.stderr.write("======= Remote traceback =======\n")
        tbtext = "\n--------------------------------\n\n".join(val._remote_tb)
        sys.stderr.write(tbtext)
        sys.stderr.write("\n======= Local exception ========\n")
    _orig_excepthook(typ, val, tb)

def install_rpyc_excepthook():
    if _orig_excepthook is not None:
        sys.excepthook = rpyc_excepthook

def uninstall_rpyc_excepthook():
    if _orig_excepthook is not None:
        sys.excepthook = _orig_excepthook


www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.