Always pass text to markdown()

Review Request #10699 — Created Sept. 7, 2019 and discarded

Information

Djblets
master

Reviewers

Previously, we were passing a binary file to markdown.markdownFromFile in
render_markdown_from_file, but this won't fly in Python 3. It expects
a file opened with an encoding. However, Django does not let you specify
an encoding when opening a file from storage, so that solution is out.
We now just read the entire file into memory, decode it from UTF-8, and
then pass that to markdown.markdown(), which returns text directly.

With this patch applied, text file attachments render correctly
in Review Board on Python 2.7, 3.5, 3.6, and 3.7.

Summary ID Author
Always pass text to markdown()
Previously, we were passing a binary file to `markdown.markdownFromFile` in `render_markdown_from_file`, but this won't fly in Python 3. It expects a file opened with an encoding. However, Django does not let you specify an encoding when opening a file from storage, so that solution is out. We now just read the entire file into memory, decode it from UTF-8, and then pass that to `markdown.markdown()`, which returns text directly. Testing done: With this patch applied, text file attachments render correctly in Review Board on Python 2.7, 3.5, 3.6, and 3.7.
461754fc77a94d7e3573e58eb4f3c6ce828efeb4 Barret Rennie
Description From Last Updated

F401 'markdown.Markdown' imported but unused

reviewbotreviewbot

What's the error you hit? I tried passing in a FileAttachment.file into markdownFromFile as input and it handled it right. …

chipx86chipx86

Can you remove the blank lines at the end of the file?

chipx86chipx86
Checks run (1 failed, 1 succeeded)
flake8 failed.
JSHint passed.

flake8

brennie
chipx86
  1. 
      
  2. djblets/markdown/__init__.py (Diff revision 2)
     
     
     
     
     
     
     
    Show all issues

    What's the error you hit? I tried passing in a FileAttachment.file into markdownFromFile as input and it handled it right.

    Looking at the code, it attempts to do a codecs.getreader()(input) if you pass it a file handle, and it only needs .read() and .close(), both of which Django's File supports.

    In my test, I was able to verify that File.read() was giving bytes normally, but passing it into markdownFromFile rendered unicode fine. This on Python 3.x.

  3. djblets/markdown/__init__.py (Diff revision 2)
     
     
     
     
    Show all issues

    Can you remove the blank lines at the end of the file?

  4. 
      
brennie
Review request changed
Status:
Discarded
Change Summary:

Discarding based on conversation with Christian.