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

import utils problem #14

Open
imrrobat opened this issue Jul 24, 2022 · 2 comments
Open

import utils problem #14

imrrobat opened this issue Jul 24, 2022 · 2 comments

Comments

@imrrobat
Copy link

imrrobat commented Jul 24, 2022

when i want to import: from rubik_solver import utils

this error happened:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in
from rubik_solver import utils
File "C:\Users---\AppData\Local\Programs\Python\Python310\lib\site-packages\rubik_solver\utils.py", line 4, in
from past.builtins import basestring
File "C:\Users---\AppData\Local\Programs\Python\Python310\lib\site-packages\past\builtins_init_.py", line 43, in
from past.builtins.noniterators import (filter, map, range, reduce, zip)
File "C:\Users---\AppData\Local\Programs\Python\Python310\lib\site-packages\past\builtins\noniterators.py", line 24, in
from past.types import basestring
File "C:\Users---\AppData\Local\Programs\Python\Python310\lib\site-packages\past\types_init_.py", line 25, in
from .oldstr import oldstr
File "C:\Users---\AppData\Local\Programs\Python\Python310\lib\site-packages\past\types\oldstr.py", line 5, in
from collections import Iterable
ImportError: cannot import name 'Iterable' from 'collections' (C:\Users---\AppData\Local\Programs\Python\Python310\lib\collections_init_.py)

@andrebhu
Copy link

andrebhu commented Nov 8, 2022

@James-E-A
Copy link

James-E-A commented May 4, 2023

Unfortunately, the version of future that this repo has pinned was affected by one of the breaking changes PSF pushed out with Python 3.9.

Without having to refactor any packaging or risk breaking anything else, here's a quick fix to make everything immediately "just work" if you're trying to run this on your system

import os, sys, re, pathlib
from tempfile import NamedTemporaryFile

def fix_pythonrubik_issue14(past):
    assert sys.version_info < (4, )
    p, = past.__path__
    r = re.compile(r'^((?:from collections import )([\w, ]+))$', re.M)
    for file in _recurse_files(pathlib.Path(p)):
        body = file.read_text(errors='surrogateescape')
        m = r.search(body)
        if m:
            newbody = r.sub(
                lambda m: 'try:\n    %s\nexcept ImportError:\n    from collections.abc import %s' % m.groups(),
                body)
            _safe_overwrite(newbody, file)

def _recurse_files(d):
    for dirpath, dirnames, filenames in os.walk(d):
        for filename in filenames:
            yield pathlib.Path(os.path.join(dirpath, filename))

def _safe_overwrite(src_content, dest):
    # Reduce risk of corrupting system libraries
    mode = 'w+b' if isinstance(src_content, bytes) else 'w+t'
    with NamedTemporaryFile(mode, dir=dest.parent, delete=False) as f:
        f_path = pathlib.Path(f.name)
        f.write(src_content)
        f.flush()
    f_path.replace(dest)  # This is as atomic as it gets, I think

if __name__ == '__main__':
    import time
    try:
        import past.builtins
        print("No need to patch")
    except ImportError:
        print("There was a problem importing!")
        print("Going to try to patch this problem.")
        print("PRESS CTRL_C WITHIN TEN SECONDS IF YOU DON'T WANT TO ATTEMPT.")
        time.sleep(10)
        print("Patching...")
        import past
        fix_pythonrubik_issue14(past)
        import past.builtins
    print(repr(past.builtins), "OK")

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

3 participants