|
16 | 16 | import os |
17 | 17 | from pathlib import Path |
18 | 18 | from pprint import pformat |
19 | | -from resource import getrusage, RUSAGE_SELF |
20 | 19 | import sys |
21 | 20 | from tempfile import gettempdir |
22 | 21 | import threading |
@@ -91,7 +90,13 @@ def qual_name(cls: type) -> str: |
91 | 90 | def render_process_stats() -> str: |
92 | 91 | """Get some basic statistics on the current process. Unix only. |
93 | 92 | """ |
94 | | - ru = getrusage(RUSAGE_SELF) |
| 93 | + if sys.platform == 'win32': # for mypy |
| 94 | + return "" |
| 95 | + try: |
| 96 | + import resource |
| 97 | + except ImportError: |
| 98 | + return "" |
| 99 | + ru = resource.getrusage(resource.RUSAGE_SELF) |
95 | 100 | total_time = ru.ru_utime + ru.ru_stime |
96 | 101 | u2s_ratio = ru.ru_utime / total_time |
97 | 102 | res_mem = ru.ru_idrss or '<unknown>' |
@@ -1173,36 +1178,36 @@ def write_tables(self, save_dir: Path, html_template: str) -> None: |
1173 | 1178 | maximum_number_of_rows=15, |
1174 | 1179 | ) or ' none.'}" |
1175 | 1180 | ) |
1176 | | - with open(save_dir / 'types_by_memory_usage.html', 'w') as f: |
1177 | | - print(html_template.replace('$body$', '\n'.join(( |
| 1181 | + with open(save_dir / 'types_by_memory_usage.html', 'wb') as f: |
| 1182 | + f.write(html_template.replace('$body$', '\n'.join(( |
1178 | 1183 | "<h2>Types by intrinsic memory usage</h2>", |
1179 | 1184 | ''.join(render_html_table(*types_by_intrinsic_memory_usage, '')), |
1180 | | - ))), file=f) |
1181 | | - with open(save_dir / 'uninstantiated_types.html', 'w') as f: |
1182 | | - print(html_template.replace('$body$', '\n'.join(( |
| 1185 | + ))).encode('utf-8')) |
| 1186 | + with open(save_dir / 'uninstantiated_types.html', 'wb') as f: |
| 1187 | + f.write(html_template.replace('$body$', '\n'.join(( |
1183 | 1188 | "<h2>Uninstantiated types <small>(excluding BaseException and its subclasses)</small></h2>", |
1184 | 1189 | '\n'.join( |
1185 | 1190 | f'<details><summary>{cls_qual_name}</summary>' |
1186 | 1191 | f'<div class="details">{html.escape(repr(cls))} at 0x{id(cls):x}</div></details>' |
1187 | 1192 | for cls, cls_qual_name in uninstantiated_types |
1188 | 1193 | ), |
1189 | | - ))), file=f) |
1190 | | - with open(save_dir / 'classes_lacking_slots.html', 'w') as f: |
1191 | | - print(html_template.replace('$body$', '\n'.join(( |
| 1194 | + ))).encode('utf-8')) |
| 1195 | + with open(save_dir / 'classes_lacking_slots.html', 'wb') as f: |
| 1196 | + f.write(html_template.replace('$body$', '\n'.join(( |
1192 | 1197 | "<h2>Instantiated classes lacking slots</h2>", |
1193 | 1198 | ''.join(render_html_table(*classes_lacking_slots[0], '')) or '<p>None found.</p>', |
1194 | 1199 | "<h2>Uninstantiated classes lacking slots</h2>", |
1195 | 1200 | ''.join(render_html_table(*classes_lacking_slots[1], '')) or ( |
1196 | 1201 | '<p>None found.</p>' if sys.version_info >= (3, 13, 0) else |
1197 | 1202 | '<p>None found (requires Python ≥ 3.13).</p>' |
1198 | 1203 | ), |
1199 | | - ))), file=f) |
| 1204 | + ))).encode('utf-8')) |
1200 | 1205 | for cls, table_data in duplicate_objects_by_type.items(): |
1201 | | - with open(save_dir / f'duplicate_{qual_name(cls)}.html', 'w') as f: |
1202 | | - print(html_template.replace('$body$', '\n'.join(( |
| 1206 | + with open(save_dir / f'duplicate_{qual_name(cls)}.html', 'wb') as f: |
| 1207 | + f.write(html_template.replace('$body$', '\n'.join(( |
1203 | 1208 | "<h2>Duplicate instances</h2>", |
1204 | 1209 | ''.join(render_html_table(*table_data, '')) or '<p>None found.</p>', |
1205 | | - ))), file=f) |
| 1210 | + ))).encode('utf-8')) |
1206 | 1211 |
|
1207 | 1212 |
|
1208 | 1213 | def types_by_duplicate_objects( |
@@ -1380,7 +1385,7 @@ def main( |
1380 | 1385 | def collect_stats() -> TypeStatsDict: |
1381 | 1386 | debug: Callable[..., None] |
1382 | 1387 | if write_debug_log: |
1383 | | - debug_log = open(save_path / 'debug.log', 'w') |
| 1388 | + debug_log = open(save_path / 'debug.log', 'w', encoding='utf-8') |
1384 | 1389 | debug = partial(print, file=debug_log) |
1385 | 1390 | else: |
1386 | 1391 | debug_log = open(os.devnull, 'w') |
|
0 commit comments