diff -r fd5d6a3eaf41 CLILauncher/launcher.c
--- a/CLILauncher/launcher.c	Mon Oct 17 15:55:48 2011 +0100
+++ b/CLILauncher/launcher.c	Tue Oct 18 10:29:36 2011 +0100
@@ -1124,6 +1124,7 @@
     void * version_data;
     VS_FIXEDFILEINFO * file_info;
     UINT block_size;
+    int filearg;
 
     wp = get_env(L"PYLAUNCH_DEBUG");
     if ((wp != NULL) && (*wp != L'\0'))
@@ -1208,22 +1209,33 @@
     }
     else {
         p = argv[1];
-        plen = wcslen(p);
-        if (p[0] != L'-') {
-            read_commands();
-            maybe_handle_shebang(&argv[1], command);
-        }
-        /* No file with shebang, or an unrecognised shebang.
-         * Is the first arg a special version qualifier?
-         */
-        valid = (*p == L'-') && validate_version(&p[1]);
-        if (valid) {
+        if (p[0] == L'-' && validate_version(&p[1])) {
             ip = locate_python(&p[1]);
             if (ip == NULL)
                 error(RC_NO_PYTHON, L"Requested Python version (%s) not \
 installed", &p[1]);
             command += wcslen(p);
             command = skip_whitespace(command);
+            valid = TRUE;
+        }
+        else {
+            /* scan each argument until we find a command that does not start 
+            with '-' that may be a file with a shebang, all other args should be 
+            ignored as they may be valid python command line args*/
+            filearg = 1;
+            while (filearg != argc && argv[filearg][0] == L'-')
+                ++filearg;
+            
+            if (filearg != argc) {
+                /* Check preceding arg is neither -c, -m, nor - */                
+                if (filearg == 1 || (_wcsicmp(argv[filearg-1], L"-c") != 0 && 
+                                     _wcsicmp(argv[filearg-1], L"-m") != 0 && 
+                                     _wcsicmp(argv[filearg-1], L"-") != 0)) {
+                    read_commands();
+                    maybe_handle_shebang(&argv[filearg], command);
+                }
+            }
+            valid = FALSE;
         }
     }
     if (!valid) {
diff -r fd5d6a3eaf41 tests.py
--- a/tests.py	Mon Oct 17 15:55:48 2011 +0100
+++ b/tests.py	Tue Oct 18 10:29:36 2011 +0100
@@ -4,8 +4,11 @@
     raise ImportError("These tests require Python 3 to run.")
 
 import ctypes
+import doctest
+import itertools
 import os
 import os.path
+import re
 import shutil
 import subprocess
 import tempfile
@@ -16,6 +19,8 @@
 SCRIPT_TEMPLATE='''%(shebang_line)s%(coding_line)simport sys
 print(sys.version)
 print(sys.argv)
+if hasattr(sys, 'flags'):
+    print("flags=" + repr(sys.flags))
 %(comment)s'''
 
 BOM_UTF8 = b'\xEF\xBB\xBF'
@@ -40,6 +45,15 @@
     'PY3': '#!python3\n',
 }
 
+PYFLAGS = [
+    [],
+    ['-u'],
+    ['-utt'],
+    ['-d'],
+    ['-B', '-O'],
+    ['-OO', '-s', '-v'],
+]
+
 COMMENT_WITH_UNICODE = '# Libert\xe9, \xe9galit\xe9, fraternit\xe9\n'
 
 VIRT_PATHS = [
@@ -47,6 +61,41 @@
     '/usr/bin/',
 ]
 
+sys_flags = {}
+for label, flags in [x.split(None, 1) for x in  '''\
+            debug -d 
+            py3k_warning -3 
+            division_warning -Q 
+            division_new -Qnew 
+            inspect -i 
+            interactive -i 
+            optimize -O or -OO 
+            dont_write_bytecode -B 
+            no_user_site -s 
+            no_site -S 
+            ignore_environment -E 
+            tabcheck -t or -tt 
+            verbose -v 
+            unicode -U 
+            bytes_warning -b'''.splitlines()]:
+    for flag in flags.split(' or '):
+        sys_flags[flag[1:].strip()] = label
+        
+def sys_flags_in_pyflags(pyflags):
+    '''Return the set of sys.flags items that should be in sys.flags, based ont
+    the command line flags passed to python.
+    
+    >>> sys_flags_in_pyflags(['-utt'])
+    {'tabcheck'}
+    >>> sorted(sys_flags_in_pyflags(['-Utt', '-v']))
+    ['tabcheck', 'unicode', 'verbose']
+    '''
+    present = set()
+    for flag, label in sys_flags.items():
+        if any(flag in pyflag for pyflag in pyflags):
+            present.add(label)
+    return present
+
 class VirtualPath: # think a C struct...
     def __init__(self, version, bits, executable):
         self.version = version
@@ -197,9 +246,8 @@
 
     def make_script(self, shebang_line='', coding_line='', encoding='ascii',
                     bom=b'', comment=''):
-        script = (SCRIPT_TEMPLATE % locals())
-        script = script.replace('\r', '').replace('\n',
-                                                  '\r\n').encode(encoding)
+        script = self.make_script_contents(shebang_line, coding_line, comment)
+        script = script.encode(encoding)
         if bom and not script.startswith(bom):
             script = bom + script
         path = os.path.join(self.work_dir, 'showver.py')
@@ -208,6 +256,12 @@
         self.last_script = script
         return path
 
+    def make_script_contents(self, shebang_line='', coding_line='',  comment=''):
+        script = (SCRIPT_TEMPLATE % locals())
+        script = script.replace('\r', '').replace('\n', '\r\n')
+        self.last_script = script.encode('utf-8')
+        return script
+        
     def save_script(self):
             with open('last_failed.py', 'wb') as f:
                 f.write(self.last_script)
@@ -220,15 +274,27 @@
             for s in self.last_streams:
                 print(repr(s))
         return result
+        
+    def flags_match(self, stdout, pyflags):
+        stdout = stdout.decode('ascii')
+        m = re.search(r"^flags=sys.flags(\([^)]*\))$", stdout, flags=re.M)
+        if m:
+            flags = eval('dict%s' % (m.group(1),))
+            for flag in sys_flags_in_pyflags(pyflags):
+                if flag in flags:
+                    self.assertNotEqual(0, flags[flag], 
+                            'Expected %s to be set in sys.flags' % (flag,))
 
     def is_encoding_error(self, message):
         return b'but no encoding declared; see' in message
 
     def run_child(self, path, env=None):
-        p = subprocess.Popen([LAUNCHER, path], stdout=subprocess.PIPE,
-                             stderr=subprocess.PIPE, shell=False,
-                             env=env)
-        stdout, stderr = p.communicate()
+        return self.run_cmd([LAUNCHER, path], env=env)
+        
+    def run_cmd(self, args, input=None, **popen_args):
+        p = subprocess.Popen(args, stdout=subprocess.PIPE, stdin=subprocess.PIPE,
+                            stderr=subprocess.PIPE, shell=False, **popen_args)
+        stdout, stderr = p.communicate(input)
         self.last_streams = stdout, stderr
         return stdout, stderr
 
@@ -262,18 +328,53 @@
                 p =  subprocess.Popen([LAUNCHER, nohyphen, script], 
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                 stdout, stderr = p.communicate()
+                self.last_streams = stdout, stderr
                 self.assertTrue(self.matches(stdout, DEFAULT_PYTHON2))
             finally:
                 os.remove(nohyphen)
  
+    def test_input_from_stdin(self):
+        "Test that - (read script from stdin) on cmdline DOES NOT parse shebang"
+        for shebang, pyflags in itertools.product(SHEBANGS.values(), PYFLAGS):
+            arg = self.make_script(shebang_line=shebang)
+            input = self.make_script_contents().encode('ascii')
+            stdout, stderr = self.run_cmd([LAUNCHER] + pyflags + ['-', arg], 
+                                          input=input)
+            self.assertTrue(self.matches(stdout, DEFAULT_PYTHON2))
+            self.flags_match(stdout, pyflags)
+        
+    def test_option_c(self):
+        "Test that -c on cmdline DOES NOT parse shebang"
+        for shebang, pyflags in itertools.product(SHEBANGS.values(), PYFLAGS):
+            script = self.make_script_contents(shebang_line=shebang)
+            stdout, stderr = self.run_cmd([LAUNCHER] + pyflags + ['-c', script])
+            self.assertTrue(self.matches(stdout, DEFAULT_PYTHON2))
+            self.flags_match(stdout, pyflags)
+
+    def test_option_m(self):
+        "Test that -m on cmdline DOES NOT parse shebang"
+        for shebang, pyflags in itertools.product(SHEBANGS.values(), PYFLAGS):
+            path = self.make_script(shebang_line=shebang) # called something.py
+            dir, script = os.path.split(path)   
+            module, ext = os.path.splitext(script)
+            # make a file called 'something' without the .py to tempt pylauncher 
+            # into parsing the shebang in that file, even though that's not what
+            # we are actually going to load.
+            shutil.copy2(path, os.path.join(dir, module)) 
+            stdout, stderr = self.run_cmd([LAUNCHER] + pyflags + ['-m', module],
+                                          cwd=dir)
+            self.assertTrue(self.matches(stdout, DEFAULT_PYTHON2))
+            self.flags_match(stdout, pyflags)
+
     # Tests with ASCII Python sources
     def test_shebang_ascii(self):
         "Test shebangs in ASCII files"
-        for shebang in SHEBANGS.values():
+        for shebang, pyflags in itertools.product(SHEBANGS.values(), PYFLAGS):
             path = self.make_script(shebang_line=shebang)
-            stdout, stderr = self.run_child(path)
+            stdout, stderr = self.run_cmd([LAUNCHER] + pyflags + [path])
             python = self.get_python_for_shebang(shebang)
             self.assertTrue(self.matches(stdout, python))
+            self.flags_match(stdout, pyflags)
 
     # Tests with UTF-8 Python sources with no BOM
     def test_shebang_utf8_nobom(self):
@@ -464,6 +565,9 @@
         stdout, stderr = self.run_child(path)
         self.assertTrue(self.matches(stdout, DEFAULT_PYTHON3))
 
+def load_tests(loader, tests, ignore):
+    tests.addTests(doctest.DocTestSuite(sys.modules[__name__]))
+    return tests
 
 if __name__ == '__main__':
     unittest.main()
