Python 3 syntax error question

R

rzed

I've tried to install PySVG in a Python 3 setting, and I get a few
errors on the build. Most are easy to fix, but this one I can't
explain or fix:

<error>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "builders.py", line 12, in <module>
from pysvg.shape import *
File "C:\Python32\lib\site-packages\pysvg\shape.py", line 91
def moveToPoint(self,(x,y)):
^
SyntaxError: invalid syntax
</error>

The moveToPoint method occurs three times in the file, with identical
signatures. The other two are not showing up as errors, though since
they occur later in the file, that may not be indicative.

I don't see anything erroneous in this line. The syntax error often
comes from the previous line, but I've moved this method around and
it has always failed on this line and no other, regardless of what
went before.

I'm new to Py3, so maybe there's some obvious thing I'm not seeing
here. Does anyone have any suggestions?
 
S

steve+comp.lang.python

rzed said:
I've tried to install PySVG in a Python 3 setting, and I get a few
errors on the build. Most are easy to fix, but this one I can't
explain or fix:

<error>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "builders.py", line 12, in <module>
from pysvg.shape import *
File "C:\Python32\lib\site-packages\pysvg\shape.py", line 91
def moveToPoint(self,(x,y)):
^
SyntaxError: invalid syntax
</error>

Function signatures with automatic tuple-unpacking are no longer allowed in
Python3. So functions or methods like this:

def moveToPoint(self,(x,y)):

have to be re-written with the tuple unpacking moved into the body of the
function, e.g. something like this:

def moveToPoint(self, x_y):
x, y = x_y


Are you aware that you're trying to install a Python2 library under Python3?
 
P

Peter Otten

rzed said:
I've tried to install PySVG in a Python 3 setting, and I get a few
errors on the build. Most are easy to fix, but this one I can't
explain or fix:

<error>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "builders.py", line 12, in <module>
from pysvg.shape import *
File "C:\Python32\lib\site-packages\pysvg\shape.py", line 91
def moveToPoint(self,(x,y)):
^
SyntaxError: invalid syntax
</error>

The moveToPoint method occurs three times in the file, with identical
signatures. The other two are not showing up as errors, though since
they occur later in the file, that may not be indicative.

I don't see anything erroneous in this line. The syntax error often
comes from the previous line, but I've moved this method around and
it has always failed on this line and no other, regardless of what
went before.

I'm new to Py3, so maybe there's some obvious thing I'm not seeing
here. Does anyone have any suggestions?

Quoting

http://docs.python.org/dev/py3k/whatsnew/3.0.html#removed-syntax

"""
You can no longer write def foo(a, (b, c)): .... Use def foo(a, b_c): b, c =
b_c instead.
"""

If there isn't a Python 3 version of PySVG you can try to run it through

http://docs.python.org/dev/py3k/library/2to3.html#to3-reference

to make the easy changes.
 
N

Noah Hall

I've tried to install PySVG in a Python 3 setting, and I get a few
errors on the build. Most are easy to fix, but this one I can't
explain or fix:

<error>
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "builders.py", line 12, in <module>
   from pysvg.shape import *
 File "C:\Python32\lib\site-packages\pysvg\shape.py", line 91
   def moveToPoint(self,(x,y)):
                        ^
SyntaxError: invalid syntax
</error>

The moveToPoint method occurs three times in the file, with identical
signatures. The other two are not showing up as errors, though since
they occur later in the file, that may not be indicative.

I don't see anything erroneous in this line. The syntax error often
comes from the previous line, but I've moved this method around and
it has always failed on this line and no other, regardless of what
went before.

I'm new to Py3, so maybe there's some obvious thing I'm not seeing
here. Does anyone have any suggestions?

Did you run it through 2to3? When I run
def a(b, (c,d)):
pass
through 2to3, it tells me what I need to change.

-def a(b, (c,d)):
+def a(b, xxx_todo_changeme):
+ (c,d) = xxx_todo_changeme

(which is what Steven said)

If you haven't used 2to3, I suggest you use it.

HTH.
 
R

rzed

(e-mail address removed) wrote in
Function signatures with automatic tuple-unpacking are no longer
allowed in Python3. So functions or methods like this:

def moveToPoint(self,(x,y)):

have to be re-written with the tuple unpacking moved into the
body of the function, e.g. something like this:

def moveToPoint(self, x_y):
x, y = x_y


Are you aware that you're trying to install a Python2 library
under Python3?

Thank you all for your responses. Yes, I am aware of the version
difference, but not of all the implications of that. I will run this
through 2to3, but even without doing that, there are only about four
syntax errors, and the others were obvious and easily corrected.

There does not seem to be a Py3 version of this package. I was hoping
to try it to see what broke. Well, I found out at least part of that,
didn't I?

I was not aware of the removal of tuple-unpacking. I expect there was
some extensive conversation about that.

As to 2to3, I have to say that:

-def a(b, (c,d)):
+def a(b, xxx_todo_changeme):
+ (c,d) = xxx_todo_changeme

.... is not terribly revealing if one is unaware of what about it
needs changing. I know, I know: RTFM....
 
C

Chris Angelico

As to 2to3, I have to say that:

-def a(b, (c,d)):
+def a(b, xxx_todo_changeme):
+    (c,d) = xxx_todo_changeme

... is not terribly revealing if one is unaware of what about it
needs changing. I know, I know: RTFM....

Sure, but you don't _have_ to look at the diff. Just run it through
2to3 and see how it runs. Never know, it might work direct out of the
box!

ChrisA
 
N

Noah Hall

(e-mail address removed) wrote in


Thank you all for your responses. Yes, I am aware of the version
difference, but not of all the implications of that. I will run this
through 2to3, but even without doing that, there are only about four
syntax errors, and the others were obvious and easily corrected.

There does not seem to be a Py3 version of this package. I was hoping
to try it to see what broke. Well, I found out at least part of that,
didn't I?

I was not aware of the removal of tuple-unpacking. I expect there was
some extensive conversation about that.

As to 2to3, I have to say that:

-def a(b, (c,d)):
+def a(b, xxx_todo_changeme):
+    (c,d) = xxx_todo_changeme

... is not terribly revealing if one is unaware of what about it
needs changing. I know, I know: RTFM....

It means delete every line with a '-' and replace them with those next
to the '+'
Of course, if you read the doc, it'll give you lots of different
options, including writing to the file, so all you need to do is
change the variable names.
 
J

Jerry Hill

Sure, but you don't _have_ to look at the diff. Just run it through
2to3 and see how it runs. Never know, it might work direct out of the
box!

This has been my experience, by the way. I've used a few small pure
python libraries written for python 2.x that don't have 3.x versions,
and they've all worked just fine with just a quick run through the
2to3 process. I can't speak for larger libraries, or ones with lots
of compiled code, but my experience with 2to3 has been great.
 
T

Terry Reedy

(e-mail address removed) wrote in
Thank you all for your responses. Yes, I am aware of the version
difference, but not of all the implications of that. I will run this
through 2to3, but even without doing that, there are only about four
syntax errors, and the others were obvious and easily corrected.

When you are done, I hope you feed results back to author as to how to
make code run under Py3 without change (the explicit unpacking needed
for Py3 works in Py2 also) or without further change after 2to3. Then
encourage author to advertise fact and add Py3 classifier if listed in PyPI.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top