Skip to content
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

Don't overwrite save file on failed JSON serialization, fixes file corruption problem #132

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

shckltn
Copy link

@shckltn shckltn commented Jun 24, 2024

I created this because sometimes the saveData had something that wasn't able to be serialized and it would corrupt the save file when running json.dump(). This change creates a temp file with a datetime that is used to test whether the json serialization will succeed. This change will output a runtime error to let the user know if there is a problem and won't break the save file. On failure, the original save before the changes is maintained.

Let me know if there is anything else I can do to support this PR or if you need more info.

This change creates a temp file with a datetime that is used to test whether the json serialization will succeed.I created this because sometimes the saveData had something that wasn't abled to be serialized and it would corrupt the save file.This change will let the user know if there is a problem and won't break the save file. On failure, the original save before the changes is maintained.
@shckltn
Copy link
Author

shckltn commented Jun 24, 2024

This is the type of error that caused me to write this code:

Traceback (most recent call last):
  File "\PyFlow\App.py", line 426, in save
    json.dump(saveData, tempPtr, indent=4)
  File "\AppData\Local\Programs\Python\Python311\Lib\json\__init__.py", line 179, in dump
    for chunk in iterable:
  File "\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 432, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 406, in _iterencode_dict
    yield from chunks
  File "\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 326, in _iterencode_list
    yield from chunks
  File "\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 406, in _iterencode_dict
    yield from chunks
  File "\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 406, in _iterencode_dict
    yield from chunks
  File "\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 406, in _iterencode_dict
    yield from chunks
  File "\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 439, in _iterencode
    o = _default(o)
        ^^^^^^^^^^^
  File "\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type getset_descriptor is not JSON serializable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "Pyflow\PyFlow\App.py", line 582, in closeEvent
    if not self.save():
           ^^^^^^^^^^^

When this error occurs the save file will abruptly stop being written and will cut off part way through writing the json output.

@simoneluce
Copy link

This is because you are using a PIN that has serialization turned on by default.
I override this issue using a custom PIN setting it is not Storable.
self.disableOptions(PinOptions.Storable)

"w" would truncate the entire file.
"r+" the stream is positioned at the beginning of the file and doesn't overwrite.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants