Python论坛  - 讨论区

标题:[python-chinese] exec*函数的使用

2006年05月08日 星期一 13:34

Shixin Zeng zeng.shixin at gmail.com
Mon May 8 13:34:33 HKT 2006

#!/bin/env python
#-*- vim: set fileencoding=GBK
import os

mplayer=r"e:\tools\mplayer\mplayer.exe"
msg = "mms://202.120.58.191/movie6/1e01在水一方.wmv"
url = msg.decode("GB18030").encode("UTF-16-LE")
print type(url) # 
print type(msg) # 
os.execv(mplayer, (mplayer, url)) #TypeError: execv() arg 2 must contain
only strings
#如果换成:
os.execv(mplayer, (mplayer, msg)) #OKay!

请帮忙解释一下这里面的原因,是不是因unicode跟str的区别?可这两个的类型都是str。想了半天还是没有想明白
谢谢!

--
Best Regards

Shixin Zeng
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060508/c1d0267b/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年05月08日 星期一 15:18

hal sunchaojun at gmail.com
Mon May 8 15:18:59 HKT 2006

我想可能是因为你的系统缺省encoding不是UTF-16-LE,所以传递给shell的参数字符串无法正确解码。

2006/5/8, Shixin Zeng <zeng.shixin at gmail.com>:
> #!/bin/env python
> #-*- vim: set fileencoding=GBK
> import os
>
> mplayer=r"e:\tools\mplayer\mplayer.exe"
> msg = "mms://202.120.58.191/movie6/1e01在水一方.wmv"
> url = msg.decode("GB18030").encode("UTF-16-LE")
> print type(url) # 
> print type(msg) # 
> os.execv(mplayer, (mplayer, url)) #TypeError: execv() arg 2 must contain
> only strings
> #如果换成:
> os.execv(mplayer, (mplayer, msg)) #OKay!
>
> 请帮忙解释一下这里面的原因,是不是因unicode跟str的区别?可这两个的类型都是str。想了半天还是没有想明白
> 谢谢!

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年05月08日 星期一 15:34

Shixin Zeng zeng.shixin at gmail.com
Mon May 8 15:34:21 HKT 2006

On 5/8/06, hal <sunchaojun at gmail.com> wrote:
>
> 我想可能是因为你的系统缺省encoding不是UTF-16-LE,所以传递给shell的参数字符串无法正确解码。


我现在的理解是:
UTF-16-LE的字符串中含有\x00字符,而这样的字符串不能做为exec*的参数。如果确实是这样的话,那么这个错误提示是有问题的。
我是根据file()的参数提示想到这个的。
如果将这个字符串做为文件名,会得到:
TypeError: file() argument 1 must be (encoded string without NULL bytes),
not str

2006/5/8, Shixin Zeng <zeng.shixin at gmail.com>:
> > #!/bin/env python
> > #-*- vim: set fileencoding=GBK
> > import os
> >
> > mplayer=r"e:\tools\mplayer\mplayer.exe"
> > msg = "mms://202.120.58.191/movie6/1e01在水一方.wmv"
> > url = msg.decode("GB18030").encode("UTF-16-LE")
> > print type(url) # 
> > print type(msg) # 
> > os.execv(mplayer, (mplayer, url)) #TypeError: execv() arg 2 must contain
> > only strings
> > #如果换成:
> > os.execv(mplayer, (mplayer, msg)) #OKay!
> >
> > 请帮忙解释一下这里面的原因,是不是因unicode跟str的区别?可这两个的类型都是str。想了半天还是没有想明白
> > 谢谢!
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


--
Best Regards

Shixin Zeng
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060508/dec94b6f/attachment-0001.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年05月08日 星期一 15:45

3751 lwm3751 at gmail.com
Mon May 8 15:45:17 HKT 2006

url跟msg的内容不一样,你怎么会指望它们的结果是一致的呢?

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年05月08日 星期一 16:10

Leo Jay python.leojay at gmail.com
Mon May 8 16:10:19 HKT 2006

On 5/8/06, Shixin Zeng <zeng.shixin at gmail.com> wrote:
>
> #!/bin/env python
> #-*- vim: set fileencoding=GBK
> import os
>
> mplayer=r"e:\tools\mplayer\mplayer.exe"
> msg = "mms://202.120.58.191/movie6/1e01在水一方.wmv"
> url = msg.decode("GB18030").encode("UTF-16-LE")
> print type(url) # 
> print type(msg) # 
> os.execv(mplayer, (mplayer, url)) #TypeError: execv() arg 2 must contain
> only strings
> #如果换成:
> os.execv(mplayer, (mplayer, msg)) #OKay!
>
> 请帮忙解释一下这里面的原因,是不是因unicode跟str的区别?可这两个的类型都是str。想了半天还是没有想明白
> 谢谢!
>
>
>
>

execv函数在posixdule.c中实现.其中有这么几句话:
  if (!PyArg_Parse((*getitem)(argv, i), "et",
     Py_FileSystemDefaultEncoding,
     &argvlist;[i])) {
   free_string_array(argvlist, i);
   PyErr_SetString(PyExc_TypeError,
     "execv() arg 2 must contain only strings");
   PyMem_Free(path);
   return NULL;

可见,问题并不在于你的url是个unicode,而在于,你的url的编码不是系统默认的编码


--
Best Regards,
Leo Jay
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060508/a59ace22/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年05月08日 星期一 17:00

Shixin Zeng zeng.shixin at gmail.com
Mon May 8 17:00:59 HKT 2006

On 5/8/06, Leo Jay <python.leojay at gmail.com> wrote:
>
> On 5/8/06, Shixin Zeng <zeng.shixin at gmail.com> wrote:
> >
> > #!/bin/env python
> > #-*- vim: set fileencoding=GBK
> > import os
> >
> > mplayer=r"e:\tools\mplayer\mplayer.exe"
> > msg = "mms://202.120.58.191/movie6/1e01在水一方.wmv"
> > url = msg.decode("GB18030").encode("UTF-16-LE")
> > print type(url) # 
> > print type(msg) # 
> > os.execv(mplayer, (mplayer, url)) #TypeError: execv() arg 2 must contain
> > only strings
> > #如果换成:
> > os.execv(mplayer, (mplayer, msg)) #OKay!
> >
> > 请帮忙解释一下这里面的原因,是不是因unicode跟str的区别?可这两个的类型都是str。想了半天还是没有想明白
> > 谢谢!
> >
> >
> >
> >
>
> execv函数在posixdule.c中实现.其中有这么几句话:
>   if (!PyArg_Parse((*getitem)(argv, i), "et",
>      Py_FileSystemDefaultEncoding,
>      &argvlist;[i])) {
>    free_string_array(argvlist, i);
>    PyErr_SetString(PyExc_TypeError,
>      "execv() arg 2 must contain only strings");
>    PyMem_Free(path);
>    return NULL;
>
> 可见,问题并不在于你的url是个unicode,而在于,你的url的编码不是系统默认的编码
>

谢谢!那看来这个错误提示还是有点误导。或许改为
exec() arg 2 must contain only strings encoded in system default encoding.
要更明确些。

--
> Best Regards,
> Leo Jay
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


--
Best Regards

Shixin Zeng
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060508/31ba4f5e/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年05月08日 星期一 21:43

Shixin Zeng zeng.shixin at gmail.com
Mon May 8 21:43:21 HKT 2006

On 5/8/06, Leo Jay <python.leojay at gmail.com> wrote:
>
> On 5/8/06, Shixin Zeng <zeng.shixin at gmail.com> wrote:
> >
> > #!/bin/env python
> > #-*- vim: set fileencoding=GBK
> > import os
> >
> > mplayer=r"e:\tools\mplayer\mplayer.exe"
> > msg = "mms://202.120.58.191/movie6/1e01在水一方.wmv"
> > url = msg.decode("GB18030").encode("UTF-16-LE")
> > print type(url) # 
> > print type(msg) # 
> > os.execv(mplayer, (mplayer, url)) #TypeError: execv() arg 2 must contain
> > only strings
> > #如果换成:
> > os.execv(mplayer, (mplayer, msg)) #OKay!
> >
> > 请帮忙解释一下这里面的原因,是不是因unicode跟str的区别?可这两个的类型都是str。想了半天还是没有想明白
> > 谢谢!
> >
> >
> >
> >
>
> execv函数在posixdule.c中实现.其中有这么几句话:
>   if (!PyArg_Parse((*getitem)(argv, i), "et",
>      Py_FileSystemDefaultEncoding,
>      &argvlist;[i])) {
>    free_string_array(argvlist, i);
>    PyErr_SetString(PyExc_TypeError,
>      "execv() arg 2 must contain only strings");
>    PyMem_Free(path);
>    return NULL;
>
> 可见,问题并不在于你的url是个unicode,而在于,你的url的编码不是系统默认的编码
>

又考虑了一下这个问题
1)将它改成UTF-8编码 url = msg.decode("GB18030").encode("UTF-8"),能运行
2)改成Big5,也能运行

3)sys.getfilesystemencoding()返回"mbcs",不知道这个究竟是什么编码?

4)查阅了PyArg_Parse的参数说明,说到"et"时,跟"es"类似,"es"有这么一段话:
This variant on "s" is used for encoding Unicode and objects convertible to
Unicode into a character buffer. It only works for encoded data without
embedded NUL bytes.
这明确说明了不能有内嵌的NUL即\x00字符,但是没有说明当指定的encoding无法编码所给定的字符串时的行为,所以不能断定是因为编码不对的问题。
5) msg = "mms://202.120.58.191/movie6/1e01\x00在水一方.wmv"
    os.execv(mplayer,(mplayer, msg)也将抛出异常
6) msg = "mms://202.120.58.191/movie6/1e01\xff\xf1在水一方.wmv"
#插入'GB18030'中都没有编码的字符(串)
   os.execv(mplayer, (mplayer, msg))没有异常产生。

从上面可以看出,异常的抛出不在于它不是系统默认的编码,而是内嵌了\x00字符。

> --
> Best Regards,
> Leo Jay
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


--
Best Regards

Shixin Zeng
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060508/8277e602/attachment-0001.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年05月09日 星期二 08:42

Lea.Cen leacen at gmail.com
Tue May 9 08:42:15 HKT 2006

>
>   又考虑了一下这个问题
> > 1)将它改成UTF-8编码 url = msg.decode("GB18030").encode("UTF-8"),能运行
> > 2)改成Big5,也能运行
> >
> > 3)sys.getfilesystemencoding()返回"mbcs",不知道这个究竟是什么编码?
> >
> > 4)查阅了PyArg_Parse的参数说明,说到"et"时,跟"es"类似,"es"有这么一段话:
> > This variant on "s" is used for encoding Unicode and objects convertible
> > to Unicode into a character buffer. It only works for encoded data without
> > embedded NUL bytes.
> >
> > 这明确说明了不能有内嵌的NUL即\x00字符,但是没有说明当指定的encoding无法编码所给定的字符串时的行为,所以不能断定是因为编码不对的问题。
> > 5) msg = "mms://202.120.58.191/movie6/1e01\x00在水一方.wmv"
> >     os.execv(mplayer,(mplayer, msg)也将抛出异常
> > 6) msg = "mms://202.120.58.191/movie6/1e01\xff\xf1在水一方.wmv"
> > #插入'GB18030'中都没有编码的字符(串)
> >    os.execv(mplayer, (mplayer, msg))没有异常产生。
> >
> > 从上面可以看出,异常的抛出不在于它不是系统默认的编码,而是内嵌了\x00字符。
> >
> >
>   --
> > Best Regards,
> > Leo Jay
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
>
> --
> Best Regards
>
>
> Shixin Zeng
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese



我也比较认同你的说法! 应该是其中包含了'\x00'的原因
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060509/a17e9d95/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号