os.system()函数的定义在哪里?

huangapple go评论136阅读模式
英文:

Where is the definition of os.system() function?

问题

我在学习模式中——这可能是一个愚蠢的问题,但我还是要问:
我知道在Python的os.py中有一个system()函数,但我在GitHub上找不到它。

根据Python文档,它说:“这是通过调用标准C函数system()来实现的,并具有相同的限制。”所以我的问题是:“这背后的机制是什么?”

代码:https://github.com/python/cpython/blob/3.11/Lib/os.py

英文:

I'm in learning mode---- this is probably a dumb question but here it is:
I know there is a system() function in os.py in python, but I can't find it in os.py in github.

According to python docs, it says that 'This is implemented by calling the Standard C function system(), and has the same limitations.'.So my question is "What is the mechanism behind this?"

code : https://github.com/python/cpython/blob/3.11/Lib/os.py

答案1

得分: 1

这个函数是用C语言编写的。

这里的代码(来自cpython/Modules/posixmodule.c)定义了os.system

#ifdef HAVE_SYSTEM
#ifdef MS_WINDOWS
/*[clinic input]
os.system -> long

    command: Py_UNICODE

在子shell中执行命令。
[clinic start generated code]*/

static long
os_system_impl(PyObject *module, const Py_UNICODE *command)
/*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
{
    long result;

    if (PySys_Audit("os.system", "(u)", command) < 0) {
        return -1;
    }

    Py_BEGIN_ALLOW_THREADS
    _Py_BEGIN_SUPPRESS_IPH
    result = _wsystem(command);
    _Py_END_SUPPRESS_IPH
    Py_END_ALLOW_THREADS
    return result;
}
#else /* MS_WINDOWS */
/*[clinic input]
os.system -&gt; long

    command: FSConverter

在子shell中执行命令。
[clinic start generated code]*/

static long
os_system_impl(PyObject *module, PyObject *command)
/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
{
    long result;
    const char *bytes = PyBytes_AsString(command);

    if (PySys_Audit("os.system", "(O)", command) < 0) {
        return -1;
    }

    Py_BEGIN_ALLOW_THREADS
    result = system(bytes);
    Py_END_ALLOW_THREADS
    return result;
}
#endif
#endif /* HAVE_SYSTEM */

请注意调用 system(bytes) 的那一行。C语言中的 system 函数是标准C库的一部分,所以如果你想了解更多关于这个C函数的信息,你可以查看标准C库的文档。

你可能会想,“为什么要用C语言编写它?” 答案很简单,C语言可以轻松与计算机进行交互,虽然Python也可以,但需要另一个C模块来从Python中执行这些操作。直接在C中编写这样的功能更容易、更美观,而且更快。

英文:

This function is written in C.

This code here (from cpython/Modules/posixmodule.c) is where os.system is defined.

#ifdef HAVE_SYSTEM
#ifdef MS_WINDOWS
/*[clinic input]
os.system -&gt; long

    command: Py_UNICODE

Execute the command in a subshell.
[clinic start generated code]*/

static long
os_system_impl(PyObject *module, const Py_UNICODE *command)
/*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
{
    long result;

    if (PySys_Audit(&quot;os.system&quot;, &quot;(u)&quot;, command) &lt; 0) {
        return -1;
    }

    Py_BEGIN_ALLOW_THREADS
    _Py_BEGIN_SUPPRESS_IPH
    result = _wsystem(command);
    _Py_END_SUPPRESS_IPH
    Py_END_ALLOW_THREADS
    return result;
}
#else /* MS_WINDOWS */
/*[clinic input]
os.system -&gt; long

    command: FSConverter

Execute the command in a subshell.
[clinic start generated code]*/

static long
os_system_impl(PyObject *module, PyObject *command)
/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
{
    long result;
    const char *bytes = PyBytes_AsString(command);

    if (PySys_Audit(&quot;os.system&quot;, &quot;(O)&quot;, command) &lt; 0) {
        return -1;
    }

    Py_BEGIN_ALLOW_THREADS
    result = system(bytes);
    Py_END_ALLOW_THREADS
    return result;
}
#endif
#endif /* HAVE_SYSTEM */

Notice the line that calls system(bytes). The system function in C is apart of the standard C library, so if you wish to learn more about the C function, you can look there.

You might be wondering, "Why is it written in C?". The answer is simple, C can easily interact with you computer, and although Python can too, another C module would be required to do this from Python. It's easier, better looking, and faster to write something like this directly in C.

答案2

得分: -1

在 os.py 中有用于查找和导入这些系统调用的代码:

# Any new dependencies of the os module and/or changes in path separator
# requires updating importlib as well.
if 'posix' in _names:
    name = 'posix'
    linesep = '\n'
    from posix import *
    try:
        from posix import _exit
        __all__.append('_exit')
    except ImportError:
        pass
    import posixpath as path

    try:
        from posix import _have_functions
    except ImportError:
        pass

    import posix
    __all__.extend(_get_exports_list(posix))
    del posix

这可能是您在 os.py 中正在寻找的内容 - 至少可以回答问题“为什么我在 os.py 中看不到这个?”。

posix - 最常见的 POSIX 系统调用模块

该模块提供对由C标准和POSIX标准(一个轻微伪装的Unix接口)标准化的操作系统功能的访问。

英文:

In os.py there is code to look for and import such system calls:

# Any new dependencies of the os module and/or changes in path separator
# requires updating importlib as well.
if &#39;posix&#39; in _names:
    name = &#39;posix&#39;
    linesep = &#39;\n&#39;
    from posix import *
    try:
        from posix import _exit
        __all__.append(&#39;_exit&#39;)
    except ImportError:
        pass
    import posixpath as path

    try:
        from posix import _have_functions
    except ImportError:
        pass

    import posix
    __all__.extend(_get_exports_list(posix))
    del posix

This may be what you were looking for in os.py -- at least as far as answer the question why don't I see this in os.py?

posix - The most common POSIX system calls

> This module provides access to operating system functionality that is
> standardized by the C Standard and the POSIX standard (a thinly
> disguised Unix interface).

huangapple
  • 本文由 发表于 2023年5月28日 07:30:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定