[issue32199] uuid.getnode() should return the MAC address on Android

Barry A. Warsaw report at bugs.python.org
Wed Dec 6 10:11:56 EST 2017


Barry A. Warsaw <barry at python.org> added the comment:

On Dec 6, 2017, at 02:06, Xavier de Gaye <report at bugs.python.org> wrote:
> 
> Whatever the change made to fix this issue, it is not possible to add a test case for this change.

Even with say, exception raising mocks for the getters?

> So following the suggestion made by Barry in PR 4696, we can add (in another issue) a new keyword parameter to getnode() named 'methods' whose value may be None (the default, meaning try all the known methods) or a tuple containing a subset of the following methods ('unix', 'ifconfig', 'ip', 'arp', 'lanscan', 'netstat',  'random') that would raise an exception if the value cannot be obtained using one of the requested method tried in the requested order. This would also improve the documentation on the methods getnode() is using. Then if we decide to make the change for 'ip link' in the current issue, one can add a test case that would first test for the avaibility of the ip command and if the command exists would fail if getnode(methods=('ip',)) raises an exception.

I am thinking about this slightly differently.

What if getnode() accepted a `handler` argument and the code was changed to something like this:

1 file changed, 4 insertions(+), 2 deletions(-)
Lib/uuid.py | 6 ++++--

modified   Lib/uuid.py
@@ -656,7 +656,7 @@ def _random_getnode():

 _node = None

-def getnode():
+def getnode(handler=None):
     """Get the hardware address as a 48-bit positive integer.

     The first time this runs, it may launch a separate program, which could
@@ -677,7 +677,9 @@ def getnode():
     for getter in getters + [_random_getnode]:
         try:
             _node = getter()
-        except:
+        except Exception as error:
+            if handler is not None:
+                handler(getter, error)
             continue
         if _node is not None:
             return _node

`handler` could log some diagnostics, reraise the exception, raise StopIteration, etc.  Then we could use that in the test suite too, because we could mock a getter to raise an exception and then pass in a handler that verified the exception was raised with the expected getter.

(Maybe we spell `handler` as `error_handler`.)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32199>
_______________________________________


More information about the Python-bugs-list mailing list