[pygobject] Raise ImportError when importing modules not found in repository
- From: Simon Feltman <sfeltman src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject] Raise ImportError when importing modules not found in repository
- Date: Tue, 25 Mar 2014 01:27:22 +0000 (UTC)
commit ac8b59ee335967efef974ab0aa89128ade9f3d0c
Author: Simon Feltman <sfeltman src gnome org>
Date: Mon Mar 24 18:09:10 2014 -0700
Raise ImportError when importing modules not found in repository
Raise an ImportError with extra information noting the typelib was not
found. This removes the need to log a similar message which adds output
noise when attempting controlled imports within try/except statements.
In Python 2, the additional information is lost but in Python 3 it shows up.
https://bugzilla.gnome.org/show_bug.cgi?id=726877
gi/importer.py | 9 ++++-----
tests/test_import_machinery.py | 18 ++++++++++++++++++
2 files changed, 22 insertions(+), 5 deletions(-)
---
diff --git a/gi/importer.py b/gi/importer.py
index aa95cf6..cea0f8b 100644
--- a/gi/importer.py
+++ b/gi/importer.py
@@ -21,7 +21,6 @@
# USA
from __future__ import absolute_import
-import logging
import sys
from ._gi import Repository
@@ -47,11 +46,11 @@ class DynamicImporter(object):
if path != self.path:
return
- if not repository.enumerate_versions(namespace):
- logging.error('Could not find any typelib for %s', namespace)
- return None
- else:
+ if repository.enumerate_versions(namespace):
return self
+ else:
+ raise ImportError('cannot import name %s, '
+ 'introspection typelib not found' % namespace)
def load_module(self, fullname):
if fullname in sys.modules:
diff --git a/tests/test_import_machinery.py b/tests/test_import_machinery.py
index e1af1f1..42b4bf8 100644
--- a/tests/test_import_machinery.py
+++ b/tests/test_import_machinery.py
@@ -1,6 +1,7 @@
# -*- Mode: Python; py-indent-offset: 4 -*-
# vim: tabstop=4 shiftwidth=4 expandtab
+import sys
import unittest
import gi.overrides
@@ -56,3 +57,20 @@ class TestModule(unittest.TestCase):
# Restore the previous cache
gi.module._introspection_modules = old_modules
+
+
+class TestImporter(unittest.TestCase):
+ def test_invalid_repository_module_name(self):
+ with self.assertRaises(ImportError) as context:
+ from gi.repository import InvalidGObjectRepositoryModuleName
+ InvalidGObjectRepositoryModuleName # pyflakes
+
+ exception_string = str(context.exception)
+
+ self.assertTrue('InvalidGObjectRepositoryModuleName' in exception_string)
+
+ # The message of the custom exception in gi/importer.py is eaten in Python 2.7
+ if sys.version_info.major < 3:
+ self.assertTrue('introspection typelib' not in exception_string)
+ else:
+ self.assertTrue('introspection typelib' in exception_string)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]