Skip to content

Fix Python 3.10 AtributeError - #72

Open
ricardo-reis-1970 wants to merge 1 commit into
pyreadline:masterfrom
ricardo-reis-1970:master
Open

ricardo-reis-1970 wants to merge 1 commit into
pyreadline:masterfrom
ricardo-reis-1970:master

Conversation

@ricardo-reis-1970

Copy link
Copy Markdown

Fixed this:

C:\Windows\system32>python
Python 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Failed calling sys.__interactivehook__
Traceback (most recent call last):
  File "C:\Program Files\Python310\lib\site.py", line 446, in register_readline
    import readline
  File "C:\Program Files\Python310\lib\site-packages\readline.py", line 34, in <module>
    rl = Readline()
  File "C:\Program Files\Python310\lib\site-packages\pyreadline\rlmain.py", line 422, in __init__
    BaseReadline.__init__(self)
  File "C:\Program Files\Python310\lib\site-packages\pyreadline\rlmain.py", line 62, in __init__
    mode.init_editing_mode(None)
  File "C:\Program Files\Python310\lib\site-packages\pyreadline\modes\emacs.py", line 633, in init_editing_mode
    self._bind_key('space',       self.self_insert)
  File "C:\Program Files\Python310\lib\site-packages\pyreadline\modes\basemode.py", line 162, in _bind_key
    if not callable(func):
  File "C:\Program Files\Python310\lib\site-packages\pyreadline\py3k_compat.py", line 8, in callable
    return isinstance(x, collections.Callable)
AttributeError: module 'collections' has no attribute 'Callable'

Fixed this:
```python
C:\Windows\system32>python
Python 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Failed calling sys.__interactivehook__
Traceback (most recent call last):
  File "C:\Program Files\Python310\lib\site.py", line 446, in register_readline
    import readline
  File "C:\Program Files\Python310\lib\site-packages\readline.py", line 34, in <module>
    rl = Readline()
  File "C:\Program Files\Python310\lib\site-packages\pyreadline\rlmain.py", line 422, in __init__
    BaseReadline.__init__(self)
  File "C:\Program Files\Python310\lib\site-packages\pyreadline\rlmain.py", line 62, in __init__
    mode.init_editing_mode(None)
  File "C:\Program Files\Python310\lib\site-packages\pyreadline\modes\emacs.py", line 633, in init_editing_mode
    self._bind_key('space',       self.self_insert)
  File "C:\Program Files\Python310\lib\site-packages\pyreadline\modes\basemode.py", line 162, in _bind_key
    if not callable(func):
  File "C:\Program Files\Python310\lib\site-packages\pyreadline\py3k_compat.py", line 8, in callable
    return isinstance(x, collections.Callable)
AttributeError: module 'collections' has no attribute 'Callable'
```
@andy-maier

andy-maier commented Dec 12, 2021 •

Copy link
Copy Markdown

Fixing the issue that way excludes Python 2.7.

Better would be:

try:
    from collections.abc import Callable
except ImportError:
    from collections import Callable

and then to use Callable directly.

@ricardo-reis-1970

Copy link
Copy Markdown
Author

Dear Andy,

Python 2.7 first came out on 2010. Its EOL was announced for almost 2 years ago. If we're to carry all these corpses in the trunk, we might as well work for the Corleone family. I'm going to risk saying that this is no longer the same language.

Now, I have very little idea what amount of trouble this would involve, but could this not simply be a pyreadline version bump, featuring end of support for Python 2? This way, users still stuck with the Two (few but not zero, I know) could have their own stable version.

I could not emphasize this loud enough: if you are a command-line animal like me, this is one of the most useful libraries in the whole Pyverse. As I mentioned, this is working great for me. I just want it to work great for everybody.

@johnslavik

Copy link
Copy Markdown

Although, if we were about to carry all these corpses, then…
collections.abc is new since version 3.3, so in my humble opinion the best solution would be:

if sys.version_info[:2] >= (3, 3):
    from collections.abc import Callable
else:
    from collections import Callable
    
def callable(x):
    return isinstance(x, Callable)

@johnslavik

Copy link
Copy Markdown

But to be honest, I pretty much agree with @ricardo-reis-1970.

@ricardo-reis-1970

Copy link
Copy Markdown
Author

But to be honest, I pretty much agree with @ricardo-reis-1970.

You mean my code suggestion or my opinion on pyreadline? 😉

@brgirgis

brgirgis commented Jan 23, 2022 •

Copy link
Copy Markdown

Please send your PR here

@backjoob

backjoob commented Feb 8, 2022

Copy link
Copy Markdown

Although, if we were about to carry all these corpses, then… collections.abc is new since version 3.3, so in my humble opinion the best solution would be:

if sys.version_info[:2] >= (3, 3):
    from collections.abc import Callable
else:
    from collections import Callable
    
def callable(x):
    return isinstance(x, Callable)

Bumping -- I like this solution.

@backjoob

backjoob commented Feb 8, 2022 •

Copy link
Copy Markdown

Fixing the issue that way excludes Python 2.7.

Better would be:

try:
    from collections.abc import Callable
except ImportError:
    from collections import Callable

and then to use Callable directly.

This codepath is not executed for Python 2, only Python 3:

...
if sys.version_info[0] >= 3:
    import collections
    PY3 = True
    def callable(x):
        return isinstance(x, collections.Callable)  # The offnding line

    def execfile(fname, glob, loc=None):
        loc = loc if (loc is not None) else glob
        with open(fname) as fil:
            txt = fil.read()
        exec(compile(txt, fname, 'exec'), glob, loc)
    unicode = str
    bytes = bytes
    from io import StringIO
else:
    PY3 = False
    ...

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

Successfully merging this pull request may close these issues.

5 participants