Skip to content

Commit

Permalink
Safely initialise libc members, not all calls are available on all pl…
Browse files Browse the repository at this point in the history
…atforms
  • Loading branch information
tehmaze committed Sep 7, 2012
1 parent 59c6a91 commit 169b548
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions getent/libc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
# Map libc function calls
for name, struct in GENERATE_MAP.iteritems():
base = '%sent' % (name,)
globals()['set%s' % (base,)] = getattr(libc, 'set%s' % (base,))
globals()['get%s' % (base,)] = getattr(libc, 'get%s' % (base,))
globals()['get%s' % (base,)].restype = POINTER(struct)
globals()['end%s' % (base,)] = getattr(libc, 'end%s' % (base,))
globals()['set%s' % (base,)] = getattr(libc, 'set%s' % (base,), None)
globals()['get%s' % (base,)] = getattr(libc, 'get%s' % (base,), None)
if globals()['get%s' % (base,)] is not None:
globals()['get%s' % (base,)].restype = POINTER(struct)
globals()['end%s' % (base,)] = getattr(libc, 'end%s' % (base,), None)
__all__.append('set%s' % (base,))
__all__.append('get%s' % (base,))
__all__.append('end%s' % (base,))
Expand Down Expand Up @@ -77,6 +78,10 @@
getservbyport = libc.getservbyport
getservbyport.argtypes = (c_int, c_char_p)
getservbyport.restype = POINTER(headers.ServiceStruct)
getspnam = libc.getspnam
getspnam.argtypes = (c_char_p,)
getspnam.restype = POINTER(headers.ShadowStruct)
# Not supported on all platforms
try:
getspnam = libc.getspnam
getspnam.argtypes = (c_char_p,)
getspnam.restype = POINTER(headers.ShadowStruct)
except AttributeError:
getspnam = None

0 comments on commit 169b548

Please sign in to comment.