如果“env"不存在,为什么 Popen 在 Windows 上会失败?参数包含一个 unicode 对象?

编程入门 行业动态 更新时间:2024-10-26 22:19:35
本文介绍了如果“env"不存在,为什么 Popen 在 Windows 上会失败?参数包含一个 unicode 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

考虑这个例子:

<预><代码>>>>将子进程导入为 sp>>>sp.Popen("notepad2.exe",env={"PATH":"C:\\users\\guillermo\\smallapps\\bin"})<subprocess.Popen 对象在 0x030DF430>>>>sp.Popen("notepad2.exe",env={"PATH":u"C:\\users\\guillermo\\smallapps\\bin"})回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件C:\Python26\lib\subprocess.py",第 633 行,在 __init__ 中错误读取,错误写入)文件C:\Python26\lib\subprocess.py",第 842 行,在 _execute_child 中启动信息)类型错误:环境只能包含字符串

我已将错误追溯到此 CPython 代码:

http://hg.python/cpython/file/ca54c27a9045/Modules/_winapi.c#l511

不过,我无法理解 PyUnicode_Check 的作用:

http://hg.python/cpython/file/26af48f65ef3/Objects/unicodeobject.c#l73

解决方案

正如错误消息所说,环境只能包含字符串.您的第一个 Popen 调用满足此条件,但第二个调用不满足此条件,因为您将 PATH 映射到使用 u"..." 创建的 Unicode 对象 语法.向 Popen 提供环境字典时仅使用字节字符串,您将不会收到此错误.

请注意,根据回溯判断,您使用的是 Python 2.6,因此链接的代码实际上并不适用,因为它来自 Python 3.3.0 beta2.PyUnicode_Check 检查对象是否为 unicode 对象,这在 Python 3 中有意义,其中字符串(内部实现为)unicode 对象.然而,在 Python 2.6 中,等效行使用的是 PyString_Check,这将使其在您的第二个示例中失败.

Consider this example:

>>> import subprocess as sp
>>> sp.Popen("notepad2.exe",env={"PATH":"C:\\users\\guillermo\\smallapps\\bin"})
<subprocess.Popen object at 0x030DF430>
>>> sp.Popen("notepad2.exe",env={"PATH":u"C:\\users\\guillermo\\smallapps\\bin"})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python26\lib\subprocess.py", line 633, in __init__
    errread, errwrite)
 File "C:\Python26\lib\subprocess.py", line 842, in _execute_child
   startupinfo)
TypeError: environment can only contain strings

I've traced back the error to this CPython code:

http://hg.python/cpython/file/ca54c27a9045/Modules/_winapi.c#l511

I'm unable to udnerstand what PyUnicode_Check does, though:

http://hg.python/cpython/file/26af48f65ef3/Objects/unicodeobject.c#l73

解决方案

As the error message says, the environment must only contain strings. Your first Popen call satisfies this condition, but the second one doesn't because you are mapping PATH to a Unicode object created with the u"..." syntax. Use only byte strings when providing environment dicts to Popen and you will not get this error.

Note that, judging by the traceback, you are using Python 2.6, so the linked code does not in fact apply, because it comes from Python 3.3.0 beta2. PyUnicode_Check checks that the object is a unicode object, which makes sense in Python 3, where strings are (internally implemented as) unicode objects. In Python 2.6, however, the equivalent line is using PyString_Check, which would make it fail in your second example.

这篇关于如果“env"不存在,为什么 Popen 在 Windows 上会失败?参数包含一个 unicode 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 22:42:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1125417.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不存在   上会   对象   参数   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!