[PyQt] Writing dual python2/python3 code and the use of PyQt5.uic

Florian Bruhin me at the-compiler.org
Mon Mar 25 12:42:42 GMT 2019


On Sun, Mar 24, 2019 at 02:18:14PM +0000, Phil Thompson wrote:
> On 24 Mar 2019, at 1:59 pm, Eli Schwartz <eschwartz at archlinux.org> wrote:
> > 
> > On 3/24/19 6:07 AM, Phil Thompson wrote:
> >> On 24 Mar 2019, at 1:48 am, Eli Schwartz <eschwartz at archlinux.org> wrote:
> >>> 
> >>> I've been devoting some time recently to incrementally porting a PyQt5
> >>> application "calibre" (https://github.com/kovidgoyal/calibre/) from
> >>> python2 to polyglot code, hoping to get it running with python3 at some
> >>> point. One recent stumbling block I hit was that it uses the uic module
> >>> to compile forms into a cStringIO.StringIO.
> >>> 
> >>> What I wanted to do was switch it to using the modern io interface, but
> >>> as it turns out, PyQt5 uses unqualified "str" instances to write out the
> >>> form. As a result, I cannot use io.StringIO on python2, and I cannot use
> >>> io.BytesIO on python3.
> >>> 
> >>> The uic module quite specifically seems to expect that it will
> >>> manipulate unicode strings. I was able to get io.StringIO buffers to
> >>> work correctly with both python2 and python3 after I modified two files
> >>> to contain a "from __future__ import unicode_literals":
> >>> 
> >>> /usr/lib/python2.7/site-packages/PyQt5/uic/__init__.py
> >>> /usr/lib/python2.7/site-packages/PyQt5/uic/Compiler/indenter.py
> >>> 
> >>> Can this be added to at least these two files in the official
> >>> distribution? It seems to be a mistake that it ever assumed the str type.
> >> 
> >> This...
> >> 
> >> https://python-future.org/unicode_literals.html
> >> 
> >> ...makes me nervous. I would prefer a more specific fix.
> > 
> > The alternative is to add u'' decorations to the specific strings that
> > cannot be python2's dual purpose str/bytes.
> > 
> > I *think* this should be sufficient:
> > https://github.com/eli-schwartz/pyqt5/commit/1a014130024530eb0241cec7ef7ffb82e2c69fc6
> > 
> > patch:
> > https://github.com/eli-schwartz/pyqt5/commit/1a014130024530eb0241cec7ef7ffb82e2c69fc6.patch
> > 
> > (Applied on top of a tarball import as I could not find any version
> > control sources for PyQt5.)
> 
> What about Python v3.3 and earlier?

To get the same effect with compatibility for all versions, you could do
something like this (untested):

  def u(s):
      return s if sys.version_info[0] == 3 else unicode(s)

And then use u("...") instead of u"..."

Florian

-- 
https://www.qutebrowser.org | me at the-compiler.org (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072 | https://the-compiler.org/pubkey.asc
         I love long mails! | https://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190325/49fdfcfd/attachment.sig>


More information about the PyQt mailing list