python sys.argv限制?

时间:2011-04-04 01:18:30

标签: python

假设我想运行这样的python脚本:python my_script.py MY_INPUT。 在这种情况下,MY_INPUT将传输到sys.argv[1]

字符MY_INPUT可以包含的数字是否有限制?

字符MY_INPUT可以包含的类型是否有限制?

有关MY_INPUT的任何其他限制吗?

更新:我正在使用Ubuntu Linux 10.04

2 个答案:

答案 0 :(得分:12)

argv的大小受操作系统的限制,并且在操作系统与操作系统之间存在很大差异。引用Linux execve(2)联机帮助页:

Limits on size of arguments and environment
   Most Unix implementations impose some limit on the total size
   of the command-line argument (argv) and environment (envp)
   strings that may be passed to a new program.  POSIX.1 allows an
   implementation to advertise this limit using the ARG_MAX
   constant (either defined in <limits.h> or available at run time
   using the call sysconf(_SC_ARG_MAX)).

   On Linux prior to kernel 2.6.23, the memory used to store the
   environment and argument strings was limited to 32 pages
   (defined by the kernel constant MAX_ARG_PAGES).  On
   architectures with a 4-kB page size, this yields a maximum size
   of 128 kB.

   On kernel 2.6.23 and later, most architectures support a size
   limit derived from the soft RLIMIT_STACK resource limit (see
   getrlimit(2)) that is in force at the time of the execve()
   call.  (Architectures with no memory management unit are
   excepted: they maintain the limit that was in effect before
   kernel 2.6.23.)  This change allows programs to have a much
   larger argument and/or environment list.  For these
   architectures, the total size is limited to 1/4 of the allowed
   stack size.  (Imposing the 1/4-limit ensures that the new
   program always has some stack space.)  Since Linux 2.6.25, the
   kernel places a floor of 32 pages on this size limit, so that,
   even when RLIMIT_STACK is set very low, applications are
   guaranteed to have at least as much argument and environment
   space as was provided by Linux 2.6.23 and earlier.  (This
   guarantee was not provided in Linux 2.6.23 and 2.6.24.)
   Additionally, the limit per string is 32 pages (the kernel
   constant MAX_ARG_STRLEN), and the maximum number of strings is
   0x7FFFFFFF.

答案 1 :(得分:1)

Python本身并未对sys.argv的长度或内容施加任何限制。但是,您的操作系统和/或命令shell肯定会。如果不详细考虑您的操作环境,就无法完全回答这个问题。