def test_constructor():
    #the argument can be callable
    copyreg.constructor(testclass)
    
    #the argument can not be callable
    AssertError(TypeError,copyreg.constructor,0)
    AssertError(TypeError,copyreg.constructor,"Hello")
    AssertError(TypeError,copyreg.constructor,True)
Exemple #2
0
 def test_constructor(self):
     #the argument can be callable
     copyreg.constructor(testclass)
     
     #the argument can not be callable
     self.assertRaises(TypeError,copyreg.constructor,0)
     self.assertRaises(TypeError,copyreg.constructor,"Hello")
     self.assertRaises(TypeError,copyreg.constructor,True)
Exemple #3
0
# keep namespace clean
__version__ = version
__author__ = author
__license__ = license
del version, author, license


# support functions for pickling and unpickling
def __createPolygon(contour, hole):
    """rebuild Polygon from pickled data"""
    p = Polygon()
    for c, h in zip(contour, hole):
        p.addContour(c, h)
    return p


def __tuples(a):
    """map an array or list of lists to a tuple of tuples"""
    return tuple(tuple(i) for i in a)


def __reducePolygon(p):
    """return pickle data for Polygon """
    return (__createPolygon, (tuple([__tuples(x) for x in p]), p.isHole()))


import copyreg
copyreg.constructor(__createPolygon)
copyreg.pickle(Polygon, __reducePolygon, __createPolygon)
del copyreg
Exemple #4
0
# keep namespace clean
__version__ = version
__author__  = author
__license__ = license
del version, author, license


# support functions for pickling and unpickling
def __createPolygon(contour, hole):
    """rebuild Polygon from pickled data"""
    p = Polygon()
    for c, h in zip(contour, hole):
        p.addContour(c, h)
    return p


def __tuples(a):
    """map an array or list of lists to a tuple of tuples"""
    return tuple(tuple(i) for i in a)


def __reducePolygon(p):
    """return pickle data for Polygon """
    return (__createPolygon, (tuple([__tuples(x) for x in p]), p.isHole()))


import copyreg
copyreg.constructor(__createPolygon)
copyreg.pickle(Polygon, __reducePolygon, __createPolygon)
del copyreg
Exemple #5
0
# another, and what about __hash__ ?  So the unpickled version doesn't
# equal the pickled version, and the cmodule cache is not happy with
# the situation. The old back-end have this same comment and use the
# same mechanism.
def GpuArray_unpickler(npa, ctx_name):
    if config.experimental__unpickle_gpu_on_cpu:
        # directly return numpy array
        warnings.warn(
            "config.experimental__unpickle_gpu_on_cpu is set to True. "
            "Unpickling GpuArray as numpy.ndarray")
        return npa
    elif pygpu:
        ctx = get_context(ctx_name)
        return pygpu.gpuarray.array(npa, copy=True, context=ctx)
    else:
        raise ImportError("pygpu not found. Cannot unpickle GpuArray")


copyreg.constructor(GpuArray_unpickler)


def GpuArray_pickler(cnda):
    ctx_name = _name_for_ctx(cnda.context)
    return (GpuArray_unpickler, (np.asarray(cnda), ctx_name))


# In case pygpu is not imported.
if pygpu is not None:
    copyreg.pickle(pygpu.gpuarray.GpuArray, GpuArray_pickler,
                   GpuArray_unpickler)
Exemple #6
0
if needsGlobal:
    # change dl flags to load dictionaries from pre-linked .so's
    dlflags = sys.getdlopenflags()
    sys.setdlopenflags(0x100 | 0x2)  # RTLD_GLOBAL | RTLD_NOW

import libPyROOT as _root

# reset dl flags if needed
if needsGlobal:
    sys.setdlopenflags(dlflags)
del needsGlobal

## convince 2.2 it's ok to use the expand function
if sys.version[0:3] == '2.2':
    import copyreg
    copyreg.constructor(_root._ObjectProxy__expand__)

## convince inspect that PyROOT method proxies are possible drop-ins for python
## methods and classes for pydoc
import inspect

inspect._old_isfunction = inspect.isfunction


def isfunction(object):
    if type(object) == _root.MethodProxy and not object.__self__.__class__:
        return True
    return inspect._old_isfunction(object)


inspect.isfunction = isfunction
Exemple #7
0
    def get_description(self):
        return self.__description

    def get_name(self):
        return self.__name

    def __str__(self):
        return 'PermissionSetting: %s' % self.__name

    __repr__ = __str__


# register PermissionSettings to be symbolic constants by identity,
# even when pickled and unpickled.
copyreg.constructor(PermissionSetting)
copyreg.pickle(PermissionSetting, PermissionSetting.get_name,
               PermissionSetting)

Allow = PermissionSetting('Allow', 'Explicit allow setting for permissions')

Deny = PermissionSetting('Deny', 'Explicit deny setting for permissions')

AllowSingle = PermissionSetting('AllowSingle',
                                'Explicit allow and not inherit permission')

Unset = PermissionSetting(
    'Unset', 'Unset constant that denotes no setting for permission')


class IRole(Interface):  # pylint: disable=E0239
Exemple #8
0
    def get_description(self):
        return self.__description

    def get_name(self):
        return self.__name

    def __str__(self):
        return 'PermissionSetting: %s' % self.__name

    __repr__ = __str__


# register PermissionSettings to be symbolic constants by identity,
# even when pickled and unpickled.
copyreg.constructor(PermissionSetting)
copyreg.pickle(PermissionSetting,
               PermissionSetting.get_name,
               PermissionSetting)


Allow = PermissionSetting(
    'Allow', 'Explicit allow setting for permissions')

Deny = PermissionSetting(
    'Deny', 'Explicit deny setting for permissions')

AllowSingle = PermissionSetting(
    'AllowSingle', 'Explicit allow and not inherit permission')

Unset = PermissionSetting(
# We do not want to actually import anything from this module
__all__ = []

# copyreg is called copy_reg in python2
try:

    import copyreg  #py3

except ImportError:

    import copy_reg as copyreg  #py2


# Model serializer
def serialize_model(model):

    dict_with_types = model.to_dict_with_types()

    return unserialize_model, (dict_with_types, )


def unserialize_model(dict_with_types):

    return ModelParser(model_dict=dict_with_types).get_model()


# Register serialization/unserialization for Model
copyreg.constructor(unserialize_model)
copyreg.pickle(Model, serialize_model, unserialize_model)
Exemple #10
0
 def update_event(self, inp=-1):
     self.set_output_val(0, copyreg.constructor(self.input(0)))
if needsGlobal:
 # change dl flags to load dictionaries from pre-linked .so's
   dlflags = sys.getdlopenflags()
   sys.setdlopenflags( 0x100 | 0x2 )    # RTLD_GLOBAL | RTLD_NOW

import libPyROOT as _root

# reset dl flags if needed
if needsGlobal:
   sys.setdlopenflags( dlflags )
del needsGlobal

## convince 2.2 it's ok to use the expand function
if sys.version[0:3] == '2.2':
   import copyreg
   copyreg.constructor( _root._ObjectProxy__expand__ )

## convince inspect that PyROOT method proxies are possible drop-ins for python
## methods and classes for pydoc
import inspect

inspect._old_isfunction = inspect.isfunction
def isfunction( object ):
   if type(object) == _root.MethodProxy and not object.__self__.__class__:
      return True
   return inspect._old_isfunction( object )
inspect.isfunction = isfunction

inspect._old_ismethod = inspect.ismethod
def ismethod( object ):
   if type(object) == _root.MethodProxy: