Skip to content

Instantly share code, notes, and snippets.

@ericsnowcurrently
ericsnowcurrently / new-runtime-api.md
Last active February 26, 2024 17:11
A rough, high-level greenfield design for the CPython runtime API

Summary:

  • C-API takes opaque context type as first arg
  • context is as specific as possible
  • separate Py_Main() from runtime initialization
  • separate global init from interpreter init
  • explicit enabling/disabling runtime components

Background Info

@ericsnowcurrently
ericsnowcurrently / pyconfig-analysis.md
Created February 16, 2024 18:26
Analysis of Pyconfig

PyConfig Fields

field type used modified conditional
isolated int
use_environment int
dev_mode int
install_signal_handlers int
use_hash_seed int
@ericsnowcurrently
ericsnowcurrently / gist:99e2c79281a02a407ddf1dd49bf2d321
Last active November 17, 2022 00:30
analysis of gilstate operations
_PyRuntimeState_GetThreadState()
_PyRuntime.gilstate.tstate_current <----
_PyRuntimeGILState_GetThreadState()
_PyRuntime.gilstate.tstate_current <----
_PyRuntimeGILState_SetThreadState(tstate)
_PyRuntime.gilstate.tstate_current = tstate <----
@ericsnowcurrently
ericsnowcurrently / hide_stdio.py
Last active August 19, 2020 17:23
A context manager for discarding stdout and stderr.
import contextlib
import os
import sys
try:
from io import StringIO as StdioStream
except ImportError:
from StringIO import StringIO # 2.7
class StdioStream(StringIO):
@ericsnowcurrently
ericsnowcurrently / ensure-ensurepip.py
Created February 27, 2020 00:07
A workaround for Ubuntu's ensurepip problem.
"""
This is a script for working around the lack of ensurepip in
Ubuntu's system Python installs.
See: https://bugs.launchpad.net/ubuntu/+source/python3.4/+bug/1290847
An alternative is the following:
$ python3 -m venv --without-pip my-venv
$ curl https://bootstrap.pypa.io/get-pip.py
$ my-venv/bin/python3 get-pip.py
@ericsnowcurrently
ericsnowcurrently / envs-api-design.md
Last active January 16, 2020 22:49
API Shape for the Environments Discovery Component

API Shape for the Environments Discovery Component

Requirements

The environment component  API should support following:

1. Discover installed python interpreters: This includes identifying interpreter
   details like name, version, bit-ness, interpreter paths
@ericsnowcurrently
ericsnowcurrently / lazyfilefinder.py
Last active September 8, 2017 00:25
A lazy file finder (and loader) that depends on a pre-computed database of lazy modules.
import importlib.machinery
import importlib.util
import sys
def load_lazydb(infile):
@ericsnowcurrently
ericsnowcurrently / startup-imports.py
Last active September 5, 2017 20:42
Python startup order and source file sizes.
import subprocess
args = [sys.executable, '-v', '-c', 'pass']
if not withsite:
args.insert(1, '-S')
out = subprocess.check_output(args, stderr=subprocess.STDOUT).decode()
imported = [line.split("'")[1]
for line in out.splitlines()
if line.startswith("import '")]
for name in imported:
try: