Debian Codegraph @

docs/guide/porting.rst

raw
1 ============================
2 Porting from Static Bindings
3 ============================
4
5 .. attention::
6 The pygtkcompat package has been deprecated since PyGObject 3.48.
7 The functionality has been removed since PyGObject 3.52.
8
9 Before PyGObject 3, bindings where not generated automatically through gobject
10 introspection and where provided as separate Python libraries like pygobject,
11 pygtk, pygst etc. We call them static bindings.
12
13 If your code contains imports like ``import gtk``, ``import gst``, ``import
14 glib`` or ``import gobject`` you are using the old bindings and you should
15 upgrade.
16
17 Note that using old and new bindings in the same process is not supported, you
18 have to switch everything at once.
19
20
21 Static Bindings Library Differences
22 -----------------------------------
23
24 **pygtk** supported GTK 2.0 and Python 2 only. PyGObject supports GTK >=3.0
25 and Python 2/3. If you port away from pygtk you also have to move to GTK 3.0
26 at the same time. **pygtkcompat** described below can help you with that
27 transition.
28
29 **pygst** supports GStreamer 0.10 and Python 2 only. Like with GTK you have
30 to move to PyGObject and GStreamer 1.0 at the same time.
31
32 **pygobject 2** supports glib 2.0 and Python 2. The new bindings also support
33 glib 2.0 and Python 2/3.
34
35 .. note::
36 Python 2 is no longer supported since PyGObject 3.38.0.
37
38 General Porting Tips
39 --------------------
40
41 PyGObject contains a shell script which can help you with the many naming
42 differences between static and dynamic bindings:
43
44 https://gitlab.gnome.org/GNOME/pygobject/raw/master/tools/pygi-convert.sh
45
46 ::
47
48 ./pygi-convert.sh mymodule.py
49
50 It just does basic text replacement. It reduces the amount of naming changes
51 you have to make in the beginning, but nothing more.
52
53 1) Run on a Python module
54 2) Check/Verify the changes made (e.g. using ``git diff``)
55 3) Finish porting the module by hand
56 4) Continue to the next module...
57
58
59 Porting Tips for GTK
60 --------------------
61
62 PyGObject does not support GTK 2.0. In order to use PyGObject, you'll need
63 to port your code to GTK 3.0 right away.
64
65 For some general advice regarding the migration from GTK 2.0 to 3.0 see the
66 `offical migration guide
67 <https://docs.gtk.org/gtk3/migrating-2to3.html>`__. If you
68 need to know how a C symbol is exposed in Python have a look at the `symbol
69 mapping listing <https://lazka.github.io/pgi-docs/#Gtk-3.0/mapping.html>`__.
70
71
72 Using the pygtkcompat Compatibility Layer
73 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
74
75 .. note
76
77 The pygtkcompat module is deprecated since PyGObject 3.46.
78 If your code is dependent on pygtkcompat, you have two options:
79
80 1. Update your code to use the GTK interface directly
81 2. Copy the bits you need into your own application
82
83 As of PyGObject 3.48, the compatibility layer will be disfunctional,
84 and it will be completely removed in 3.50.
85
86 PyGObject versions prior to 3.48 ship a compatibility layer for pygtk which partially emulates the
87 old interfaces:
88
89 ::
90
91 from gi import pygtkcompat
92 pygtkcompat.enable()
93 pygtkcompat.enable_gtk(version='3.0')
94
95 import gtk
96
97 ``enable()`` has to be called once before the first ``gtk`` import.
98
99 Note that pygtkcompat is just for helping you through the transition by
100 allowing you to port one module at a time. Only a limited subset of the
101 interfaces are emulated correctly and you should try to get rid of it in the
102 end.
103
104
105 Default Encoding Changes
106 ^^^^^^^^^^^^^^^^^^^^^^^^
107
108 Importing ``gtk`` had the side effect of changing the default Python encoding
109 from ASCII to UTF-8 (check ``sys.getdefaultencoding()``) and that no longer
110 happens with PyGObject. Since text with pygtk is returned as utf-8 encoded
111 str, your code is likely depending auto-decoding in many places and you can
112 change it manually by doing:
113
114 ::
115
116 # Python 2 only
117 import sys
118 reload(sys)
119 sys.setdefaultencoding("utf-8")
120 # see if auto decoding works:
121 assert '\xc3\xb6' + u'' == u'\xf6'
122
123 While this is not officially supported by Python I don't know of any
124 downsides. Once you are sure that you explicitly decode in all places or you
125 move to Python 3 where things are unicode by default you can remove this
126 again.