Skip to content

Commit 5e3d459

Browse files
committed
don't fail on Windows
1 parent 7846d50 commit 5e3d459

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

memory_inventory/__init__.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import os
1717
from pathlib import Path
1818
from pprint import pformat
19-
from resource import getrusage, RUSAGE_SELF
2019
import sys
2120
from tempfile import gettempdir
2221
import threading
@@ -91,7 +90,13 @@ def qual_name(cls: type) -> str:
9190
def render_process_stats() -> str:
9291
"""Get some basic statistics on the current process. Unix only.
9392
"""
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)
95100
total_time = ru.ru_utime + ru.ru_stime
96101
u2s_ratio = ru.ru_utime / total_time
97102
res_mem = ru.ru_idrss or '<unknown>'
@@ -1173,36 +1178,36 @@ def write_tables(self, save_dir: Path, html_template: str) -> None:
11731178
maximum_number_of_rows=15,
11741179
) or ' none.'}"
11751180
)
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((
11781183
"<h2>Types by intrinsic memory usage</h2>",
11791184
''.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((
11831188
"<h2>Uninstantiated types <small>(excluding BaseException and its subclasses)</small></h2>",
11841189
'\n'.join(
11851190
f'<details><summary>{cls_qual_name}</summary>'
11861191
f'<div class="details">{html.escape(repr(cls))} at 0x{id(cls):x}</div></details>'
11871192
for cls, cls_qual_name in uninstantiated_types
11881193
),
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((
11921197
"<h2>Instantiated classes lacking slots</h2>",
11931198
''.join(render_html_table(*classes_lacking_slots[0], '')) or '<p>None found.</p>',
11941199
"<h2>Uninstantiated classes lacking slots</h2>",
11951200
''.join(render_html_table(*classes_lacking_slots[1], '')) or (
11961201
'<p>None found.</p>' if sys.version_info >= (3, 13, 0) else
11971202
'<p>None found (requires Python ≥ 3.13).</p>'
11981203
),
1199-
))), file=f)
1204+
))).encode('utf-8'))
12001205
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((
12031208
"<h2>Duplicate instances</h2>",
12041209
''.join(render_html_table(*table_data, '')) or '<p>None found.</p>',
1205-
))), file=f)
1210+
))).encode('utf-8'))
12061211

12071212

12081213
def types_by_duplicate_objects(
@@ -1380,7 +1385,7 @@ def main(
13801385
def collect_stats() -> TypeStatsDict:
13811386
debug: Callable[..., None]
13821387
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')
13841389
debug = partial(print, file=debug_log)
13851390
else:
13861391
debug_log = open(os.devnull, 'w')

0 commit comments

Comments
 (0)