Skip to content

Instantly share code, notes, and snippets.

@JamesTheAwesomeDude
Last active November 8, 2020 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesTheAwesomeDude/a1ee5ff451e6cd101fbcede292b2c095 to your computer and use it in GitHub Desktop.
Save JamesTheAwesomeDude/a1ee5ff451e6cd101fbcede292b2c095 to your computer and use it in GitHub Desktop.
import fcntl
def modfl(fd, flags):
'''Adds all given positive flags to, and removes all given negative flags from, the given file descriptor'''
# e.g.: modfl(f, (+os.O_SYNC, -os.O_NONBLOCK))
# would make f synchronous and blocking
#OR-in positive flags; NAND-out negative ones
ins = lambda fl, x: (fl | x) if x >= 0 else (fl &~ -x)
# 1. Get the current flag field
cur = fcntl.fcntl(fd,fcntl.F_GETFL)
# 2. Calculate the new flag field
new = reduce(ins, flags, cur)
# 3. Apply the new flag field
return fcntl.fcntl(fd, fcntl.F_SETFL, new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment