python 2.7.12 on Linux behaving differently than on Windows

eryk sun eryksun at gmail.com
Tue Dec 6 23:30:01 EST 2016


On Tue, Dec 6, 2016 at 10:35 PM, Nathan Ernst <nathan.ernst at gmail.com> wrote:
> One other consideration in regards to globbing in the argument list:
> there's a static limit to the byte length of argv. On windows, it's 8191
> bytes (I'm assuming a null-terminator brings that to 8192, which is a weird
> 2**13).

I know strings in cmd.exe are limited to about 8K characters, so
that's probably the limit you observed. The Windows command line can
actually contain up to 65535 bytes. It's a sized UNICODE_STRING, which
uses a C unsigned short for the length. That's 32767 16-bit wide
characters, or 32766 sans the terminating NUL. For example:

parent:

    >>> subprocess.call('python -ic " "' + ' a'*16375 + ' b')

child:

    >>> import sys, win32api
    >>> len(win32api.GetCommandLine())
    32766

    >>> sys.argv[:5]
    ['-c', 'a', 'a', 'a', 'a']
    >>> sys.argv[-1]
    'b'
    >>> len(' '.join(sys.argv)) + len('python i " "')
    32766


More information about the Python-list mailing list