Exemplo n.º 1
0
def getStreamedConstant(constant_value):
    # Note: The marshal module cannot persist all unicode strings and
    # therefore cannot be used. Instead we use pickle.
    try:
        saved = cpickle.dumps(
            constant_value,
            protocol = 0 if type(constant_value) is unicode else pickle_protocol
        )
    except TypeError:
        warning("Problem with persisting constant '%r'." % constant_value)
        raise

    saved = pickletools.optimize(saved)

    # Check that the constant is restored correctly.
    try:
        restored = cpickle.loads(
            saved
        )
    except:
        warning("Problem with persisting constant '%r'." % constant_value)
        raise

    if not Constants.compareConstants(restored, constant_value):
        raise AssertionError(
            "Streaming of constant changed value",
            constant_value,
            "!=",
            restored,
            "types:",
            type(constant_value),
            type(restored)
        )

    return saved
Exemplo n.º 2
0
def getStreamedConstant(constant_value):
    # Note: The marshal module cannot persist all unicode strings and
    # therefore cannot be used. Instead we use pickle.
    try:
        saved = cpickle.dumps(
            constant_value,
            protocol=0 if type(constant_value) is unicode else pickle_protocol)
    except TypeError:
        warning("Problem with persisting constant '%r'." % constant_value)
        raise

    saved = pickletools.optimize(saved)

    # Check that the constant is restored correctly.
    try:
        restored = cpickle.loads(saved)
    except:
        warning("Problem with persisting constant '%r'." % constant_value)
        raise

    if not Constants.compareConstants(restored, constant_value):
        raise AssertionError("Streaming of constant changed value",
                             constant_value, "!=", restored, "types:",
                             type(constant_value), type(restored))

    return saved