我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用marshal.dumps()。
def get_encoded_library_string(arch): filepath=None if arch=="x86": filepath=os.path.join("resources","libraryx86.zip") elif arch=="x64": filepath=os.path.join("resources","libraryx64.zip") else: raise Exception("unknown arch %s"%arch) f = StringIO.StringIO() f.write(open(filepath, "rb").read()) zip = zipfile.ZipFile(f) modules = dict([(z.filename, zip.open(z.filename,).read()) for z in zip. infolist() if os.path.splitext(z.filename)[1] in [".py",".pyd",".dll",".pyc",".pyo"]]) return zlib.compress(marshal.dumps(modules),9)
def geocode(api, params, l4): params['autocomplete']=0 params['q'] = params['q'].strip() try: r = requests.get(api, params) j = json.loads(r.text) global geocode_count geocode_count += 1 if 'features' in j and len(j['features'])>0: j['features'][0]['l4'] = l4 return(j['features'][0]) else: return(None) except: print(json.dumps({'action':'erreur','params': params, 'l4': l4})) return(None)
def dump_stream(self, iterator, stream): batch, best = 1, self.bestSize iterator = iter(iterator) while True: vs = list(itertools.islice(iterator, batch)) if not vs: break bytes = self.serializer.dumps(vs) write_int(len(bytes), stream) stream.write(bytes) size = len(bytes) if size < best: batch *= 2 elif size > best * 10 and batch > 1: batch //= 2
def _on_request(self, event, request, response): if request.path in ("/+login", "/+logout"): return repo = short(self.environ.storage.repo_node()) sess = md5(dumps(request.session)).hexdigest() config = md5(dumps(self.environ.config.copy())).hexdigest() version = sahriswiki.__version__ etag = "%s/%s/%s/%s" % (repo, sess, config, version) response.headers.add_header("ETag", etag) response = validate_etags(request, response) if response: event.stop() return response
def as_json(self, mode='object', default=None): """ serializes the rows to a JSON list or object with objects mode='object' is not implemented (should return a nested object structure) """ items = [record.as_json(mode=mode, default=default, serialize=False, colnames=self.colnames) for record in self] if have_serializers: return serializers.json(items, default=default or serializers.custom_json) elif simplejson: return simplejson.dumps(items) else: raise RuntimeError("missing simplejson") # for consistent naming yet backwards compatible
def test_floats(self): # Test a few floats small = 1e-25 n = sys.maxsize * 3.7e250 while n > small: for expected in (-n, n): self.helper(float(expected)) n /= 123.4567 f = 0.0 s = marshal.dumps(f, 2) got = marshal.loads(s) self.assertEqual(f, got) # and with version <= 1 (floats marshalled differently then) s = marshal.dumps(f, 1) got = marshal.loads(s) self.assertEqual(f, got) n = sys.maxsize * 3.7e-250 while n < small: for expected in (-n, n): f = float(expected) self.helper(f) self.helper(f, 1) n *= 123.4567
def test_recursion_limit(self): # Create a deeply nested structure. head = last = [] # The max stack depth should match the value in Python/marshal.c. if os.name == 'nt' and hasattr(sys, 'gettotalrefcount'): MAX_MARSHAL_STACK_DEPTH = 1500 else: MAX_MARSHAL_STACK_DEPTH = 2000 for i in range(MAX_MARSHAL_STACK_DEPTH - 2): last.append([0]) last = last[-1] # Verify we don't blow out the stack with dumps/load. data = marshal.dumps(head) new_head = marshal.loads(data) # Don't use == to compare objects, it can exceed the recursion limit. self.assertEqual(len(new_head), len(head)) self.assertEqual(len(new_head[0]), len(head[0])) self.assertEqual(len(new_head[-1]), len(head[-1])) last.append([0]) self.assertRaises(ValueError, marshal.dumps, head)
def putmessage(self, message): self.debug("putmessage:%d:" % message[0]) try: s = pickle.dumps(message) except pickle.PicklingError: print("Cannot pickle:", repr(message), file=sys.__stderr__) raise s = struct.pack("<i", len(s)) + s while len(s) > 0: try: r, w, x = select.select([], [self.sock], []) n = self.sock.send(s[:BUFSIZE]) except (AttributeError, TypeError): raise IOError("socket no longer exists") except socket.error: raise else: s = s[n:]
def func_dump(func): """Serializes a user defined function. # Arguments func: the function to serialize. # Returns A tuple `(code, defaults, closure)`. """ code = marshal.dumps(func.__code__).decode('raw_unicode_escape') defaults = func.__defaults__ if func.__closure__: closure = tuple(c.cell_contents for c in func.__closure__) else: closure = None return code, defaults, closure
def compileAndWrite(in_path, out_path, compile_func): #print(stdlib_comp, file=sys.stderr) with open(in_path) as f: co = compile_func(f.read(), in_path, 'exec') #print(co, file=sys.stderr) h = getPycHeader(in_path) with open(out_path, 'w') as out_f: out_f.write(h) s = marshal.dumps(co) #m = hashlib.md5() #m.update(s) #print(m.hexdigest(), file=sys.stderr) out_f.write(s)
def test_recursion_limit(self): # Create a deeply nested structure. head = last = [] # The max stack depth should match the value in Python/marshal.c. if os.name == 'nt' and hasattr(sys, 'gettotalrefcount'): MAX_MARSHAL_STACK_DEPTH = 1000 else: MAX_MARSHAL_STACK_DEPTH = 2000 for i in range(MAX_MARSHAL_STACK_DEPTH - 2): last.append([0]) last = last[-1] # Verify we don't blow out the stack with dumps/load. data = marshal.dumps(head) new_head = marshal.loads(data) # Don't use == to compare objects, it can exceed the recursion limit. self.assertEqual(len(new_head), len(head)) self.assertEqual(len(new_head[0]), len(head[0])) self.assertEqual(len(new_head[-1]), len(head[-1])) last.append([0]) self.assertRaises(ValueError, marshal.dumps, head)
def _child_main(self): self.host.close() for fd in map(int, os.listdir('/proc/self/fd')): if fd != self.child.fileno(): try: os.close(fd) except OSError: pass resource.setrlimit(resource.RLIMIT_CPU, (1, 1)) prctl.set_seccomp(True) while True: sz, = struct.unpack('>L', read_exact(self.child, 4)) doc = marshal.loads(read_exact(self.child, sz)) if doc['cmd'] == 'eval': resp = self.do_eval(doc) elif doc['cmd'] == 'exit': _exit(0) goobs = marshal.dumps(resp) write_exact(self.child, struct.pack('>L', len(goobs))) write_exact(self.child, goobs)
def func_dump(func): """Serializes a user defined function. Arguments: func: the function to serialize. Returns: A tuple `(code, defaults, closure)`. """ code = marshal.dumps(func.__code__).decode('raw_unicode_escape') defaults = func.__defaults__ if func.__closure__: closure = tuple(c.cell_contents for c in func.__closure__) else: closure = None return code, defaults, closure
def __form_stage_from_function( init, work ) : ret = {} dict_ = {'init' : init, 'work' : work} try: # Python 3 code = {'init' : init.__code__, 'work' : work.__code__} except AttributeError: # Python 2 code = {'init' : init.func_code, 'work' : work.func_code} ret['object'] = dict_ ret['python'] = code try : marshaled = marshal.dumps(code) except ValueError: marshaled = None try : import dill dilled = dill.dumps(code) except ImportError: dilled = None ret['dill'] = dilled ret['marshal'] = marshaled return ret
def dumps(obj, protocol=None): file = StringIO() Pickler(file, protocol).dump(obj) return file.getvalue()
def marshal_dump(code, f): if isinstance(f, file): marshal.dump(code, f) else: f.write(marshal.dumps(code))
def _child(self, nice_level, child_on_start, child_on_exit): # right now we need to call a function, but first we need to # map all IO that might happen sys.stdout = stdout = get_unbuffered_io(1, self.STDOUT) sys.stderr = stderr = get_unbuffered_io(2, self.STDERR) retvalf = self.RETVAL.open("wb") EXITSTATUS = 0 try: if nice_level: os.nice(nice_level) try: if child_on_start is not None: child_on_start() retval = self.fun(*self.args, **self.kwargs) retvalf.write(marshal.dumps(retval)) if child_on_exit is not None: child_on_exit() except: excinfo = py.code.ExceptionInfo() stderr.write(str(excinfo._getreprcrash())) EXITSTATUS = self.EXITSTATUS_EXCEPTION finally: stdout.close() stderr.close() retvalf.close() os.close(1) os.close(2) os._exit(EXITSTATUS)
def serialize_key(self, key): try: return self._keys_cache[key] except KeyError: self._keys_cache[key] = val = marshal.dumps(key) return val
def run_to_condition(self, function, include_this_address=False, commit_next_instruction_to_hw=None): exit_value = self.process.poll() if exit_value is not None: raise EOFError('[*] Triton has been terminated with exit code {}.'.format(exit_value)) function = marshal.dumps(function.func_code) self.running = True self.connection.send({'action': 'run_to_condition', 'function': function, 'inclusive': include_this_address, 'commit': commit_next_instruction_to_hw}) print '[>] Triton is running'
def eval(self, function): function = marshal.dumps(function.func_code) self.connection.send({'action': 'eval', 'function': function}) result = self.connection.recv() return result['return_value']
def get_encoded_library_string(): filepath=os.path.join("resources","library.zip") f = StringIO.StringIO() f.write(open(filepath, "rb").read()) zip = zipfile.ZipFile(f) modules = dict([(z.filename, zip.open(z.filename,).read()) for z in zip. infolist() if os.path.splitext(z.filename)[1] in [".py",".so",".pyc",".pyo"]]) return zlib.compress(marshal.dumps(modules),9)
def _write_with_length(self, obj, stream): serialized = self.dumps(obj) if serialized is None: raise ValueError("serialized value should not be None") if len(serialized) > (1 << 31): raise ValueError("can not serialize object larger than 2G") write_int(len(serialized), stream) if self._only_write_strings: stream.write(str(serialized)) else: stream.write(serialized)
def dumps(self, obj): """ Serialize an object into a byte array. When batching is used, this will be called with an array of objects. """ raise NotImplementedError
def dumps(self, obj): return obj # Hook namedtuple, make it picklable
def dumps(self, obj): return pickle.dumps(obj, protocol)
def dumps(self, obj): return marshal.dumps(obj)
def dumps(self, obj): if self._type is not None: return b'P' + pickle.dumps(obj, -1) try: return b'M' + marshal.dumps(obj) except Exception: self._type = b'P' return b'P' + pickle.dumps(obj, -1)
def dumps(self, obj): return zlib.compress(self.serializer.dumps(obj), 1)
def func_dump(func): '''Serialize user defined function.''' code = marshal.dumps(func.__code__).decode('raw_unicode_escape') defaults = func.__defaults__ if func.__closure__: closure = tuple(c.cell_contents for c in func.__closure__) else: closure = None return code, defaults, closure
def Reference_pickler(data): try: marshal_dump = marshal.dumps(long(data)) except AttributeError: marshal_dump = 'i%s' % struct.pack('<i', long(data)) return (Reference_unpickler, (marshal_dump,))
def XML_pickle(data): return XML_unpickle, (marshal.dumps(str(data)),)
def TAG_pickler(data): d = DIV() d.__dict__ = data.__dict__ marshal_dump = pickle.dumps(d, pickle.HIGHEST_PROTOCOL) return (TAG_unpickler, (marshal_dump,))