+ if running_on_windows:
+ # This works around <http://bugs.python.org/issue2128> for Python 2.
+ # All arguments are retrieved in unicode format and converted to utf-8.
+ # In this way, when launched from the command line, lyxpak.py can deal
+ # with any non-ascii names. Unfortunately, this is not the case when
+ # launched by LyX, because LyX converts the arguments of the converters
+ # to the filesystem encoding. On Windows this corresponds to the current
+ # code page and not to the UTF-16 encoding used by NTFS, such that they
+ # are transliterated if not exactly encodable. As an example, α may
+ # become a, β may become ß, and so on. However, this is a problem only
+ # if the full path of the LyX document contains an unencodable character
+ # as all other paths are extracted from the document in utf-8 format.
+ from ctypes import WINFUNCTYPE, windll, POINTER, byref, c_int
+ from ctypes.wintypes import LPWSTR, LPCWSTR
+ GetCommandLineW = WINFUNCTYPE(LPWSTR)(("GetCommandLineW", windll.kernel32))
+ CommandLineToArgvW = WINFUNCTYPE(POINTER(LPWSTR), LPCWSTR, POINTER(c_int))(("CommandLineToArgvW", windll.shell32))
+ argc = c_int(0)
+ argv_unicode = CommandLineToArgvW(GetCommandLineW(), byref(argc))
+ # unicode_argv[0] is the Python interpreter, so skip that.
+ argv = [argv_unicode[i].encode('utf-8') for i in xrange(1, argc.value)]
+ # Also skip option arguments to the Python interpreter.
+ while len(argv) > 0:
+ if not argv[0].startswith("-"):
+ break
+ argv = argv[1:]
+ sys.argv = argv
+