def flatten_unknown_value(self, value, caps, freezing):
        # Flatten enums
        if isinstance(value, enum.Enum):
            return self.flatten_enum_value(value, caps, freezing)

        # Flatten types and interfaces
        if isinstance(value, (type, InterfaceClass)):
            return self.flatten_type_value(value, caps, freezing)

        if self._externalizer is not None:
            extid = self._externalizer.identify(value)
            if extid is not None:
                return self.flatten_external(extid, caps, freezing)

        # Checks if value support the current required protocol
        # Could be ISnapshotable or ISerializable
        if freezing:

            try:
                snapshotable = ISnapshotable(value)
            except TypeError:
                raise_(
                    TypeError, "Freezing of type %s values "
                    "not supported by %s. Value = %r." %
                    (type(value).__name__, reflect.canonical_name(self),
                     value),
                    sys.exc_info()[2])

            return self.flatten_instance(snapshotable, caps, freezing)

        else:

            try:
                serializable = ISerializable(value)
            except TypeError:
                raise_(
                    TypeError, "Serialization of type %s values "
                    "not supported by %s. Value = %r." %
                    (reflect.canonical_name(value),
                     reflect.canonical_name(self), value),
                    sys.exc_info()[2])

            return self.flatten_instance(serializable, caps, freezing)
Beispiel #2
0
 def get_identifier(self, instance):
     if ISerializable.providedBy(instance):
         return instance.type_name, id(instance)
     return id(instance)
Beispiel #3
0
def assert_equal_but_different(
        value, expected, idx, valids, expids, strict=True):
    '''
    idx is used to identify every values uniquely to be able to verify
    references are made to the same value, valids and expids are
    dictionaries with instance id() for key and idx for value.
    Types and interfaces are assumed to be immutable atoms.'''

    # Only check references for type that can be referenced.
    # Let the immutable type do what they want, sometime strings
    # are interned sometime no, we don't care.
    basic_types = (int, long, float, bool, str, unicode, type(None))
    if not isinstance(expected, basic_types):
        # Get unique instance identifiers
        if not strict:
            value, expected = _get_resolved_dereference(value, expected)
        expid = id(expected)
        valid = id(value)

        if expid in expids:
            # Expected value is a reference, check the other value is too
            assert valid in valids
            # Check the two reference the same value inside the structure
            assert valids[valid] == expids[expid]
            return idx

        # Check the other value is not a reference if it wasn't expected
        assert valid not in valids

        # Store the instance identifiers for later checks
        expids[expid] = idx
        valids[valid] = idx

    if expected is None:
        assert expected == value
    elif isinstance(expected, (list, tuple)):
        if expected != ():  # Special case for tuple singleton
            assert value is not expected

        assert len(expected) == len(value)
        for exp, val in zip(expected, value):
            idx = assert_equal_but_different(
                val, exp, idx + 1, valids, expids, strict=strict)
    elif isinstance(expected, set):
        assert len(expected) == len(value)
        for exp in expected:
            assert exp in value
            val = [v for v in value if v == exp][0]
            idx = assert_equal_but_different(
                val, exp, idx + 1, valids, expids, strict=strict)
    elif isinstance(expected, dict):
        assert len(expected) == len(value)
        for exp_key, exp_val in expected.items():
            assert exp_key in value
            val_key = [k for k in value if k == exp_key][0]
            val_val = value[val_key]
            idx = assert_equal_but_different(
                val_key, exp_key, idx + 1, valids, expids, strict=strict)
            idx = assert_equal_but_different(
                val_val, exp_val, idx + 1, valids, expids, strict=strict)
    elif isinstance(value, float):
        assert round(value - expected, 7) == 0
    elif isinstance(value, (int, long, bool, str, unicode,
                            type, InterfaceClass, enum.Enum)):
        assert value == expected
    else:
        assert expected is not value
        if ISerializable.providedBy(expected):
            assert ISerializable.providedBy(value)
        idx = assert_equal_but_different(
            value.__dict__, expected.__dict__, idx + 1, valids, expids,
            strict=strict)
    return idx
 def get_identifier(self, instance):
     if ISerializable.providedBy(instance):
         return instance.type_name, id(instance)
     return id(instance)
Beispiel #5
0
def assert_equal_but_different(value,
                               expected,
                               idx,
                               valids,
                               expids,
                               strict=True):
    '''
    idx is used to identify every values uniquely to be able to verify
    references are made to the same value, valids and expids are
    dictionaries with instance id() for key and idx for value.
    Types and interfaces are assumed to be immutable atoms.'''

    # Only check references for type that can be referenced.
    # Let the immutable type do what they want, sometime strings
    # are interned sometime no, we don't care.
    basic_types = (int, long, float, bool, str, unicode, type(None))
    if not isinstance(expected, basic_types):
        # Get unique instance identifiers
        if not strict:
            value, expected = _get_resolved_dereference(value, expected)
        expid = id(expected)
        valid = id(value)

        if expid in expids:
            # Expected value is a reference, check the other value is too
            assert valid in valids
            # Check the two reference the same value inside the structure
            assert valids[valid] == expids[expid]
            return idx

        # Check the other value is not a reference if it wasn't expected
        assert valid not in valids

        # Store the instance identifiers for later checks
        expids[expid] = idx
        valids[valid] = idx

    if expected is None:
        assert expected == value
    elif isinstance(expected, (list, tuple)):
        if expected != ():  # Special case for tuple singleton
            assert value is not expected

        assert len(expected) == len(value)
        for exp, val in zip(expected, value):
            idx = assert_equal_but_different(val,
                                             exp,
                                             idx + 1,
                                             valids,
                                             expids,
                                             strict=strict)
    elif isinstance(expected, set):
        assert len(expected) == len(value)
        for exp in expected:
            assert exp in value
            val = [v for v in value if v == exp][0]
            idx = assert_equal_but_different(val,
                                             exp,
                                             idx + 1,
                                             valids,
                                             expids,
                                             strict=strict)
    elif isinstance(expected, dict):
        assert len(expected) == len(value)
        for exp_key, exp_val in expected.items():
            assert exp_key in value
            val_key = [k for k in value if k == exp_key][0]
            val_val = value[val_key]
            idx = assert_equal_but_different(val_key,
                                             exp_key,
                                             idx + 1,
                                             valids,
                                             expids,
                                             strict=strict)
            idx = assert_equal_but_different(val_val,
                                             exp_val,
                                             idx + 1,
                                             valids,
                                             expids,
                                             strict=strict)
    elif isinstance(value, float):
        assert round(value - expected, 7) == 0
    elif isinstance(
            value,
        (int, long, bool, str, unicode, type, InterfaceClass, enum.Enum)):
        assert value == expected
    else:
        assert expected is not value
        if ISerializable.providedBy(expected):
            assert ISerializable.providedBy(value)
        idx = assert_equal_but_different(value.__dict__,
                                         expected.__dict__,
                                         idx + 1,
                                         valids,
                                         expids,
                                         strict=strict)
    return idx