Skip to content

Commit e33ecf1

Browse files
committed
tests: Adapt to pygobject 3.55.0
After upgrading pygobject from 3.54.5 to 3.55.2, Python tests expecting a process failure on setting an immutable (G_PARAM_CONSTRUCT_ONLY) property started to fail like this: Traceback (most recent call last): File "/home/test/libmodulemd-devel/redhat-linux-build/../modulemd/tests/ModulemdTests/defaults.py", line 114, in test_module_name self.assertProcessFailure(_set_module_name_to_none, defs) ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/test/libmodulemd-devel/modulemd/tests/ModulemdTests/base.py", line 60, in assertProcessFailure callable(*args) ~~~~~~~~^^^^^^^ File "/home/test/libmodulemd-devel/redhat-linux-build/../modulemd/tests/ModulemdTests/defaults.py", line 43, in _set_module_name_to_none defs.props.module_name = None ^^^^^^^^^^^^^^^^^^^^^^ TypeError: property 'module-name' can only be set in constructor The cause was that pygobject-3.55.0 started to raise a Python TypeError exception instead of calling Glib functions which would fail on its own depending on Glib warning fatality and Glib version. An example: cat /tmp/test.py #!/usr/bin/python3 import gi gi.require_version("Modulemd", "2.0") from gi.repository import Modulemd object = Modulemd.Defaults.new(Modulemd.DefaultsVersionEnum.LATEST, "foo") object.props.module_name = "bar" Before: $ /tmp/test.py /tmp/test.py:8: Warning: g_object_set_is_valid_property: construct property "module-name" for object 'ModulemdDefaultsV1' can't be set after construction object.props.module_name = "bar" After: $ /tmp/test.py Traceback (most recent call last): File "/tmp/test.py", line 8, in <module> object.props.module_name = "bar" ^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: property 'module-name' can only be set in constructor That was an intentional change in pygobject 3b6e4804de4f26cfb9472666f18f44ac731d874c commit (gi: Factor out pygi_set_property_gvalue_from_property_info). Probably an optimization. This patch adjusts the tests to pass if TypeError exception is raised regardless of Glib warning fatality. Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2440570
1 parent f3039d8 commit e33ecf1

5 files changed

Lines changed: 38 additions & 4 deletions

File tree

modulemd/tests/ModulemdTests/base.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,36 @@ def assertProcessFailure(self, callable, *args):
6767
if os.WIFSIGNALED(status):
6868
raise AssertionError("Child process was unexpectedly aborted")
6969

70+
def assertTypeExceptionOrProcessFailure(self, callable, *args):
71+
"""Calls the callable in a subprocess and checks that the process
72+
raised a TypeError exception, or was killed depending on Glib warning
73+
fatality.
74+
75+
Since pygobject-3.55.0 setting a G_PARAM_CONSTRUCT_ONLY property
76+
raises a Python exception. Old pygobject continues down to Glib
77+
which kills the process if Glib warnings a fatal, otherwise Glib
78+
warning is printed and the code continues.
79+
"""
80+
pid = os.fork()
81+
if pid == 0:
82+
try:
83+
callable(*args)
84+
except TypeError:
85+
os._exit(1)
86+
os._exit(0)
87+
_, status = os.waitpid(pid, 0)
88+
if os.WIFEXITED(status) and os.WEXITSTATUS(status) == 1:
89+
return
90+
if self.warnings_fatal:
91+
if not os.WIFSIGNALED(status):
92+
raise AssertionError(
93+
"Child process did not raise TypeError "
94+
"exception or was not aborted"
95+
)
96+
else:
97+
if os.WIFSIGNALED(status):
98+
raise AssertionError("Child process was unexpectedly aborted")
99+
70100
@property
71101
def warnings_fatal(self):
72102
gdebug = os.getenv("G_DEBUG", "").split(",")

modulemd/tests/ModulemdTests/defaults.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ def test_module_name(self):
111111
assert defs.get_module_name() == "foo"
112112

113113
# Ensure we cannot set the module_name
114-
self.assertProcessFailure(_set_module_name_to_none, defs)
114+
self.assertTypeExceptionOrProcessFailure(
115+
_set_module_name_to_none, defs
116+
)
115117

116118
def test_modified(self):
117119
defs = Modulemd.Defaults.new(

modulemd/tests/ModulemdTests/profile.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ def test_get_name(self):
9494
assert p.get_name() == "testprofile"
9595
assert p.props.name == "testprofile"
9696

97-
self.assertProcessFailure(_set_props_name, p, "notadrill")
97+
self.assertTypeExceptionOrProcessFailure(
98+
_set_props_name, p, "notadrill"
99+
)
98100

99101
def test_get_set_description(self):
100102
p = Modulemd.Profile(name="testprofile")

modulemd/tests/ModulemdTests/servicelevel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def test_get_name(self):
103103
assert sl.props.name == "foo"
104104

105105
# This property is not writable, make sure it fails to attempt it
106-
self.assertProcessFailure(_set_props_name, sl, "bar")
106+
self.assertTypeExceptionOrProcessFailure(_set_props_name, sl, "bar")
107107

108108
def test_get_set_eol(self):
109109
sl = Modulemd.ServiceLevel.new("foo")

modulemd/tests/ModulemdTests/translationentry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def test_get_locale(self):
172172
assert te.get_locale() == "en_US"
173173
assert te.props.locale == "en_US"
174174

175-
self.assertProcessFailure(_set_locale, te)
175+
self.assertTypeExceptionOrProcessFailure(_set_locale, te)
176176

177177
def test_get_set_summary(self):
178178
te = Modulemd.TranslationEntry(locale="en_US")

0 commit comments

Comments
 (0)