-
Notifications
You must be signed in to change notification settings - Fork 3
Ctype wrapper is used to call getnetgrent for #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
the lists of host
| @@ -0,0 +1,31 @@ | |||
|
|
|||
| from ctypes import * | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use '*' imports in libraries. This has multiple disadvantages.
|
|
||
| from ctypes import * | ||
|
|
||
| def find_hostname(netgroup): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer get instead of find and the plural because multiple hostnames are returned:
| def find_hostname(netgroup): | |
| def get_hostnames(netgroup): |
| from ctypes import * | ||
|
|
||
| def find_hostname(netgroup): | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a doctring that describes what the function does, what are the inputs and outputs and any special or surprising behavior one needs to know to use it. Best would be to also add an example.
| libc = CDLL("libc.so.6") | ||
| if (libc != None): | ||
| pass | ||
| else: | ||
| libc = CDLL("libc.so.7") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a better way that going through a fixed list of library names? Why are "libc.so.5" or libc.so.8 not included in that list?
Also it is probably better to not do the lookup at every function call but only once at module load time.
|
|
||
|
|
||
| hosts = None | ||
| if libc.setnetgrent(netgroup): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One needs to check that netgroup is of the correct type, otherwise this might cause a segfault.
|
|
||
| hosts = None | ||
| if libc.setnetgrent(netgroup): | ||
| # returns 1 if successful and 0 otherwise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment should be above or next to the line it refers to.
| while libc.getnetgrent(byref(host), byref(user), byref(domain)): | ||
| if host: | ||
| # check is host is not a NULL pointer | ||
| hosts.append(host.value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this always safe? What about interleaving calls from multiple threads?
Should this return str instead of bytes?
| # check is host is not a NULL pointer | ||
| hosts.append(host.value) | ||
|
|
||
| return(hosts) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The difference between None and [] might be too subtle. Maybe throw an error instead of returning None?
|
|
||
| hosts = find_hostname(b"a3p62-hosts") | ||
| print(hosts) | ||
| print("done") No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| hosts = find_hostname(b"a3p62-hosts") | |
| print(hosts) | |
| print("done") |
the lists of host