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

Porting to Python 3 #47

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stanford_corenlp_pywrapper/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from sockwrap import *
from .sockwrap import *
25 changes: 12 additions & 13 deletions stanford_corenlp_pywrapper/sockwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def command(mode=None, configfile=None, configdict=None, comm_mode=None,
else: assert False, "need comm_mode to be SOCKET or PIPE but got " + repr(comm_mode)


cmd = """exec {java_command} {java_options} -cp '{classpath}'
cmd = """exec {java_command} {java_options} -cp '{classpath}'
corenlp.SocketServer {comm_info} {more_config}"""
return cmd.format(**d).replace("\n", " ")

Expand All @@ -85,7 +85,7 @@ class SubprocessCrashed(Exception):

class CoreNLP:

def __init__(self, mode=None,
def __init__(self, mode=None,
configfile=None, configdict=None,
corenlp_jars=(
"/home/sw/corenlp/stanford-corenlp-full-2015-04-20/*",
Expand Down Expand Up @@ -168,7 +168,7 @@ def start_server(self):
if self.comm_mode=='PIPE':
if not os.path.exists(self.outpipe):
os.mkfifo(self.outpipe)

cmd = command(**self.__dict__)
LOG.info("Starting java subprocess, and waiting for signal it's ready, with command: %s" % cmd)
self.proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE)
Expand All @@ -178,7 +178,7 @@ def start_server(self):
sock = self.get_socket(num_retries=100, retry_interval=STARTUP_BUSY_WAIT_INTERVAL_SEC)
sock.close()
elif self.comm_mode=='PIPE':
self.outpipe_fp = open(self.outpipe, 'r')
self.outpipe_fp = open(self.outpipe, 'rb')

while True:
# This loop is for if you have timeouts for the socket connection
Expand All @@ -191,7 +191,7 @@ def start_server(self):
assert ret == "PONG", "Bad return data on startup ping: " + ret
LOG.info("Successful ping. The server has started.")
break
except socket.error, e:
except socket.error as e:
LOG.info("Waiting for startup: ping got exception: %s %s" % (type(e), e))
LOG.info("pausing before retry")
time.sleep(STARTUP_BUSY_WAIT_INTERVAL_SEC)
Expand Down Expand Up @@ -251,7 +251,7 @@ def send_command_and_parse_result(self, cmd, timeout, raw=False):
LOG.warning("Bad JSON length %d, starts with: %s" % (len(data), repr(data[:1000])))
return None
return decoded
except socket.timeout, e:
except socket.timeout as e:
LOG.info("Socket timeout happened, returning None: %s %s" % (type(e), e))
return None
# This is tricky. maybe the process is running smoothly but just
Expand All @@ -266,14 +266,13 @@ def send_command_and_get_string_result(self, cmd, timeout):
sock.sendall(cmd + "\n")
size_info_str = sock.recv(8)
elif self.comm_mode == 'PIPE':
self.proc.stdin.write(cmd + "\n")
self.proc.stdin.write((cmd+"\n").encode('utf-8'))
self.proc.stdin.flush()
size_info_str = self.outpipe_fp.read(8)

# java "long" is 8 bytes, which python struct calls "long long".
# java default byte ordering is big-endian.
size_info = struct.unpack('>Q', size_info_str)[0]
# print "size expected", size_info
size_info = struct.unpack(('>Q').encode('utf-8'), size_info_str)[0]

chunks = []
curlen = lambda: sum(len(x) for x in chunks)
Expand All @@ -283,7 +282,7 @@ def send_command_and_get_string_result(self, cmd, timeout):
data = sock.recv(remaining_size)
elif self.comm_mode == 'PIPE':
data = self.outpipe_fp.read(remaining_size)
chunks.append(data)
chunks.append(data.decode("utf-8"))
if curlen() >= size_info: break
if len(chunks) > 1000:
LOG.warning("Incomplete value from server")
Expand Down Expand Up @@ -329,7 +328,7 @@ def test_paths():
def assert_no_java(msg=""):
ps_output = os.popen("ps wux").readlines()
javalines = [x for x in ps_output if re.search(r'\bbin/java\b', x)]
print ''.join(javalines)
print (''.join(javalines))
assert len(javalines) == 0, msg

# def test_doctimeout():
Expand All @@ -345,8 +344,8 @@ def assert_no_java(msg=""):
import sys
if sys.argv[1]=='modes':
for mode,d in MODES_items:
print " * `%s`: %s" % (mode, d['description'])
print (" * `%s`: %s" % (mode, d['description']))
if sys.argv[1]=='modes_json':
# import json as stdjson
# print stdjson.dumps(MODES, indent=4)
print '"%s"' % json.dumps(MODES).replace('"', r'\"')
print ('"%s"' % json.dumps(MODES).replace('"', r'\"'))