![]() |
#1 |
Deviser
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 2,265
Karma: 2090983
Join Date: Aug 2013
Location: Texas
Device: none
|
Python 2 to Python 3, Polyglot & Plugins
Kovid,
For future planning purposes only, by what month/year will plugin developers need to have changed their Python 2 code to use "...\src\polyglot"? When do you expect polyglot to become stable enough that plugin developers can begin using it and hence begin completely re-testing their plugins? I have 15 plugins, so it is not something that can be done quickly and easily. Thanks. DaltonST |
![]() |
![]() |
![]() |
#2 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 44,103
Karma: 22669824
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
polyglot is already stable, feel free to use it whenever you like.
|
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6,602
Karma: 84812983
Join Date: Nov 2011
Location: Tampa Bay, Florida
Device: Kindles
|
I have questions about the planned Python 3 migration.
Are you going to put out a Python 3 beta version for Windows anytime soon? Are you planning a hard cutover from 2 to 3 are you going to release both versions together for awhile? |
![]() |
![]() |
![]() |
#4 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 44,103
Karma: 22669824
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Not any time soon no. The python 3 port is nowhere near ready. If and when it is ready, there will be a long period when both python 2 and python 3 builds of calibre on all platforms will be available so people have time to port plugins and find regressions in the python 3 port.
At the moment the plan is that when the python 3 port is ready and many plugins have been ported, I will stop releasing python 2 builds and all future calibre releases will be python 3 based. Note however that this is very early stages, these plans might change depending on ground realties. The one this that is certain is that I do intend to provide at least a few months for plugin porting. Last edited by kovidgoyal; 04-21-2019 at 01:54 AM. |
![]() |
![]() |
![]() |
#5 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6,602
Karma: 84812983
Join Date: Nov 2011
Location: Tampa Bay, Florida
Device: Kindles
|
Thanks. That sounds great.
|
![]() |
![]() |
Advert | |
|
![]() |
#6 |
Ex-Helpdesk Junkie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 19,421
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
|
Note that you don't actually need to use polyglot at all, but if you do, it's simple enough to just import from it and bump the minimum calibre version. But they are mostly pretty simple things.
I'd suggest going forward, to reduce the cognitive burden you should rely on any __future__ imports available (especially unicode_literals), make sure to mark all relevant strings as b'' and use modern python idioms that work natively on both. The latter two are things that any relatively recent code should probably have been doing anyway. ... It's quite easy to reimplement the bits that you need, since they are, by and large, very simple try/except blocks. You could also rely on the fact that calibre has always had dependencies (dateutil, more recently html5lib) which require python-six, and just import needed functionality from e.g. six.moves which would make things a lot simpler. I think six 1.10 has been available at a minimum since a very long time... Kovid has decided that he does not wish to rely on python-six, but you're not required to hold the same opinions. ![]() Note: polyglot has been available since calibre 3.32 with the following contents (many more things were only added in the latest release): Code:
from polyglot.builtins import is_py3, reraise, zip, map, filter, iteritems, iterkeys, itervalues from polyglot.urllib import urlopen, urlencode |
![]() |
![]() |
![]() |
#7 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6,212
Karma: 16534894
Join Date: Sep 2009
Location: UK
Device: Kobo: KA1, ClaraHD, Forma, Libra2, Clara2E. PocketBook: TouchHD3
|
Quote:
I've read the above many times and I still don't really understand what you're saying. I'm pretty sure the problem is at my end ![]()
|
|
![]() |
![]() |
![]() |
#8 | |
Ex-Helpdesk Junkie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 19,421
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
|
1) Yes! All __future__ imports are defined by the python language committee to always remain valid, in perpetuity. https://docs.python.org/3/library/__future__.html
Quote:
You're doing everything you need to, here. However, there are other cases where you need simple modernization. For example, in several cases your ScrambleEbook plugin uses the idiom: Code:
raise MyException, errmsg Code:
File "/home/eschwartz/git/calibre/src/calibre/customize/zipplugin.py", line 182, in load_module import_name), 'exec', dont_inherit=True) File "calibre_plugins.scrambleebook_plugin.action", line 117 raise SelectedBookError, errmsg ^ SyntaxError: invalid syntax Code:
raise MyException(errmsg) Code:
# python2 >>> {"one":"two", "three": "four"}.items() [('three', 'four'), ('one', 'two')] # this is a list >>> {"one":"two", "three": "four"}.iteritems() <dictionary-itemiterator object at 0x7f6874c5f940> # this is an iterator >>> {"one":"two", "three": "four"}.viewitems() dict_items([('three', 'four'), ('one', 'two')]) # this is an iterator # python3 {"one":"two", "three": "four"}.items() dict_items([('one', 'two'), ('three', 'four')]) # this is an iterator So you trade inefficiency for portability if you use a simple items(). 2) b'' strings are for example what you get if you: Code:
>>> with open('filename', 'wb') as f: >>> f.write('Hello World') TypeError: a bytes-like object is required, not 'str' One example of an article discussing the difference and why it matters, is here: https://medium.com/@andreacolangelo/...w-27dc02ff2686 To use a practical example, after I updated your plugin to use "raise SelectedBookError(errmsg)", and updated all dict.iteritems() to use from polyglot.builtins import iteritems (requires calibre >= 3.41.0) and iteritems(dict), I still got the following message. But this came from calibre itself. Code:
Traceback (most recent call last): File "calibre_plugins.scrambleebook_plugin.action", line 161, in show_dialog File "calibre_plugins.scrambleebook_plugin.scrambleebook", line 189, in __init__ File "calibre_plugins.scrambleebook_plugin.scrambleebook", line 256, in initialise_new_file File "calibre_plugins.scrambleebook_plugin.scrambleebook", line 1094, in get_run_check_error File "/home/eschwartz/git/calibre/src/calibre/ebooks/oeb/polish/check/main.py", line 81, in run_checks errors += check_opf(container) File "/home/eschwartz/git/calibre/src/calibre/ebooks/oeb/polish/check/opf.py", line 373, in check_opf n, c = raw.index('name="'), raw.index('content="') TypeError: argument should be integer or bytes-like object, not 'str' raw.index is acting on Code:
raw = etree.tostring(cover) Code:
'name="' Code:
b'name="' EDIT: now officially merged: https://github.com/kovidgoyal/calibr...0543e13ef72ac0 The final fix to make the ScrambleEbook plugin work flawlessly AFAICT is to replace all use of unicode() with unicode_type() as imported from polyglot.builtins once again. Although when you used 'mystring %s' % unicode(len(foo)), you can simply drop the unicode cast. You can directly format the string using an integer returned by len() without first converting it to a unicode string. Last edited by eschwartz; 04-23-2019 at 01:09 AM. |
|
![]() |
![]() |
![]() |
#9 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 44,103
Karma: 22669824
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
I should note that a good way to find many of the more common/simple things that need changing and even to change them automatically in your plugin's python files is to use the modernize tool
https://python-modernize.readthedocs...est/index.html or the 2to3 tool https://docs.python.org/3/library/2to3.html This tool however will make your files incompatible with python 2 so it should be used to detect issues only, not fix them automatically. |
![]() |
![]() |
![]() |
#10 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6,212
Karma: 16534894
Join Date: Sep 2009
Location: UK
Device: Kobo: KA1, ClaraHD, Forma, Libra2, Clara2E. PocketBook: TouchHD3
|
@eschwartz,
Thank you very much for explaining things. It was good of you to take the time with such a detailed reply ![]() @kovid, Thank you for the links ![]() |
![]() |
![]() |
![]() |
#11 | |
Resident Curmudgeon
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 74,938
Karma: 131375774
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
|
Quote:
|
|
![]() |
![]() |
![]() |
#12 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6,300
Karma: 12116851
Join Date: Jun 2009
Location: Madrid, Spain
Device: Kobo Clara/Aura One/Forma,XiaoMI 5, iPad, Huawei MediaPad, YotaPhone 2
|
Quote:
![]() |
|
![]() |
![]() |
![]() |
#13 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6,212
Karma: 16534894
Join Date: Sep 2009
Location: UK
Device: Kobo: KA1, ClaraHD, Forma, Libra2, Clara2E. PocketBook: TouchHD3
|
Quote:
Having said all that I'm always happy to share code with other plugin hobbyists as a base for creating their own stuff. But I don't want ongoing maintenance responsibility - especially now - during the transition from python 2 to 3. |
|
![]() |
![]() |
![]() |
#14 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6,300
Karma: 12116851
Join Date: Jun 2009
Location: Madrid, Spain
Device: Kobo Clara/Aura One/Forma,XiaoMI 5, iPad, Huawei MediaPad, YotaPhone 2
|
Quote:
![]() ![]() ![]() ![]() ![]() |
|
![]() |
![]() |
![]() |
#15 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6,300
Karma: 12116851
Join Date: Jun 2009
Location: Madrid, Spain
Device: Kobo Clara/Aura One/Forma,XiaoMI 5, iPad, Huawei MediaPad, YotaPhone 2
|
Quote:
![]() ![]() ![]() |
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Python error & now Book Details is on the Side | wildoatz | Devices | 4 | 06-27-2017 08:46 PM |
Python + PyQt enviroment for Kindle & PaperWhite devices. | bosito7 | Kindle Developer's Corner | 8 | 11-26-2016 03:43 AM |
KTerm & Python Issues | aBP | Kindle Developer's Corner | 4 | 02-10-2016 05:23 PM |
Python help please | AndyW1691 | Library Management | 3 | 02-08-2012 09:51 AM |
Plugins and optional Python modules | pdurrant | Plugins | 14 | 03-02-2010 03:28 AM |