Skip to content

importhook.cs breaks python's import interface #1245

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

Closed
SStarosielec opened this issue Oct 5, 2020 · 2 comments
Closed

importhook.cs breaks python's import interface #1245

SStarosielec opened this issue Oct 5, 2020 · 2 comments

Comments

@SStarosielec
Copy link

SStarosielec commented Oct 5, 2020

Environment

  • Pythonnet version: 2.5.1
  • Python version: 3.7.9
  • Operating System: Windows 10

Details

  • Describe what you were trying to get done.

pythonnet overwrites the python's __import__ routine, but does not implement python's signature precisely:
original python's signature is

__import__(name, globals=None, locals=None, fromlist=(), level=0)

Most other python modules call the name parameter as positional: __import__("foo", ...

Some packages (e.g. kivy) use __import__ as

    __import__(
        name='{2}.{0}.{1}'.format(basemodule, modulename, base),
        globals=globals(), locals=locals(), fromlist=[modulename], level=0
    )

i.e. the name argument used as keyword argument in the call. Both calls are valid in python's __import__, but fail at the overwritten __import__ with

TypeError: __import__() takes at least 1 argument (0 given)

A hotfix is to alter the other modules code to

    __import__(
        '{2}.{0}.{1}'.format(basemodule, modulename, base),
        globals=globals(), locals=locals(), fromlist=[modulename], level=0
    )

but the broken code is within pythonnet.

@SStarosielec
Copy link
Author

SStarosielec commented Oct 5, 2020

I'm not sure this section from https://docs.python.org/3/reference/expressions.html#calls applies (I don't understand all the lingo):

CPython implementation detail: An implementation may provide built-in functions whose positional parameters do not have names, even if they are ‘named’ for the purpose of documentation, and which therefore cannot be supplied by keyword. In CPython, this is the case for functions implemented in C that use PyArg_ParseTuple() to parse their arguments.

It then seems an incompatibiliy between python anc CPython, should CPython be fixed instead ?

@filmor
Copy link
Member

filmor commented Oct 5, 2020

This should be relatively easy to fix and PRs are welcome, thank you for looking into this in detail (was reported before in #547).

The error here is partially on Kivy for calling __import__ directly instead of using importlib, and as you quoted we are kind-of allowed to make __import__ behave in this way.

Mid-term (hopefully in pythonnet 3), this should not be a problem anymore by implementing proper PEP302-style importing instead of overriding __import__.

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