Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Python 3 #11

Closed
mojca opened this issue Feb 2, 2016 · 2 comments
Closed

Support for Python 3 #11

mojca opened this issue Feb 2, 2016 · 2 comments

Comments

@mojca
Copy link
Contributor

mojca commented Feb 2, 2016

It would be nice if xasy was properly ported to Python 3.

I spent a few hours debugging xasy against Python 3 and here's where I'm stuck:

The function def updateCode(self,mag=1.0) from xasy2asy calls
self.asyCode += self.makeNodeStr(self.controlSet[count][0])

The problem is that self.controlSet is a list of map objects and Python 3 is no longer happy about it:

TypeError: 'map' object is not subscriptable

My first naive approach was the following:

cs = list(self.controlSet[count])
for node in self.nodeSet[1:]:
  self.asyCode += "..controls"
  cs = list(self.controlSet[count])
  self.asyCode += self.makeNodeStr(cs[0])
  self.asyCode += "and"
  self.asyCode += self.makeNodeStr(cs[1])
  self.asyCode += ".." + self.makeNodeStr(node) + "\n"
  count += 1

The problem is that list(map(...)) somehow deletes the contents of map, so that one can only access those variables once. See also http://www.artima.com/weblogs/viewpost.jsp?thread=98196. Do you know how to convert maps into proper array entries?

@mojca
Copy link
Contributor Author

mojca commented Feb 5, 2016

The above mentioned problem has been addressed by replacing

controls = [map(eval,a.replace("controls","").split("and")) for a in tokens if a.startswith("controls")]

with

[eval(a.replace("controls", "").split("and")) for a in tokens if a.startswith("controls")]

in GUI/xasyFile.py.

The next problem seems to be fin.readline() from asyfyThread in xasy2asy.py which doesn't seem to be able to read anything. This might be related to some problems in Popen or os.fdopen, but it's not yet clear what exactly is going on.

Below is a standalone example to reproduce the problem:

#!/usr/bin/env python

import sys,os,signal,subprocess,threading,tempfile

AsyTempDir=tempfile.mkdtemp(prefix="asy_")+os.sep
print("AsyTempDir: '{}'".format(AsyTempDir))

(rx,wx) = os.pipe()
(ra,wa) = os.pipe()
cmd = ['asy',"-noV","-multiline","-q", "-o"+AsyTempDir,"-inpipe="+str(rx),"-outpipe="+str(wa)]
print(cmd)

# when close_fds=True, it hangs on 2.7 as well
quickAsy=subprocess.Popen(cmd,close_fds=False)
fout=os.fdopen(wx,'w')
fin=os.fdopen(ra,'r')

#######
fout.write("reset;\n")
fout.write("initXasyMode();\n")
fout.write("atexit(null);\n")
fout.write('xformStack.push((0, 0, 1, 0, 0, 1));\n')
fout.write('label(Label("test",(0, 0),rgb(0,0,0)+0.5,align=SE));\n')
fout.write('deconstruct(1.000000);\n')
fout.flush()

text = fin.readline()
print("    readline:")
print("'{}'".format(text))
fout.write("exit;\n");

fin.close()
fout.close()

The parameter close_fds=False doesn't seem to make any difference in Python 3 (it's set to False in Python 2 and to True in Python 3 by default), but if I set it to True, then Python 2 hangs as well.

@mojca
Copy link
Contributor Author

mojca commented Feb 5, 2016

I fixed the problem with pipes thanks to some great answers from MacPorters and will describe more details soon.

John, is the second replacement for oneLiner OK?

Old code:

oneLiner = "".join(split(join(pathStrLines)))

New code:

# oneLiner = "".join(" ".join(pathStrLines).split()) # this is literal translation
oneLiner = "".join(pathStrLines).replace(" ", "") # this is probably more straightforward

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants