0

I want to print the source code of a built-in method. for example, math is a built-in module of python and i want to print the source code of ceil

i know how to print the source code of a custom module using inspect.getsource

need help
I am trying to create a programme where i can call any builtin methods or functions and it will display only the source code of that function or module.
Python has almost everything in builtin library, i want to use these libraries

example:

input: factorial
output:
        def factorial(n):
            if n == 0:        
                return 1      
            else:      
                return n * factorial(n-1)

works just fine
import inspect
inspect.getsource(factorial)
doesn't work ... results in Type Error
import inspect
import math
print(inspect.getsource(math.ceil)
TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object

thanks in advance :)

7
  • 2
    I don't think this is possible. I think most builtins are pretty much compiled C functions with a python interface wrapper. (If you want' to inspect the code, you can always check the repository) Commented Feb 26, 2018 at 7:50
  • 1
    In ipython you can use the function?? magic to see the source code - if it is written in Python. But if compiled from C code it will be marked as builtin. Usually I look online for the development repository (e.g. github) But matching the Python function name with the C function name can be tricky. Commented Feb 26, 2018 at 7:54
  • Do you really need to see the source code to use the libraries? But as stated, if you are using CPython, most of hte built-in libraries are implemented in C, and you won't have access to the source. You can find it on the github repo, though. Commented Feb 26, 2018 at 7:56
  • On the cpython github, I found a math.ceil in cpython/Modules/mathmodule.c that looks like it's the source. Commented Feb 26, 2018 at 8:01
  • 1
    Download the C source from python.org/downloads, then either construct a table of module/source file names, or write the functionality of grep to search the C source code. Commented Feb 26, 2018 at 8:06

1 Answer 1

6

If the source is Python you can do this:

import inspect
import math

try:
    print(inspect.getsource(math))
except OSError:
    print("Source not available, possibly a C module.")

As other people already commented, many builtin modules are C. If it is the case you will have to dive in the source - fortunately it is not that hard to find, the structure is quite intuitive.

For math.ceil the source code is at line 1073 of Modules/mathmodule.c in cpython:

/*[clinic input]
math.ceil
    x as number: object
    /
Return the ceiling of x as an Integral.
This is the smallest integer >= x.
[clinic start generated code]*/

static PyObject *
math_ceil(PyObject *module, PyObject *number)
/*[clinic end generated code: output=6c3b8a78bc201c67 
input=2725352806399cab]*/
{
    _Py_IDENTIFIER(__ceil__);
    PyObject *method, *result;

    method = _PyObject_LookupSpecial(number, &PyId___ceil__);
    if (method == NULL) {
        if (PyErr_Occurred())
            return NULL;
        return math_1_to_int(number, ceil, 0);
    }
    result = _PyObject_CallNoArg(method);
    Py_DECREF(method);
    return result;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.