annotate src/testdir/test_channel.py @ 7916:54602dcac207 v7.4.1254

commit https://github.com/vim/vim/commit/3b05b135e3ee4cfd59983fd63461e8f7642c1713 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Feb 3 23:25:07 2016 +0100 patch 7.4.1254 Problem: Opening a second channel causes a crash. (Ken Takata) Solution: Don't re-allocate the array with channels.
author Christian Brabandt <cb@256bit.org>
date Wed, 03 Feb 2016 23:30:04 +0100
parents 35973ce58c84
children ce5a7a613867
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1 #!/usr/bin/python
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
2 #
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
3 # Server that will accept connections from a Vim channel.
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
4 # Run this server and then in Vim you can open the channel:
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
5 # :let handle = ch_open('localhost:8765', 'json')
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
6 #
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
7 # Then Vim can send requests to the server:
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
8 # :let response = ch_sendexpr(handle, 'hello!')
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
9 #
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
10 # See ":help channel-demo" in Vim.
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
11 #
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
12 # This requires Python 2.6 or later.
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
13
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
14 from __future__ import print_function
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
15 import json
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
16 import socket
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
17 import sys
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
18 import threading
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
19
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
20 try:
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
21 # Python 3
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
22 import socketserver
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
23 except ImportError:
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
24 # Python 2
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
25 import SocketServer as socketserver
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
26
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
27 class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
28
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
29 def handle(self):
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
30 print("=== socket opened ===")
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
31 while True:
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
32 try:
7914
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
33 received = self.request.recv(4096).decode('utf-8')
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
34 except socket.error:
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
35 print("=== socket error ===")
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
36 break
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
37 except IOError:
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
38 print("=== socket closed ===")
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
39 break
7914
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
40 if received == '':
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
41 print("=== socket closed ===")
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
42 break
7914
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
43 print("received: {}".format(received))
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
44
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
45 # We may receive two messages at once. Take the part up to the
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
46 # matching "]" (recognized by finding "][").
7914
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
47 todo = received
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
48 while todo != '':
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
49 splitidx = todo.find('][')
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
50 if splitidx < 0:
7914
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
51 used = todo
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
52 todo = ''
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
53 else:
7914
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
54 used = todo[:splitidx + 1]
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
55 todo = todo[splitidx + 1:]
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
56 if used != received:
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
57 print("using: {}".format(used))
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
58
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
59 try:
7914
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
60 decoded = json.loads(used)
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
61 except ValueError:
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
62 print("json decoding failed")
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
63 decoded = [-1, '']
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
64
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
65 # Send a response if the sequence number is positive.
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
66 if decoded[0] >= 0:
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
67 if decoded[1] == 'hello!':
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
68 # simply send back a string
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
69 response = "got it"
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
70 elif decoded[1] == 'make change':
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
71 # Send two ex commands at the same time, before replying to
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
72 # the request.
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
73 cmd = '["ex","call append(\\"$\\",\\"added1\\")"]'
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
74 cmd += '["ex","call append(\\"$\\",\\"added2\\")"]'
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
75 print("sending: {}".format(cmd))
7916
54602dcac207 commit https://github.com/vim/vim/commit/3b05b135e3ee4cfd59983fd63461e8f7642c1713
Christian Brabandt <cb@256bit.org>
parents: 7914
diff changeset
76 self.request.sendall(cmd.encode('utf-8'))
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
77 response = "ok"
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
78 elif decoded[1] == 'eval-works':
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
79 # Send an eval request. We ignore the response.
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
80 cmd = '["eval","\\"foo\\" . 123", -1]'
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
81 print("sending: {}".format(cmd))
7916
54602dcac207 commit https://github.com/vim/vim/commit/3b05b135e3ee4cfd59983fd63461e8f7642c1713
Christian Brabandt <cb@256bit.org>
parents: 7914
diff changeset
82 self.request.sendall(cmd.encode('utf-8'))
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
83 response = "ok"
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
84 elif decoded[1] == 'eval-fails':
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
85 # Send an eval request that will fail.
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
86 cmd = '["eval","xxx", -2]'
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
87 print("sending: {}".format(cmd))
7916
54602dcac207 commit https://github.com/vim/vim/commit/3b05b135e3ee4cfd59983fd63461e8f7642c1713
Christian Brabandt <cb@256bit.org>
parents: 7914
diff changeset
88 self.request.sendall(cmd.encode('utf-8'))
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
89 response = "ok"
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
90 elif decoded[1] == 'eval-result':
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
91 # Send back the last received eval result.
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
92 response = last_eval
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
93 elif decoded[1] == '!quit!':
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
94 # we're done
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
95 sys.exit(0)
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
96 elif decoded[1] == '!crash!':
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
97 # Crash!
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
98 42 / 0
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
99 else:
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
100 response = "what?"
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
101
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
102 encoded = json.dumps([decoded[0], response])
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
103 print("sending: {}".format(encoded))
7916
54602dcac207 commit https://github.com/vim/vim/commit/3b05b135e3ee4cfd59983fd63461e8f7642c1713
Christian Brabandt <cb@256bit.org>
parents: 7914
diff changeset
104 self.request.sendall(encoded.encode('utf-8'))
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
105
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
106 # Negative numbers are used for "eval" responses.
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
107 elif decoded[0] < 0:
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
108 last_eval = decoded
7906
ea1fd8d750a6 commit https://github.com/vim/vim/commit/fcb1e3d16832ce06da0dc38ecb7ab9aaa3ee4383
Christian Brabandt <cb@256bit.org>
parents: 7902
diff changeset
109
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
110 class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
111 pass
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
112
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
113 if __name__ == "__main__":
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
114 HOST, PORT = "localhost", 0
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
115
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
116 server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
117 ip, port = server.server_address
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
118
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
119 # Start a thread with the server -- that thread will then start one
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
120 # more thread for each request
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
121 server_thread = threading.Thread(target=server.serve_forever)
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
122
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
123 # Exit the server thread when the main thread terminates
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
124 server_thread.daemon = True
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
125 server_thread.start()
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
126
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
127 # Write the port number in Xportnr, so that the test knows it.
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
128 f = open("Xportnr", "w")
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
129 f.write("{}".format(port))
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
130 f.close()
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
131
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
132 # Block here
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
133 print("Listening on port {}".format(port))
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
134 server.serve_forever()