-3

There are functions such as print that are embedded in CPython and there are other functions such as os.makedirs that are written in external .py files (i.e. in os.py).

As far as I know, print() would call some C code that is already written inside CPython and that C code would be compiled to bytecode to be understood by the computer.

What happens in the case of os.makedirs? That is not embedded in CPython so CPython doesn't know anything about os.makedirs.

Or is makedirs making use of some objects that can be traced back to built-in objects that CPython knows about?

For example, os.py imports abc.py which imports _weakrefset.py which eventually imports the builtin _weakref object? So, os.py can be traced to the builtin _weakref object, for which, CPython has some C code.

Andrew
  • 1
  • 1
  • `sys.path` is not a function; it's a list. – chepner Mar 02 '19 at 15:14
  • Oop, just changed that to `print`. – Andrew Mar 02 '19 at 15:25
  • 1
    Not sure what you are asking, maybe you are confusing the language and its implementation. This question may help you: https://stackoverflow.com/questions/17130975/python-vs-cpython – Valentino Mar 02 '19 at 15:36
  • 1
    *C code would be compiled to bytecode* - no, C is not compiled to byte code, it is Python code which is compiled to byte code. C is compiled to a form close to machine code, which is a lower level than byte code. `print` is a builtin function so is logically part of the `builtins` module, `os.makedirs` is part of the `os` module and it could be written in C (or any other language in theory). How and where they are implemented is not defined by the language. The point is that these implementation details should not be a concern. – cdarke Mar 02 '19 at 15:50

1 Answers1

1

Does everything come down to builtin objects in Python? It depends what you mean by the question, but guessing that you are talking about code flow, then the answer is no.

Take the C implementation of python as our benchmark - remember that there are versions of Python written in Java, C#, and even Python. Modules can be written in C or C++ since there is an API for that.

Take creating a directory (mkdir). At some point that will require a kernel call which depends on the operating system. There will, for example, be a Windows and a UNIX or Posix lower-level interface to make the kernel call, and it will almost certainly be written in C. This is because most modern operating systems have C kernel APIs, not Python ones.

See, for example, Modules/posixmodule.c in the Python source, which is where the OS kernel interface mkdir is called. The C interface for creating a directory only has need of a text string (not a python str object), a C octal integer for permissions, and a variable to store its result. These have to be converted between C and Python, but there can be other types used internally by the module which are pure C and have no need to be converted back to python.

Just because a module is written in C does not mean it is a builtin (part of the builtin module). You can write your own C module and call it from python - the python user does not need to be aware what language it is written in, that is usually invisible.

cdarke
  • 42,728
  • 8
  • 80
  • 84