Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add more tests.
  • Loading branch information
serhiy-storchaka committed Sep 22, 2025
commit fa9847b1b2e2cb5ace40d792938fa8ab3c4ad97f
32 changes: 32 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,20 @@ def ns_to_sec(ns):
# issue, os.utime() rounds towards minus infinity.
return (ns * 1e-9) + 0.5e-9

@staticmethod
def ns_to_sec_decimal(ns):
# Convert a number of nanosecond (int) to a number of seconds (Decimal).
# Round towards infinity by adding 0.5 nanosecond to avoid rounding
# issue, os.utime() rounds towards minus infinity.
return decimal.Decimal('1e-9') * ns + decimal.Decimal('0.5e-9')

@staticmethod
def ns_to_sec_fraction(ns):
# Convert a number of nanosecond (int) to a number of seconds (Fraction).
# Round towards infinity by adding 0.5 nanosecond to avoid rounding
# issue, os.utime() rounds towards minus infinity.
return fractions.Fraction(ns, 10**9) + fractions.Fraction(1, 2*10**9)

def test_utime_by_indexed(self):
# pass times as floating-point seconds as the second indexed parameter
def set_time(filename, ns):
Expand All @@ -968,6 +982,24 @@ def set_time(filename, ns):
os.utime(filename, times=(atime, mtime))
self._test_utime(set_time)

def test_utime_decimal(self):
# pass times as Decimal seconds
def set_time(filename, ns):
atime_ns, mtime_ns = ns
atime = self.ns_to_sec_decimal(atime_ns)
mtime = self.ns_to_sec_decimal(mtime_ns)
os.utime(filename, (atime, mtime))
self._test_utime(set_time)

def test_utime_fraction(self):
# pass times as Fraction seconds
def set_time(filename, ns):
atime_ns, mtime_ns = ns
atime = self.ns_to_sec_fraction(atime_ns)
mtime = self.ns_to_sec_fraction(mtime_ns)
os.utime(filename, (atime, mtime))
self._test_utime(set_time)

@unittest.skipUnless(os.utime in os.supports_follow_symlinks,
"follow_symlinks support for utime required "
"for this test.")
Expand Down
18 changes: 15 additions & 3 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import _thread as thread
import array
import contextlib
import decimal
import errno
import fractions
import gc
import io
import itertools
Expand Down Expand Up @@ -1310,10 +1312,20 @@ def testDefaultTimeout(self):
self.assertEqual(s.gettimeout(), None)

# Set the default timeout to 10, and see if it propagates
with socket_setdefaulttimeout(10):
self.assertEqual(socket.getdefaulttimeout(), 10)
with socket_setdefaulttimeout(10.125):
self.assertEqual(socket.getdefaulttimeout(), 10.125)
with socket.socket() as sock:
self.assertEqual(sock.gettimeout(), 10)
self.assertEqual(sock.gettimeout(), 10.125)

socket.setdefaulttimeout(decimal.Decimal('11.125'))
self.assertEqual(socket.getdefaulttimeout(), 11.125)
with socket.socket() as sock:
self.assertEqual(sock.gettimeout(), 11.125)

socket.setdefaulttimeout(fractions.Fraction(97, 8))
self.assertEqual(socket.getdefaulttimeout(), 12.125)
with socket.socket() as sock:
self.assertEqual(sock.gettimeout(), 12.125)

# Reset the default timeout to None, and see if it propagates
socket.setdefaulttimeout(None)
Expand Down