"""Test the secrets module. As most of the functions in secrets are thin wrappers around functions defined elsewhere, we don't need to test them exhaustively. """ import secrets import unittest import string # === Unit tests === class Compare_Digest_Tests(unittest.TestCase): """Test secrets.compare_digest function.""" def test_equal(self): # Test compare_digest functionality with equal (byte/text) strings. for s in ("a", "bcd", "xyz123"): a = s*100 b = s*100 self.assertTrue(secrets.compare_digest(a, b)) self.assertTrue(secrets.compare_digest(a.encode('utf-8'), b.encode('utf-8'))) def test_unequal(self): # Test compare_digest functionality with unequal (byte/text) strings. self.assertFalse(secrets.compare_digest("abc", "abcd")) self.assertFalse(secrets.compare_digest(b"abc", b"abcd")) for s in ("x", "mn", "a1b2c3"): a = s*100 + "q" b = s*100 + "k" self.assertFalse(secrets.compare_digest(a, b)) self.assertFalse(secrets.compare_digest(a.encode('utf-8'), b.encode('utf-8'))) def test_bad_types(self): # Test that compare_digest raises with mixed types. a = 'abcde' b = a.encode('utf-8') assert isinstance(a, str) assert isinstance(b, bytes) self.assertRaises(TypeError, secrets.compare_digest, a, b) self.assertRaises(TypeError, secrets.compare_digest, b, a) def test_bool(self): # Test that compare_digest returns a bool. self.assertIsInstance(secrets.compare_digest("abc", "abc"), bool) self.assertIsInstance(secrets.compare_digest("abc", "xyz"), bool) class Random_Tests(unittest.TestCase): """Test wrappers around SystemRandom methods.""" def test_randbits(self): # Test randbits. errmsg = "randbits(%d) returned %d" for numbits in (3, 12, 30): for i in range(6): n = secrets.randbits(numbits) self.assertTrue(0 <= n < 2**numbits, errmsg % (numbits, n)) def test_choice(self): # Test choice. items = [1, 2, 4, 8, 16, 32, 64] for i in range(10): self.assertTrue(secrets.choice(items) in items) def test_randbelow(self): # Test randbelow. for i in range(2, 10): self.assertIn(secrets.randbelow(i), range(i)) self.assertRaises(ValueError, secrets.randbelow, 0) self.assertRaises(ValueError, secrets.randbelow, -1) class Token_Tests(unittest.TestCase): """Test token functions.""" def test_token_defaults(self): # Test that token_* functions handle default size correctly. for func in (secrets.token_bytes, secrets.token_hex, secrets.token_urlsafe): with self.subTest(func=func): name = func.__name__ try: func() except TypeError: self.fail("%s cannot be called with no argument" % name) try: func(None) except TypeError: self.fail("%s cannot be called with None" % name) size = secrets.DEFAULT_ENTROPY self.assertEqual(len(secrets.token_bytes(None)), size) self.assertEqual(len(secrets.token_hex(None)), 2*size) def test_token_bytes(self): # Test token_bytes. for n in (1, 8, 17, 100): with self.subTest(n=n): self.assertIsInstance(secrets.token_bytes(n), bytes) self.assertEqual(len(secrets.token_bytes(n)), n) def test_token_hex(self): # Test token_hex. for n in (1, 12, 25, 90): with self.subTest(n=n): s = secrets.token_hex(n) self.assertIsInstance(s, str) self.assertEqual(len(s), 2*n) self.assertTrue(all(c in string.hexdigits for c in s)) def test_token_urlsafe(self): # Test token_urlsafe. legal = string.ascii_letters + string.digits + '-_' for n in (1, 11, 28, 76): with self.subTest(n=n): s = secrets.token_urlsafe(n) self.assertIsInstance(s, str) self.assertTrue(all(c in legal for c in s)) if __name__ == '__main__': unittest.main() te deref offsets in compiler (gh-25152)" This reverts commit b2bf2bc1ece673d387341e06c8d3c2bc6e259747. * Revert "bpo-43693: Add new internal code objects fields: co_fastlocalnames and co_fastlocalkinds. (gh-26388)" This reverts commit 2c1e2583fdc4db6b43d163239ea42b0e8394171f. These two commits are breaking the refleak buildbots. * bpo-43693: Compute deref offsets in compiler (gh-25152)Mark Shannon2021-06-044-155/+164 | | | | | | Merges locals and cells into a single array. Saves a pointer in the interpreter and means that we don't need the LOAD_CLOSURE opcode any more https://bugs.python.org/issue43693 * bpo-32280: Store _PyRuntime in a named section (GH-4802)Max Bélanger2021-06-031-2/+28 | | | | | This commit stores the _PyRuntime structure in a section of the same name. This allows a debugging or crash reporting tool to quickly locate this structure at runtime without requiring the symbol table. Co-authored-by: Pablo Galindo <pablogsal@gmail.com> * bpo-11105: Do not crash when compiling recursive ASTs (GH-20594)Batuhan Taskaya2021-06-031-0/+808 | | | | | | | When compiling an AST object with a direct / indirect reference cycles, on the conversion phase because of exceeding amount of calls, a segfault was raised. This patch adds recursion guards to places for preventing user inputs to not to crash AST but instead raise a RecursionError. * bpo-43693: Add new internal code objects fields: co_fastlocalnames and ↵Eric Snow2021-06-038-5389/+5366 | | | | | | | | | | | | | | | | | co_fastlocalkinds. (gh-26388) A number of places in the code base (notably ceval.c and frameobject.c) rely on mapping variable names to indices in the frame "locals plus" array (AKA fast locals), and thus opargs. Currently the compiler indirectly encodes that information on the code object as the tuples co_varnames, co_cellvars, and co_freevars. At runtime the dependent code must calculate the proper mapping from those, which isn't ideal and impacts performance-sensitive sections. This is something we can easily address in the compiler instead. This change addresses the situation by replacing internal use of co_varnames, etc. with a single combined tuple of names in locals-plus order, along with a minimal array mapping each to its kind (local vs. cell vs. free). These two new PyCodeObject fields, co_fastlocalnames and co_fastllocalkinds, are not exposed to Python code for now, but co_varnames, etc. are still available with the same values as before (though computed lazily). Aside from the (mild) performance impact, there are a number of other benefits: * there's now a clear, direct relationship between locals-plus and variables * code that relies on the locals-plus-to-name mapping is simpler * marshaled code objects are smaller and serialize/de-serialize faster Also note that we can take this approach further by expanding the possible values in co_fastlocalkinds to include specific argument types (e.g. positional-only, kwargs). Doing so would allow further speed-ups in _PyEval_MakeFrameVector(), which is where args get unpacked into the locals-plus array. It would also allow us to shrink marshaled code objects even further. https://bugs.python.org/issue43693 * bpo-44298: Fix line numbers for early exits in with statements. (GH-26513)Mark Shannon2021-06-033-2971/+2981 | * bpo-17792: more accurate error message for unbound variable access ↵Irit Katriel2021-06-021-3/+3 | | | | exceptions (GH-24976) * bpo-43693: Add _PyCode_New(). (gh-26375)Eric Snow2021-05-276-4435/+4415 | | | | | This is an internal-only API that helps us manage the many values used to create a code object. https://bugs.python.org/issue43693 * bpo-43693: Clean up the PyCodeObject fields. (GH-26364)Eric Snow2021-05-261-9/+8 | | | | | | | | | | * Move up the comment about fields using in hashing/comparision. * Group the fields more clearly. * Add co_ncellvars and co_nfreevars. * Raise ValueError if nlocals != len(varnames), rather than aborting. * bpo-43879: Add native_thread_id field to PyThreadState (GH-25458)Gabriele N. Tornetta2021-05-261-0/+5 | * bpo-44156: Make cached string constants in compile.c subinterpreter ↵Ken Jin2021-05-25