Skip to content

Commit 0464d7c

Browse files
committed
finally flushing
1 parent feaf8a9 commit 0464d7c

File tree

3 files changed

+40
-61
lines changed

3 files changed

+40
-61
lines changed

index.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,6 @@ PythonShell.prototype.send = function (message) {
191191
return this;
192192
};
193193

194-
PythonShell.prototype.pauseInput = function (message) {
195-
this.stdin.pause();
196-
return this;
197-
};
198-
199-
PythonShell.prototype.flushInput = function (message) {
200-
// this.stdin.write(os.EOL);
201-
return this;
202-
};
203-
204194
/**
205195
* Parses data received from the Python shell stdout stream and emits "message" events
206196
* This method is not used in binary mode

test/python/conversation.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,13 @@ def handleLine(line):
5959
return handleAction(parsed)
6060

6161
# simple JSON echo script
62-
# while not ended:
63-
# line = sys.stdin.read()
64-
for line in sys.stdin:
62+
while not ended:
63+
line = sys.stdin.readline()
64+
if not line:
65+
break
66+
# for line in sys.stdin:
67+
6568
response = handleLine(line)
6669

6770
print json.dumps(response)
68-
# print json.dumps({
69-
# 'action':'knockknockjoke',
70-
# 'message': "Who's there?"
71-
# })
72-
sys.stdout.flush()
73-
# time.sleep(1)
71+
sys.stdout.flush()

test/test-python-shell.js

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe('PythonShell', function () {
100100
var pyshell = new PythonShell('conversation.py', {
101101
mode: 'json'
102102
});
103-
var output = '';
103+
var outputs = [];
104104

105105
function makeKnockKnockMessage(message) {
106106
return {
@@ -109,21 +109,31 @@ describe('PythonShell', function () {
109109
};
110110
}
111111

112+
var outgoingMessages = [
113+
"Knock, knock.",
114+
"Orange.",
115+
"Orange you glad I didn't say, 'banana'?"
116+
];
117+
118+
var incomingMessages = [
119+
"Who's there?",
120+
"Orange who?",
121+
"Ha ha."
122+
];
123+
112124
var makeKnockKnockReply = makeKnockKnockMessage;
113125

114126
function handleReply(reply) {
115127
switch(reply.message) {
116-
case "Who's there?":
128+
case incomingMessages[0]:
117129
// awaitReply();
118-
pyshell.send(makeKnockKnockMessage('Orange.'));
119-
flushStdIn();
130+
pyshell.send(makeKnockKnockMessage(outgoingMessages[1]));
120131
break;
121-
case "Orange who?":
132+
case incomingMessages[1]:
122133
// awaitReply();
123-
pyshell.send(makeKnockKnockMessage("Orange you glad I didn't say, 'banana'?"));
124-
flushStdIn();
134+
pyshell.send(makeKnockKnockMessage(outgoingMessages[2]));
125135
break;
126-
case "Ha ha.":
136+
case incomingMessages[2]:
127137
endAndAssert();
128138
break;
129139
}
@@ -132,11 +142,11 @@ describe('PythonShell', function () {
132142
}
133143

134144
// function awaitReply() {
135-
pyshell.stdout.on('data', function (data) {
136-
output += ''+data;
137-
145+
pyshell.on('message', function (data) {
138146
console.log("Data to stdout: ", data);
139147

148+
outputs.push(data);
149+
140150
switch(data.action) {
141151
case 'knockknockjoke':
142152
handleReply(data);
@@ -146,7 +156,7 @@ describe('PythonShell', function () {
146156
}
147157
});
148158

149-
pyshell.stdout.resume();
159+
// pyshell.stdout.resume();
150160
// }
151161

152162
// function awaitReply() {
@@ -165,9 +175,9 @@ describe('PythonShell', function () {
165175
// });
166176
// }
167177

168-
pyshell.stderr.on('data', function(err) {
169-
console.error(err);
170-
});
178+
// pyshell.stderr.on('data', function(err) {
179+
// console.error(err);
180+
// });
171181

172182
pyshell.on('close', function(err) {
173183
if (err) {
@@ -176,44 +186,25 @@ describe('PythonShell', function () {
176186
return done('Unexpectedly closed.');
177187
});
178188
// awaitReply();
179-
pyshell.send(makeKnockKnockMessage('Knock, knock.'));
180-
flushStdIn();
181-
182-
function flushStdIn() {
183-
// pyshell.stdin.write(os.EOF);
184-
pyshell.pauseInput();
185-
// pyshell.flushInput();
186-
}
189+
pyshell.send(makeKnockKnockMessage(outgoingMessages[0]));
187190

188191
function endAndAssert() {
189192
pyshell.end(function (err) {
190193
if (err) {
191194
return done(err);
192195
}
193-
var outputs = output.split("\n");
194-
195-
var parsedOutputs = [];
196-
outputs
197-
.slice(0, 3)
198-
.forEach(function(outputRaw) {
199-
try {
200-
var parsed = JSON.parse(outputRaw);
201-
parsedOutputs.push(parsed);
202-
} catch(err) {
203-
done(err);
204-
}
205-
});
196+
console.log(outputs);
206197

207-
should(parsedOutputs[0])
208-
.eql(makeKnockKnockReply("Who's there?"),
198+
should(outputs[0])
199+
.eql(makeKnockKnockReply(incomingMessages[0]),
209200
"Correct knock-knock reply received.");
210201

211-
should(parsedOutputs[1])
212-
.eql(makeKnockKnockReply("Orange who?"),
202+
should(outputs[1])
203+
.eql(makeKnockKnockReply(incomingMessages[1]),
213204
"Correct knock-knock reply received.");
214205

215-
should(parsedOutputs[2])
216-
.eql(makeKnockKnockReply("Ha ha."),
206+
should(outputs[2])
207+
.eql(makeKnockKnockReply(incomingMessages[1]),
217208
"Correct knock-knock reply received.");
218209

219210
done();

0 commit comments

Comments
 (0)