Specify utf-8 encoding when writing a file in router.py#231
Open
Nixellion wants to merge 1 commit intoklen:developfrom
Open
Specify utf-8 encoding when writing a file in router.py#231Nixellion wants to merge 1 commit intoklen:developfrom
Nixellion wants to merge 1 commit intoklen:developfrom
Conversation
Author
|
A note, same change required for the but it's missing from this PR. I can create another PR with that change if needed |
Author
|
Until this PR is implemented a patch can be applied like this, in your own code, by subclassing the Router class: from peewee_migrate import Router as BadRouter
from peewee_migrate.router import void
from peewee_migrate.template import TEMPLATE
class Router(BadRouter):
def compile(self, name, migrate="", rollback="", num=None) -> str: # noqa: A003
"""Create a migration."""
if num is None:
num = len(self.todo)
name = "{:03}_".format(num + 1) + name
filename = name + ".py"
path = self.migrate_dir / filename
with path.open("w", encoding="utf-8") as f:
f.write(TEMPLATE.format(migrate=migrate, rollback=rollback, name=filename))
return name
def read(self, name):
"""Read migration from file."""
path = self.migrate_dir / (name + ".py")
with path.open("r", encoding="utf-8") as f:
code = f.read()
scope = {}
code = compile(code, "<string>", "exec", dont_inherit=True)
exec(code, scope, None)
return scope.get("migrate", void), scope.get("rollback", void) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Without specifying "utf-8" encoding when writing a file the script fails to write text to a file in many cases.
I can't say if it's specific to OS region\language or contents of the file, but this simple change fixes any such issues, tested locally.
Changes in this PR
Add
encoding="utf-8"to the file open on write.cc/ @klen