diff --git a/third_party/nailgun/README.amp b/third_party/nailgun/README.amp
index f3c552875366..9ba429cfca70 100644
--- a/third_party/nailgun/README.amp
+++ b/third_party/nailgun/README.amp
@@ -12,4 +12,9 @@ Files in this directory (copied from https://github.com/facebook/nailgun/release
- nailgun-runner (copied from ng.py)
Local Modifications:
-None.
+- nailgun-runner: [python3] Fix memoryview to work with python3
+ https://github.com/facebook/nailgun/commit/90167a65969d90094d7997e0308d9ddb1124d4b6
+- nailgun-runner: [python3] Ensure UTF8 strings are sent properly
+ https://github.com/facebook/nailgun/commit/f923d52beff7963f508b808f846f9970f97be0f6
+- nailgun-runner: [python3] Remove bytes -> str conversion
+ https://github.com/facebook/nailgun/commit/878f95db2ec6cb3f69144ec456a4b3fa3f1eb2f9
diff --git a/third_party/nailgun/nailgun-runner b/third_party/nailgun/nailgun-runner
index d4705a09173e..a87ccf641a96 100755
--- a/third_party/nailgun/nailgun-runner
+++ b/third_party/nailgun/nailgun-runner
@@ -25,9 +25,10 @@ import select
import socket
import struct
import sys
+from io import BytesIO
from threading import Condition, Event, Thread, RLock
-is_py2 = sys.version[0] == "2"
+is_py2 = sys.version_info[0] == 2
if is_py2:
import Queue as Queue
import __builtin__ as builtin
@@ -45,11 +46,6 @@ else:
return bytes(s, "utf-8")
-def bytes_to_str(bytes_to_convert):
- """Version independent way of converting bytes to string."""
- return bytes_to_convert if is_py2 else bytes_to_convert.decode("utf-8")
-
-
# @author Marty Lamb
# @author Pete Kirkham (Win32 port)
# @author Sergey Balabanov, Ben Hamilton (Python port)
@@ -89,6 +85,21 @@ EVENT_STDIN_CLOSED = 1
EVENT_STDIN_EXCEPTION = 2
+def compat_memoryview_py2(buf):
+ return memoryview(buf)
+
+
+def compat_memoryview_py3(buf):
+ return memoryview(buf).cast("c")
+
+
+# memoryview in python3, while wrapping ctypes.create_string_buffer has problems with
+# that type's default format (ic", header_buf, 0, len(buf), chunk_type)
bbuf = to_bytes(buf)
+ struct.pack_into(">ic", header_buf, 0, len(bbuf), chunk_type)
# these chunk types are not required for server to accept and process and server may terminate
# any time without waiting for them