-
-
Notifications
You must be signed in to change notification settings - Fork 185
Debug method #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Debug method #2
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
28a8b5d
added pygame._debug module and aliased pygame._debug.print_debug_info…
oddbookworm d1f1848
ran formatter
oddbookworm 4b399e7
manually added space around '='
oddbookworm 12245c6
made linter ignore unused argument instead of a dummy usage of it
oddbookworm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from typing import Tuple, Union, Optional, Callable | ||
|
||
ImportResult = Tuple[str, bool, Optional[Callable]] | ||
|
||
def str_from_tuple(version_tuple: Union[Tuple[int, int, int], None]) -> str: ... | ||
def attempt_import(module: str, function_name: str, output_str: str = "") -> ImportResult: ... | ||
def print_debug_info(filename: Optional[str] = None) -> None: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
"""Debug functionality that allows for more useful issue reporting | ||
""" | ||
|
||
import sys | ||
import traceback | ||
import importlib | ||
from typing import Tuple, Optional, Callable | ||
|
||
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 messagess 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 dummy value will be returned for the version\n" | ||
output_str += traceback.format_exc() + "\n" + "=" * 20 + "\n" | ||
success = False | ||
|
||
return (output_str, success, i) | ||
|
||
|
||
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 dummy_return(linked=True): | ||
# pylint: disable=unused-argument | ||
return (-1, -1, -1) | ||
|
||
from pygame.display import get_driver, get_init as display_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 = dummy_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 = dummy_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 = dummy_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 = dummy_return | ||
else: | ||
ft_version = freetype[1] | ||
|
||
from pygame.version import ver | ||
|
||
import platform | ||
|
||
debug_str += f"Platform:\t\t{platform.platform()}\n" | ||
|
||
debug_str += f"System:\t\t\t{platform.system()}\n" | ||
|
||
debug_str += f"System Version:\t\t{platform.version()}\n" | ||
|
||
debug_str += f"Processor:\t\t{platform.processor()}\n" | ||
|
||
debug_str += ( | ||
f"Architecture:\t\tBits: {platform.architecture()[0]}\t" | ||
f"Linkage: {platform.architecture()[1]}\n" | ||
) | ||
|
||
if display_init(): | ||
debug_str += f"Driver:\t\t\t{get_driver()}\n\n" | ||
else: | ||
debug_str += "Driver:\t\t\tDisplay Not Initialized\n\n" | ||
|
||
debug_str += f"Python:\t\t\t{platform.python_implementation()}\n" | ||
|
||
debug_str += f"pygame version:\t\t{ver}\n" | ||
|
||
debug_str += f"python version:\t\t{str_from_tuple(sys.version_info[0:3])}\n\n" | ||
|
||
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))}" | ||
) | ||
|
||
if filename is None: | ||
print(debug_str) | ||
|
||
else: | ||
with open(filename, "w", encoding="utf8") as debugfile: | ||
debugfile.write(debug_str) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import unittest, unittest.mock | ||
import io | ||
|
||
import pygame | ||
|
||
|
||
class DebugTest(unittest.TestCase): | ||
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO) | ||
def assert_stdout(self, expected_output, mock_stdout): | ||
pygame.print_debug_info() | ||
self.assertEqual(mock_stdout.getvalue(), expected_output) | ||
|
||
def test_print_debug(self): | ||
import os | ||
|
||
pygame.print_debug_info("temp_file.txt") | ||
with open("temp_file.txt", "r") as temp_file: | ||
text = temp_file.read() | ||
|
||
self.assertNotEqual(text, "") | ||
self.assert_stdout(text + "\n") | ||
|
||
os.remove("temp_file.txt") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.