Python marshal.loads() Examples

The following are 30 code examples of marshal.loads(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module marshal , or try the search function .
Example #1
Source File: __init__.py    From pytest-forked with MIT License 6 votes vote down vote up
def forked_run_report(item):
    # for now, we run setup/teardown in the subprocess
    # XXX optionally allow sharing of setup/teardown
    from _pytest.runner import runtestprotocol
    EXITSTATUS_TESTEXIT = 4
    import marshal

    def runforked():
        try:
            reports = runtestprotocol(item, log=False)
        except KeyboardInterrupt:
            os._exit(EXITSTATUS_TESTEXIT)
        return marshal.dumps([serialize_report(x) for x in reports])

    ff = py.process.ForkedFunc(runforked)
    result = ff.waitfinish()
    if result.retval is not None:
        report_dumps = marshal.loads(result.retval)
        return [runner.TestReport(**x) for x in report_dumps]
    else:
        if result.exitstatus == EXITSTATUS_TESTEXIT:
            pytest.exit("forked test item %s raised Exit" % (item,))
        return [report_process_crash(item, result)] 
Example #2
Source File: SockPuppet.py    From ALF with Apache License 2.0 6 votes vote down vote up
def recv_data(self):
        data_remaining = struct.unpack("I", self.conn.recv(4))[0]
        if not data_remaining:
            log.debug("no data?!")
            return None
        log.debug("<- recving %d bytes", data_remaining)
        data = []
        while data_remaining:
            recv_bytes = data_remaining if data_remaining < self.SOCK_BUF else self.SOCK_BUF
            data.append(self.conn.recv(recv_bytes))
            data_len = len(data[-1])
            if data_len == 0:
                break
            data_remaining -= data_len
        data = pickle.loads("".join(data))
        if data["cmd"] != self.ACK:
            self.send_ack()
        return data 
Example #3
Source File: test_marshal.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_loads_recursion(self):
        def run_tests(N, check):
            # (((...None...),),)
            check(b'(\x01\x00\x00\x00' * N + b'N')
            # [[[...None...]]]
            check(b'[\x01\x00\x00\x00' * N + b'N')
            # {None: {None: {None: ...None...}}}
            check(b'{N' * N + b'N' + b'0' * N)
            # frozenset([frozenset([frozenset([...None...])])])
            check(b'>\x01\x00\x00\x00' * N + b'N')
        # Check that the generated marshal data is valid and marshal.loads()
        # works for moderately deep nesting
        run_tests(100, marshal.loads)
        # Very deeply nested structure shouldn't blow the stack
        def check(s):
            self.assertRaises(ValueError, marshal.loads, s)
        run_tests(2**20, check) 
Example #4
Source File: forkedfunc.py    From py with MIT License 6 votes vote down vote up
def waitfinish(self, waiter=os.waitpid):
        pid, systemstatus = waiter(self.pid, 0)
        if systemstatus:
            if os.WIFSIGNALED(systemstatus):
                exitstatus = os.WTERMSIG(systemstatus) + 128
            else:
                exitstatus = os.WEXITSTATUS(systemstatus)
        else:
            exitstatus = 0
        signal = systemstatus & 0x7f
        if not exitstatus and not signal:
            retval = self.RETVAL.open('rb')
            try:
                retval_data = retval.read()
            finally:
                retval.close()
            retval = marshal.loads(retval_data)
        else:
            retval = None
        stdout = self.STDOUT.read()
        stderr = self.STDERR.read()
        self._removetemp()
        return Result(exitstatus, signal, retval, stdout, stderr) 
Example #5
Source File: generic_utils.py    From lambda-packs with MIT License 6 votes vote down vote up
def func_load(code, defaults=None, closure=None, globs=None):
  """Deserializes a user defined function.

  Arguments:
      code: bytecode of the function.
      defaults: defaults of the function.
      closure: closure of the function.
      globs: dictionary of global objects.

  Returns:
      A function object.
  """
  if isinstance(code, (tuple, list)):  # unpack previous dump
    code, defaults, closure = code
    if isinstance(defaults, list):
      defaults = tuple(defaults)
  code = marshal.loads(code.encode('raw_unicode_escape'))
  if globs is None:
    globs = globals()
  return python_types.FunctionType(
      code, globs, name=code.co_name, argdefs=defaults, closure=closure) 
Example #6
Source File: forkedfunc.py    From python-netsurv with MIT License 6 votes vote down vote up
def waitfinish(self, waiter=os.waitpid):
        pid, systemstatus = waiter(self.pid, 0)
        if systemstatus:
            if os.WIFSIGNALED(systemstatus):
                exitstatus = os.WTERMSIG(systemstatus) + 128
            else:
                exitstatus = os.WEXITSTATUS(systemstatus)
        else:
            exitstatus = 0
        signal = systemstatus & 0x7f
        if not exitstatus and not signal:
            retval = self.RETVAL.open('rb')
            try:
                retval_data = retval.read()
            finally:
                retval.close()
            retval = marshal.loads(retval_data)
        else:
            retval = None
        stdout = self.STDOUT.read()
        stderr = self.STDERR.read()
        self._removetemp()
        return Result(exitstatus, signal, retval, stdout, stderr) 
Example #7
Source File: test_marshal.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_int64(self):
        # Simulate int marshaling on a 64-bit box.  This is most interesting if
        # we're running the test on a 32-bit box, of course.

        def to_little_endian_string(value, nbytes):
            bytes = []
            for i in range(nbytes):
                bytes.append(chr(value & 0xff))
                value >>= 8
            return ''.join(bytes)

        maxint64 = (1L << 63) - 1
        minint64 = -maxint64-1

        for base in maxint64, minint64, -maxint64, -(minint64 >> 1):
            while base:
                s = 'I' + to_little_endian_string(base, 8)
                got = marshal.loads(s)
                self.assertEqual(base, got)
                if base == -1:  # a fixed-point for shifting right 1
                    base = 0
                else:
                    base >>= 1 
Example #8
Source File: forkedfunc.py    From python-netsurv with MIT License 6 votes vote down vote up
def waitfinish(self, waiter=os.waitpid):
        pid, systemstatus = waiter(self.pid, 0)
        if systemstatus:
            if os.WIFSIGNALED(systemstatus):
                exitstatus = os.WTERMSIG(systemstatus) + 128
            else:
                exitstatus = os.WEXITSTATUS(systemstatus)
        else:
            exitstatus = 0
        signal = systemstatus & 0x7f
        if not exitstatus and not signal:
            retval = self.RETVAL.open('rb')
            try:
                retval_data = retval.read()
            finally:
                retval.close()
            retval = marshal.loads(retval_data)
        else:
            retval = None
        stdout = self.STDOUT.read()
        stderr = self.STDERR.read()
        self._removetemp()
        return Result(exitstatus, signal, retval, stdout, stderr) 
Example #9
Source File: test_marshal.py    From BinderFilter with MIT License 6 votes vote down vote up
def test_int64(self):
        # Simulate int marshaling on a 64-bit box.  This is most interesting if
        # we're running the test on a 32-bit box, of course.

        def to_little_endian_string(value, nbytes):
            bytes = []
            for i in range(nbytes):
                bytes.append(chr(value & 0xff))
                value >>= 8
            return ''.join(bytes)

        maxint64 = (1L << 63) - 1
        minint64 = -maxint64-1

        for base in maxint64, minint64, -maxint64, -(minint64 >> 1):
            while base:
                s = 'I' + to_little_endian_string(base, 8)
                got = marshal.loads(s)
                self.assertEqual(base, got)
                if base == -1:  # a fixed-point for shifting right 1
                    base = 0
                else:
                    base >>= 1 
Example #10
Source File: test_marshal.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_fuzz(self):
        # simple test that it's at least not *totally* trivial to
        # crash from bad marshal data
        for c in [chr(i) for i in range(256)]:
            try:
                marshal.loads(c)
            except Exception:
                pass 
Example #11
Source File: test_marshal.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_ints(self):
        # Test the full range of Python ints.
        n = sys.maxint
        while n:
            for expected in (-n, n):
                s = marshal.dumps(expected)
                got = marshal.loads(s)
                self.assertEqual(expected, got)
                marshal.dump(expected, file(test_support.TESTFN, "wb"))
                got = marshal.load(file(test_support.TESTFN, "rb"))
                self.assertEqual(expected, got)
            n = n >> 1
        os.unlink(test_support.TESTFN) 
Example #12
Source File: rpc.py    From BinderFilter with MIT License 5 votes vote down vote up
def unpickle_code(ms):
    co = marshal.loads(ms)
    assert isinstance(co, types.CodeType)
    return co 
Example #13
Source File: test_bool.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_marshal(self):
        import marshal
        self.assertIs(marshal.loads(marshal.dumps(True)), True)
        self.assertIs(marshal.loads(marshal.dumps(False)), False) 
Example #14
Source File: test_marshal.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_loads_recursion(self):
        s = 'c' + ('X' * 4*4) + '{' * 2**20
        self.assertRaises(ValueError, marshal.loads, s) 
Example #15
Source File: test_marshal.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_exact_type_match(self):
        # Former bug:
        #   >>> class Int(int): pass
        #   >>> type(loads(dumps(Int())))
        #   <type 'int'>
        for typ in (int, long, float, complex, tuple, list, dict, set, frozenset):
            # Note: str and unicode subclasses are not tested because they get handled
            # by marshal's routines for objects supporting the buffer API.
            subtyp = type('subtyp', (typ,), {})
            self.assertRaises(ValueError, marshal.dumps, subtyp())

    # Issue #1792 introduced a change in how marshal increases the size of its
    # internal buffer; this test ensures that the new code is exercised. 
Example #16
Source File: test_marshal.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_invalid_longs(self):
        # Issue #7019: marshal.loads shouldn't produce unnormalized PyLongs
        invalid_string = 'l\x02\x00\x00\x00\x00\x00\x00\x00'
        self.assertRaises(ValueError, marshal.loads, invalid_string) 
Example #17
Source File: bccache.py    From RSSNewsGAE with Apache License 2.0 5 votes vote down vote up
def marshal_load(f):
        if isinstance(f, file):
            return marshal.load(f)
        return marshal.loads(f.read()) 
Example #18
Source File: test_bool.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_cpickle(self):
        import cPickle
        self.assertIs(cPickle.loads(cPickle.dumps(True)), True)
        self.assertIs(cPickle.loads(cPickle.dumps(False)), False)
        self.assertIs(cPickle.loads(cPickle.dumps(True, True)), True)
        self.assertIs(cPickle.loads(cPickle.dumps(False, True)), False) 
Example #19
Source File: test_bool.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_pickle(self):
        import pickle
        self.assertIs(pickle.loads(pickle.dumps(True)), True)
        self.assertIs(pickle.loads(pickle.dumps(False)), False)
        self.assertIs(pickle.loads(pickle.dumps(True, True)), True)
        self.assertIs(pickle.loads(pickle.dumps(False, True)), False) 
Example #20
Source File: test_marshal.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_version_argument(self):
        # Python 2.4.0 crashes for any call to marshal.dumps(x, y)
        self.assertEqual(marshal.loads(marshal.dumps(5, 0)), 5)
        self.assertEqual(marshal.loads(marshal.dumps(5, 1)), 5) 
Example #21
Source File: test_getargs.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_with_marshal(self):
        arg = unicode(r'\222', 'unicode-escape')
        self.assertRaises(UnicodeError, marshal.loads, arg) 
Example #22
Source File: pickle.py    From BinderFilter with MIT License 5 votes vote down vote up
def loads(str):
    file = StringIO(str)
    return Unpickler(file).load()

# Doctest 
Example #23
Source File: convert_pyc_opcodes.py    From poc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def unmarshal_pyc_code(filename):
    bytes = read_file(filename)
    obj = marshal.loads(bytes[8:])
    return obj

# flip a dictionary so the keys become values. vice versa 
Example #24
Source File: test_marshal.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_binary_floats(self):
        self.assertEqual(marshal.dumps(2.0, 2), 'g\x00\x00\x00\x00\x00\x00\x00@')
        self.assertEqual(marshal.dumps(2.0), 'g\x00\x00\x00\x00\x00\x00\x00@')
        if is_cli: #https://github.com/IronLanguages/main/issues/854
            self.assertEqual(marshal.dumps(2.0, 1), 'f\x032.0')
        else:
            self.assertEqual(marshal.dumps(2.0, 1), 'f\x012')
        self.assertEqual(marshal.loads(marshal.dumps(2.0, 2)), 2.0) 
Example #25
Source File: test_marshal.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_buffer(self):
        for s in ['', ' ', 'abc ', 'abcdef']:
            x = marshal.dumps(buffer(s))
            self.assertEqual(marshal.loads(x), s)

        for s in ['', ' ', 'abc ', 'abcdef']:
            with open(self.tfn, 'wb') as f:
                marshal.dump(buffer(s), f)

            with open(self.tfn, 'rb') as f:
                x2 = marshal.load(f)

            self.assertEqual(s, x2) 
Example #26
Source File: test_marshal.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_write_object_to_file(self):
        obj = ('\u20ac', b'abc', 123, 45.6, 7+8j, 'long line '*1000)
        for v in range(marshal.version + 1):
            _testcapi.pymarshal_write_object_to_file(obj, test_support.TESTFN, v)
            with open(test_support.TESTFN, 'rb') as f:
                data = f.read()
            test_support.unlink(test_support.TESTFN)
            self.assertEqual(marshal.loads(data), obj) 
Example #27
Source File: test_marshal.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_invalid_longs(self):
        # Issue #7019: marshal.loads shouldn't produce unnormalized PyLongs
        invalid_string = 'l\x02\x00\x00\x00\x00\x00\x00\x00'
        self.assertRaises(ValueError, marshal.loads, invalid_string) 
Example #28
Source File: test_marshal.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_exact_type_match(self):
        # Former bug:
        #   >>> class Int(int): pass
        #   >>> type(loads(dumps(Int())))
        #   <type 'int'>
        for typ in (int, long, float, complex, tuple, list, dict, set, frozenset):
            # Note: str and unicode subclasses are not tested because they get handled
            # by marshal's routines for objects supporting the buffer API.
            subtyp = type('subtyp', (typ,), {})
            self.assertRaises(ValueError, marshal.dumps, subtyp())

    # Issue #1792 introduced a change in how marshal increases the size of its
    # internal buffer; this test ensures that the new code is exercised. 
Example #29
Source File: test_marshal.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_fuzz(self):
        # simple test that it's at least not *totally* trivial to
        # crash from bad marshal data
        for c in [chr(i) for i in range(256)]:
            try:
                marshal.loads(c)
            except Exception:
                pass 
Example #30
Source File: test_marshal.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_sets(self):
        for constructor in (set, frozenset):
            t = constructor(self.d.keys())
            new = marshal.loads(marshal.dumps(t))
            self.assertEqual(t, new)
            self.assertTrue(isinstance(new, constructor))
            self.assertNotEqual(id(t), id(new))
            marshal.dump(t, file(test_support.TESTFN, "wb"))
            new = marshal.load(file(test_support.TESTFN, "rb"))
            self.assertEqual(t, new)
            os.unlink(test_support.TESTFN)