FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-106498: Fix an extreme case in `colorsys.rgb_to_hls` by gaogaotiantian · Pull Request #106530 · python/cpython · GitHub

Repository navigation

gh-106498: Fix an extreme case in colorsys.rgb_to_hls - #106530

Closed
gaogaotiantian wants to merge 2 commits into
python:mainfrom
gaogaotiantian:fix-colorsys-corner-case
Closed

gaogaotiantian wants to merge 2 commits into
python:mainfrom
gaogaotiantian:fix-colorsys-corner-case

Conversation

gaogaotiantian commented Jul 7, 2023 •
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Member

colorsys.rgb_to_hls(1, 1, 0.9999999999999999) raises ZeroDivisionError now (this probably is the ONLY case). Not sure if it's worth fixing, but I think "correct" in all cases is more important than "very slightly faster in theory".

This optimization is originally introduced in #23306, using the benchmark given in the PR, there's no observable performance regression with the extra check.

Benchmark Code
# /usr/bin/env python

import timeit
import statistics
from Lib.test.test_colorsys import ColorsysTest


if __name__ == '__main__':
    number = 100
    repeat = 100
    results = timeit.repeat('ColorsysTest().test_hls_roundtrip()',
                    setup='from Lib.test.test_colorsys import ColorsysTest',
                    number=number,
                    repeat=repeat)

    running_times = list(sorted(results))

    mean = statistics.mean(running_times)
    std = statistics.stdev(running_times)
    print(f"Running time: {mean} ± {std} ms.  "
    f"(number, repeat) = ({number}, {repeat})")

Another possible implementation is to do a try-except block for the division. As try is almost no cost now, it would be theoretically faster than the current one. The return value would be different (a near white color can be represented in many forms in hls space). I prefer the current solution as it's easy to understand with the comment, but I do not oppose the other way.

Copy link
Copy Markdown
Member Author

@terryjreedy as the reviewer of the related PR, could you share thoughts on this matter? Thanks!

nedveder left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

minc == maxc and sumc == 2.0 are checking for the same thing - preventing division by zero. Another possible fix would be to check if l==1 on line 91.

Copy link
Copy Markdown
Member

@gaogaotiantian Thank you for finding the change that caused the regression (which I merged, not just reviewed). As explained on this issue, the fix is incorrect. I am preparing a new PR with a different test addition.

gaogaotiantian deleted the fix-colorsys-corner-case branch June 5, 2024 17:23
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL