Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from ctypes import *
- import sys
- # $ foo='abcdéf' python3 getenv.py foo
- # abcdéf
- # Even if your system is not actually UTF-8, this will
- # work--you may get mojibake when you try to print, but the
- # original bytes will be recoverable to pass on to another
- # ctypes function.
- class Utf8ifier(object):
- @classmethod
- def from_param(cls, value):
- return value.encode('utf-8', 'surrogateescape')
- def utf8decoder(value, func, args):
- return value.decode('utf-8', 'surrogateescape')
- libc = cdll.LoadLibrary('libSystem.dylib')
- libc.getenv.argtypes = [Utf8ifier]
- libc.getenv.restype = c_char_p
- libc.getenv.errcheck = utf8decoder
- ret = libc.getenv(sys.argv[1])
- print(ret)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement