forked from pygame-community/pygame-ce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_debug.py
191 lines (156 loc) · 5.88 KB
/
_debug.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""Debug functionality that allows for more useful issue reporting"""
import platform
import sys
import traceback
import importlib
from collections.abc import Callable
from typing import Optional
from os import environ
from pygame.version import ver
from pygame.system import get_cpu_instruction_sets
ImportResult = tuple[str, bool, Optional[Callable]]
def str_from_tuple(version_tuple):
"""Converts a tuple like (2, 0, 20) into a string joined by periods
Args:
version_tuple: tuple(version_major, version_minor, version_patch)
Returns:
str: "major.minor.patch"
"""
if version_tuple is None:
return "None"
strs = map(str, version_tuple)
return ".".join(strs)
def attempt_import(module, function_name, output_str=""):
"""Attempts to import function_name from module
Args:
module: string representing module name
function_name: string representing function name to be imported
output_str: optional string to prepend error messages to if one occurs
Returns:
tuple(str, bool, Any):
[0]: output_str + error_message
[1]: True if successful, False if failed
[2]: if successful, the thing that was imported, else None
"""
try:
mod = importlib.import_module(module)
i = getattr(mod, function_name)
success = True
except (ImportError, AttributeError):
i = None
output_str += f"There was a problem with {module} import\n"
output_str += "A default value will be returned for the version\n"
output_str += traceback.format_exc() + "\n" + "=" * 20 + "\n"
success = False
return (output_str, success, i)
def _get_platform_info():
"""
Internal helper to get platform information
"""
cpu_inst_dict = get_cpu_instruction_sets()
sse2 = 'Yes' if cpu_inst_dict['SSE2'] else 'No'
avx2 = 'Yes' if cpu_inst_dict['AVX2'] else 'No'
neon = 'Yes' if cpu_inst_dict['NEON'] else 'No'
ret = f"Platform:\t\t{platform.platform()}\n"
ret += f"System:\t\t\t{platform.system()}\n"
ret += f"System Version:\t\t{platform.version()}\n"
ret += f"Processor:\t\t{platform.processor()}\tSSE2: {sse2}\tAVX2: {avx2}\tNEON: {neon}\n"
ret += (
f"Architecture:\t\tBits: {platform.architecture()[0]}\t"
f"Linkage: {platform.architecture()[1]}\n\n"
)
ret += f"Python:\t\t\t{platform.python_implementation()} {sys.version}\n"
ret += (
f"GIL Enabled:\t\t{sys._is_gil_enabled()}\n"
if hasattr(sys, "_is_gil_enabled")
else "GIL Enabled:\t\tTrue\n"
)
ret += f"pygame version:\t\t{ver}\n"
return ret
def print_debug_info(filename=None):
"""Gets debug information for reporting bugs. Prints to console
if filename is not specified, otherwise writes to that file
(note: if filename is not an empty file, it will overwrite whatever is
in there)
Args:
filename: string name of the file to save
"""
debug_str = ""
# keyword for compat with getters
def default_return(linked=True):
# pylint: disable=unused-argument
return (-1, -1, -1)
from pygame.display import (
get_driver as get_display_driver,
get_init as display_init,
)
from pygame.mixer import (
get_driver as get_mixer_driver,
get_init as mixer_init,
)
from pygame.base import get_sdl_version
debug_str, *mixer = attempt_import(
"pygame.mixer", "get_sdl_mixer_version", debug_str
)
if not mixer[0]:
get_sdl_mixer_version = default_return
else:
get_sdl_mixer_version = mixer[1]
debug_str, *font = attempt_import("pygame.font", "get_sdl_ttf_version", debug_str)
if not font[0]:
get_sdl_ttf_version = default_return
else:
get_sdl_ttf_version = font[1]
debug_str, *image = attempt_import(
"pygame.image", "get_sdl_image_version", debug_str
)
if not image[0]:
get_sdl_image_version = default_return
else:
get_sdl_image_version = image[1]
debug_str, *freetype = attempt_import("pygame.freetype", "get_version", debug_str)
if not freetype[0]:
ft_version = default_return
else:
ft_version = freetype[1]
debug_str += _get_platform_info()
debug_str += (
f"SDL versions:\t\tLinked: {str_from_tuple(get_sdl_version())}\t"
f"Compiled: {str_from_tuple(get_sdl_version(linked = False))}\n"
)
debug_str += (
f"SDL Mixer versions:\tLinked: {str_from_tuple(get_sdl_mixer_version())}\t"
f"Compiled: {str_from_tuple(get_sdl_mixer_version(linked = False))}\n"
)
debug_str += (
f"SDL Font versions:\tLinked: {str_from_tuple(get_sdl_ttf_version())}\t"
f"Compiled: {str_from_tuple(get_sdl_ttf_version(linked = False))}\n"
)
debug_str += (
f"SDL Image versions:\tLinked: {str_from_tuple(get_sdl_image_version())}\t"
f"Compiled: {str_from_tuple(get_sdl_image_version(linked = False))}\n"
)
debug_str += (
f"Freetype versions:\tLinked: {str_from_tuple(ft_version())}\t"
f"Compiled: {str_from_tuple(ft_version(linked = False))}\n\n"
)
if display_init():
driver = get_display_driver()
if driver.upper() != "X11":
debug_str += f"Display Driver:\t\t{driver}\n"
else:
is_xwayland = (environ.get("XDG_SESSION_TYPE") == "wayland") or (
"WAYLAND_DISPLAY" in environ
)
debug_str += f"Display Driver:\t\t{driver} ( xwayland == {is_xwayland} )\n"
else:
debug_str += "Display Driver:\t\tDisplay Not Initialized\n"
if mixer_init():
debug_str += f"Mixer Driver:\t\t{get_mixer_driver()}"
else:
debug_str += "Mixer Driver:\t\tMixer Not Initialized"
if filename is None:
print(debug_str)
else:
with open(filename, "w", encoding="utf8") as debugfile:
debugfile.write(debug_str)