mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 07:31:38 +00:00 
			
		
		
		
	gh-137481: Fix abbreviation of day names in TextCalendar (GH-137482)
Use the length of the longest day name in the current locale, rather than a constant 9, to decide if the names should be abbreviated.
This commit is contained in:
		
							parent
							
								
									7fda8b66de
								
							
						
					
					
						commit
						3143ceeb1d
					
				
					 3 changed files with 49 additions and 1 deletions
				
			
		| 
						 | 
					@ -378,7 +378,7 @@ def formatweekday(self, day, width):
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        Returns a formatted week day name.
 | 
					        Returns a formatted week day name.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        if width >= 9:
 | 
					        if width >= max(map(len, day_name)):
 | 
				
			||||||
            names = day_name
 | 
					            names = day_name
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            names = day_abbr
 | 
					            names = day_abbr
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -696,6 +696,52 @@ def test_locale_calendar_formatweekday(self):
 | 
				
			||||||
        except locale.Error:
 | 
					        except locale.Error:
 | 
				
			||||||
            raise unittest.SkipTest('cannot set the en_US locale')
 | 
					            raise unittest.SkipTest('cannot set the en_US locale')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # These locales have weekday names all shorter than English's longest
 | 
				
			||||||
 | 
					    # 'Wednesday'. They should not be abbreviated unnecessarily
 | 
				
			||||||
 | 
					    @support.run_with_locales("LC_ALL",
 | 
				
			||||||
 | 
					            'Chinese', 'zh_CN.UTF-8',
 | 
				
			||||||
 | 
					            'French', 'fr_FR.UTF-8',
 | 
				
			||||||
 | 
					            'Norwegian', 'nb_NO.UTF-8',
 | 
				
			||||||
 | 
					            'Malay', 'ms_MY.UTF8'
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    def test_locale_calendar_short_weekday_names(self):
 | 
				
			||||||
 | 
					        names = (datetime.date(2001, 1, i+1).strftime('%A') for i in range(7))
 | 
				
			||||||
 | 
					        max_length = max(map(len, names))
 | 
				
			||||||
 | 
					        if max_length >= 9:
 | 
				
			||||||
 | 
					            self.skipTest('weekday names are too long')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        def get_weekday_names(width):
 | 
				
			||||||
 | 
					            return calendar.TextCalendar().formatweekheader(width).split()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Weekday names should not be abbreviated if the width is sufficient
 | 
				
			||||||
 | 
					        self.assertEqual(
 | 
				
			||||||
 | 
					            get_weekday_names(max_length),
 | 
				
			||||||
 | 
					            get_weekday_names(max_length + 10)
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Any width shorter than necessary should produce abbreviations
 | 
				
			||||||
 | 
					        self.assertNotEqual(
 | 
				
			||||||
 | 
					            get_weekday_names(max_length),
 | 
				
			||||||
 | 
					            get_weekday_names(max_length - 1)
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # These locales have a weekday name longer than 'Wednesday'
 | 
				
			||||||
 | 
					    # They should be properly abbreviated rather than truncated
 | 
				
			||||||
 | 
					    @support.run_with_locales("LC_ALL",
 | 
				
			||||||
 | 
					            'Portuguese', 'pt_PT.UTF-8',
 | 
				
			||||||
 | 
					            'German',  'de_DE.UTF-8',
 | 
				
			||||||
 | 
					            'Russian', 'ru_RU.UTF-8',
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    def test_locale_calendar_long_weekday_names(self):
 | 
				
			||||||
 | 
					        names = (datetime.date(2001, 1, i+1).strftime('%A') for i in range(7))
 | 
				
			||||||
 | 
					        max_length = max(map(len, names))
 | 
				
			||||||
 | 
					        if max_length <= 9:
 | 
				
			||||||
 | 
					            self.skipTest('weekday names are too short')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        def get_weekday_names(width):
 | 
				
			||||||
 | 
					            return calendar.TextCalendar().formatweekheader(width).split()
 | 
				
			||||||
 | 
					        self.assertEqual(get_weekday_names(4), get_weekday_names(9))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_locale_calendar_formatmonthname(self):
 | 
					    def test_locale_calendar_formatmonthname(self):
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            # formatmonthname uses the same month names regardless of the width argument.
 | 
					            # formatmonthname uses the same month names regardless of the width argument.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,2 @@
 | 
				
			||||||
 | 
					Calendar uses the lengths of the locale's weekdays to decide if the width
 | 
				
			||||||
 | 
					requires abbreviation.
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue