Skip to content

Commit

Permalink
A bug in Python 3.8 importlib.metadata requires a workaround
Browse files Browse the repository at this point in the history
python/importlib_metadata#122 needs to be workedaround in 0.13

fixes pybuilder#807
  • Loading branch information
arcivanov committed Oct 2, 2021
1 parent e0029fb commit 6f609b2
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
67 changes: 67 additions & 0 deletions src/integrationtest/python/issue_807_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
#
# This file is part of PyBuilder
#
# Copyright 2011-2021 PyBuilder Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import textwrap
import unittest

from itest_support import IntegrationTestSupport


class Issue807Test(IntegrationTestSupport):
def test(self):
if sys.version_info[:2] < (3, 8):
# importlib.metadata does not exist before Python 3.8
return

self.write_build_file("""
from pybuilder.core import use_plugin, init
use_plugin("python.core")
use_plugin("python.unittest")
@init
def init (project):
project.set_property("verbose", True)
""")

self.create_directory("src/main/python")
self.create_directory("src/unittest/python")
self.write_file("src/main/python/code.py", textwrap.dedent(
"""
from importlib import metadata
def run_code():
return metadata.version("setuptools")
"""))
self.write_file("src/unittest/python/code_tests.py", textwrap.dedent(
"""
import unittest
import code
class CodeTests(unittest.TestCase):
def test_code(self):
code.run_code()
"""
))
reactor = self.prepare_reactor()
reactor.build("verify")


if __name__ == "__main__":
unittest.main()
9 changes: 8 additions & 1 deletion src/main/python/pybuilder/extern/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,17 @@ def load_module(self, fullname):
"distribution.".format(**locals())
)

def find_distributions(self, context):
def _find_distributions(self, context):
context.path.insert(0, pybuilder._vendor.__file__[:-len("__init__.py") - 1])
return []

# https://github.com/pybuilder/pybuilder/issues/807
if sys.version_info[:2] == (3, 8):
def find_distributions(self, context):
return iter(self._find_distributions(context))
else:
find_distributions = _find_distributions

def install(self):
"""
Install this importer into sys.meta_path if not already present.
Expand Down

0 comments on commit 6f609b2

Please sign in to comment.