Skip to content

test summary.py fails on python 3.8 with: AttributeError: module 'cgi' has no attribute 'escape' #20032

@diablodale

Description

@diablodale

modules/ts/misc/summary.py fails in Python 3.8 due to cgi.escape was removed in Python 3.8.

Traceback (most recent call last):
  File "../modules/ts/misc/summary.py", line 289, in <module>
    htmlPrintHeader(sys.stdout, "Summary report for %s tests from %s test logs" % (len(test_cases), setsCount))
  File "/mnt/c/repos-nobackup/opencv/modules/ts/misc/table_formatter.py", line 470, in htmlPrintHeader
    titletag = "<title>%s</title>\n" % htmlEncode([str(title)])
  File "/mnt/c/repos-nobackup/opencv/modules/ts/misc/table_formatter.py", line 26, in htmlEncode
    return '<br/>'.join([cgi.escape(s) for s in str])
  File "/mnt/c/repos-nobackup/opencv/modules/ts/misc/table_formatter.py", line 26, in <listcomp>
    return '<br/>'.join([cgi.escape(s) for s in str])
AttributeError: module 'cgi' has no attribute 'escape'

As reported at https://bugzilla.redhat.com/show_bug.cgi?id=1827745

cgi.escape which was deprecated in Python 3.7 and removed in 3.8. A possible alternative is html.escape.

Setup

  • Ubuntu 20.04.2 LTS
  • Python 3.8.5 fails when run
  • OpenCV 4.5.2

Repro

  1. Create multiple performance test output XML files with run.py
  2. Create summary report that combines that multiple output, e.g. python3 modules/ts/misc/summary.py -o html *.xml > report.html

Result

Traceback (most recent call last):
  File "../modules/ts/misc/summary.py", line 289, in <module>
    htmlPrintHeader(sys.stdout, "Summary report for %s tests from %s test logs" % (len(test_cases), setsCount))
  File "/mnt/c/repos-nobackup/opencv/modules/ts/misc/table_formatter.py", line 470, in htmlPrintHeader
    titletag = "<title>%s</title>\n" % htmlEncode([str(title)])
  File "/mnt/c/repos-nobackup/opencv/modules/ts/misc/table_formatter.py", line 26, in htmlEncode
    return '<br/>'.join([cgi.escape(s) for s in str])
  File "/mnt/c/repos-nobackup/opencv/modules/ts/misc/table_formatter.py", line 26, in <listcomp>
    return '<br/>'.join([cgi.escape(s) for s in str])
AttributeError: module 'cgi' has no attribute 'escape'

Expected

No errors and a summary html output.

Workaround

This issue is widely reported on the internet https://www.google.com/search?q=AttributeError%3A+module+%27cgi%27+has+no+attribute+%27escape%27
Most everyone has the same approach, to replace cgi.escape with html.escape. Naturally, you will need to consider any compatibility issues with prior versions including if 2.x is desired to be supported.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions