okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
f5082f41a
main
1"""No raw NUL byte in a source file. A shell heredoc that loses the backslash of '\\0' leaves a literal 0x00 inside a 2character constant: gcc only warns ("null character(s) preserved in literal"), MSVC stops with an error (0.1.43.1's 3generate.cpp; expert_source.cpp and pinned.cu after it). No binary, no GPU. 4 5python -m unittest tools.test_source_nul_bytes 6""" 7from __future__import annotations 8 9import subprocess 10import unittest 11from pathlib import Path 12 13ROOT = Path (__file__ ).resolve ().parent .parent 14SOURCE_SUFFIXES = {".c" ,".cc" ,".cpp" ,".cxx" ,".h" ,".hpp" ,".cu" ,".cuh" ,".hip" ,".mm" ,".metal" ,".py" ,".cmake" , 15".txt" ,".sh" ,".bat" ,".ps1" ,".js" ,".html" ,".css" ,".json" ,".jinja" ,".md" ,".in" } 16SOURCE_DIRS = ("src" ,"include" ,"sycl" ,"metal" ,"serve" ,"tools" ,"tests" ,"cmake" ,"bench" ,"docs" ) 17 18 19def source_files ()-> list [Path ]: 20try : 21out = subprocess .run (["git" ,"ls-files" ,"-z" ,"--" ,* SOURCE_DIRS ,"*.py" ,"*.txt" ,"*.sh" ,"*.bat" ], 22cwd = ROOT ,capture_output = True ,check = True ).stdout 23names = [n .decode ("utf-8" ,"surrogateescape" )for n in out .split (b"\0" )if n ] 24paths = [ROOT / n for n in names ] 25except (OSError ,subprocess .CalledProcessError ):# not a git checkout (a release zip): walk the tree instead 26paths = [p for d in SOURCE_DIRS if (ROOT / d ).is_dir ()for p in (ROOT / d ).rglob ("*" )] 27paths += [p for p in ROOT .iterdir ()if p .is_file ()] 28return sorted ({p for p in paths if p .is_file ()and p .suffix .lower ()in SOURCE_SUFFIXES }) 29 30 31def nul_lines (path :Path )-> list [int ]: 32data = path .read_bytes () 33if b"\0" not in data : 34return [] 35return [i + 1 for i ,line in enumerate (data .split (b"\n" ))if b"\0" in line ] 36 37 38class SourceNulBytes (unittest .TestCase ): 39def test_no_source_file_holds_a_nul_byte (self ): 40files = source_files () 41self .assertGreater (len (files ),50 ,"the source scan found almost nothing" ) 42bad = {str (p .relative_to (ROOT )):nul_lines (p )for p in files } 43bad = {k :v for k ,v in bad .items ()if v } 44self .assertEqual (bad , {},"raw NUL bytes (write '\\0' instead) at file: [lines]" ) 45 46def test_the_check_sees_a_nul (self ): 47import tempfile 48with tempfile .TemporaryDirectory ()as d : 49p = Path (d )/ "x.cpp" 50p .write_bytes (b"int a;\nchar c = '\0';\n" ) 51self .assertEqual (nul_lines (p ), [2 ]) 52 53 54if __name__ == "__main__" : 55unittest .main ()