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

Python 2.6 Support #235

Closed
Ahuge opened this issue Aug 18, 2017 · 3 comments
Closed

Python 2.6 Support #235

Ahuge opened this issue Aug 18, 2017 · 3 comments

Comments

@Ahuge
Copy link
Collaborator

Ahuge commented Aug 18, 2017

Hey all

I am slightly ignorant as to the benefits of importlib, looking at it's source it looks fairly basic, and much of it's complexity seems more than we need.

Python2.6 does not appear to have importlib and thus Qt.py cannot be used in anything below 2.7

Does anyone have any problem with me creating an implementation of how we are using importlib but only using import instead?

It would be something like this:

def _import_sub_module(module, name):
    module = __import__(module.__name__ + "." +  name)
    for level in name.split("."):
        module = getattr(module, level)
    return module

With a little bit of sanity checking in there.

Our studio is planning on updating to python2.7 soon, but that has been the plan for a while, so if you don't think you want to add this complexity to support 2.6 I understand.

Thanks

@fredrikaverpil
Copy link
Collaborator

fredrikaverpil commented Aug 19, 2017

@Ahuge as long as it doesn't hamper future development and cause Python 3 issues (which I don't think it'll do), I don't see a problem with this.

To me it looks like your code should work exactly as-is, so you could just set up a PR and we'll take it from there.

@mottosso
Copy link
Owner

As it happens, importlib is not a native citizen of Python 2.7 either, but is actually (partially) backported from Python 3.

Here is the entire definition of importlib in Python 2.7.

python27/lib/importlib/__init__.py

"""Backport of importlib.import_module from 3.x."""
# While not critical (and in no way guaranteed!), it would be nice to keep this
# code compatible with Python 2.3.
import sys

def _resolve_name(name, package, level):
    """Return the absolute name of the module to be imported."""
    if not hasattr(package, 'rindex'):
        raise ValueError("'package' not set to a string")
    dot = len(package)
    for x in xrange(level, 1, -1):
        try:
            dot = package.rindex('.', 0, dot)
        except ValueError:
            raise ValueError("attempted relative import beyond top-level "
                              "package")
    return "%s.%s" % (package[:dot], name)


def import_module(name, package=None):
    """Import a module.

    The 'package' argument is required when performing a relative import. It
    specifies the package to use as the anchor point from which to resolve the
    relative import to an absolute import.

    """
    if name.startswith('.'):
        if not package:
            raise TypeError("relative imports require the 'package' argument")
        level = 0
        for character in name:
            if character != '.':
                break
            level += 1
        name = _resolve_name(name[level:], package, level)
    __import__(name)
    return sys.modules[name]

Incidentally, it even mentioned compatibility with Python 2.3, so embedding this as-is into Qt.py and thus supporting Python 2.6 (and below?) shouldn't be a problem!

Ahuge pushed a commit to Ahuge/Qt.py that referenced this issue Aug 29, 2017
@hugovk
Copy link

hugovk commented Oct 15, 2017

#245 has been merged, I guess this can be closed now (although I'd recommend testing it on Travis).

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

4 participants