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

Implement Python 3.12 compatible loader. Fixes #130. #131

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Install dependencies (macOS)
if: matrix.os == 'macos-latest'
run: |
brew install qt@5 python@3.8
brew install qt@5 python@3.12
echo PATH=/usr/local/opt/qt@5/bin:$PATH >> ${GITHUB_ENV}
- run: qmake
- run: make
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Requirements
------------

* Qt 5.1.0 or newer (Qt 6.x also supported)
* Python 3.3.0 or newer
* Python 3.8.0 or newer


Building
Expand All @@ -28,7 +28,7 @@ make install
To build against a specific Python version, use:

```
qmake PYTHON_CONFIG=python3.3-config # use "qmake6" for Qt 6
qmake PYTHON_CONFIG=python3.8-config # use "qmake6" for Qt 6
make
make install
```
Expand Down
6 changes: 4 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ Building PyOtherSide
The following build requirements have to be satisfied to build PyOtherSide:

* Qt 5.1.0 or newer (Qt 6.x also supported)
* Python 3.3.0 or newer
* Python 3.8.0 or newer

If you have the required build-dependencies installed, building and installing
the PyOtherSide plugin should be as simple as:
Expand All @@ -1185,7 +1185,7 @@ pass a suitable ``python-config`` to ``qmake`` at configure time:

.. code-block:: sh

qmake PYTHON_CONFIG=python3.3-config # For Qt 6, use "qmake6"
qmake PYTHON_CONFIG=python3.8-config # For Qt 6, use "qmake6"
make
make install

Expand All @@ -1199,6 +1199,8 @@ ChangeLog
Version UNRELEASED (YYYY-MM-DD)
-------------------------------

* Dropped support for Python < 3.8 (PR#131)
* Added support for Python 3.12 (PR#131)
* Support for Qt 6.5 and newer (backwards-incompatible ``Q_RETURN_ARG()`` change) (fixes #128)

Version 1.6.0 (2022-08-05)
Expand Down
35 changes: 23 additions & 12 deletions src/qrc_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,32 @@
#

import sys
from importlib import abc
from importlib.util import spec_from_loader

import pyotherside

from importlib import abc

class PyOtherSideQtRCImporter(abc.MetaPathFinder, abc.SourceLoader):
def find_module(self, fullname, path):
if path is None or all(x.startswith('qrc:') for x in path):
if self.get_filename(fullname):
return self
class PyOtherSideQtRCLoader(abc.Loader):
def __init__(self, filepath):
self.filepath = filepath

def create_module(self, spec):
return None

def exec_module(self, module):
Copy link
Contributor Author

@apollo13 apollo13 Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know Python's import System well enough. Maybe https://docs.python.org/3.11/library/importlib.html#importlib.abc.SourceLoader could be used instead of having to manually exec stuff?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have managed to rewrite it back to the SourceLoader, let me know what you think.

data = pyotherside.qrc_get_file_contents(self.filepath[len('qrc:') :])
code = compile(data, self.filepath, 'exec')
exec(code, module.__dict__)


class PyOtherSideQtRCImporter(abc.MetaPathFinder):
def find_spec(self, fullname, path, target=None):
if path is None:
fname = self.get_filename(fullname)
if fname:
return spec_from_loader(fullname, PyOtherSideQtRCLoader(fname))
return None

def get_filename(self, fullname):
basename = fullname.replace('.', '/')
Expand All @@ -38,10 +55,4 @@ def get_filename(self, fullname):
if pyotherside.qrc_is_file(filename[len('qrc:'):]):
return filename

def get_data(self, path):
return pyotherside.qrc_get_file_contents(path[len('qrc:'):])

def module_repr(self, m):
return "<module '{}' from '{}'>".format(m.__name__, m.__file__)

sys.meta_path.append(PyOtherSideQtRCImporter())