python / python/cpython

difflib adding an additional html column when making tables using difflib.HtmlDiff()

Đang mở
#117,943 4 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug report

Bug description:

I am working with the python library difflib and specifically the class HtmlDiff. For some reason, the function make_table is adding a blank column in when generating an HTML table, throwing off difference highlights and defeating the entire purpose of the diff function alltogether.

Input python function:

# diff_tool.py

import argparse
import difflib
import sys

from pathlib import Path


def create_diff(output_file: Path = None):
        
    # short list of strings to compare, nonproblematic (see expected output)
    thing1 = ["this", "that"]
    thing2 = ["thiis", "that2"]

    # longer list of strings to compare, mirroring the problematic data (see actual output)
    file_2 = ['Ab', 
    'my_id: ID1234, attribute: abcd fghijklmn py stuvwx zabc efg ijklmnopy st vwxy abcd f , name: abcd fghijklmn py stuvwx zabc efg ijklmnopy st vwxy abcd f ', 
    'my_id: ID5678, attribute: abcde ghijklm opyrst vwxyz bc efghij lmnopyr tuv xyzab defabcde ghijklmno yrstuvwxyza cdefghijklmnop rstuvw yz b defab defgh jklmnop rstuvwxyz bcd fghijkl nopyr tuvwxyza cdefabcd fghijkl no yrs uvwxyzabcd fghij l n, name: ab defghi klmnopy stuvwxy abcde ghijklm op']
                   
    file_2_new = ['Abcdefghijklmn', 
    'my_id: ID1234, attribute: abcde ghijklm opyrst vwxyz bc efghij lmnopyr tuv xyzab defabcde ghijklmno yrstuvwxyza cdefghijklmnop rstuvw yz b defab defgh jklmnop rstuvwxyz bcd fghijkl nopyr tuvwxyza cdefabcd fghijkl no yrs uvwxyzabcd fghij l n, name: ab defghi klmnopy stuvwxy abcde ghijklm op', 
    'my_id: ID5678, attribute: abcd fghijklmn py stuvwx zabc efg ijklmnopy st vwxy abcd f , name: abcd fghijklmn py stuvwx zabc efg ijklmnopy st vwxy abcd f ']

    # if output HTML file option at command line
    if output_file:
    
        # create HtmlDiff Object
        my_html = difflib.HtmlDiff()
        
        # make table with problematic data, delta1 is a string
        delta1 = my_html.make_table(
            file_2, file_2_new, "table1A" , "table1B"
        )     

        # replace nowrap tags and weird space characters
        delta1 = delta1.replace(" ", " ")
        delta1 = delta1.replace("nowrap=\"nowrap\"", "")

        # html header which goes at beggining of html file
        html_header = '''

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<body style="background-color:white;">
<head>
    <meta http-equiv="Content-Type"
          content="text/html; charset=utf-8" />
    <title></title>
    <style type="text/css">
        table.diff {font-family:Courier; color: black; border:medium;table-layout: auto; width: 96%; word-wrap: break-word; margin-left: auto; margin-right: auto;}
        td.diff_header {text-align:left}
        .diff_header {background-color:#e0e0e0}
        .diff_next {background-color:#c0c0c0}
        .diff_add {background-color:#aaffaa}
        .diff_chg {background-color:#ffff77}
        .diff_sub {background-color:#ffaaaa}
        .widthA{width:1%}
        .widthB{width:7%}
        .widthC{width:40%}
        .element_title{color: black; text-align:center}
        .element_diff{color: black; width: 96%; margin-left: auto; margin-right: auto}
    </style>
</head>

'''
        # html footer which goes at the end of html file
        html_footer = '''
        </body>

</html>
        '''
        
        # open output file path and write header, table and footer
        with open(output_file, "w") as f:
            f.write(html_header)
            f.write(delta1)
            f.write(html_footer)
    

def main():

    # parse command line option for output HTML file
    parser = argparse.ArgumentParser()
    parser.add_argument("--html", help="specify html to write to")
    args = parser.parse_args()


    if args.html:
        output_file = Path(args.html)
    else:
        output_file = None

    # call create_diff function which compares two lists of strings
    # and returns a formatted HTML table comparing them
    create_diff(output_file)


if __name__ == "__main__":
    main()

Output HTML file with problematic data:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<body style="background-color:white;">
<head>
    <meta http-equiv="Content-Type"
          content="text/html; charset=utf-8" />
    <title></title>
    <style type="text/css">
        table.diff {font-family:Courier; color: black; border:medium;table-layout: auto; width: 96%; word-wrap: break-word; margin-left: auto; margin-right: auto;}
        td.diff_header {text-align:left}
        .diff_header {background-color:#e0e0e0}
        .diff_next {background-color:#c0c0c0}
        .diff_add {background-color:#aaffaa}
        .diff_chg {background-color:#ffff77}
        .diff_sub {background-color:#ffaaaa}
        .widthA{width:1%}
        .widthB{width:7%}
        .widthC{width:40%}
        .element_title{color: black; text-align:center}
        .element_diff{color: black; width: 96%; margin-left: auto; margin-right: auto}
    </style>
</head>


    <table class="diff" id="difflib_chg_to0__top"
           cellspacing="0" cellpadding="0" rules="groups" >
        <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup>
        <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup>
        <thead><tr><th class="diff_next"><br /></th><th colspan="2" class="diff_header">table1A</th><th class="diff_next"><br /></th><th colspan="2" class="diff_header">table1B</th></tr></thead>
        <tbody>
            <tr><td class="diff_next" id="difflib_chg_to0__0"><a href="#difflib_chg_to0__top">t</a></td><td class="diff_header" id="from0_1">1</td><td ><span class="diff_sub">Ab</span></td><td class="diff_next"><a href="#difflib_chg_to0__top">t</a></td><td class="diff_header" id="to0_1">1</td><td ><span class="diff_add">Abcdefghijklmn</span></td></tr>
            <tr><td class="diff_next"></td><td class="diff_header" id="from0_2">2</td><td ><span class="diff_sub">my_id: ID1234, attribute: abcd fghijklmn py stuvwx zabc efg ijklmnopy st vwxy abcd f , name: abcd fghijklmn py stuvwx zabc efg ijklmnopy st vwxy abcd f </span></td><td class="diff_next"></td><td class="diff_header"></td><td ></td></tr>
            <tr><td class="diff_next"></td><td class="diff_header" id="from0_3">3</td><td >my_id: ID<span class="diff_chg">5678</span>, attribute: abcde ghijklm opyrst vwxyz bc efghij lmnopyr tuv xyzab defabcde ghijklmno yrstuvwxyza cdefghijklmnop rstuvw yz b defab defgh jklmnop rstuvwxyz bcd fghijkl nopyr tuvwxyza cdefabcd fghijkl no yrs uvwxyzabcd fghij l n, name: ab defghi klmnopy stuvwxy abcde ghijklm op</td><td class="diff_next"></td><td class="diff_header" id="to0_2">2</td><td >my_id: ID<span class="diff_chg">1234</span>, attribute: abcde ghijklm opyrst vwxyz bc efghij lmnopyr tuv xyzab defabcde ghijklmno yrstuvwxyza cdefghijklmnop rstuvw yz b defab defgh jklmnop rstuvwxyz bcd fghijkl nopyr tuvwxyza cdefabcd fghijkl no yrs uvwxyzabcd fghij l n, name: ab defghi klmnopy stuvwxy abcde ghijklm op</td></tr>
            <tr><td class="diff_next"></td><td class="diff_header"></td><td ></td><td class="diff_next"></td><td class="diff_header" id="to0_3">3</td><td ><span class="diff_add">my_id: ID5678, attribute: abcd fghijklmn py stuvwx zabc efg ijklmnopy st vwxy abcd f , name: abcd fghijklmn py stuvwx zabc efg ijklmnopy st vwxy abcd f </span></td></tr>
        </tbody>
    </table>
        </body>

</html>
        

The problematic line in the HTML is:

<tr><td class="diff_next"></td><td class="diff_header" id="from0_2">2</td><td ><span class="diff_sub">my_id: ID1234, attribute: abcd fghijklmn py stuvwx zabc efg ijklmnopy st vwxy abcd f , name: abcd fghijklmn py stuvwx zabc efg ijklmnopy st vwxy abcd f </span></td><td class="diff_next"></td><td class="diff_header"></td><td ></td></tr>

Where there is an extra <td ></td> at the end, adding an extra column.

This is what the table looks like with the unwanted column

This is what the table is supposed to look like when the columns are aligned correctly

I have tried changing the length of the string, checking for invisible characters, removing colons. There is something wrong with the strings that I am providing as input which throws off the make_table function. I have provided make_table with longer strings and the output was just fine. The behavior is very inconsistent.

Thanks for the help.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với việc tái hiện trong diff_tool.py và entry point difflib.HtmlDiff().make_table, sử dụng các danh sách file_2 và file_2_new được cung cấp. Kiểm tra cách các hàng và cột được tạo ra được lắp ráp, sau đó xác minh rằng đầu vào có vấn đề không còn tạo ra một ô trống thừa và thêm kiểm thử hồi quy cho trường hợp đã tái hiện.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
tooling
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
42/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.