如何让Python使用包含冒号的路径?

发布于 2024-11-03 05:35:52 字数 792 浏览 1 评论 0原文

我有一个包含嵌入式 Python 2.6 解释器的程序。当我调用解释器时,我调用 PySys_SetPath() 将解释器的导入路径设置为安装在包含 Python 脚本文件的可执行文件旁边的子目录...如下所示:(

PySys_SetPath("/path/to/my/program/scripts/type1:/path/to/my/program/scripts/type2");

除了路径字符串是根据我的程序可执行文件的当前位置,而不是像上面的示例那样硬编码)

这​​工作正常......除非聪明的用户决定将我的程序安装在名称中带有冒号的文件夹下。在这种情况下,我的 PySys_SetPath() 命令最终看起来像这样(注意存在名为“path:to”的文件夹):

PySys_SetPath("/path:to/my/program/scripts/type1:/path:to/my/program/scripts/type2");

...这会破坏我的所有 Python 脚本,因为现在 Python 在“/”中查找脚本文件path”和“to/my/program/scripts/type1”而不是“/path:to/myprogram/scripts/type1”,因此所有导入语句都不起作用。

我的问题是,除了告诉用户避免在文件夹名称中使用冒号之外,是否有任何解决方案可以解决此问题?

我查看了 Python/sysmodule.c 中的 makepathobject() 函数,它似乎不支持任何类型的引用或转义来处理文字冒号......但也许我错过了一些细微差别。

I have a program that includes an embedded Python 2.6 interpreter. When I invoke the interpreter, I call PySys_SetPath() to set the interpreter's import-path to the subdirectories installed next to my executable that contain my Python script files... like this:

PySys_SetPath("/path/to/my/program/scripts/type1:/path/to/my/program/scripts/type2");

(except that the path strings are dynamically generated based on the current location of my program's executable, not hard-coded as in the example above)

This works fine... except when the clever user decides to install my program underneath a folder that has a colon in its name. In that case, my PySys_SetPath() command ends up looking like this (note the presence of a folder named "path:to"):

PySys_SetPath("/path:to/my/program/scripts/type1:/path:to/my/program/scripts/type2");

... and this breaks all my Python scripts, because now Python looks for script files in "/path", and "to/my/program/scripts/type1" instead of in "/path:to/myprogram/scripts/type1", and so none of the import statements work.

My question is, is there any fix for this issue, other than telling the user to avoid colons in his folder names?

I looked at the makepathobject() function in Python/sysmodule.c, and it doesn't appear to support any kind of quoting or escaping to handle literal colons.... but maybe I am missing some nuance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

您遇到的问题是 PySys_SetPath 函数使用冒号作为分隔符来解析您传递的字符串。该解析器将每个 : 字符视为分隔路径,并且没有办法解决此问题(无法转义)。

但是,您可以通过创建各个路径的列表(每个路径可能包含冒号)并使用 PySys_SetObject 来设置 sys.path 来绕过此问题:

PyListObject *path; 

path = (PyListObject *)PyList_New(0); 
PyList_Append((PyObject *) path, PyString_FromString("foo:bar")); 
PySys_SetObject("path", (PyObject *)path); 

现在解释器将将“foo:bar”视为 sys.path 的独特组件。

The problem you're running into is the PySys_SetPath function parses the string you pass using a colon as the delimiter. That parser sees each : character as delimiting a path, and there isn't a way around this (can't be escaped).

However, you can bypass this by creating a list of the individual paths (each of which may contain colons) and use PySys_SetObject to set the sys.path:

PyListObject *path; 

path = (PyListObject *)PyList_New(0); 
PyList_Append((PyObject *) path, PyString_FromString("foo:bar")); 
PySys_SetObject("path", (PyObject *)path); 

Now the interpreter will see "foo:bar" as a distinct component of the sys.path.

时间你老了 2024-11-10 05:35:52

在文件路径中支持冒号会在多个操作系统上引发大量蠕虫病毒;例如,它在 Windows 或 Mac OS X 上不是有效的路径字符,而且正是由于这个原因,在脚本环境上下文中支持它似乎不是一个特别合理的事情。事实上,我有点惊讶 Linux 也允许使用冒号文件名,特别是因为 : 是一个非常常见的路径分隔符。

您可以尝试转义冒号,即将 /path:to/ 转换为 /path\:to/ 并查看是否有效。除此之外,只需告诉用户避免在文件名中使用冒号即可。他们会在很多不同的环境中遇到各种各样的问题,这显然是一个坏主意。

Supporting colons in a file path opens up a huge can of worms on multiple operating systems; it is not a valid path character on Windows or Mac OS X, for example, and it doesn't seem like a particularly reasonable thing to support in the context of a scripting environment either for exactly this reason. I'm actually a bit surprised that Linux allows colon filenames too, especially since : is a very common path separator character.

You might try escaping the colon out, i.e. converting /path:to/ to /path\:to/ and see if that works. Other than that, just tell the user to avoid using colons in their file names. They will run into all sorts of problems in quite a few different environments and it's a just plain bad idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文