diff options
author | James Taylor <user234683@users.noreply.github.com> | 2019-06-01 23:23:18 -0700 |
---|---|---|
committer | James Taylor <user234683@users.noreply.github.com> | 2019-06-02 02:25:39 -0700 |
commit | af9c4e0554c3475d959014e9e7cef78eff88afa5 (patch) | |
tree | ced7a2ccd6d0ab8e9d251dcd61bba09f3bb87074 /python/urllib3/packages/backports/makefile.py | |
parent | 3905e7e64059b45479894ba1fdfb0ef9cef64475 (diff) | |
parent | 9f93b9429c77e631972186049fbc7518e2cf5d4b (diff) | |
download | yt-local-af9c4e0554c3475d959014e9e7cef78eff88afa5.tar.lz yt-local-af9c4e0554c3475d959014e9e7cef78eff88afa5.tar.xz yt-local-af9c4e0554c3475d959014e9e7cef78eff88afa5.zip |
Bring up to date with master
Diffstat (limited to 'python/urllib3/packages/backports/makefile.py')
-rw-r--r-- | python/urllib3/packages/backports/makefile.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/python/urllib3/packages/backports/makefile.py b/python/urllib3/packages/backports/makefile.py new file mode 100644 index 0000000..740db37 --- /dev/null +++ b/python/urllib3/packages/backports/makefile.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +""" +backports.makefile +~~~~~~~~~~~~~~~~~~ + +Backports the Python 3 ``socket.makefile`` method for use with anything that +wants to create a "fake" socket object. +""" +import io + +from socket import SocketIO + + +def backport_makefile(self, mode="r", buffering=None, encoding=None, + errors=None, newline=None): + """ + Backport of ``socket.makefile`` from Python 3.5. + """ + if not set(mode) <= {"r", "w", "b"}: + raise ValueError( + "invalid mode %r (only r, w, b allowed)" % (mode,) + ) + writing = "w" in mode + reading = "r" in mode or not writing + assert reading or writing + binary = "b" in mode + rawmode = "" + if reading: + rawmode += "r" + if writing: + rawmode += "w" + raw = SocketIO(self, rawmode) + self._makefile_refs += 1 + if buffering is None: + buffering = -1 + if buffering < 0: + buffering = io.DEFAULT_BUFFER_SIZE + if buffering == 0: + if not binary: + raise ValueError("unbuffered streams must be binary") + return raw + if reading and writing: + buffer = io.BufferedRWPair(raw, raw, buffering) + elif reading: + buffer = io.BufferedReader(raw, buffering) + else: + assert writing + buffer = io.BufferedWriter(raw, buffering) + if binary: + return buffer + text = io.TextIOWrapper(buffer, encoding, errors, newline) + text.mode = mode + return text |