import copy
import os
import sys
+import socket
+
+def _string_bytearray(s):
+ # gets us bytes in py2 and py3
+ if not isinstance(s, bytes):
+ s = s.encode('utf-8') # sigh, python 2/3 compat
+ return bytearray(s)
def _string_escape_for_c(s):
out = ''
- for c in bytearray(s): # gets us bytes in py2 and py3
+ for c in _string_bytearray(s):
if c == ord('\\') or c == ord('"') or c < 32 or c > 126:
out += '\\x%02x' % c
else:
return '{' + ', '.join(['(%s)' % v for v in val_lit_strs]) + ' }'
def _lit_string_uncasted(s):
- if not isinstance(s, bytes):
- s = s.encode('utf-8') # sigh, python 2/3 compat
- b = bytearray(s)
- return _lit_aggregate_uncasted(map(_lit_integer, b) + [ '0' ])
+ b = _string_bytearray(s)
+ return _lit_aggregate_uncasted([_lit_integer(x) for x in b] + [ '0' ])
def _lit_array(elemtype, val_lit_strs):
return (
l._posns = { }
for f in x.type.fields():
l._posns[f.name] = len(l._template)
- try: f.type.fields(); blank = '{ }'
+ try: f.type.fields(); blank = '{ }'
+ except TypeError: blank = '0'
except AttributeError: blank = '0'
l._template.append(blank)
+ sys.stderr.write('## STRUCT %s template %s fields %s\n'
+ % (typename, l._template, l._posns))
def substitute(l, values):
build = copy.deepcopy(l._template)
# assembling structs
# sigh, we have to record the order of the arguments!
- def _find_fields(typename):
+ def _find_fields(di, typename):
try:
fields = di._structs[typename]
- except AttributeError:
+ except KeyError:
fields = DonorStructLayout(typename)
di._structs[typename] = fields
return fields
- def _make(typename, values):
+ def _make(di, typename, values):
fields = di._find_fields(typename)
return fields.substitute(values)
# wrappers for the syscalls that do what we want
def _sendmsg(di, carrier, control_msg):
- iov_base = _lit_array('int', map(str,fds))
+ iov_base = _lit_array('int', [str(x) for x in fds])
iov = di._make('struct iovec', {
'iov_base': iov_base,
'iov_len' : len(fds),