Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2014
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. from ctypes import *
  2. import sys
  3.  
  4. # $ foo='abcdéf' python3 getenv.py foo
  5. # abcdéf
  6.  
  7. # Even if your system is not actually UTF-8, this will
  8. # work--you may get mojibake when you try to print, but the
  9. # original bytes will be recoverable to pass on to another
  10. # ctypes function.
  11.  
  12. class Utf8ifier(object):
  13.     @classmethod
  14.     def from_param(cls, value):
  15.         return value.encode('utf-8', 'surrogateescape')
  16.  
  17. def utf8decoder(value, func, args):
  18.     return value.decode('utf-8', 'surrogateescape')
  19.  
  20. libc = cdll.LoadLibrary('libSystem.dylib')
  21. libc.getenv.argtypes = [Utf8ifier]
  22. libc.getenv.restype = c_char_p
  23. libc.getenv.errcheck = utf8decoder
  24.  
  25. ret = libc.getenv(sys.argv[1])
  26. print(ret)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement