Support Python 3

Bug #809884 reported by Andreas Becker
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
JessyInk
Fix Committed
Medium
Hannes Hochreiner

Bug Description

Please change the Python code for the Inkscape plugin to support both Python 2.x and Python 3. It is very easy, because the tool “2to3” can convert the code.
In Python 3, the “u” prefix to strings is not supported, because there, all strings are unicode. A solution is either to remove the prefix, if it also works without it in Python 2.x, or to replace “u” by a function “u(...)” and define this function like the following:
def u(string):
    try:
        return unicode(string)
    except:
        return string

Related branches

Revision history for this message
Hannes Hochreiner (hannes-hochreiner) wrote :

Thanks for pointing this problem out. Adding the "u(...)"-function would not be a big deal and I'll happily do it, since the scripts would then run on python 2 and 3. However, I did some quick tests with the 2to3 tool and there are at least two other constructs that are quite frequently used that would need to be replaced:

- if node.attrib.has_key("{" + inkex.NSS["jessyink"] + "}view"):
+ if "{" + inkex.NSS["jessyink"] + "}view" in node.attrib:

- for id, node in self.selected.items():
+ for id, node in list(self.selected.items()):

Do you know whether the suggested changed still work in python 2?

Changed in jessyink:
importance: Undecided → Medium
status: New → Confirmed
assignee: nobody → Hannes Hochreiner (hannes-hochreiner)
Revision history for this message
Andreas Becker (abecker) wrote :

According to the documentation, both changes do work in every Python 2 version (http://docs.python.org/release/2.0)

A somewhat problematic change would be for example the exception handling, which leads to incompatibilities between <2.6 and >=3.0. Python 3 only allows "except Exception as e:", but the keyword "as" does not exist in <2.6.
Fortunately, I could not find deprecated constructs like "except Exception, e:" in JessyInk, so this is *currently* no problem.

Another hint for the u(...) function: instead of try/except, it is also possible to use sys.hexversion or sys.version_info.

Revision history for this message
Hannes Hochreiner (hannes-hochreiner) wrote :

Thanks for checking that out. I'll try and fix the scripts so they run on python 2 and 3.

Changed in jessyink:
status: Confirmed → Fix Committed
milestone: none → 1.5.6
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.