Python Forum
Navigating cpython source using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Navigating cpython source using python
#1
I am trying to read python source for some built in function/classes, but sometimes PyCharm is taking me to an autogenerated stub file which makes me think that the source might be written in C.

For example, when I try to get to the source of int(), PyCharm take me to this file,

[Image: EYvFmyu]
Image link: https://imgur.com/a/EYvFmyu

The file location in the PyCharm title bar tells me that it is an autogenerated file.

I know that cpython source is available at https://github.com/python/cpython, but it's hard to know where to find the source of a particular python function/class.

So my question is, where is the C source for int() class? And is there any well known guide out there for someone who is trying to connect python function/classes with corresponding cpython source?
Reply
#2
The code for the integers is in longobject.c. I don't know a guide to C python source tree, but there are only a limited number of built-in types, it is not very difficult to find the corresponding code with a little experience.
snippsat and quazirfan like this post
Reply
#3
(Aug-07-2021, 05:46 PM)quazirfan Wrote: And is there any well known guide out there for someone who is trying to connect python function/classes with corresponding cpython source?
Python source code layout Exploring CPython’s Internals
Quote:
  • Some exceptions:
builtin type int is at Objects/longobject.c
In documentation look at top if there Python code(Not C) it will link to source code eg calendar Source code: Lib/calendar.py
So this source file or method/function if has will also PyCharm or VS Code(Press Ctrl) for linking to source code.

pdir2 is fine for getting a nice layout how code is structured,example with calendar in image under.
[Image: j8ZK0K.png]
Reply
#4
Thanks! They both looks fantastic.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Algorithm for extracting comments from Python source code Pavel1982 6 546 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 726 Oct-29-2023, 12:40 PM
Last Post: Mark17
  python move specific files from source to destination including duplicates mg24 3 1,114 Jan-21-2023, 04:21 AM
Last Post: deanhystad
  Python Snippets Source kucingkembar 0 645 Oct-18-2022, 12:50 AM
Last Post: kucingkembar
  [CPython] Can't run script python VietVH 2 1,214 May-21-2022, 04:49 PM
Last Post: VietVH
  ImportError: /home/ali/Documents/test/charm/core/crypto/AES.cpython-39-x86_64-linux-g Anldra12 0 1,240 Apr-28-2022, 07:13 AM
Last Post: Anldra12
  Long-term stable source to get news headlines with Python? sandufi 4 1,945 Dec-23-2021, 09:48 AM
Last Post: sandufi
  Reducing runtime memory usage in Cpython interpreter david_the_graower 2 2,227 Oct-18-2021, 09:56 PM
Last Post: david_the_graower
  IronPython from CPython amasis 3 2,220 Feb-05-2021, 12:26 PM
Last Post: jefsummers
  ImportError: /home/pybind11_example.cpython-37m-x86_64-linux-gnu.so: undefined symbol chaitra 2 5,130 Feb-03-2021, 05:14 AM
Last Post: chaitra

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020