Apple
This is the CPython source code repository - the reference implementation of the Python programming language. It contains the Python interpreter, the complete standard library, build systems for multiple platforms (Windows, macOS, Linux, Android, iOS), and all tooling needed to compile and distribute Python itself.
Key Features
- Asyncio Framework
- Multiprocessing and Concurrency
- Cross-Platform Build System
- Import Machinery
- Compression Libraries
- Database and Serialization
- IDLE IDE
- AST and Parser Tools
Entry Points
Start here when working with this codebase
Apple/__main__.pyOrchestrates building, packaging, and testing CPython XCframeworks for Apple platforms (iOS, macOS, tvOS, watchOS)
Start here when adding Apple platform support, modifying iOS/macOS build process, or adding new architecture targets
Android/android.pyHandles Android port automation - source configuration, cross-compilation, and device testing
Start here when modifying Android support, adding Android-specific features, or debugging Android test infrastructure
Doc/conf.pyCentral Sphinx configuration for building Python's official documentation
Start here when modifying documentation build process, adding new doc extensions, or changing doc output formats
Modules/getpath.pyRuntime path configuration initialization - determines sys.path, prefix, and exec_prefix at interpreter startup
Start here when modifying how Python finds modules, changing installation paths, or debugging import path issues
Parser/asdl.pyParses ASDL specification files into structured objects for AST code generation
Start here when modifying Python's abstract syntax tree structure or adding new AST node types
Lib/__future__.pyRegistry of Python language features with optional/mandatory release versions and compiler flags
Start here when adding new __future__ imports or deprecating language features
Lib/_collections_abc.pyDefines Abstract Base Class hierarchy for all Python collection types
Start here when adding new collection ABCs, modifying type checking behavior, or extending collection interfaces
How to Make Changes
Common modification patterns for this codebase
Add a new AST node type
Parser/Python.asdlAdd the new node definition to the ASDL grammar file
Pattern: Follow existing node definitions like 'expr = BoolOp(boolop op, expr* values)' - specify constructor name, fields with types
Parser/asdl.pyVerify ASDL parser handles your new construct (usually no changes needed)
Pattern: Check ASDLParser class methods like p_product and p_sum for grammar rules
Parser/asdl_c.pyRegenerate C code by running this script - it auto-generates from ASDL
Pattern: Run 'python Parser/asdl_c.py Parser/Python.asdl' to regenerate Include/internal/pycore_ast.h and Python/Python-ast.c
Lib/_ast_unparse.pyAdd unparse method for the new node to convert AST back to source
Pattern: Follow visit_BoolOp or visit_Compare methods - create visit_YourNode that writes proper Python syntax
Lib/ast.pyAdd any high-level helper functions for the new node type
Pattern: Follow existing helpers like literal_eval or walk
Conventions to follow:
Add a new standard library module
Lib/yourmodule.pyCreate the new module file in Lib/ directory
Pattern: Follow Lib/pathlib/__init__.py for class-based modules or Lib/_aix_support.py for utility modules - include __all__ export list and proper docstrings
Lib/test/test_yourmodule.pyCreate comprehensive test file
Pattern: Follow Lib/test/test_pathlib/test_pathlib_abc.py structure - use unittest.TestCase, group related tests in classes
Doc/library/yourmodule.rstCreate documentation file
Pattern: Follow Doc/library/pathlib.rst format - include module docstring, class/function documentation with examples
Doc/library/index.rstAdd module to documentation index under appropriate category
Pattern: Add 'yourmodule' to the appropriate toctree section
Lib/__future__.pyOnly if module introduces new language features requiring __future__ import
Pattern: Follow existing _Feature definitions with mandatory_release and compiler_flag
Conventions to follow:
Add a new collection Abstract Base Class
Lib/_collections_abc.pyDefine new ABC class with @abstractmethod decorators
Pattern: Follow Mapping class definition - inherit from appropriate parent ABC, define __slots__ = (), use @abstractmethod for required methods, provide default implementations for optional methods
Lib/_collections_abc.pyAdd to __all__ list at top of file
Pattern: Add string name to __all__ tuple in alphabetical order
Lib/_collections_abc.pyRegister built-in types that implement the ABC
Pattern: Follow 'Mapping.register(dict)' pattern at bottom of file
Lib/collections/abc.pyVerify re-export (usually automatic via import *)
Pattern: Check that 'from _collections_abc import *' captures your new ABC
Lib/test/test_collections.pyAdd test cases for the new ABC
Pattern: Follow TestOneTrickPonyABCs or TestCollectionABCs classes - test isinstance checks, registration, and method behavior
Conventions to follow:
Add a new asyncio feature
Lib/asyncio/base_events.pyAdd core event loop method if feature requires loop integration
Pattern: Follow create_task or run_in_executor methods - define in BaseEventLoop class with proper async/await syntax
Lib/asyncio/tasks.pyAdd task-related functionality
Pattern: Follow Task class or gather/wait functions - use @coroutine or async def appropriately
Lib/asyncio/streams.pyAdd stream-related functionality if applicable
Pattern: Follow StreamReader/StreamWriter classes for I/O operations
Lib/asyncio/__init__.pyExport new public APIs in __all__
Pattern: Add to __all__ list and import from appropriate submodule
Lib/test/test_asyncio/test_tasks.pyAdd comprehensive async tests
Pattern: Follow existing test classes - use self.loop.run_until_complete() or asyncio.run() for test execution
Conventions to follow:
Add a new compression algorithm
Lib/compression/yourcodec/__init__.pyCreate new compression module package
Pattern: Follow Lib/compression/zstd/__init__.py structure - define compress(), decompress(), open() functions and Compressor/Decompressor classes
Lib/compression/yourcodec/_yourcodecmodule.cCreate C extension module if wrapping native library
Pattern: Follow Modules/_zstd/_zstdmodule.c - use PyModule_Create, define PyMethodDef array
Lib/compression/__init__.pyRegister codec in compression package
Pattern: Add import and include in __all__ list
Lib/test/test_yourcodec.pyCreate test suite
Pattern: Follow Lib/test/test_zstd.py - test compress/decompress roundtrip, streaming, error handling
PCbuild/get_external.pyAdd external dependency download for Windows builds if needed
Pattern: Follow existing get_external patterns for zlib, openssl
Conventions to follow:
Add a new XML-RPC server feature
Lib/xmlrpc/server.pyExtend SimpleXMLRPCServer or SimpleXMLRPCRequestHandler class
Pattern: Follow DocXMLRPCServer class extension pattern - override do_GET for new HTTP endpoints, override _dispatch for new RPC behavior
Lib/xmlrpc/server.pyAdd introspection methods if extending RPC capabilities
Pattern: Follow system.listMethods, system.methodHelp pattern in SimpleXMLRPCDispatcher
Lib/xmlrpc/client.pyAdd client-side support if needed
Pattern: Follow ServerProxy class for client implementation
Lib/test/test_xmlrpc.pyAdd server and client tests
Pattern: Follow SimpleServerTestCase class - use threading for server, test both success and error cases
Conventions to follow:
Add Windows build dependency
PCbuild/get_external.pyAdd dependency download configuration
Pattern: Follow existing EXTERNALS dict entries - specify URL, hash, and extraction path
PCbuild/prepare_ssl.pyAdd preparation script if dependency needs build configuration (like OpenSSL)
Pattern: Follow prepare_ssl.py structure - handle architecture detection, run configure/build commands
PCbuild/python.propsAdd MSBuild properties for include/lib paths
Pattern: Follow existing PropertyGroup entries for external dependencies
PCbuild/readme.txtDocument the new dependency and build requirements
Pattern: Follow existing dependency documentation format
Conventions to follow:
Add a new __future__ feature
Lib/__future__.pyAdd _Feature instance for the new feature
Pattern: Follow 'annotations = _Feature((3, 7, 0, "beta", 1), (3, 11, 0, "alpha", 0), CO_FUTURE_ANNOTATIONS)' - specify optional_release, mandatory_release, and compiler_flag
Include/compile.hDefine CO_FUTURE_* compiler flag constant
Pattern: Follow existing CO_FUTURE_DIVISION, CO_FUTURE_ANNOTATIONS definitions
Python/compile.cHandle the compiler flag in compilation
Pattern: Check for flag in compiler_mod and adjust code generation accordingly
Lib/test/test___future__.pyAdd tests for the new feature flag
Pattern: Follow existing test patterns - verify feature attributes and compiler behavior
Conventions to follow:
Add Android platform support for a module
Android/android.pyAdd module to build configuration if it requires special handling
Pattern: Follow existing module handling in configure_build() function
Lib/_android_support.pyAdd Android-specific support code if module needs platform adaptation
Pattern: Follow TextIOWrapper redirection pattern for stdout/stderr
Android/testbed/app/src/main/python/Add Android-specific tests
Pattern: Follow existing test structure in testbed app
Lib/test/test_yourmodule.pyAdd skipUnless/skipIf decorators for Android-incompatible tests
Pattern: Use @unittest.skipUnless(hasattr(sys, 'getandroidapilevel'), 'Android only') or similar
Conventions to follow:
Freeze a module into the interpreter
Programs/_freeze_module.pyUnderstand the freezing process - this script converts .py to frozen C code
Pattern: Run 'python Programs/_freeze_module.py module_name Lib/module.py Python/frozen_module.h' to generate frozen bytecode
Python/frozen.cAdd entry to _PyImport_FrozenModules array
Pattern: Follow existing entries like {"_frozen_importlib", _Py_M__importlib__bootstrap, ...}
Makefile.pre.inAdd freeze target for the module
Pattern: Follow existing FROZEN_FILES targets
Programs/freeze_test_frozenmain.pyReference for testing frozen module behavior
Pattern: Follow the test pattern for verifying frozen bytecode arrays
Conventions to follow:
Coding Conventions
Standards and patterns used in this codebase
Module docstrings must include comprehensive descriptions with copyright notices, version info, and detailed explanations of module purpose and usage
"""This is the Sphinx configuration file for the Python documentation.
All configuration values have a default value; values that are commented out
serve to show the default value.
"""Group imports in order: standard library, third-party, local imports, with blank lines between groups. Use explicit imports over wildcard imports
import dataclasses
import os
import re
import subprocess
import sys
from pathlib import Path
from os.path import relpathUse argparse with subparsers for complex CLI tools, defining common arguments at parent level and command-specific arguments in subparsers
def parse_args():
parser = argparse.ArgumentParser()
subcommands = parser.add_subparsers(dest="subcommand")
build = subcommands.add_parser("build", help="Build a framework")
build.add_argument("--target", default="arm64-apple-ios")
return parser.parse_args()Define custom exception classes for domain-specific errors, inheriting from appropriate base exceptions
class ASDLSyntaxError(Exception):
def __init__(self, lineno, msg=None, filename=None):
self.lineno = lineno
self.msg = msg
self.filename = filenameUse dedicated visitor pattern classes with emit() methods for generating code, tracking indentation level explicitly
class EmitVisitor(ASDLVisitor):
def __init__(self, file):
self.file = file
def emit(self, s, depth, reflow=True):
if reflow:
lines = reflow_c_string(s, depth)
else:
lines = [s]
for line in lines:
if line:
self.file.write(" " * TABSIZE * depth + line + "\n")Define module-level constants using UPPER_CASE naming, grouped at top of file after imports
SRCDIR = Path(__file__).resolve().parent.parent
TABSIZE = 4
MAX_COL = 80
DEFAULT_HOST = "$(CC) --print-multiarch"Use dataclasses with type annotations for structured data, especially for AST nodes and configuration objects
@dataclasses.dataclass
class Module:
name: str
dfns: list
@dataclasses.dataclass
class Type:
name: str
value: objectCreate wrapper functions for subprocess calls that handle logging, error checking, and environment configuration consistently
def run(*args, check=True, quiet=False, **kwargs):
if not quiet:
log(" ".join(map(str, args[0])))
return subprocess.run(*args, check=check, **kwargs)Use pathlib.Path for all filesystem operations, resolving paths relative to script location using __file__
SRCDIR = Path(__file__).resolve().parent.parent
BUILD_DIR = SRCDIR / "build"
CROSS_BUILD_DIR = BUILD_DIR / "cross-build"Use if __name__ == '__main__' guard with sys.exit() wrapping main function for proper exit code handling
if __name__ == "__main__":
sys.exit(main())Use named tuples or dataclasses for feature flag definitions with mandatory and optional release version fields
_Feature = collections.namedtuple('_Feature', [
'optional_release',
'mandatory_release',
'compiler_flag'
])
annotations = _Feature((3, 7, 0, 'beta', 1), (3, 14, 0, 'alpha', 1), CO_FUTURE_ANNOTATIONS)Implement simple logging functions that respect verbosity flags and write to stderr for build/automation scripts
def log(msg=""):
print(msg, file=sys.stderr, flush=True)
def log_cmd(args):
if verbose:
log(" ".join(map(str, args)))