+def unicode_to_argv(s, mangle=False):
+ """
+ Encode the given Unicode argument as a bytestring.
+ If the argument is to be passed to a different process, then the 'mangle' argument
+ should be true; on Windows, this uses a mangled encoding that will be reversed by
+ code in runner.py.
+ """
+ precondition(isinstance(s, unicode), s)
+
+ if mangle and sys.platform == "win32":
+ # This must be the same as 'mangle' in bin/tahoe-script.template.
+ return str(re.sub(ur'[^\x20-\x7F]', lambda m: u'\x7F%x;' % (ord(m.group(0)),), s))
+ else:
+ return s.encode(argv_encoding)
+