Audit UTF-8 Defaults and Profiling Migration
Core idea
Python 3.15 enables UTF-8 mode by default, changing implicit encoding behavior on systems that formerly used another locale encoding. Explicit encodings remain important for file formats and external protocols. The release also introduces profiling.tracing for deterministic call tracing and profiling.sampling (Tachyon) for statistical profiling. cProfile remains a compatibility alias; profile is deprecated for removal in 3.17. Sampling and tracing have different overhead and evidence characteristics.
from pathlib import Pathfrom tempfile import TemporaryDirectorywith TemporaryDirectory() as root: path = Path(root, 'report.txt') path.write_text('سلام', encoding='utf-8') print(path.read_text(encoding='utf-8') == 'سلام') print(len(path.read_bytes()))The four Persian letters use eight UTF-8 bytes. Explicit encoding makes the contract independent of default locale behavior.
Expected output:
True8Target: Python 3.15 deterministic tracing
from profiling.tracing import Profilefrom pstats import Stats
def calculate(): return sum(range(5))profiler = Profile()result = profiler.runcall(calculate)stats = Stats(profiler)print(result)print(any(key[2] == 'calculate' for key in stats.stats))A tracing profile records the function call. The example checks the result and presence of the function, not unstable timing values.
Documented expected output (target runtime not tested locally):
10TrueCommon mistake
Do not assume all data files are UTF-8 because the interpreter defaults changed. Preserve explicit legacy encodings where required; encoding="locale" deliberately selects the locale. UTF-8 mode can be disabled at startup, so inspect flags rather than assume. Do not interpret sampling estimates as exact call counts or promise a speedup without measurement.
Practice
On 3.15, compare sys.flags.utf8_mode using default startup and -X utf8=0, and inspect locale.getencoding(). Read a known legacy-encoded file with the correct explicit encoding. Profile a small trusted workload with tracing; review python -m profiling.sampling --help before attempting statistical profiling.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
UTF-8 defaults and profiling changes
Check your understanding of utf-8 defaults and profiling changes.
On Python 3.15 (documented expectation; target runtime not tested locally), what is printed?
import sysprint(sys.flags.utf8_mode)What is printed?
print('سلام'.encode('utf-8').decode('utf-8') == 'سلام')Which profiler records calls deterministically?
How should an application read a known legacy-encoded file?