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

Method __new__

Lib/_pydatetime.py:637–748  ·  view source on GitHub ↗
(cls, days=0, seconds=0, microseconds=0,
                milliseconds=0, minutes=0, hours=0, weeks=0)

Source from the content-addressed store, hash-verified

635 __slots__ = '_days', '_seconds', '_microseconds', '_hashcode'
636
637 def __new__(cls, days=0, seconds=0, microseconds=0,
638 milliseconds=0, minutes=0, hours=0, weeks=0):
639 # Doing this efficiently and accurately in C is going to be difficult
640 # and error-prone, due to ubiquitous overflow possibilities, and that
641 # C double doesn't have enough bits of precision to represent
642 # microseconds over 10K years faithfully. The code here tries to make
643 # explicit where go-fast assumptions can be relied on, in order to
644 # guide the C implementation; it's way more convoluted than speed-
645 # ignoring auto-overflow-to-long idiomatic Python could be.
646
647 for name, value in (
648 ("days", days),
649 ("seconds", seconds),
650 ("microseconds", microseconds),
651 ("milliseconds", milliseconds),
652 ("minutes", minutes),
653 ("hours", hours),
654 ("weeks", weeks)
655 ):
656 if not isinstance(value, (int, float)):
657 raise TypeError(
658 f"unsupported type for timedelta {name} component: {type(value).__name__}"
659 )
660
661 # Final values, all integer.
662 # s and us fit in 32-bit signed ints; d isn't bounded.
663 d = s = us = 0
664
665 # Normalize everything to days, seconds, microseconds.
666 days += weeks*7
667 seconds += minutes*60 + hours*3600
668 microseconds += milliseconds*1000
669
670 # Get rid of all fractions, and normalize s and us.
671 # Take a deep breath <wink>.
672 if isinstance(days, float):
673 dayfrac, days = _math.modf(days)
674 daysecondsfrac, daysecondswhole = _math.modf(dayfrac * (24.*3600.))
675 assert daysecondswhole == int(daysecondswhole) # can't overflow
676 s = int(daysecondswhole)
677 assert days == int(days)
678 d = int(days)
679 else:
680 daysecondsfrac = 0.0
681 d = days
682 assert isinstance(daysecondsfrac, float)
683 assert abs(daysecondsfrac) <= 1.0
684 assert isinstance(d, int)
685 assert abs(s) <= 24 * 3600
686 # days isn't referenced again before redefinition
687
688 if isinstance(seconds, float):
689 secondsfrac, seconds = _math.modf(seconds)
690 assert seconds == int(seconds)
691 seconds = int(seconds)
692 secondsfrac += daysecondsfrac
693 assert abs(secondsfrac) <= 2.0
694 else:

Callers

nothing calls this directly

Calls 5

isinstanceFunction · 0.85
roundFunction · 0.85
absFunction · 0.70
divmodFunction · 0.50
__new__Method · 0.45

Tested by

no test coverage detected