From 88b6c57a1c12187f17b534db0172b2325f646dc6 Mon Sep 17 00:00:00 2001
From: david-sarah <david-sarah@jacaranda.org>
Date: Tue, 27 Jul 2010 23:27:31 -0700
Subject: [PATCH] Skip option arguments to the python interpreter when
 reconstructing Unicode argv on Windows.

---
 bin/tahoe-script.template       | 14 ++++++++++++++
 src/allmydata/windows/fixups.py | 24 +++++++++++++++++++-----
 2 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/bin/tahoe-script.template b/bin/tahoe-script.template
index 48bdbf18..347bb82b 100644
--- a/bin/tahoe-script.template
+++ b/bin/tahoe-script.template
@@ -65,6 +65,20 @@ if sys.platform == "win32":
         return str(re.sub(ur'[^\x20-\x7F]', lambda m: u'\x7F%x;' % (ord(m.group(0)),), s))
 
     argv = [mangle(argv_unicode[i]) for i in xrange(1, argc.value)]
+
+    # Skip option arguments to the Python interpreter.
+    while len(argv) > 0:
+        arg = argv[0]
+        if not arg.startswith(u"-") or arg == u"-":
+            break
+        argv = argv[1:]
+        if arg == u'-m':
+            # sys.argv[0] should really be the absolute path of the module source, but never mind
+            break
+        if arg == u'-c':
+            argv[0] = u'-c'
+            break
+
     local_tahoe = "Scripts\\tahoe.pyscript"
 else:
     argv = sys.argv
diff --git a/src/allmydata/windows/fixups.py b/src/allmydata/windows/fixups.py
index 92e2fc59..ce8af4b2 100644
--- a/src/allmydata/windows/fixups.py
+++ b/src/allmydata/windows/fixups.py
@@ -169,17 +169,31 @@ def initialize():
 
     # Because of <http://bugs.python.org/issue8775> (and similar limitations in
     # twisted), the 'bin/tahoe' script cannot invoke us with the actual Unicode arguments.
-    # Instead it "mangles" or escapes them using \x7f as an escape character, which we
+    # Instead it "mangles" or escapes them using \x7F as an escape character, which we
     # unescape here.
     def unmangle(s):
-        return re.sub(ur'\x7f[0-9a-fA-F]*\;', lambda m: unichr(int(m.group(0)[1:-1], 16)), s)
+        return re.sub(ur'\x7F[0-9a-fA-F]*\;', lambda m: unichr(int(m.group(0)[1:-1], 16)), s)
 
     try:
-        sys.argv = [unmangle(argv_unicode[i]).encode('utf-8') for i in xrange(1, argc.value)]
+        argv = [unmangle(argv_unicode[i]).encode('utf-8') for i in xrange(1, argc.value)]
     except Exception, e:
         _complain("%s:  could not unmangle Unicode arguments.\n%r"
                   % (sys.argv[0], [argv_unicode[i] for i in xrange(1, argc.value)]))
         raise
 
-    if sys.argv[0].endswith('.pyscript'):
-        sys.argv[0] = sys.argv[0][:-9]
+    # Skip option arguments to the Python interpreter.
+    while len(argv) > 0:
+        arg = argv[0]
+        if not arg.startswith(u"-") or arg == u"-":
+            if arg.endswith('.pyscript'):
+                argv[0] = arg[:-9]
+            break
+        argv = argv[1:]
+        if arg == u'-m':
+            # sys.argv[0] should really be the absolute path of the module source, but never mind
+            break
+        if arg == u'-c':
+            argv[0] = u'-c'
+            break
+
+    sys.argv = argv
-- 
2.45.2