Umemura style Python expedition day 1

For more information on the Umemura-style Python expedition, see "Umemura-style Python expedition day 0" !

On the first day of exploration, I would like to proceed with the theme of "Exploring Builtin functions from source code".

What is the Buitin function? ??

Also called a built-in function. It is a built-in function that can be used without importing. A typical place is print. Please refer to the tutorial for a list of Builtin functions. http://docs.python.jp/2/library/functions.html

Functions that are imported and used in contrast to the Builtin function (eg os.path) are easy to understand. This is because it is saved in .py format (basically) in the Lib folder inside the folder where Python is installed. On the other hand, the Builtin function is literally built in the python executable file, so you can't find it without hitting the source code.

Let's take a look at the source code!

Let's actually hit the source code. This time I downloaded Python-3.6.0.tgz and took a look inside, but since I only check the Builtin function, any version should be fine. The place is here: https://www.python.org/downloads/source/

If you look at the contents, you will see a folder containing the source code for installing Python on the system. (That's right.) The folder of the Builtin function we are aiming for this time is below. /Python/bltinmodule.c

Let's take a look inside. How is a typical print source written? (Line 1744)

static PyObject *
builtin_print(PyObject *self, PyObject *args,  PyObject *kwds)
{
    static char *kwlist[] = {"sep", "end", "file", "flush", 0};
    static PyObject *dummy_args;
    PyObject *sep = NULL, *end = NULL, *file =  NULL, *flush = NULL;
    int i, err;
    ....

When you build a Python executable file in C language, you also build it by incorporating the print function like this, I see.

Take a look at the list of Builtin functions

You can see a list of functions to build in the array of builtin_methods. (Line 2613)

static PyMethodDef builtin_methods[] = {
{"__build_class__", (PyCFunction)builtin___build_class__,
 METH_VARARGS | METH_KEYWORDS, build_class_doc},
{"__import__",      (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
BUILTIN_ABS_METHODDEF
BUILTIN_ALL_METHODDEF
BUILTIN_ANY_METHODDEF
BUILTIN_ASCII_METHODDEF
BUILTIN_BIN_METHODDEF
BUILTIN_CALLABLE_METHODDEF
BUILTIN_CHR_METHODDEF
BUILTIN_COMPILE_METHODDEF
BUILTIN_DELATTR_METHODDEF
{"dir",             builtin_dir,        METH_VARARGS, dir_doc},
BUILTIN_DIVMOD_METHODDEF
BUILTIN_EVAL_METHODDEF
BUILTIN_EXEC_METHODDEF
BUILTIN_FORMAT_METHODDEF
{"getattr",         builtin_getattr,    METH_VARARGS, getattr_doc},
BUILTIN_GLOBALS_METHODDEF
BUILTIN_HASATTR_METHODDEF
BUILTIN_HASH_METHODDEF
BUILTIN_HEX_METHODDEF
BUILTIN_ID_METHODDEF
BUILTIN_INPUT_METHODDEF
BUILTIN_ISINSTANCE_METHODDEF
BUILTIN_ISSUBCLASS_METHODDEF
{"iter",            builtin_iter,       METH_VARARGS, iter_doc},
BUILTIN_LEN_METHODDEF
BUILTIN_LOCALS_METHODDEF
{"max",             (PyCFunction)builtin_max,        METH_VARARGS | METH_KEYWORDS, max_doc},
{"min",             (PyCFunction)builtin_min,        METH_VARARGS | METH_KEYWORDS, min_doc},
{"next",            (PyCFunction)builtin_next,       METH_VARARGS, next_doc},
BUILTIN_OCT_METHODDEF
BUILTIN_ORD_METHODDEF
BUILTIN_POW_METHODDEF
{"print",           (PyCFunction)builtin_print,      METH_VARARGS | METH_KEYWORDS, print_doc},
BUILTIN_REPR_METHODDEF
{"round",           (PyCFunction)builtin_round,      METH_VARARGS | METH_KEYWORDS, round_doc},
BUILTIN_SETATTR_METHODDEF
BUILTIN_SORTED_METHODDEF
BUILTIN_SUM_METHODDEF
{"vars",            builtin_vars,       METH_VARARGS, vars_doc},
{NULL,              NULL},
};

Hmm? You can see 2 patterns. One is a function that describes a structure, such as print. The other is a function that has been replaced by a variable. The former seems to have the source code in this c file, but where is the latter?

The answer was in the header file below! /Python/clinic/bltinmodule.c.h

For example, the abs function is ...

#define BUILTIN_ABS_METHODDEF    \
    {"abs", (PyCFunction)builtin_abs, METH_O, builtin_abs__doc__},

I don't know why the definition is split in two here. It may be easy to build, but I will not try to find it this time.

Summary

How was the Builtin function exploring the source code? Hmm? Is it unsatisfactory? If so, I'm sorry, but I'm thinking of continuing the report that lightly investigated what I was a little worried about at this level. We would appreciate it if you could get along with us.

Recommended Posts

Umemura style Python expedition Day 0
Umemura style Python expedition day 1
Python day 1
Python study day 1
[1day1lang AdventCalender] day4 Python
Effective Python Learning Memorandum Day 15 [15/100]
Effective Python Learning Memorandum Day 6 [6/100]
Effective Python Learning Memorandum Day 9 [9/100]
Effective Python Learning Memorandum Day 8 [8/100]
Sparta Camp Python 2019 Day2 Challenge
Effective Python Learning Memorandum Day 14 [14/100]
Effective Python Learning Memorandum Day 1 [1/100]
Effective Python Learning Memorandum Day 3 [3/100]
Effective Python Learning Memorandum Day 4 [4/100]
Effective Python Learning Memorandum Day 7 [7/100]
Effective Python Learning Memorandum Day 2 [2/100]
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)