mirror of
				https://github.com/vim/vim.git
				synced 2025-10-31 09:57:14 -04:00 
			
		
		
		
	patch 7.4.1234
Problem: Demo server only runs with Python 2. Solution: Make it run with Python 3 as well. (Ken Takata)
This commit is contained in:
		| @@ -11,49 +11,59 @@ | |||||||
| # | # | ||||||
| # See ":help channel-demo" in Vim. | # See ":help channel-demo" in Vim. | ||||||
|  |  | ||||||
| import SocketServer | from __future__ import print_function | ||||||
| import json | import json | ||||||
| import socket | import socket | ||||||
| import sys | import sys | ||||||
| import threading | import threading | ||||||
|  |  | ||||||
|  | try: | ||||||
|  |     # Python 3 | ||||||
|  |     import socketserver | ||||||
|  | except ImportError: | ||||||
|  |     # Python 2 | ||||||
|  |     import SocketServer as socketserver | ||||||
|  |  | ||||||
| thesocket = None | thesocket = None | ||||||
|  |  | ||||||
| class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler): | class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler): | ||||||
|  |  | ||||||
|     def handle(self): |     def handle(self): | ||||||
|         print "=== socket opened ===" |         print("=== socket opened ===") | ||||||
|         global thesocket |         global thesocket | ||||||
|         thesocket = self.request |         thesocket = self.request | ||||||
|         while True: |         while True: | ||||||
|             try: |             try: | ||||||
|                 data = self.request.recv(4096) |                 data = self.request.recv(4096).decode('utf-8') | ||||||
|             except socket.error: |             except socket.error: | ||||||
|                 print "=== socket error ===" |                 print("=== socket error ===") | ||||||
|                 break |                 break | ||||||
|             except IOError: |             except IOError: | ||||||
|                 print "=== socket closed ===" |                 print("=== socket closed ===") | ||||||
|                 break |                 break | ||||||
|             if data == '': |             if data == '': | ||||||
|                 print "=== socket closed ===" |                 print("=== socket closed ===") | ||||||
|                 break |                 break | ||||||
|             print "received: {}".format(data) |             print("received: {}".format(data)) | ||||||
|             try: |             try: | ||||||
|                 decoded = json.loads(data) |                 decoded = json.loads(data) | ||||||
|             except ValueError: |             except ValueError: | ||||||
|                 print "json decoding failed" |                 print("json decoding failed") | ||||||
|                 decoded = [0, ''] |                 decoded = [-1, ''] | ||||||
|  |  | ||||||
|             if decoded[1] == 'hello!': |             # Send a response if the sequence number is positive. | ||||||
|                 response = "got it" |             # Negative numbers are used for "eval" responses. | ||||||
|             else: |             if decoded[0] >= 0: | ||||||
|                 response = "what?" |                 if decoded[1] == 'hello!': | ||||||
|             encoded = json.dumps([decoded[0], response]) |                     response = "got it" | ||||||
|             print "sending {}".format(encoded) |                 else: | ||||||
|             self.request.sendall(encoded) |                     response = "what?" | ||||||
|  |                 encoded = json.dumps([decoded[0], response]) | ||||||
|  |                 print("sending {}".format(encoded)) | ||||||
|  |                 self.request.sendall(encoded.encode('utf-8')) | ||||||
|         thesocket = None |         thesocket = None | ||||||
|  |  | ||||||
| class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): | class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): | ||||||
|     pass |     pass | ||||||
|  |  | ||||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||||
| @@ -69,19 +79,19 @@ if __name__ == "__main__": | |||||||
|     # Exit the server thread when the main thread terminates |     # Exit the server thread when the main thread terminates | ||||||
|     server_thread.daemon = True |     server_thread.daemon = True | ||||||
|     server_thread.start() |     server_thread.start() | ||||||
|     print "Server loop running in thread: ", server_thread.name |     print("Server loop running in thread: ", server_thread.name) | ||||||
|  |  | ||||||
|     print "Listening on port {}".format(PORT) |     print("Listening on port {}".format(PORT)) | ||||||
|     while True: |     while True: | ||||||
|         typed = sys.stdin.readline() |         typed = sys.stdin.readline() | ||||||
|         if "quit" in typed: |         if "quit" in typed: | ||||||
|             print "Goodbye!" |             print("Goodbye!") | ||||||
|             break |             break | ||||||
|         if thesocket is None: |         if thesocket is None: | ||||||
|             print "No socket yet" |             print("No socket yet") | ||||||
|         else: |         else: | ||||||
|             print "sending {}".format(typed) |             print("sending {}".format(typed)) | ||||||
|             thesocket.sendall(typed) |             thesocket.sendall(typed.encode('utf-8')) | ||||||
|  |  | ||||||
|     server.shutdown() |     server.shutdown() | ||||||
|     server.server_close() |     server.server_close() | ||||||
|   | |||||||
| @@ -742,6 +742,8 @@ static char *(features[]) = | |||||||
|  |  | ||||||
| static int included_patches[] = | static int included_patches[] = | ||||||
| {   /* Add new patch number below this line */ | {   /* Add new patch number below this line */ | ||||||
|  | /**/ | ||||||
|  |     1234, | ||||||
| /**/ | /**/ | ||||||
|     1233, |     1233, | ||||||
| /**/ | /**/ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user