Skip to content
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

OSError On Clicking "Special Plots" in Demo (Windows) #7

Open
ADevAJ opened this issue Oct 26, 2023 · 6 comments
Open

OSError On Clicking "Special Plots" in Demo (Windows) #7

ADevAJ opened this issue Oct 26, 2023 · 6 comments

Comments

@ADevAJ
Copy link

ADevAJ commented Oct 26, 2023

I've installed textual-plotext
Ran demo with
python -m textual_plotext in command prompt

When I click "Special Plots" the demo crashes with:
OSError [Errno 22] Invalid Argument
As Attached
the last error is in:
...site-packages\plotext_date.py:56 in time_to_string
│ return self.datetime_to_string(dt.fromtimestamp(time + self.time0), output_form)

full screenshots of stack trace below
1

Running
plotext 5.2.8
Textual 0.40.0
Python 3.11.3
Windows10 Pro build 19045
error appears in both Command Prompt and Windows PowerShell

2
3

@davep
Copy link
Contributor

davep commented Oct 26, 2023

It's not clear to me which plot this might be. However, the demos in this library are simply the Plotext examples but placed inside a Textual application. My suspicion would be that it's the event plot that is causing the problem (this is a guess based on the error seemingly being related to datetime issues). As such, could you do me a favour and follow Plotext's example for the event plot and let me know what happens?

Here's what I get:

Screenshot 2023-10-26 at 08 35 29

@ADevAJ
Copy link
Author

ADevAJ commented Oct 26, 2023

error_event

It is producing the same error when I run the Event Plot example.

@davep
Copy link
Contributor

davep commented Oct 26, 2023

Right, as such, it would seem to be a bug either in Plotext itself, or at least their demo code. It might be a good idea to raise an issue there; I'll have a dive in and see if I can see the issue when I get some time, and I'll keep the issue open for now too -- perhaps when you raise the issue with them link to this.

@davep
Copy link
Contributor

davep commented Oct 26, 2023

As an aside, from within the Python library code (as in the core libraries of Python itself), I see this:

            # On Windows localtime_s throws an OSError for negative values,
            # thus we can't perform fold detection for values of time less
            # than the max time fold. See comments in _datetimemodule's
            # version of this method for more details.
            if t < max_fold_seconds and sys.platform.startswith("win"):
                return result

that mention of OSError there has me wondering if this is a Python on Windows and/or timezone issue.

@ADevAJ
Copy link
Author

ADevAJ commented Nov 3, 2023

Indeed , there is some issue with time0 and Windows... in _date.py line 56...
I wrapped the line in a try / except block and changed from:
return self.datetime_to_string(dt.fromtimestamp(time + self.time0), output_form)
to:
return self.datetime_to_string(dt.fromtimestamp(time), output_form)
and the page rendered OK, except the EventPlot rendered as attached (dots), not sure if that is correct? it looks more like Lines than dot in the example markdown.

ex_event_plot2

after inspect of the variables:
self.time0 = -2208988800.0

time = range of values 540 - 86220.0 (seconds/day?)

sum = -2208903600.0 ~ self.time0

calling "dt.fromtimestamp(time + self.time0)" = <- produces the error , irrespective of passing its value to any other function
and that is a built in Python function.... in Python311/Lib/datetime.py, line 1787

@classmethod
def fromtimestamp(cls, t, tz=None):
    """Construct a datetime from a POSIX timestamp (like time.time()).
    A timezone info object may be passed in as well.
    """
    _check_tzinfo_arg(tz)

    return cls._fromtimestamp(t, tz is not None, tz)

SO I guess the real question is... why does passing in - -2208903600.0 produce an error in the datetime module of Python ?

Perhaps there is a clue here....

@classmethod
def _fromtimestamp(cls, t, utc, tz):
    """Construct a datetime from a POSIX timestamp (like time.time()).

    A timezone info object may be passed in as well.
    """
    frac, t = _math.modf(t)
    us = round(frac * 1e6)
    if us >= 1000000:
        t += 1
        us -= 1000000
    elif us < 0:
        t -= 1
        us += 1000000

    converter = _time.gmtime if utc else _time.localtime
    y, m, d, hh, mm, ss, weekday, jday, dst = converter(t)
    ss = min(ss, 59)    # clamp out leap seconds if the platform has them
    result = cls(y, m, d, hh, mm, ss, us, tz)
    if tz is None and not utc:
        # As of version 2015f max fold in IANA database is
        # 23 hours at 1969-09-30 13:00:00 in Kwajalein.
        # Let's probe 24 hours in the past to detect a transition:
        max_fold_seconds = 24 * 3600

        # On Windows localtime_s throws an OSError for negative values,
        # thus we can't perform fold detection for values of time less
        # than the max time fold. See comments in _datetimemodule's
        # version of this method for more details.
        if t < max_fold_seconds and sys.platform.startswith("win"):
            return result

        y, m, d, hh, mm, ss = converter(t - max_fold_seconds)[:6]
        probe1 = cls(y, m, d, hh, mm, ss, us, tz)
        trans = result - probe1 - timedelta(0, max_fold_seconds)
        if trans.days < 0:
            y, m, d, hh, mm, ss = converter(t + trans // timedelta(0, 1))[:6]
            probe2 = cls(y, m, d, hh, mm, ss, us, tz)
            if probe2 == result:
                result._fold = 1
    elif tz is not None:
        result = tz.fromutc(result)
    return result

I don't know anything about time folds or posix time at all, but I do see that a very big negative number is being passed into a function with a comment that it will OSError on negative numbers in Windows...

So editing line 56 of _date.py , with abs() to ensure a positive number:
return self.datetime_to_string(dt.fromtimestamp(abs(time + self.time0)), output_form)

Renders the page without error (same as the try/except, still dots instead of lines for me, but maybe this is my terminal.)

@davep
Copy link
Contributor

davep commented Nov 6, 2023

Thanks for diving into this further. I think, given the problem seems to be somewhere between Plotext itself and Python on Windows, the easiest thing for me to do for the demo is to just omit that particular plot from the demo when running on Windows.

davep added a commit that referenced this issue Nov 6, 2023
See #7 and piccolomo/plotext#189 -- this seems to
be an issue somewhere between Plotext itself and Python on Windows; while we
could dive into this and try and solve it, for the purposes of a quick demo
it makes more sense to remove the problematic plot for now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants