MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_bytes_and_bytearray_format

Method test_bytes_and_bytearray_format

Lib/test/test_format.py:324–396  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

322 raise TestFailed('"%*d"%(maxsize, -127) should fail')
323
324 def test_bytes_and_bytearray_format(self):
325 # %c will insert a single byte, either from an int in range(256), or
326 # from a bytes argument of length 1, not from a str.
327 testcommon(b"%c", 7, b"\x07")
328 testcommon(b"%c", b"Z", b"Z")
329 testcommon(b"%c", bytearray(b"Z"), b"Z")
330 testcommon(b"%5c", 65, b" A")
331 testcommon(b"%-5c", 65, b"A ")
332 # %b will insert a series of bytes, either from a type that supports
333 # the Py_buffer protocol, or something that has a __bytes__ method
334 class FakeBytes(object):
335 def __bytes__(self):
336 return b'123'
337 fb = FakeBytes()
338 testcommon(b"%b", b"abc", b"abc")
339 testcommon(b"%b", bytearray(b"def"), b"def")
340 testcommon(b"%b", fb, b"123")
341 testcommon(b"%b", memoryview(b"abc"), b"abc")
342 # # %s is an alias for %b -- should only be used for Py2/3 code
343 testcommon(b"%s", b"abc", b"abc")
344 testcommon(b"%s", bytearray(b"def"), b"def")
345 testcommon(b"%s", fb, b"123")
346 testcommon(b"%s", memoryview(b"abc"), b"abc")
347 # %a will give the equivalent of
348 # repr(some_obj).encode('ascii', 'backslashreplace')
349 testcommon(b"%a", 3.25, b"3.25")
350 testcommon(b"%a", b"ghi", b"b'ghi'")
351 testcommon(b"%a", "jkl", b"'jkl'")
352 testcommon(b"%a", "\u0544", b"'\\u0544'")
353 # %r is an alias for %a
354 testcommon(b"%r", 3.25, b"3.25")
355 testcommon(b"%r", b"ghi", b"b'ghi'")
356 testcommon(b"%r", "jkl", b"'jkl'")
357 testcommon(b"%r", "\u0544", b"'\\u0544'")
358
359 # Test exception for unknown format characters, etc.
360 if verbose:
361 print('Testing exceptions')
362 test_exc(b'%g', '1', TypeError, "float argument required, not str")
363 test_exc(b'%g', b'1', TypeError, "float argument required, not bytes")
364 test_exc(b'no format', 7, TypeError,
365 "not all arguments converted during bytes formatting")
366 test_exc(b'no format', b'1', TypeError,
367 "not all arguments converted during bytes formatting")
368 test_exc(b'no format', bytearray(b'1'), TypeError,
369 "not all arguments converted during bytes formatting")
370 test_exc(b"%c", -1, OverflowError,
371 "%c arg not in range(256)")
372 test_exc(b"%c", 256, OverflowError,
373 "%c arg not in range(256)")
374 test_exc(b"%c", 2**128, OverflowError,
375 "%c arg not in range(256)")
376 test_exc(b"%c", b"Za", TypeError,
377 "%c requires an integer in range(256) or a single byte, not a bytes object of length 2")
378 test_exc(b"%c", "Y", TypeError,
379 "%c requires an integer in range(256) or a single byte, not str")
380 test_exc(b"%c", 3.14, TypeError,
381 "%c requires an integer in range(256) or a single byte, not float")

Callers

nothing calls this directly

Calls 5

TestFailedClass · 0.90
testcommonFunction · 0.85
FakeBytesClass · 0.85
test_excFunction · 0.85
printFunction · 0.50

Tested by

no test coverage detected