Commit Graph

62 Commits

Author SHA1 Message Date
Ijtaba Hussain 9d582250d8
gh-103186: Fix or catch 'extra' stderr output from unittests (#103196)
Reduce test noise by fixing or catching and testing stderr messages from individual tests.

test_cmd_line_script.test_script_as_dev_fd calls spawn_python and hence subprocess.Popen with incompatible arguments. On POSIX, pass_fds forces close_fds to be True (subprocess.py line 848). Correct the call.

test_uuid.test_cli_namespace_required_for_uuid3: when the namespace is omitted, uuid.main calls argparse.Argument_Parser.error, which prints to stderr before calling sys.exit, which raises SystemExit. Unittest assertRaises catches the exception but not the previous output. Catch the output and test it.

test_warnings.test_catchwarnings_with_simplefilter_error similarly prints before raising. Catch the output and test it.
---------

Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
2023-07-10 16:29:03 -04:00
MonadChains 413b7db8a4
gh-94684: uuid: support bytes in the name argument to uuid3/5 (#94709)
RFC 4122 does not specify that name should be a string, so for completness the functions should also support a name given as a raw byte sequence.
2023-03-23 17:42:43 -06:00
achhina 952a1d9cc9
GH-88597: Rename uuid's new CLI args to be in line with uuidgen. (#101248)
this way they match an existing uuidgen command line tool.
2023-01-25 09:39:42 -08:00
achhina 95f5b05a8c
GH-88597: Added command line interface to UUID module. (#99463)
The `uuid` module now supports command line usage.

```python
❯ ./python.exe -m uuid             
5f2d57b1-90e8-417c-ba5d-69b9b6f74289

❯ ./python.exe -m uuid -h          
usage: uuid.py [-h] [-u {uuid1,uuid3,uuid4,uuid5}] [-ns NAMESPACE] [-n NAME]
...
```
2023-01-21 22:59:31 -08:00
Christian Heimes 91e33ac3d0
bpo-40280: Add requires_fork test helper (GH-30622) 2022-01-16 22:52:43 +01:00
Ethan Furman a02cb474f9
bpo-38659: [Enum] add _simple_enum decorator (GH-25497)
add:

* `_simple_enum` decorator to transform a normal class into an enum
* `_test_simple_enum` function to compare
* `_old_convert_` to enable checking `_convert_` generated enums

`_simple_enum` takes a normal class and converts it into an enum:

    @simple_enum(Enum)
    class Color:
        RED = 1
        GREEN = 2
        BLUE = 3

`_old_convert_` works much like` _convert_` does, using the original logic:

    # in a test file
    import socket, enum
    CheckedAddressFamily = enum._old_convert_(
            enum.IntEnum, 'AddressFamily', 'socket',
            lambda C: C.isupper() and C.startswith('AF_'),
            source=_socket,
            )

`_test_simple_enum` takes a traditional enum and a simple enum and
compares the two:

    # in the REPL or the same module as Color
    class CheckedColor(Enum):
        RED = 1
        GREEN = 2
        BLUE = 3

    _test_simple_enum(CheckedColor, Color)

    _test_simple_enum(CheckedAddressFamily, socket.AddressFamily)

Any important differences will raise a TypeError
2021-04-21 10:20:44 -07:00
Ethan Furman 503cdc7c12
Revert "bpo-38659: [Enum] add _simple_enum decorator (GH-25285)" (GH-25476)
This reverts commit dbac8f40e8.
2021-04-19 19:12:24 -07:00
Ethan Furman dbac8f40e8
bpo-38659: [Enum] add _simple_enum decorator (GH-25285)
add:

_simple_enum decorator to transform a normal class into an enum
_test_simple_enum function to compare
_old_convert_ to enable checking _convert_ generated enums
_simple_enum takes a normal class and converts it into an enum:

@simple_enum(Enum)
class Color:
    RED = 1
    GREEN = 2
    BLUE = 3

_old_convert_ works much like _convert_ does, using the original logic:

# in a test file
import socket, enum
CheckedAddressFamily = enum._old_convert_(
        enum.IntEnum, 'AddressFamily', 'socket',
        lambda C: C.isupper() and C.startswith('AF_'),
        source=_socket,
        )

test_simple_enum takes a traditional enum and a simple enum and
compares the two:

# in the REPL or the same module as Color
class CheckedColor(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

_test_simple_enum(CheckedColor, Color)

_test_simple_enum(CheckedAddressFamily, socket.AddressFamily)

Any important differences will raise a TypeError
2021-04-19 18:04:53 -07:00
pxinwr 98a5417193
bpo-41439: Skip test_ssl and test_uuid tests if fork() is not supported (GH-21684) 2020-12-09 00:20:19 +01:00
Hai Shi 883bc63833
bpo-40275: Use new test.support helper submodules in tests (GH-21314) 2020-07-06 11:12:49 +02:00
Steve Dower d6b727e2c9
bpo-40501: Replace ctypes code in uuid with native module (GH-19948) 2020-05-12 23:32:32 +01:00
Victor Stinner 57572b103e
bpo-40443: Remove unused imports in tests (GH-19805) 2020-04-30 01:48:37 +02:00
Victor Stinner 278c1e159c
bpo-40094: Add test.support.wait_process() (GH-19254)
Moreover, the following tests now check the child process exit code:

* test_os.PtyTests
* test_mailbox.test_lock_conflict()
* test_tempfile.test_process_awareness()
* test_uuid.testIssue8621()
* multiprocessing resource tracker tests
2020-03-31 20:08:12 +02:00
Victor Stinner ebf6bb9f5e
bpo-39991: Enhance uuid parser for MAC address (GH-19045)
Reject valid IPv6 addresses which doesn't contain "::" but have
a length of 17 characters.
2020-03-17 18:36:44 +01:00
Victor Stinner eb886db1e9
bpo-39991: uuid._netstat_getnode() ignores IPv6 addresses (GH-19043)
uuid.getnode() now skips IPv6 addresses with the same string length
than a MAC address (17 characters): only use MAC addresses.
2020-03-17 15:51:42 +01:00
Pablo Galindo 293dd23477
Remove binding of captured exceptions when not used to reduce the chances of creating cycles (GH-17246)
Capturing exceptions into names can lead to reference cycles though the __traceback__ attribute of the exceptions in some obscure cases that have been reported previously and fixed individually. As these variables are not used anyway, we can remove the binding to reduce the chances of creating reference cycles.

See for example GH-13135
2019-11-19 21:34:03 +00:00
Michael Felt 0bcbfa43d5 bpo-28009: Fix uuid.uuid1() and uuid.get_node() on AIX (GH-8672) 2019-09-26 22:43:15 +03:00
Michael Felt 3a1d50e7e5 bpo-28009: Fix uuid SkipUnless logic to be based on platform programs capable of introspection (GH-12777)
uuid could try fallback methods that had no chance of working on a particular
platform, and this could cause spurious test failures, as well as degraded
performance as fallback options were tried and failed.

This fixes both the uuid module and its test's SkipUnless logic to use a
prefiltered list of techniques that may at least potentially work on that platform.

Patch by Michael Felt (aixtools).
2019-06-16 01:52:29 +10:00
David H f1d8e7cf17 bpo-35701: Added __weakref__ slot to uuid.UUID (GH-11570)
Added test for weakreferencing a uuid.UUID object.
2019-01-17 13:16:51 +01:00
Victor Stinner 62a68b762a
bpo-31784: Use time.time_ns() in uuid.uuid1() (GH-11189)
uuid.uuid1() now calls time.time_ns() rather than
int(time.time() * 1e9). Replace also int(nanoseconds/100)
with nanoseconds // 100. Add an unit test.
2018-12-18 11:45:13 +01:00
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి) 70f61b933f bpo-35202: Remove unused imports in Lib directory (GH-10450) 2018-11-15 21:59:19 +09:00
Tal Einat 54752533b2
bpo-30977: rework code changes according to post-merge code review (GH-9106)
also mention the change and its consequences in What's New
2018-09-10 16:11:04 +03:00
Tal Einat 3e2b29dccc
bpo-30977: make uuid.UUID use __slots__ (GH-9078)
Co-Authored-By: Wouter Bolsterlee.
2018-09-06 14:34:25 +03:00
Bo Bayles 6b273f7f40 bpo-32502: Discard 64-bit (and other invalid) hardware addresses (#5254) 2018-01-23 20:11:44 -05:00
Serhiy Storchaka e69fbb6a56
Fix a regression in uuid added in bpo-32107. (#4677)
uuid.get_node() always must return a stable result.
Also added a test for non-reproducibility of _random_getnode().
Original patch by Xavier de Gaye.
2017-12-04 11:51:55 +02:00
Barry Warsaw 23df2d1304
bpo-32107 - Improve MAC address calculation and fix test_uuid.py (#4600)
``uuid.getnode()`` now preferentially returns universally administered MAC addresses if available, over locally administered MAC addresses.  This makes a better guarantee for global uniqueness of UUIDs returned from ``uuid.uuid1()``.  If only locally administered MAC addresses are available, the first such one found is returned.

Also improve internal code style by being explicit about ``return None`` rather than falling off the end of the function.

Improve the test robustness.
2017-11-28 17:26:04 -05:00
Victor Stinner c9409f7c45
Revert "bpo-32107 - Better merge of #4494 (#4576)" (#4593)
This reverts commit 9522a218f7.
2017-11-28 00:30:21 +01:00
Barry Warsaw 9522a218f7
bpo-32107 - Better merge of #4494 (#4576)
Improve UUID1 MAC address calculation and related tests.

There are two bits in the MAC address that are relevant to UUID1.  The first is the locally administered vs. universally administered bit (second least significant of the first octet).   Physical network interfaces such as ethernet ports and wireless adapters will always be universally administered, but some interfaces --such as the interface that MacBook Pros communicate with their Touch Bars-- are locally administered.  The former are guaranteed to be globally unique, while the latter are demonstrably *not* globally unique and are in fact the same on every MBP with a Touch Bar.  With this bit is set, the MAC is locally administered; with it unset it is universally administered.

The other bit is the multicast bit (least significant bit of the first octet).  When no other MAC address can be found, RFC 4122 mandates that a random 48-bit number be generated.  This randomly generated number *must* have the multicast bit set.

The improvements in uuid.py include:

* Preferentially return a universally administered MAC address, falling back to a locally administered address if none of the former can be found.
* Improve several coding style issues, such as adding explicit returns of None, using a more readable bitmask pattern, and assuming that the ultimate fallback, random MAC generation will not fail (and propagating any exception there instead of swallowing them).

Improvements in test_uuid.py include:

* Always testing the calculated MAC for universal administration, unless explicitly disabled (i.e. for the random case), or implicitly disabled due to running in the Travis environment.  Travis test machines have *no* universally administered MAC address at the time of this writing.
2017-11-27 14:40:10 -05:00
Antoine Pitrou a106aec2ed bpo-11063, bpo-20519: avoid ctypes and improve import time for uuid (#3796)
bpo-11063, bpo-20519: avoid ctypes and improve import time for uuid.
2017-09-28 23:03:06 +02:00
Victor Stinner c209b70d61 bpo-29925: Skip test_uuid1_safe() on OS X Tiger (#971) 2017-04-19 13:01:03 +02:00
Benjamin Peterson afbb5d1cbd require uuid_generate_time_safe for all tests of it (#390)
The way mocking is written in these tests, we need to have the underlying function around.
2017-03-01 23:34:19 -08:00
Benjamin Peterson 0b8432538a correct check for _uuid_generate_time (#388)
If ctypes is not available, _uuid_generate_time will be None not its restype attribute.
2017-03-01 21:53:14 -08:00
Barry Warsaw 8c130d7f81 bpo-22807: Expose platform UUID generation safety information. (#138)
bpo-22807: Expose platform UUID generation safety information.
2017-02-18 15:45:49 -05:00
Berker Peksag 6b5e4a86a7 Issue #26267: Improve uuid.UUID documentation
* Document how comparison of UUID objects work
* Document str(uuid) returns the braceless standard form
* Add a test for comparison of a UUID object with a non-UUID object

Patch by Ammar Askar.
2016-12-31 20:08:16 +03:00
Berker Peksag e2021f2ecd Issue #12813: uuid.uuid4() no longer depends on ctypes
uuid.uuid4() always uses os.urandom() after 756d040aa8e8.
2016-03-20 17:29:56 +02:00
Serhiy Storchaka 0e32ea1089 Issue #23015: Improved testing of the uuid module. 2014-12-15 12:06:22 +02:00
Serhiy Storchaka 79b81738ef Issue #23015: Improved testing of the uuid module. 2014-12-15 12:03:44 +02:00
Serhiy Storchaka ac4aa7b6aa Issue #22902: The "ip" command is now used on Linux to determine MAC address
in uuid.getnode().  Pach by Bruno Cauet.
2014-11-30 20:39:04 +02:00
Serhiy Storchaka 57b967791a Issue #17293: uuid.getnode() now determines MAC address on AIX using netstat.
Based on patch by Aivars Kalvāns.
2014-11-07 12:23:30 +02:00
Serhiy Storchaka e66bb96929 Issue #17293: uuid.getnode() now determines MAC address on AIX using netstat.
Based on patch by Aivars Kalvāns.
2014-11-07 12:19:40 +02:00
Victor Stinner b9d0199c07 Issue #22637: avoid using a shell in uuid
Replace os.popen() with subprocess.Popen() in the uuid module.
2014-10-21 22:33:10 +02:00
Serhiy Storchaka cce440fab8 Issue #19804: The test_find_mac test in test_uuid is now skipped if the
ifconfig executable is not available.
2014-01-10 15:06:59 +02:00
Serhiy Storchaka c303cfdb8a Skip test_find_mac on Windows (issue #19804).
This test requires the ifconfig executable on $PATH, /sbin/, or /usr/sbin.
2013-11-27 08:57:51 +02:00
Serhiy Storchaka 56507c7862 Issue #11508: Fixed uuid.getnode() and uuid.uuid1() on environment with
virtual interface.  Original patch by Kent Frazier.
2013-11-26 22:47:16 +02:00
Richard Oudkerk 0e547b66dc Issue #18174: Fix fd leaks in tests. 2013-06-10 16:29:19 +01:00
Serhiy Storchaka 7d15b54f37 Issue #18094: test_uuid no more reports skipped tests as passed. 2013-05-31 22:31:02 +03:00
Marc-André Lemburg 8f36af7a4c Normalize the encoding names for Latin-1 and UTF-8 to
'latin-1' and 'utf-8'.

These are optimized in the Python Unicode implementation
to result in more direct processing, bypassing the codec
registry.

Also see issue11303.
2011-02-25 15:42:01 +00:00
Ronald Oussoren ac764d3d5f Merged revisions 80784 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r80784 | ronald.oussoren | 2010-05-05 16:48:37 +0200 (Wed, 05 May 2010) | 9 lines

  The C function used by uuid.uuid4 is broken on
  OSX 10.6 in that after os.fork() the parent and
  child generate the same sequence of UUIDs.

  This patch falls back to the the Python implementation
  on OSX 10.6 or later.

  Fixes issue #8621.
........
2010-05-05 15:32:33 +00:00
Stefan Krah 6108ea8dad Merged revisions 79954 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r79954 | stefan.krah | 2010-04-11 17:15:54 +0200 (Sun, 11 Apr 2010) | 17 lines


  Fix for issues #3581, #1481 and #7650:

  1. The assumptions in check_node() were too restrictive:

       - Hardware addresses with universal_local_bit=1 are valid
         (locally administered).

       - Many of the tested functions (including uuid.getnode())
         may return valid RFC 4122 random node IDs. These are pretty
         much random 48-bit values with the multicast bit set to 1.

  2. _unixdll_getnode() calls _uuid_generate_time(), which may be None
     on some platforms. The resulting TypeError is now caught.
........
2010-04-11 16:49:20 +00:00
Georg Brandl 1d523e1ae1 #7380: Fix some str/bytearray/bytes issues in uuid docs and implementation. 2009-12-19 18:23:28 +00:00