Title: Match CPython's islower/isupper exactly by joshuamegnauth54 · Pull Request #7646 · RustPython/RustPython · GitHub
Open Graph Title: Match CPython's islower/isupper exactly by joshuamegnauth54 · Pull Request #7646 · RustPython/RustPython
X Title: Match CPython's islower/isupper exactly by joshuamegnauth54 · Pull Request #7646 · RustPython/RustPython
Description: This PR fixes a regression from my last islower/isupper patch. Python's Bytes doesn't assume an encoding, so methods like islower should only consider ASCII casing. I updated islower/isupper for UTF-8 and WTF-8 to match CPython exactly. The two functions now use the same properties as CPython and in the exact same way that CPython. I updated the unit tests to pass on Python 3.15. Unicode updates sometimes cause properties to shift. I previously tested everything on Python 3.14, but that lead to failures that I assumed were bugs but were actually due to Unicode differences. For example, U+0295 is a lower case letter in older Unicode versions but is NOT in newer versions. With Python 3.14: assert "\u0295".islower() assert not "\u0295".isupper() With Python 3.15 and RustPython: assert not "\u0295".islower() assert not "\u0295".isupper() assert not "\u0295".istitle() ^It's imperative to test with the latest Python because of Unicode differences. I, uh, spent like three hours trying to fix a bug that didn't exist because I was crosschecking the tests with Python 3.14... Oops. I bumped CI's Python version to 3.15 as well. Reverted because it's more complicated than I thought. Here is CPython's isupper implementation for comparison. CPython's core logic for islower (isupper is basically the same) /* Special case for empty strings */ if (length == 0) Py_RETURN_FALSE; cased = 0; for (i = 0; i < length; i++) { const Py_UCS4 ch = PyUnicode_READ(kind, data, i); if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) Py_RETURN_FALSE; else if (!cased && Py_UNICODE_ISLOWER(ch)) cased = 1; } return PyBool_FromLong(cased); RustPython's islower: let mut all_cased = false; for chunk in self.as_bytes().utf8_chunks().map(|c| c.valid()) { if chunk.chars().any(|c| { INVALID::for_char(c) || GeneralCategoryGroup::TitlecaseLetter.contains(GeneralCategory::for_char(c)) }) { return false; } if !all_cased && chunk.chars().any(VALID::for_char) { all_cased = true; } } all_cased They're the same now! 😁 Summary by CodeRabbit Bug Fixes Improved case-checking: text strings use Unicode casedness detection while byte/encoded checks use ASCII-only scanning, preventing misclassification for mixed/invalid byte sequences and certain rare or non-cased characters. Tests Updated and expanded tests for Unicode edge cases, version-dependent casing, non-cased scripts, and bytes case-checking with invalid/mixed sequences.
Open Graph Description: This PR fixes a regression from my last islower/isupper patch. Python's Bytes doesn't assume an encoding, so methods like islower should only consider ASCII casing. I updated islower/isuppe...
X Description: This PR fixes a regression from my last islower/isupper patch. Python's Bytes doesn't assume an encoding, so methods like islower should only consider ASCII casing. I updated islowe...
Opengraph URL: https://github.com/RustPython/RustPython/pull/7646
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:89cf939a-7114-0fe6-d744-b0012e6216c9 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | A640:6973B:5BEA12:7A7873:6A613DD7 |
| html-safe-nonce | 7324eaaf8e5d8b285c986395e10257d5e8bd90bebf971877214f602466c5915b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNjQwOjY5NzNCOjVCRUExMjo3QTc4NzM6NkE2MTNERDciLCJ2aXNpdG9yX2lkIjoiMTY1ODEwMDMzNTc1MDc1Nzg0NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | db07f6a892eb8eccc87570476f0c0c19c29d467b059cba26776da9b135ccd484 |
| hovercard-subject-tag | pull_request:3559611571 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/RustPython/RustPython/pull/7646/files |
| twitter:image | https://avatars.githubusercontent.com/u/48846352?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/48846352?s=400&v=4 |
| og:image:alt | This PR fixes a regression from my last islower/isupper patch. Python's Bytes doesn't assume an encoding, so methods like islower should only consider ASCII casing. I updated islower/isuppe... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 2b7a3d1e6770dc7153450d820fcfe9a7851c4cf2251dcc29c7ad3c5cd1b9e88b |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git |
| octolytics-dimension-user_id | 39710557 |
| octolytics-dimension-user_login | RustPython |
| octolytics-dimension-repository_id | 135201145 |
| octolytics-dimension-repository_nwo | RustPython/RustPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 135201145 |
| octolytics-dimension-repository_network_root_nwo | RustPython/RustPython |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 78cf9d43f120d1f03636ca22ab420bcbb1712804 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width