-
-
Notifications
You must be signed in to change notification settings - Fork 62
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
Encode output inside mako cmd and write as binary #377
Conversation
This change is a proposed fix for sqlalchemy#376
Encode output inside mako cmd and write as binary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Id like to remove all binary mode from Mako as we are in python 3 now
encoded_output = rendered.encode() | ||
else: | ||
encoded_output = rendered.encode(output_encoding) | ||
open(output_file, "wb").write(encoded_output) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please fix this on the read side instead; we should not be reading files in binary mode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about that but that type of change would be higher impact, i.e. it would affect users of mako.template
# Long-standing template.render behavior
template = mako.template.Template(filename="template_file")
rendered = template.render()
# now:
# rendered == 'Line 1\r\nLine 2\r\nLine 3\r\n'
# if we changed to read as text:
# rendered == 'Line 1\nLine 2\nLine 3\n'
I'd be more concerned about changing this behavior since it's established that mako does not do newline conversion.
I'm less concerned with the impact of changing things in mako.cmd, especially since the existing behavior is completely misaligned with any newline convention (i.e. the awkward \r\r\n
sequences).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reading text mode on the front is as you mention not that feasible unless I want to no longer support "magic encoding comments", which I don't, but i dont have the appetite to rework that now.
the fix here is not the encoding, it's the newline conversion. we dont need it and it can be turned off directly without otherwise changing how text mode wants to write files:
diff --git a/mako/cmd.py b/mako/cmd.py
index 93a6733..deb8b95 100755
--- a/mako/cmd.py
+++ b/mako/cmd.py
@@ -86,12 +86,12 @@ def cmdline(argv=None):
kw = dict(varsplit(var) for var in options.var)
try:
- rendered = template.render(**kw)
+ rendered = template.render_unicode(**kw)
except:
_exit()
else:
if output_file:
- open(output_file, "wt", encoding=output_encoding).write(rendered)
+ open(output_file, "wt", newline="", encoding=output_encoding).write(rendered)
else:
sys.stdout.write(rendered)
can you do that here? thanks
Mike Bayer has proposed a fix for this issue in the main branch: switch to textmode in all cases https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/4450 |
encoded_output = rendered.encode() | ||
else: | ||
encoded_output = rendered.encode(output_encoding) | ||
open(output_file, "wb").write(encoded_output) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reading text mode on the front is as you mention not that feasible unless I want to no longer support "magic encoding comments", which I don't, but i dont have the appetite to rework that now.
the fix here is not the encoding, it's the newline conversion. we dont need it and it can be turned off directly without otherwise changing how text mode wants to write files:
diff --git a/mako/cmd.py b/mako/cmd.py
index 93a6733..deb8b95 100755
--- a/mako/cmd.py
+++ b/mako/cmd.py
@@ -86,12 +86,12 @@ def cmdline(argv=None):
kw = dict(varsplit(var) for var in options.var)
try:
- rendered = template.render(**kw)
+ rendered = template.render_unicode(**kw)
except:
_exit()
else:
if output_file:
- open(output_file, "wt", encoding=output_encoding).write(rendered)
+ open(output_file, "wt", newline="", encoding=output_encoding).write(rendered)
else:
sys.stdout.write(rendered)
can you do that here? thanks
I dont understand this PR or my comments about "fix this on the read side". If this is for #376 why isn't it just the fix I proposed at #376 (comment) ? Changing Mako to not read from binary mode or whatever can be something else. can we make a PR that fixes #376 in the most simple way possible, thanks. |
looks like I requested that here at #377 (review) already, so this PR is stalled. We can reopen if someone has time to work on this or make a new PR against #376. |
This change is a proposed fix for #376