BUG: Use isinstance for is_iterator on Cygwin
State: closed

Fixes #45158 

Keep original implementation if not on Cygwin, and choose the implementation at compile time to avoid performance hits from the extra branch.

[Test results on Cygwin are available are available here](https://github.com/DWesl/pandas/runs/5648361139?check_suite_focus=true)

- [X] closes #45158
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [X] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.


Labels:
name: Stale

Comments:
As noted in #45158, `pandas._libs.lib.is_iterator` doesn't agree with `list`, `next`, or `collections.abs.Iterator` on Cygwin.  
```pycon
>>> import pandas as pd
>>> stamp = pd.Timestamp("2010-01-03T07:23:48")
>>> pd._libs.lib.is_iterator(stamp)
True
>>> list(stamp)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'Timestamp' object is not iterable
>>> pd.__version__
'1.3.4'
>>> import collections.abc
>>> isinstance(stamp, collections.abc.Iterator)
False
>>> next(stamp)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'Timestamp' object is not an iterator
```
Since there are multiple places in the code-base that apply `list` to iterators (several of them in the indexing code), this causes rather a lot of crashes in code that works on other platforms.  I would like those to stop crashing, so I can use recent pandas with `DatetimeIndex`es on my laptop.

`pandas._libs.lib.is_instance` relies on [`PyIter_Check`](https://docs.python.org/3/c-api/iter.html#c.PyIter_Check), so [this is likely a bug](https://github.com/pandas-dev/pandas/issues/45158#issuecomment-1043177279) in [python/cpython](https://github.com/python/cpython); however, I don't know how to test changes to CPython, but I can make a workaround for pandas and show that it works.  To see what's broken in the current version of pandas, you can check the [log outputs here](https://github.com/DWesl/pandas/runs/5186489669?check_suite_focus=true#step:11:3009) [as noted in the discussion on #45158](https://github.com/pandas-dev/pandas/issues/45158#issuecomment-1039320440).  I can't figure out the bug-report process on [the CPython issue tracker](https://bugs.python.org/), and don't really feel like creating an account and learning the interface for [a website that won't be active in two days](https://discuss.python.org/t/github-issues-migration-is-coming-soon/13791) when I can just wait two weeks and report it [on their GitHub](https://github.com/python/cpython/issues/new).

As for *how* this works, the 
```cython
IF ...:
    ...
ELSE:
    ...
```
construct [here](https://github.com/pandas-dev/pandas/pull/46477/files) is a [Cython compile-time conditional](https://cython.readthedocs.io/en/latest/src/userguide/language_basics.html#conditional-statements) that "works in a similar way to the `#if` preprocessor directive in C".  The way I used it here means that, on Cygwin, this code will use [`collections.abc.Iterable`'s definition of an iterable](https://docs.python.org/3/library/collections.abc.html#collections.abc.Iterable), which agrees with that of `list` on Cygwin (see the first code block in this comment, above), which is most of what pandas uses it for.  On other platforms,  [the `PyIter_Check` definition](https://github.com/python/cpython/blob/ab9301a28fa431d7a32163126fc96de3b2ce6107/Objects/abstract.c#L2827-L2833) will remain, since it agrees with `list` there and presumably there's some speed benefit to a C macro call over a few Python calls (at least, I suspect this is why this function was added).  That is to say, on platforms other than Cygwin (Windows, MSYS, WSL, OSX, Linux, BSD, ...) this should do nothing (besides add a little bit of compile time; the CI tests should check if the behavior changed), while on Cygwin it will change to an implementation that agrees with the functions assumed to agree with it (see the first code block in this comment, copied from #45158, and [note that the error in the test logs from a test run without this patch](https://github.com/DWesl/pandas/runs/5186489669?check_suite_focus=true#step:11:3009) does not occur in [a test run with this patch](https://github.com/DWesl/pandas/runs/5648361139?check_suite_focus=true)).
@DWesl i get your explanation. We don't have *any* compile time defs like this for cygwin, nor do we test for it at all. 

This really should be fixed upstream. 
Alternately, you could just declare that Cygwin is an unsupported platform and close as WONTFIX, then perhaps suggest that anyone who needs pandas on Cygwin should use a version from before #31294 went in (probably `pandas<1.0.0`) which just happened to work, or fix the underlying CPython bug on Cygwin.

EDIT: you replied before I finished.  I'll report it to CPython once the issue tracker migration is complete.
This pull request is stale because it has been open for thirty days with no activity. Please [update](https://pandas.pydata.org/pandas-docs/stable/development/contributing.html#updating-your-pull-request) and respond to this comment if you're still interested in working on this.
Thanks for the PR, but agreed that this should probably be fixed upstream. Since this is unlikely to move forward. Closing