Python:os.mkdir的代码在哪里? [英] Python: where is the code for os.mkdir?

查看:102
本文介绍了Python:os.mkdir的代码在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览os模块的代码(为清楚起见,我在查看文件/usr/lib/python2.7/os.py),并且我一直在尝试寻找mkdir函数的代码。据我所知,它来自 posix模块及其内置函数,与range或max相同:

I've been looking through the code of the os module (just to be clear, I'm looking at the file /usr/lib/python2.7/os.py), and I've been trying to find the code for the mkdir function. From what I could tell, it comes from the 'posix' module, and its a built-in function, same as range or max:

>>> import posix
>>> posix.mkdir
<built-in function mkdir>
>>> max
<built-in function max>

我猜这些代码是用C编写的,Python解释器知道在哪里找到他们。有人可以解释一下,或向我指出一些有用的资源,这些内置函数的编写方式和位置以及如何与解释器集成吗?

I'm guessing the code for these is written in C somewhere, and the python interpreter knows where to find them. Could someone explain, or point me to some resources that do, how and where these built-in function are written and how they are integrated with the interpreter?

谢谢! / p>

Thanks!

推荐答案

在POSIX平台(以及Windows和OS / 2)上, os 模块从 posixmodule定义的C模块导入.c

On POSIX platforms (and on Windows and OS/2) the os module imports from a C module, defined in posixmodule.c.

此模块定义了 posix_mkdir()函数,该函数包装了 mkdir() C调用在POSIX平台上, Windows上的> CreateDirectoryW

This module defines a posix_mkdir() function that wraps the mkdir() C call on POSIX platforms, CreateDirectoryW on Windows.

该模块与其他功能一起在模块 PyMethodDef posix_方法结构。导入模块后,Python会调用 PyMODINIT_FUNC()函数,该函数使用该结构创建具有 posix_methods 结构的适当的模块对象,并添加一系列常量(例如 open()标志常量)添加到模块。

The module registers this function, together with others, in the module PyMethodDef posix_methods structure. When the module is imported, Python calls the PyMODINIT_FUNC() function, which uses that structure to create an approriate module object with the posix_methods structure and adds a series of constants (such as the open() flag constants) to the module.

请参见用C扩展Python或有关C扩展的工作原理的C ++ 教程。

这篇关于Python:os.mkdir的代码在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆