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

pyudev.glib incompatible with Python 3 #33

Closed
Arfrever opened this issue Aug 19, 2011 · 12 comments
Closed

pyudev.glib incompatible with Python 3 #33

Arfrever opened this issue Aug 19, 2011 · 12 comments
Assignees
Milestone

Comments

@Arfrever
Copy link

$ python2.7 -c 'import pyudev.glib'
$ python3.2 -c 'import pyudev.glib'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib64/python3.2/site-packages/pyudev/glib.py", line 40, in <module>
    from . import glib
ImportError: cannot import name glib

I use pygobject-2.28.6 with some patches backported from pygobject repository. This version of pygobject has been separately built and installed for each version of Python.

2to3 changed 'import glib' into 'from . import glib' during processing of pyudev/glib.py.
If I manually revert this change, then the following error occurs:

$ python3.2 -c 'import pyudev.glib'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib64/python3.2/site-packages/pyudev/glib.py", line 45, in <module>
    class GUDevMonitorObserver(gobject.GObject):
  File "/usr/lib64/python3.2/site-packages/gobject/__init__.py", line 60, in __init__
    cls._type_register(cls.__dict__)
  File "/usr/lib64/python3.2/site-packages/gobject/__init__.py", line 115, in _type_register
    type_register(cls, namespace.get('__gtype_name__'))
TypeError: __gsignals__ keys must be strings
@ghost ghost assigned swsnr Aug 20, 2011
@swsnr
Copy link
Contributor

swsnr commented Aug 20, 2011

glib support isn't tested against Python 3, because I didn't know that pygobject already supports Python 3.

Do you know which version (unpatched of course) of pygobject introduced Python 3 support?

@swsnr
Copy link
Contributor

swsnr commented Aug 20, 2011

So there is no released version with Python 3 support yet? In this case, you are on your own for now. I'm not using the glib module myself, and thus need the unit tests to make sure that the glib module works. The unit tests don't run with checkouts from git, but with released versions, and I simply lack the time to adapt the test toolchain to support tests against a git clone of pygobject. I'm sorry.

I don't see any reason to support something that isn't even released yet anyway. Closing as invalid for now, please re-open, if I'm mistaken and there is a released pygobject version with Python 3, or if a new pygobject release provides Python 3 support. Of course, if you can provide patches for pyudev.glib to add Python 3 support, I'll gladly accept these and commit them,as long as they are covered by tests.

@swsnr swsnr closed this as completed Aug 20, 2011
@Arfrever
Copy link
Author

This patch avoid problem during importing:

--- pyudev/glib.py
+++ pyudev/glib.py
@@ -35,9 +35,7 @@
 from __future__ import (print_function, division, unicode_literals,
                         absolute_import)

-# thanks to absolute imports, this really imports the glib binding and not this
-# module again
-import glib
+glib = __import__("glib")
 import gobject


@@ -64,16 +62,16 @@

     __gsignals__ = {
         # glib apparently expects byte-strings as signal names
-        b'device-event': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
-                          (gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)),
-        b'device-added': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
-                          (gobject.TYPE_PYOBJECT,)),
-        b'device-removed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
-                            (gobject.TYPE_PYOBJECT,)),
-        b'device-changed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
-                            (gobject.TYPE_PYOBJECT,)),
-        b'device-moved': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
-                          (gobject.TYPE_PYOBJECT,)),
+        'device-event': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
+                         (gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)),
+        'device-added': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
+                         (gobject.TYPE_PYOBJECT,)),
+        'device-removed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
+                           (gobject.TYPE_PYOBJECT,)),
+        'device-changed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
+                           (gobject.TYPE_PYOBJECT,)),
+        'device-moved': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
+                         (gobject.TYPE_PYOBJECT,)),
         }

     def __init__(self, monitor):

@Arfrever
Copy link
Author

Already existing tests for pyudev.glib in tests/test_observer.py pass with this patch applied.

@swsnr
Copy link
Contributor

swsnr commented Aug 20, 2011

I doubt that the patch passes tests on Python 2. Did you try that?

And please don't use __import__ for importing regular modules. Fix the import by using a proper relative or absolute import.

@Arfrever
Copy link
Author

setup.py uses 2to3, which changes 'import glib' into 'from . import glib'. How do you suggest to fix it?

@Arfrever
Copy link
Author

This patch works with Python 2 and 3:

--- pyudev/glib.py
+++ pyudev/glib.py
@@ -32,12 +32,9 @@
 """


-from __future__ import (print_function, division, unicode_literals,
-                        absolute_import)
+from __future__ import print_function

-# thanks to absolute imports, this really imports the glib binding and not this
-# module again
-import glib
+glib = __import__("glib")
 import gobject


@@ -63,17 +60,16 @@
         'change': 'device-changed', 'move': 'device-moved'}

     __gsignals__ = {
-        # glib apparently expects byte-strings as signal names
-        b'device-event': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
-                          (gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)),
-        b'device-added': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
-                          (gobject.TYPE_PYOBJECT,)),
-        b'device-removed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
-                            (gobject.TYPE_PYOBJECT,)),
-        b'device-changed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
-                            (gobject.TYPE_PYOBJECT,)),
-        b'device-moved': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
-                          (gobject.TYPE_PYOBJECT,)),
+        'device-event': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
+                         (gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)),
+        'device-added': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
+                         (gobject.TYPE_PYOBJECT,)),
+        'device-removed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
+                           (gobject.TYPE_PYOBJECT,)),
+        'device-changed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
+                           (gobject.TYPE_PYOBJECT,)),
+        'device-moved': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
+                         (gobject.TYPE_PYOBJECT,)),
         }

     def __init__(self, monitor):

@swsnr swsnr reopened this Aug 30, 2011
@swsnr
Copy link
Contributor

swsnr commented Aug 30, 2011

I managed to get pygobject building against Python 3, so now I'm able to run the pyudev.glib tests with Python 3. The bug is indeed very valid, sorry for all the hassle. I'll take care for this issue asap, I just need to figure out the best way to fix the glitches. The import is the biggest problem. In theory it is written to support python 2 and python 3, but unfortunately 2to3 fails to handle such imports correctly (an upstream bug in python). I'll probably have to adjust the 2to3 fixers used to generate the Python 3 code.

swsnr pushed a commit that referenced this issue Aug 30, 2011
2to3 "import" fixers blews up absolute imports by changing them back to
relative imports in some python versions, see #8358.  Affects the "pyudev.glib"
module, which uses an absolute import for "glib".  Applying the "import" fixer
to this module breaks pyudev.glib for python 3.
@swsnr swsnr closed this as completed in 51ca8f6 Aug 30, 2011
@Arfrever
Copy link
Author

Python issue #8358 was already fixed in Python version used by me.
I have filed Python issue #12873.

@swsnr
Copy link
Contributor

swsnr commented Sep 1, 2011

I already had doubts about #8358 myself, because it's marked fixed since over a year ago. But I didn't investigate the cause of this issue further, because it was obviously solved by disabling the import fixer. Thanks for doing so yourself. Luckily #12873 didn't hit pyudev at other places, multiline future imports are used in every pyudev module.

@swsnr
Copy link
Contributor

swsnr commented Sep 1, 2011

And just for the record: Yesterday I released pyudev 0.12 which fixes this issue.

@swsnr swsnr mentioned this issue Mar 11, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants