| | 1206 | try: |
|---|
| | 1207 | import fcntl |
|---|
| | 1208 | except ImportError: |
|---|
| | 1209 | try: |
|---|
| | 1210 | from ctypes import windll, WinError |
|---|
| | 1211 | except ImportError: |
|---|
| | 1212 | def prevent_socket_inheritance(sock): |
|---|
| | 1213 | """Dummy function, since neither fcntl nor ctypes are available.""" |
|---|
| | 1214 | pass |
|---|
| | 1215 | else: |
|---|
| | 1216 | def prevent_socket_inheritance(sock): |
|---|
| | 1217 | """Mark the given socket fd as non-inheritable (Windows).""" |
|---|
| | 1218 | if not windll.kernel32.SetHandleInformation(sock.fileno(), 1, 0): |
|---|
| | 1219 | raise WinError() |
|---|
| | 1220 | else: |
|---|
| | 1221 | def prevent_socket_inheritance(sock): |
|---|
| | 1222 | """Mark the given socket fd as non-inheritable (POSIX).""" |
|---|
| | 1223 | fd = sock.fileno() |
|---|
| | 1224 | old_flags = fcntl.fcntl(fd, fcntl.F_GETFD) |
|---|
| | 1225 | fcntl.fcntl(fd, fcntl.F_SETFD, old_flags | fcntl.FD_CLOEXEC) |
|---|
| | 1226 | |
|---|