comparison planemo/lib/python3.7/site-packages/urllib3/packages/backports/makefile.py @ 1:56ad4e20f292 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:32:28 -0400
parents
children
comparison
equal deleted inserted replaced
0:d30785e31577 1:56ad4e20f292
1 # -*- coding: utf-8 -*-
2 """
3 backports.makefile
4 ~~~~~~~~~~~~~~~~~~
5
6 Backports the Python 3 ``socket.makefile`` method for use with anything that
7 wants to create a "fake" socket object.
8 """
9 import io
10
11 from socket import SocketIO
12
13
14 def backport_makefile(
15 self, mode="r", buffering=None, encoding=None, errors=None, newline=None
16 ):
17 """
18 Backport of ``socket.makefile`` from Python 3.5.
19 """
20 if not set(mode) <= {"r", "w", "b"}:
21 raise ValueError("invalid mode %r (only r, w, b allowed)" % (mode,))
22 writing = "w" in mode
23 reading = "r" in mode or not writing
24 assert reading or writing
25 binary = "b" in mode
26 rawmode = ""
27 if reading:
28 rawmode += "r"
29 if writing:
30 rawmode += "w"
31 raw = SocketIO(self, rawmode)
32 self._makefile_refs += 1
33 if buffering is None:
34 buffering = -1
35 if buffering < 0:
36 buffering = io.DEFAULT_BUFFER_SIZE
37 if buffering == 0:
38 if not binary:
39 raise ValueError("unbuffered streams must be binary")
40 return raw
41 if reading and writing:
42 buffer = io.BufferedRWPair(raw, raw, buffering)
43 elif reading:
44 buffer = io.BufferedReader(raw, buffering)
45 else:
46 assert writing
47 buffer = io.BufferedWriter(raw, buffering)
48 if binary:
49 return buffer
50 text = io.TextIOWrapper(buffer, encoding, errors, newline)
51 text.mode = mode
52 return text