Compare commits

..

1660 Commits
v0.2.0 ... main

Author SHA1 Message Date
b333ffa781 merge upstream
Some checks failed
CI / Run rust tests (macos-latest) (push) Has been cancelled
CI / Run rust tests (ubuntu-latest) (push) Has been cancelled
CI / Run rust tests (windows-latest) (push) Has been cancelled
CI / Ensure compilation on various targets (push) Has been cancelled
CI / Run snippets and cpython tests (macos-latest) (push) Has been cancelled
CI / Run snippets and cpython tests (ubuntu-latest) (push) Has been cancelled
CI / Run snippets and cpython tests (windows-latest) (push) Has been cancelled
CI / Check Rust code with rustfmt and clippy (push) Has been cancelled
CI / Run tests under miri (push) Has been cancelled
CI / Check the WASM package and demo (push) Has been cancelled
CI / Run snippets and cpython tests on wasm-wasi (push) Has been cancelled
2025-02-08 16:33:07 +09:00
Jeong, YunWon
8f5cc6174c fix windows sleep 2025-02-07 07:53:28 +09:00
Jeong YunWon
29d014a0e1 Pin malachite versions to avoid API incompatibility 2025-02-03 11:57:30 +09:00
Ashwin Naren
396a0ca563 Basic Match statements (#5485)
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
2025-01-25 23:14:15 +09:00
Jeong YunWon
a500178b3c update parser to fix match crash 2025-01-22 13:41:01 +09:00
Jeong YunWon
7d770f55fb more assertions in switch_to_block 2025-01-21 23:53:23 +09:00
Jeong, YunWon
db283a66e8 Merge pull request #5477 from youknowone/better-downcast-error
Add better panic for abnormal downcast error
2025-01-21 13:54:45 +09:00
Jeong, YunWon
c642aef8ca Merge pull request #5481 from arihant2math/builtins-312
Update tests for builtin objects to python 3.12.8
2025-01-20 14:04:28 +09:00
Ashwin Naren
396df1a506 update test_listcomps.py to 3.12.8 2025-01-19 20:05:35 -08:00
Ashwin Naren
9c9fa7e537 update test_list to 3.12.8 2025-01-19 20:05:35 -08:00
Ashwin Naren
86e2eb0648 update test_float to 3.12.8 2025-01-19 20:05:34 -08:00
Ashwin Naren
491db2f0c6 update test_unary to 3.12.8 2025-01-17 18:04:25 -08:00
Ashwin Naren
f0fb375028 update numbers tests 2025-01-17 15:48:59 -08:00
Ashwin Naren
16d8bab61a Update Logging to 3.12.7 (#5478)
* updated logging to 3.12, added logging tests, and added smtplib and tests

* fix expected failures on test_smtplib.py

* mark all rustpython fails on test_logging.py
2025-01-17 19:44:06 +09:00
Ashwin Naren
2d83a67bd6 Update zlib from 3.12.6 and _ZlibDecompressor implementation (#5476)
* add is_s390x and skip_on_s390x to test support

* update zlib tests to 3.12

* _ZlibDecompressor implementation
2025-01-16 13:28:09 +09:00
Jeong YunWon
5ad7e97e05 Add better panic for abnormal downcast error 2025-01-16 00:57:09 +09:00
Jeong YunWon
b7a7b6b923 remove warnings from wasm build 2025-01-13 15:06:29 +09:00
Jeong YunWon
0e00d2328d wasm32-wasi -> wasm32-wasip1 2025-01-13 15:06:29 +09:00
Shubham Patil
53db70e784 Support recursion in JIT-ed functions (#5473) 2025-01-13 14:55:27 +09:00
Sacha Dupuydauby
76c699b4ba Update contextlib from CPython 3.12 2025-01-12 00:40:41 +09:00
Noa
c901bc07a4 Upgrade wasm deps + fix demo 2025-01-11 18:48:27 +09:00
Noa
b7db23bbae Fix warnings for Rust 1.84 2025-01-11 18:48:27 +09:00
308b95ec13 Update .github/workflows/ai-review.yml 2025-01-11 01:53:46 +09:00
5f77ce5b4f Update .github/workflows/ai-review.yml 2025-01-11 00:44:09 +09:00
4da57d42de Update .github/workflows/ai-review.yml
Some checks failed
CI / Run rust tests (macos-latest) (push) Has been cancelled
CI / Run rust tests (windows-latest) (push) Has been cancelled
CI / Ensure compilation on various targets (push) Has been cancelled
CI / Run snippets and cpython tests (macos-latest) (push) Has been cancelled
CI / Run snippets and cpython tests (ubuntu-latest) (push) Has been cancelled
CI / Run snippets and cpython tests (windows-latest) (push) Has been cancelled
CI / Check Rust code with rustfmt and clippy (push) Has been cancelled
CI / Run tests under miri (push) Has been cancelled
CI / Check the WASM package and demo (push) Has been cancelled
CI / Run snippets and cpython tests on wasm-wasi (push) Has been cancelled
CI / Run rust tests (ubuntu-latest) (push) Has been cancelled
2025-01-10 13:54:55 +09:00
2777588bb4 Add ci
Some checks failed
CI / Run rust tests (macos-latest) (push) Has been cancelled
CI / Run rust tests (windows-latest) (push) Has been cancelled
CI / Ensure compilation on various targets (push) Has been cancelled
CI / Run snippets and cpython tests (macos-latest) (push) Has been cancelled
CI / Run snippets and cpython tests (ubuntu-latest) (push) Has been cancelled
CI / Run snippets and cpython tests (windows-latest) (push) Has been cancelled
CI / Check Rust code with rustfmt and clippy (push) Has been cancelled
CI / Run tests under miri (push) Has been cancelled
CI / Check the WASM package and demo (push) Has been cancelled
CI / Run snippets and cpython tests on wasm-wasi (push) Has been cancelled
CI / Run rust tests (ubuntu-latest) (push) Has been cancelled
2025-01-10 11:48:55 +09:00
Jeong, YunWon
389b20d977 Merge pull request #5444 from key262yek/update_fstring_from_v3.12.7
Update fstring from v3.12.7
2025-01-10 10:44:31 +09:00
Bob McWhirter
d06459fa49 guard signal-handling init more broadly
If `install_signal_handlers` is false (due to embedding),
the VM still inits the signal stdlib and installs a lot
of signal-handling, including touching *ever* signal,
including SIGINT.

When running multiple concurrent interpreters with
varying inits at varying times, this can break the
hosting application's signal-handling so lovingly
set up before starting anything with RustPython.
2025-01-09 16:21:25 -06:00
Shubham Patil
e2a55cbf34 Handle pre-release flag being empty for schedule triggers in release workflow
GitHub workflow_dispatch input variables are always empty for other triggers
2025-01-09 17:31:02 +09:00
Jeong, YunWon
a5e6ade9cb Merge pull request #5454 from coolreader18/rust-1.83
Bump MSRV to 1.83
2025-01-07 13:13:42 +09:00
Jeong, YunWon
a1e32566d3 Merge pull request #5469 from fu050409/patch-1 2025-01-07 12:38:08 +09:00
Noa
8c7bfb3e1a Fix redox 2025-01-06 13:09:49 -06:00
苏向夜
bb0480e978 docs(readme): fix installation command for cargo 2025-01-07 00:52:16 +08:00
Jeong, YunWon
2ccc745513 Merge pull request #5465 from crazymerlyn/caseless-bump 2025-01-04 11:43:30 +09:00
Jeong, YunWon
bea83fe94d Merge pull request #5466 from theshubhamp/gh-release 2025-01-04 11:43:10 +09:00
Shubham Patil
3feaf689d8 Re-enable Release Notes Generation 2025-01-03 21:49:17 +05:30
Shubham Patil
bd627b58af Schedule Pre-Release on Monday 9AM UTC 2025-01-03 21:49:17 +05:30
Shubham Patil
c561d33cb2 Support both Release & Pre-Release in Release Workflow
On Push "main" is removed
2025-01-03 21:49:17 +05:30
Ashwin Naren
c8fd3bd683 Build wasm on release 2025-01-03 14:56:58 +09:00
Ankit Goel
fef1e31634 Bump rust-caseless to 0.2.2 2024-12-31 12:26:29 +00:00
Shubham Patil
1abaf87abe Temporarily disable release notes generation
Release creation fails with this error:
```
HTTP 422: Validation Failed (https://api.github.com/repos/RustPython/RustPython/releases)
body is too long (maximum is 125000 characters)
Error: Process completed with exit code 1.
```

Most likely because there's no previous release to diff of from.

Disabling, getting a green release & enabling back might fix this.
2024-12-30 16:44:55 +09:00
Shubham Patil
38593fbd85 Check operand types in bool or, and, xor to be PyInt (#5461)
* Added Tests for Bitwise or, and, xor type error

* Sync binary operator order comment with actual implementation

* Check operand types in bool or, and, xor to be PyInt

PyNumber methods are expected to type check both arguments.

Dispatch is not done by inverting parameter order for __r<op>__ (example __ror__) when calls are handled via PyNumberMethods
2024-12-30 16:44:27 +09:00
Ankit Goel
8d187fd275 Bump result-like to 0.5.0 2024-12-28 11:38:00 +09:00
Shubham Patil
646cc81656 Add GitHub Binary Release Pipeline for RustPython (#5456) 2024-12-28 11:34:53 +09:00
carsonzhu
01f7536b36 expose run_shell 2024-12-11 17:33:36 +09:00
Jeong, YunWon
97e5ec02f8 Merge pull request #5449 from key262yek/update_test_float_from_CPython_v3.12.7
Update test float from c python v3.12.7
2024-12-06 12:50:55 +09:00
Oskar Skog
3dced01af0 Move os.system from posix.rs to os.rs
Fixes #5100
2024-12-06 12:19:34 +09:00
0cf4534c5c copy new file "Lib/test/support/testcase.py" for test_float.py
Some checks failed
CI / Run rust tests (macos-latest) (pull_request) Has been cancelled
CI / Run rust tests (windows-latest) (pull_request) Has been cancelled
CI / Ensure compilation on various targets (pull_request) Has been cancelled
CI / Run snippets and cpython tests (macos-latest) (pull_request) Has been cancelled
CI / Run snippets and cpython tests (ubuntu-latest) (pull_request) Has been cancelled
CI / Run snippets and cpython tests (windows-latest) (pull_request) Has been cancelled
CI / Check Rust code with rustfmt and clippy (pull_request) Has been cancelled
CI / Run tests under miri (pull_request) Has been cancelled
CI / Check the WASM package and demo (pull_request) Has been cancelled
CI / Run snippets and cpython tests on wasm-wasi (pull_request) Has been cancelled
CI / Run rust tests (ubuntu-latest) (pull_request) Has been cancelled
2024-12-05 15:29:35 +09:00
044f66fba3 copy from cpython v3.12.7 2024-12-05 15:29:35 +09:00
40a9ddad4e update test_fstring.py from cpython 3.12.7
add expectedFailure to tag what should rustpython do
add comment for some syntaxerror which make test run broken
2024-12-05 15:04:56 +09:00
Noa
848db340da Merge pull request #5442 from coolreader18/cli-fixes
Miscellaneous cli-related parity fixes
2024-12-03 23:57:57 -06:00
Noa
c6da4ffcdd Try to fix universal write on windows 2024-12-03 18:01:44 -06:00
Noa
8ac7e34be2 Updates for Rust 1.83 2024-12-03 17:05:24 -06:00
Noa
e4be882994 Miscellaneous cli-related parity fixes 2024-12-03 16:28:32 -06:00
Noa
adc05e663f Merge pull request #5443 from coolreader18/incremental-newline-decoder
Implement IncrementalNewlineDecoder in rust
2024-12-03 16:28:00 -06:00
Jeong, YunWon
0bc236ac98 Merge pull request #5450 from key262yek/resolve_lint_fails_w_rust_1.83_clippy
Resolve lint fails w rust 1.83 clippy
2024-12-02 12:42:37 +09:00
da3d3d9296 allow manual c string literal 2024-11-29 11:57:42 +09:00
6a6af6a952 change to use c str literal 2024-11-29 11:19:10 +09:00
582a4ab267 delete elided lifetime 2024-11-29 11:18:53 +09:00
ebde8546c9 remove elided lifetime 2024-11-29 11:05:44 +09:00
fffd0f5465 new clippy lint update Rust v1.83.0 2024-11-29 09:53:54 +09:00
dependabot[bot]
3b6db8e21a Bump the github-actions group with 7 updates
Bumps the github-actions group with 7 updates:

| Package | From | To |
| --- | --- | --- |
| [actions/checkout](https://github.com/actions/checkout) | `3` | `4` |
| [actions/setup-python](https://github.com/actions/setup-python) | `4` | `5` |
| [actions/setup-node](https://github.com/actions/setup-node) | `3` | `4` |
| [mwilliamson/setup-wabt-action](https://github.com/mwilliamson/setup-wabt-action) | `1` | `3` |
| [peaceiris/actions-gh-pages](https://github.com/peaceiris/actions-gh-pages) | `2` | `4` |
| [wasmerio/setup-wasmer](https://github.com/wasmerio/setup-wasmer) | `2` | `3` |
| [codecov/codecov-action](https://github.com/codecov/codecov-action) | `3` | `5` |


Updates `actions/checkout` from 3 to 4
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v3...v4)

Updates `actions/setup-python` from 4 to 5
- [Release notes](https://github.com/actions/setup-python/releases)
- [Commits](https://github.com/actions/setup-python/compare/v4...v5)

Updates `actions/setup-node` from 3 to 4
- [Release notes](https://github.com/actions/setup-node/releases)
- [Commits](https://github.com/actions/setup-node/compare/v3...v4)

Updates `mwilliamson/setup-wabt-action` from 1 to 3
- [Release notes](https://github.com/mwilliamson/setup-wabt-action/releases)
- [Commits](https://github.com/mwilliamson/setup-wabt-action/compare/v1...v3)

Updates `peaceiris/actions-gh-pages` from 2 to 4
- [Release notes](https://github.com/peaceiris/actions-gh-pages/releases)
- [Changelog](https://github.com/peaceiris/actions-gh-pages/blob/main/CHANGELOG.md)
- [Commits](https://github.com/peaceiris/actions-gh-pages/compare/v2...v4)

Updates `wasmerio/setup-wasmer` from 2 to 3
- [Release notes](https://github.com/wasmerio/setup-wasmer/releases)
- [Commits](https://github.com/wasmerio/setup-wasmer/compare/v2...v3)

Updates `codecov/codecov-action` from 3 to 5
- [Release notes](https://github.com/codecov/codecov-action/releases)
- [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/codecov/codecov-action/compare/v3...v5)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: github-actions
- dependency-name: actions/setup-python
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: github-actions
- dependency-name: actions/setup-node
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: github-actions
- dependency-name: mwilliamson/setup-wabt-action
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: github-actions
- dependency-name: peaceiris/actions-gh-pages
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: github-actions
- dependency-name: wasmerio/setup-wasmer
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: github-actions
- dependency-name: codecov/codecov-action
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: github-actions
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-11-27 17:44:35 -07:00
Noa
19a0b7dd76 Merge pull request #5445 from cclauss/patch-2
Keep GitHub Actions up to date with GitHub's Dependabot
2024-11-27 13:21:11 -06:00
Noa
9647ebe206 Fix prettier formatting 2024-11-27 13:20:19 -06:00
Noa
0c726a1275 Merge pull request #5447 from isbm/isbm-move-exception-loglevel
Move log message to the debug level
2024-11-27 13:04:03 -06:00
Jeong, YunWon
f71eb55f1f Merge pull request #5438 from key262yek/update_random_from_v3.12.3
Update random from v3.12.7
2024-11-26 16:33:36 -07:00
Jeong, YunWon
edcc0b7dbc Merge pull request #5437 from key262yek/update_numbers_from_v3.12.3
Update numbers.py from v3.12.7
2024-11-26 16:30:37 -07:00
Buciu Theodor Marian
4910b308ee Fix compile error when using ``vm-tracing-logging`` 2024-11-26 16:26:00 -07:00
Bo Maryniuk
0cc1c323ed Move log message to the debug level 2024-11-26 21:29:08 +01:00
Christian Clauss
24fd19b35d Keep GitHub Actions up to date with GitHub's Dependabot
Fixes software supply chain safety warnings like at the bottom right of

https://github.com/RustPython/RustPython/actions/runs/11870777239
* [Keeping your actions up to date with Dependabot](https://docs.github.com/en/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot)
* [Configuration options for the dependabot.yml file - package-ecosystem](https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#package-ecosystem)
2024-11-26 16:38:54 +01:00
Noa
fbd0c7a99e Implement IncrementalNewlineDecoder in rust 2024-11-14 23:06:53 -06:00
4f5b26698c skip test 'super' object has no attribute 'getstate' 2024-11-11 00:17:57 +09:00
d887e5c24c first copy from cpython v3.12.3 2024-11-11 00:17:30 +09:00
0c69391bbd Copy test for numbers.py from v3.12.3 2024-11-10 00:15:21 +09:00
91598f9121 change numbers.py from [v3.12.3](6293d00e72/Lib/numbers.py (L8)) 2024-11-10 00:13:13 +09:00
Jeong YunWon
98d09e7816 Remove time.daylight from freebsd 2024-10-31 16:13:52 +09:00
7ae9432524 Change deprecated type PanicInfo to PanicHookInfo 2024-10-31 01:45:59 +09:00
Noa
c883f0ad8a Updates for Rust 1.82 2024-10-17 16:32:47 -05:00
Noa
eae60113af Update some stuff for inline const & associated type bounds 2024-10-17 16:32:17 -05:00
Noa
1aab5240cf Update for rust 1.77 2024-10-17 16:32:17 -05:00
dependabot[bot]
edb7abde5a Bump pyo3 from 0.22.3 to 0.22.4
Bumps [pyo3](https://github.com/pyo3/pyo3) from 0.22.3 to 0.22.4.
- [Release notes](https://github.com/pyo3/pyo3/releases)
- [Changelog](https://github.com/PyO3/pyo3/blob/main/CHANGELOG.md)
- [Commits](https://github.com/pyo3/pyo3/compare/v0.22.3...v0.22.4)

---
updated-dependencies:
- dependency-name: pyo3
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-10-16 05:37:46 +09:00
Noa
29d95340b0 Merge pull request #5418 from crazymerlyn/memoryerror
Handle MemoryError for sequences
2024-09-29 10:50:01 -05:00
Ankit Goel
6fb19ac74f Handle MemoryError for sequences 2024-09-29 16:24:30 +01:00
Noa
37dc28a69d Update deps (#5417)
* Upgrade nix

* Update deps

* Upgrade pyo3, winreg

* Fix errors from upgrading
2024-09-27 13:11:00 +09:00
Ankit Goel
7623668256 Raise TypeError if BaseException receives Keyword arguments 2024-09-24 11:57:37 +09:00
Noa
bbf7aacd4d Merge pull request #5409 from crazymerlyn/cache-frozenset-hash
Cache hash value for FrozenSets
2024-09-23 17:43:55 -05:00
Noa
d6c1e08ef4 Merge pull request #5413 from crazymerlyn/remove-proc-macro-crate
Remove dependency on proc-macro-crate
2024-09-23 17:42:40 -05:00
Ankit Goel
d1f95f04a7 Remove dependency on proc-macro-crate
This dependency is only useful when modifying the crate name of
`num_enum` crate. RustPython doesn't do that so it's unnecessary.
2024-09-23 16:54:21 +01:00
Ankit Goel
0dacf8a326 Remove dependency on uuid-macro-internal
`uuid-macro-internal` is only relevant when using `uuid!` macro to parse
uuids at compile time. RustPython doesn't do that so it's unnecessary.
2024-09-23 15:07:57 +09:00
Noa
e534b10722 Fix build with ossl1 2024-09-22 17:38:26 +09:00
Jeong, YunWon
0785cc5aa9 Merge pull request #5406 from coolreader18/improve-posixsubprocess
Improve posixsubprocess
2024-09-22 17:37:34 +09:00
Ankit Goel
48025e0102 Cache hash value for FrozenSets
Adds a hash field to the `PyFrozenSet` data type in order to avoid
recomputing the hash of an immutable object.
2024-09-22 06:15:54 +01:00
Noa
eeb719e8f7 Merge pull request #5411 from crazymerlyn/decimal-from-float
Fix inconsistent behavior of Decimal.from_float
2024-09-21 22:58:23 -05:00
Noa
7933edad43 Add missing functionality to posixsubprocess 2024-09-21 22:40:15 -05:00
Ankit Goel
5f75728ede Fix inconsistent behavior of Decimal.from_float 2024-09-22 01:42:28 +01:00
Ankit Goel
8cff0ed6c2 Avoid allocating a vector of elements when hashing frozenset (#5408)
Adds a `try_fold_keys` method to Dict which allows performing common
operations on all elements without needing to create a Vec first.
2024-09-21 23:18:26 +09:00
Noa
a8964f4108 Add select.epoll 2024-09-20 11:46:01 +09:00
Noa
740aeedca3 Merge pull request #5405 from crazymerlyn/fix-set-intersection-update
Fix set intersection_update implementation
2024-09-19 21:21:16 -05:00
Noa
8152e7e62c Make Gid/Uid less janky 2024-09-19 17:50:52 -05:00
Ankit Goel
b36c95b91e Fix set intersection_update implementation 2024-09-19 20:59:12 +01:00
Ricardo Robles
b5c1fd95dc Add zoneinfo from Python 3.12.6 (#5400) 2024-09-19 11:18:48 +09:00
Noa
23f7cbf1c3 Make sqlite optional 2024-09-19 11:13:51 +09:00
hongmengning
ae78ecc2c5 Add missing symbols in exceptions.rs 2024-09-17 16:07:30 +09:00
Ankit Goel
dd06516d1c Deprecate delegating int() to __trunc__ 2024-09-17 16:06:09 +09:00
Ankit Goel
8066f36a4e Fix copysign behavior for NaNs (#5396) 2024-09-11 15:19:21 +09:00
Jeong YunWon
3bbf6b9d53 less strict time bound for test_re.test_search_anchor_at_beginning 2024-09-10 16:31:43 +09:00
dependabot[bot]
8bc5944178 Bump webpack from 4.28.2 to 5.94.0 in /wasm/example
Bumps [webpack](https://github.com/webpack/webpack) from 4.28.2 to 5.94.0.
- [Release notes](https://github.com/webpack/webpack/releases)
- [Commits](https://github.com/webpack/webpack/compare/v4.28.2...v5.94.0)

---
updated-dependencies:
- dependency-name: webpack
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-08-28 14:14:51 +09:00
Niels Buwen
a13b99642b Add get/set methods for function module/annotations (#5392)
---------

Co-authored-by: Jeong YunWon <jeong@youknowone.org>
2024-08-19 00:59:51 +09:00
Jeong, YunWon
060db5983c Merge pull request #5390 from youknowone/wasmbind
recreating wasmbind patch
2024-08-09 09:37:49 +09:00
Jeong YunWon
42bba6920e apply suggestion 2024-08-09 02:23:49 +09:00
Noa
ea11d78995 Fix compilation without compiler feature 2024-08-09 02:23:49 +09:00
Jeong YunWon
fe63ca762f separate wasm-unknown-test 2024-08-09 02:23:49 +09:00
Jeong YunWon
a82982725e fix getrandom 2024-08-09 02:23:49 +09:00
Noa
7dfb760421 Make rustpython-vm compatible with non-js wasm32-unknown & add tests 2024-08-09 02:04:25 +09:00
Jeong YunWon
b6e9a3f37e rustpython_wasm uses the new feature 2024-08-09 00:59:29 +09:00
Jeong YunWon
3f28309b7b revert unnecessary change 2024-08-09 00:58:53 +09:00
Jeong YunWon
d2a4a330f9 following chrono/wasmbind convention 2024-08-09 00:58:11 +09:00
Benjamin DeMann
d8c35770ab enable js feature of getrandom only for wasmbind 2024-08-09 00:29:47 +09:00
Benjamin DeMann
dbb6794a41 add cfg for not wasmbind for time 2024-08-09 00:29:25 +09:00
Benjamin DeMann
63c9909aa0 run the first block if wasmbind is not present 2024-08-09 00:29:25 +09:00
Benjamin DeMann
f1dac5087e address pr notes 2024-08-09 00:29:25 +09:00
Benjamin DeMann
4f80d7013e add wasmbind feature 2024-08-09 00:29:25 +09:00
Jeong YunWon
2919df1df5 Mark rust-version 1.78 2024-08-08 23:20:17 +09:00
Jeong YunWon
a2df2f014b Fix dis.dis without compiler feature 2024-08-07 09:48:09 +09:00
Jeong YunWon
8673169ee7 0.4.0 2024-08-07 08:38:21 +09:00
Jeong YunWon
2c2e0fb172 pyperf bench script 2024-08-07 08:38:21 +09:00
Jeong YunWon
d45c5de906 ignore local config files 2024-08-07 08:38:21 +09:00
Jeong YunWon
eb985beed6 freeze-stdlib imply stdlib 2024-08-07 07:48:42 +09:00
Jeong YunWon
ff9aa0fc54 Add example projects 2024-08-07 07:48:42 +09:00
Jeong YunWon
0bd1a3efb2 vm.print 2024-08-07 07:06:27 +09:00
Jeong YunWon
08e7ec948b Don't let Interpreter::run silently ignore result value 2024-08-07 07:06:15 +09:00
Jeong YunWon
6092c07eb5 better error message for freeze-stdlib 2024-08-07 07:05:28 +09:00
CPython Developers
09d74336fa Update fraction,test_numeric_tower from CPython 3.12.4 2024-07-29 19:53:44 +09:00
Jeong YunWon
694354e5e6 temp fix slot member_count 2024-07-29 16:45:35 +09:00
Andrew Bowen
55f04db6b8 Update codecs and test_codecs from cpython v3.12.4 (#5372)
* updated test_codecs.py from cpython v3.12.4

* updated codecs.py from cpython v3.12.4
2024-07-29 14:55:32 +09:00
Jeong YunWon
6f8360a878 sys.exception 2024-07-29 14:30:59 +09:00
Jeong YunWon
6ca4685fee __objclass__ 2024-07-29 14:30:46 +09:00
Jeong, YunWon
d86b592add Merge pull request #5370 from youknowone/update-io
Update io, _pyio, test_io from CPython 3.12
2024-07-28 21:12:13 +09:00
Jeong YunWon
9944b3dac1 Mark failing tests 2024-07-28 20:24:15 +09:00
Jeong YunWon
8ed79bd1af add a dummy parameter for support.skip_if_sanitizer 2024-07-28 20:24:15 +09:00
CPython Developers
0600ae6213 Update io, pyio, test_io from CPython 2024-07-28 20:24:15 +09:00
Jeong YunWon
623415d843 IncrementalNewlineDecoder from pyio 2024-07-28 20:23:53 +09:00
Jeong YunWon
87b84a83cc Fix TextIOWrapper.reconfigure coder 2024-07-28 20:23:53 +09:00
Jeong YunWon
aa5eba9723 fix BufferedMixin::tell 2024-07-28 20:23:53 +09:00
Jeong YunWon
c2cd883f93 TextIOWrapper.reconfigure 2024-07-28 16:24:32 +09:00
Jeong YunWon
43e20a8cd9 Fix os.putenv & test_os.test_envronb 2024-07-28 16:24:17 +09:00
Jeong YunWon
48299cdd7f Add faulthandler to wasm32 2024-07-28 12:11:56 +09:00
Dan Näsman
2335ca0f72 Add proper underscore handling to float and complex types. (#5356) 2024-07-28 12:11:34 +09:00
Jeong YunWon
e42c1a33b5 upgrade unicode_names2 2024-07-28 11:43:00 +09:00
Jeong, YunWon
b1a38c2d50 Fix rust 1.80 warnings (#5363)
* Fix rust 1.80 warnings

* Fix nightly clippy warnings
2024-07-28 10:43:40 +09:00
dependabot[bot]
f1f05303db Bump openssl from 0.10.62 to 0.10.66
Bumps [openssl](https://github.com/sfackler/rust-openssl) from 0.10.62 to 0.10.66.
- [Release notes](https://github.com/sfackler/rust-openssl/releases)
- [Commits](https://github.com/sfackler/rust-openssl/compare/openssl-v0.10.62...openssl-v0.10.66)

---
updated-dependencies:
- dependency-name: openssl
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-07-23 11:14:45 +09:00
Jeong YunWon
8424a733a2 type constructor 2024-07-14 22:08:09 +09:00
Jeong YunWon
2bf7a4a08c FuncArgs::is_empty() 2024-07-14 22:08:09 +09:00
Jeong, YunWon
b4bae8173b Merge pull request #5348 from JamesClarke7283/developer-experiance-tweaks
DX improvements: Updated MSRV, added cargo deny toml file, and added lints to workspace
2024-07-01 21:09:49 +09:00
James Clarke
3c10888e3a Disabled some uneeded lits 2024-06-30 13:41:40 +01:00
James Clarke
1bd143027a Switched to system implementation of libffi and fixed minor lint errors 2024-06-30 13:29:04 +01:00
James Clarke
18d31f2d5b Merge branch 'RustPython:main' into developer-experiance-tweaks 2024-06-30 12:34:12 +01:00
James Clarke
e142d655b9 changed int_max_str_digits from -1 to 4300 to be more cpython complient (#5343)
* changed int_max_str_digits from -1 to 4300 to be more cpython complient, fixes #5139
2024-06-27 15:27:54 +09:00
James Clarke
e1ce1f66cd Updated MSRV 2024-06-25 04:56:22 +01:00
James Clarke
08c9a4d86b Added lints and deny.toml 2024-06-25 03:59:40 +01:00
Kirill Podoprigora
a620b38ba0 Add test_copyreg.py from 3.12 2024-06-24 19:00:54 +09:00
Jeong, YunWon
8e3c0cd658 Merge pull request #5314 from youknowone/datetime
Update datetime from CPython v3.12.3
2024-06-22 17:38:59 +09:00
Jeong YunWon
f709a2805d mark failing tests 2024-06-22 16:18:34 +09:00
Jeong, YunWon
adbadfc4f5 Merge pull request #5342 from youknowone/getstate
object.__getstate__
2024-06-22 15:49:23 +09:00
CPython Developers
9c7a9cbace Update datetime from CPython v3.12.3 2024-06-22 15:27:19 +09:00
CPython Developers
1333688a4e update copyreg from CPython 3.12.3 2024-06-22 15:24:42 +09:00
Jeong YunWon
17852b78d5 mark hanging test 2024-06-22 15:24:42 +09:00
Jeong YunWon
dd15ae5560 Unmark successful tests 2024-06-22 15:24:42 +09:00
Jeong YunWon
9d33990199 object.__getstate__ 2024-06-22 15:15:49 +09:00
Cherry
866e7cf371 Make a shared run function in ThreadedVirtualMachine (#5337) 2024-06-22 14:43:07 +09:00
Jeong YunWon
f5c1596ddf PyCFunction_GET_SELF 2024-06-22 00:40:30 +09:00
Jeong YunWon
64cff172a1 pymethod(raw) 2024-06-22 00:40:13 +09:00
kingler
c10b99b29a Update keyword.py and test_keyword.py from CPython v3.12.0 2024-06-21 22:37:15 +09:00
Snowapril
bca90f191c Fix missing_transmute_annotations, legacy_numeric_constants added in Rust 1.79.0 clippy (#5339) 2024-06-16 20:26:22 +09:00
Jonathan Rotter
7996a10116 await in list comprehension (#5334)
* check if comprehension element contains await

* force execution to pause in async gen
2024-05-27 16:54:56 +09:00
Dmitry Mottl
db4562f67d Fixes a typo 2024-05-22 10:07:44 +09:00
Noa
3fd0382a51 Use RPITIT where applicable 2024-05-21 14:47:10 +09:00
Noa
3e98e5e86c Bump libsqlite3-sys 2024-05-21 09:05:49 +09:00
Jeong YunWon
7b17965b26 Enable macOS SSL 2024-05-21 09:05:26 +09:00
Jeong YunWon
5c050d5779 feature ssl-vendor implies ssl 2024-05-20 19:41:51 +09:00
Jeong, YunWon
5d6d1a6da7 OpenSSL3 in README 2024-05-20 13:07:36 +09:00
Daniel Chiquito
b59d876e76 Make __bases__ mutable (#5325)
* Make PyType.bases and PyType.mro mutable

* Add a set function for __bases__

* Re-enable fixed test_descr tests
2024-05-20 12:28:07 +09:00
Jeong YunWon
9028aede6e Reduce rc ops from best_base 2024-05-19 19:24:52 +09:00
Snowapril
aa0353a501 Fix best_base to select proper base class (#5324)
* add __basicsize__ getter

* modify best_base function to get proper base

* inherit basicsize from the base type

---------

Signed-off-by: snowapril <sinjihng@gmail.com>
2024-05-19 15:48:56 +09:00
Jeong YunWon
63c3f31c99 skip flaky test in test_context 2024-05-17 14:02:40 +09:00
Jeong YunWon
2fe44af8ba time.{tzname,timezone,daylight} 2024-05-14 11:17:58 +09:00
matheusfgoncalves95
515f0bf9c2 Adding generator_stop to compile_future_features (#5320)
* Adding generator_stop to compile_future_features.

* Removing expected failure from PEP 479 unit test.
2024-05-13 23:18:55 +09:00
hydrogen602
27a52a7962 test_class.py: testTypeAttributeAccessErrorMessages fix 2024-05-13 16:25:37 +09:00
hydrogen602
61feb43aba test_builtin.py: test_type_qualname fix 2024-05-13 14:18:18 +09:00
Jonathan Rotter
07fdcb6160 Fix for test future.py test annotations (#5319)
* enable test fixed in parser

* update parser dependency
2024-05-13 08:40:14 +09:00
Daniel Chiquito
ac08f4447f Add 3.12 typing features to the compiler (#5302) 2024-05-10 20:09:04 +09:00
Jeong, YunWon
d243c90d0a Update site from CPython v3.12.3 (#5313)
* Update site from CPython v3.12.3

* mark failing test

---------

Co-authored-by: CPython Developers <>
2024-05-09 23:04:31 +09:00
Jeong, YunWon
72033ab689 Update ipaddress from CPython v3.12.3 (#5312)
Co-authored-by: CPython Developers <>
2024-05-09 15:56:11 +09:00
Jeong, YunWon
3f63501fa8 Merge pull request #5311 from youknowone/pathlib
Update pathlib from CPython v3.12.3
2024-05-09 15:46:25 +09:00
Jeong YunWon
019496e3a8 Instruction::ReturnConst 2024-05-09 15:26:16 +09:00
Jeong YunWon
52695a9ece Skip tests to avoid slot bug 2024-05-09 15:20:38 +09:00
CPython Developers
a20ce951e7 Update pathlib from CPython v3.12.3 2024-05-09 15:20:25 +09:00
Jeong YunWon
85fa157d5a Update test_os from CPython v3.12.3 2024-05-09 11:51:05 +09:00
Jeong, YunWon
1068219c56 Update Lib/asyncio from CPython v3.12.3 (#3858)
Co-authored-by: CPython Developers <>
2024-05-09 05:02:46 +09:00
Jeong, YunWon
427ec50624 _overlapped 2024-05-09 04:41:39 +09:00
Jeong, YunWon
52ce1509a5 optimize range.__contains__ and iter_search 2024-05-09 01:26:09 +09:00
Jeong YunWon
fe06583643 posix.sysconf 2024-05-08 18:46:17 +09:00
Jeong YunWon
3e3c69b5bc Fix non-msvc build 2024-05-08 16:30:41 +09:00
Jeong YunWon
3b0802535d _winapi.NULL 2024-05-08 16:30:41 +09:00
Jeong YunWon
75415090bd ConfName -> PathConfName 2024-05-08 14:50:51 +09:00
Jeong YunWon
6f664cb05a Fix initgroups nul error 2024-05-08 14:50:51 +09:00
Jonathan Rotter
a8ea67178d adjusted SyntaxError args and __str__() to match CPython (#5294) 2024-05-08 12:57:03 +09:00
Jeong, YunWon
64a464aefb Update audiotests from CPython 3.12.3 (#5304)
Co-authored-by: moonlightaria <edch66@gmail.com>
2024-05-08 12:52:33 +09:00
jparag
67885ad9fa Update gettext.py and related test to 3.12 version (#5287)
Co-authored-by: Parag Jain <parag.jain2@capitalone.com>
2024-05-08 00:02:59 +09:00
Jeong YunWon
e5bf72e897 contextvars 2024-05-07 22:43:21 +09:00
Jeong YunWon
1f62190eef Use Sequence protocol for in operation 2024-05-07 21:33:14 +09:00
Jeong YunWon
f839e6cc79 more #[pyclass(Py, PyRef)] 2024-05-06 01:32:46 +09:00
Jeong YunWon
52aad1ec08 DefaultConstructor impls Constructor; Unconstructible is not anymore 2024-05-06 00:54:39 +09:00
Jeong YunWon
5f0d1252b6 suppress empty-doc warning 2024-05-04 02:00:18 +09:00
Jeong YunWon
3370f0f23d Update wasm-bindgen 2024-05-04 02:00:18 +09:00
Jeong YunWon
e1574b1485 sys.{get,set}_asyncgen_hooks 2024-05-02 13:45:03 +09:00
Jeong YunWon
78fae736f9 sys.get_coroutine_origin_tracking_depth 2024-05-01 21:32:14 +09:00
Jeong YunWon
ed3811a65c wasi select module 2024-05-01 17:54:24 +09:00
Jeong, YunWon
4e915de35d Merge pull request #5290 from youknowone/subprocess
Update subprocess from CPython 3.12.3
2024-05-01 16:24:35 +09:00
CPython Developers
94e6648ee5 Update selectors from CPython v3.12.3 2024-05-01 15:59:23 +09:00
Jeong YunWon
75a985e63e mark failng tests 2024-05-01 15:59:23 +09:00
CPython Developers
5714558785 Update subprocess from CPython 3.12.3 2024-05-01 04:23:14 +09:00
Jeong, YunWon
c3ccb5b7bf Merge pull request #5289 from youknowone/fixes
Align Settings to CPython PyConfig
2024-04-30 23:18:43 +09:00
Jeong YunWon
87849cda4f fix grammar 2024-04-30 23:18:14 +09:00
Jeong YunWon
f62114c7eb zlib-ng as default 2024-04-30 23:18:14 +09:00
Jeong YunWon
24f57dde2f Align Settings to PyConfig 2024-04-30 23:18:14 +09:00
Jeong, YunWon
cdfcd8a4c9 Merge pull request #5288 from youknowone/enable-wasi-unittest
Enable wasi unittest
2024-04-30 23:13:07 +09:00
Jeong YunWon
1bc1370701 Add wasi unittest CI 2024-04-30 22:21:56 +09:00
Jeong YunWon
6b7b080827 Add sys._stdlib_dir when freeze-stdlib 2024-04-30 22:21:56 +09:00
Jeong YunWon
3556e1320d wasm os.getpid 2024-04-29 14:42:02 +09:00
Jeong YunWon
621640ca71 Add const-only signals for wasm32 2024-04-28 10:31:38 +09:00
Jeong YunWon
2c0e439d0d Disable jit for macos from CI to avoid arm64 CI failure 2024-04-28 00:07:55 +09:00
Jeong YunWon
a392d8425e Fix wasmer example 2024-04-26 20:57:32 +09:00
Jeong YunWon
0273d78de9 Update signal from CPython 3.12.3 2024-04-26 01:39:02 +09:00
Jeong YunWon
ed51d8dcf6 upgrade geckodriver 2024-04-25 23:11:56 +09:00
Jeong, YunWon
3d78ca8b09 Merge pull request #5263 from youknowone/multiprocessing
Update multiprocessing from CPython 3.12.3
2024-04-25 22:05:11 +09:00
Jeong YunWon
1cec856c63 skip flaky test_socket sendmsg tests in macos 2024-04-25 22:04:40 +09:00
Jeong, YunWon
6212c81ec6 Merge pull request #5276 from youknowone/async-for-comprehension
Async for comprehension
2024-04-25 21:00:22 +09:00
Jeong YunWon
408459b883 vm runtime debug prints 2024-04-25 20:34:53 +09:00
Jeong YunWon
f6d88042b5 Uncomment test_grammar tests 2024-04-25 20:23:34 +09:00
Jeong YunWon
b58bdc9e32 Uncomment async for tests 2024-04-25 20:23:34 +09:00
Jeong YunWon
f3501f44cb Basic async for comprehension support 2024-04-25 20:23:25 +09:00
HyeockJinKim
61f37b10e2 implement async for function in compiler 2024-04-25 20:20:58 +09:00
Aaron Long
2856aff757 Begin async list comprehension implementation 2024-04-25 20:20:22 +09:00
Jeong YunWon
3949ecc054 implement more GetANext 2024-04-25 19:16:48 +09:00
Jeong YunWon
acd9ea57d7 Fix async for block 2024-04-25 17:40:15 +09:00
Jeong YunWon
794a2a1892 remove pop-push 2024-04-25 17:40:15 +09:00
hydrogen602
af8bed6d3f filter out doc strings with opt level 2 2024-04-25 16:11:04 +09:00
Jeong YunWon
462f54f906 Fix with __exit__ error handling 2024-04-25 15:17:50 +09:00
Jeong YunWon
1c4e99cf2c popblock debug 2024-04-25 15:17:50 +09:00
Jeong YunWon
a349b9bdfe frame debug codes 2024-04-24 21:46:14 +09:00
Jeong YunWon
75e790836a debuggable 2024-04-24 20:44:17 +09:00
hardlydearly
5ba677cd36 chore: fix some typos in comments
Signed-off-by: hardlydearly <799511800@qq.com>
2024-04-24 17:52:58 +09:00
Jeong, YunWon
a7b761a89c windows symlink flag 2024-04-24 02:43:01 +09:00
Jeong, YunWon
6e0b00d60c Merge pull request #5262 from youknowone/ntpath
Update ntpath from CPython 3.12.3
2024-04-24 01:46:49 +09:00
Jeong YunWon
c107a938be remove -u all from mac/windows 2024-04-24 01:46:27 +09:00
Jeong YunWon
c6fc9cee8e Update filecmp from CPython 3.12.2 2024-04-24 01:46:27 +09:00
Jeong YunWon
99a4bf5eb5 remove import dummp_os 2024-04-24 01:46:27 +09:00
CPython Developers
46410ff933 Add test_multiprocessing from CPython 3.12.3 2024-04-24 01:15:34 +09:00
CPython Developers
75e868e71e Update multiprocessing from CPython 3.12.3 2024-04-24 01:15:08 +09:00
Jeong, YunWon
53b1b4d682 Mark failing tests in test_ntpath 2024-04-24 00:54:12 +09:00
CPython Developers
566d9bee5c Update ntpath from CPython 3.12.3 2024-04-24 00:54:12 +09:00
Jeong, YunWon
b8bf65d68e os.listdrives 2024-04-24 00:54:09 +09:00
Jeong YunWon
075c69a3cd Skip setattr(__doc__) if not given 2024-04-23 22:44:22 +09:00
Jeong, YunWon
ada10067dd Merge pull request #5260 from youknowone/update-pickle
Update pickle from CPython 3.12.3
2024-04-23 21:37:32 +09:00
CPython Developers
d0f680b379 Update pickletools from CPython 3.12.3 2024-04-23 18:05:50 +09:00
Jeong YunWon
ca2c1d0b48 Update pickle from CPython 3.12.2 2024-04-23 16:46:16 +09:00
Jeong YunWon
5fd5939395 skip flaky socket test in macOS 2024-04-23 16:24:15 +09:00
Jeong, YunWon
e5ca631b52 Add bare ExceptionGroup (#5259) 2024-04-23 16:23:31 +09:00
Jonathan Rotter
3313fdeb32 test_builtin.py test_compile unit test fix (#5251)
* compile accepts memoryview now

* Update test_compile to what it is in CPython3.12

* compile optimize flag

* added undocumented flags to flag validator
2024-04-23 14:07:02 +09:00
Jeong, YunWon
2bd8ff0b6c Merge pull request #5257 from youknowone/os-posix
Update os/posix/posixpath from CPython 3.12.2
2024-04-23 13:33:40 +09:00
CPython Developers
5c9d6d455d Update genericpath/posixpath from CPython 3.12.2 2024-04-23 12:56:43 +09:00
CPython Developers
8f6cf6fef7 Update test_posix from CPython 3.12.2 2024-04-23 12:56:43 +09:00
CPython Developers
a5f8d42d34 Update os from CPython 3.12.2 2024-04-23 12:56:32 +09:00
CPython Developers
153ec2823d Update __future__ from CPython 3.12.2 2024-04-23 11:57:46 +09:00
CPython Developers
9f65a30504 Update encodings from CPyhton 3.12.2 2024-04-22 12:46:32 +09:00
CPython Developers
b018123f19 Update getopt from CPython 3.12.2 2024-04-22 12:46:32 +09:00
CPython Developers
97e3e969e4 Update glob from CPython 3.12.2 2024-04-22 12:46:32 +09:00
Noa
84099514e6 Implement socket.socket.sendmsg (#5205)
* Implement socket.socket.sendmsg

* debugger-friendly newlines

* Fix control_buf error on macOS

---------

Co-authored-by: Jeong YunWon <jeong@youknowone.org>
2024-04-22 11:21:10 +09:00
Jeong, YunWon
3286e683e6 win32_xstat (#5247)
* win32_xstat

* vm::windows

* Add file_id
2024-04-21 22:02:09 +09:00
hydrogen602
be6ea2a7b8 fix for test test_bool.BoolTest.test_subclass 2024-04-21 07:09:08 +09:00
Jeong, YunWon
4a939141f6 Fix macos shutil (#5248)
* Fix macos shutil

* chmod+follow_symlinks=False calls lchmod
2024-04-21 07:07:58 +09:00
Jonathan Rotter
1034477f1e unit test fix - only allow catching exceptions - test_catch_non_BaseException (#5249)
* test_baseexception.test_catch_non_BaseException

* enabling test_except_star_invalid_exception_type
2024-04-21 01:30:20 +09:00
Jeong, YunWon
49cfcd817d remove some skipIf and enable shutil CI for windows (#5235)
* mark failing shtuil tests in windows

* unsetenv raise more informed error

* Remove a few skipIf for windows
2024-04-20 03:00:27 +09:00
Jeong, YunWon
39822d7e57 use less vm.new_os_error (#5245)
* vm.new_errno_error

* replace new_os_error in mmap

* replace posix new_os_error to new_errno_error
2024-04-20 01:32:33 +09:00
Jeong YunWon
142d333c5c Fix os.unsetenv 2024-04-20 00:36:00 +09:00
Jeong YunWon
13c491712b add six install to CI 2024-04-19 22:45:25 +09:00
Jeong, YunWon
5c527c2a28 Fix shutil test_copy_dir 2024-04-19 22:04:22 +09:00
toofooboo
41979f0823 chore: fix some typos in comments (#5243)
Signed-off-by: toofooboo <cmaker@foxmail.com>
2024-04-19 21:10:22 +09:00
Jeong, YunWon
192b0a8fb5 fileutils and fstat (#5242) 2024-04-19 19:21:35 +09:00
Jonathan Rotter
6a7be1e142 compat fix for testHashStuff in test_class.py (#5240)
* compat fix for testHashStuff in test_class.py

* accidently fixed 3 more tests
2024-04-19 16:44:23 +09:00
Jeong, YunWon
b3666060d4 better OSError.filename (#5239)
* Fix OSError message

* IOError with filename

* more filenames
2024-04-19 15:53:11 +09:00
Jeong, YunWon
c9c07a6bbc vm/ospath module 2024-04-19 13:25:19 +09:00
Jeong, YunWon
d57287b89b Fix OSError.winerror and errno/winerror converions 2024-04-19 04:56:25 +09:00
Jeong, YunWon
79e7015a32 CreateJunction 2024-04-19 03:00:33 +09:00
Jeong, YunWon
6c4ee951e0 fix windows OSError 2024-04-18 10:23:12 +09:00
Jeong YunWon
f0f4633346 Fix nightly clippy 2024-04-18 03:37:29 +09:00
hydrogen602
6c37e8f4e7 compat fix for test_field_metadata_custom_mapping in test_dataclasses.py 2024-04-17 21:52:10 +09:00
CPython Developers
4d05416ce3 Update ensurepip from CPython 3.12 2024-04-17 12:03:31 +09:00
Lee Dogeon
60993b9d23 Bump shutil to 3.12.3 (#5228)
* Bump shutil to 3.12.3

* Implement windows platform target `is_junction` with junction library

* Add _winapi.NeedCurrentDirectoryForExePath

---------

Co-authored-by: Jeong, YunWon <jeong@youknowone.org>
2024-04-17 01:33:14 +09:00
Moreal
959e7c11ce Unmark fixed tests 2024-04-15 16:22:53 +09:00
Moreal
21ae739eec Check downcast to type first 2024-04-15 16:22:53 +09:00
Moreal
b1c3c9a9d6 Bump CPython version to 3.12.3 in CI 2024-04-14 13:07:13 +09:00
Moreal
bf985c8ac6 Fix None.__ne__ bug 2024-04-14 13:07:13 +09:00
Moreal
21c5eae717 Add testcase for __ne__ method 2024-04-14 13:07:13 +09:00
Lee Dogeon
4d5cf249a0 Allow update __class__ for mutable types or module subclasses (#5225)
* Allow update `__class__` for module subclasses

* Set `IMMUTABLETYPE` flag for static classes

* Unmark fixed tests
2024-04-14 13:03:48 +09:00
Jeong YunWon
8c7b811135 Fix _sha512 support 2024-04-11 21:17:53 +09:00
Jeong, YunWon
940b879950 Merge pull request #5222 from youknowone/vm-build
Fix rustpython-vm --no-default-features build
2024-04-11 15:29:43 +09:00
Jeong YunWon
ccf650d4ca Fix rustpython-vm --no-default-features build 2024-04-10 19:15:22 +09:00
Jeong, YunWon
6342f16eb5 Merge pull request #5219 from youknowone/0.3.1
0.3.1
2024-04-10 17:41:28 +09:00
Jeong YunWon
8816cd41d5 0.3.1 2024-04-10 17:39:57 +09:00
Jeong, YunWon
4b190eb412 Merge pull request #5221 from youknowone/update-indexmap
Update indexmap
2024-04-09 12:50:42 +09:00
Jeong YunWon
9f24841f6d remove -u from macOS/windows 2024-04-09 11:18:14 +09:00
Jeong YunWon
a0b6c36928 Update indexmap 2024-04-09 11:17:25 +09:00
Jeong, YunWon
3f691de7a3 Update chrono (#5220) 2024-04-09 04:13:39 +09:00
Jeong, YunWon
247572863a rustpython_vm::import::import_source (#5214)
* rustpython_vm::import::import_source

* always print exceptions when panic by expect_pyresult

if users want simple panic, Result::expect could be used.
2024-04-09 02:37:30 +09:00
Jeong, YunWon
5a5ac35f36 Update a few deps (#5217)
Co-authored-by: ognevnydemon <maksapple2306@gmail.com>
2024-04-09 02:12:15 +09:00
Jeong, YunWon
220594f263 always print exceptions when panic by expect_pyresult (#5215)
if users want simple panic, Result::expect could be used.
2024-04-09 01:04:27 +09:00
Jeong, YunWon
f541ca9dda Require cpu resource to test_search_anchor_at_beginning (#5218)
cherry-pick python/cpython#117616

Co-authored-by: CPython Developers <>
2024-04-09 01:01:46 +09:00
Jeong, YunWon
6a64af1066 Merge pull request #5213 from youknowone/vm-finalize
Add vm.finalize and enter/run docs
2024-04-08 01:43:48 +09:00
Jeong YunWon
35141266c1 vm.enter_and_expect 2024-04-08 00:43:17 +09:00
Jeong YunWon
6adb72727e Add vm.finalize and enter/run docs 2024-04-08 00:14:28 +09:00
Jeong, YunWon
6eed8f42c7 split vm.import and vm.import_from (#5212) 2024-04-08 00:07:38 +09:00
Jeong, YunWon
ae72316629 Merge pull request #5209 from Poppro/itertools-batched
Add itertools.batched Support
2024-04-06 19:26:02 +09:00
Jeong YunWon
90ab0e33a2 apply rustfmt 2024-04-06 19:15:15 +09:00
Hunter Harloff
9f7eb08fca Change to overflow error 2024-04-06 19:14:58 +09:00
Hunter Harloff
6a53ec3c5d Handle usize > usize::MAX case 2024-04-06 19:14:58 +09:00
Hunter Harloff
c5550cd466 Simplify Optional Syntax
Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com>
2024-04-06 19:14:58 +09:00
Hunter Harloff
f6569313d7 Move test_itertools to CPython 3.12 Spec 2024-04-06 19:14:58 +09:00
Hunter Harloff
880036c0ae CPython Compliance: throw ValueError before TypeError 2024-04-06 19:14:58 +09:00
Hunter Harloff
da53eeea57 Add itertools.batched Support 2024-04-06 19:14:58 +09:00
Jeong, YunWon
e349b0f18b Merge pull request #5125 from qingshi163/sre-dev
Update Sre Engine Implementing to CPython 3.12
2024-03-30 18:50:00 +09:00
Jeong, YunWon
34ffa93945 Remove commented out code 2024-03-30 18:49:33 +09:00
Jeong, YunWon
7be6308248 Merge pull request #5206 from dchiquito/fix-test-zipfile
Increase threshold for zipfile test_many_opens
2024-03-28 17:03:47 +09:00
Kangzhi Shi
57e7f4db95 bump string.py mark passed tests 2024-03-22 14:59:19 +09:00
Jeong YunWon
f5fbb5b06f replace sre-engine to rustpython-sre_engine 2024-03-22 14:59:19 +09:00
Kangzhi Shi
d9375b9fe1 impl re.template(),
template_compile template_expand subx
2024-03-22 14:59:19 +09:00
Kangzhi Shi
1e3d57817c Replace re_test.py from CPython 3.12 and mark failed tests 2024-03-22 14:59:09 +09:00
Kangzhi Shi
ebe555203a Replace Lib/sre_* from CPython 2024-03-22 14:54:02 +09:00
Kangzhi Shi
280337a305 Add Lib/re/* from CPython 3.12 2024-03-22 14:54:02 +09:00
Jeong, YunWon
02cec859e5 Merge pull request #5202 from youknowone/sre-engine
Import sre-engine repository to main RustPython
2024-03-22 14:53:25 +09:00
Jeong YunWon
1dd9a2fbe4 suppress clippy warnings 2024-03-22 11:28:49 +09:00
Daniel Chiquito
df363c0ba7 Skip typing test which causes other failures (#5207) 2024-03-22 10:26:40 +09:00
Daniel Chiquito
90724b32ec Implement new clippy lints (#5208)
* Implement new clippy lints

clippy was just updated and has a few minor issues with the code base.

* Forgotten lint hidden behind feature
2024-03-22 10:25:53 +09:00
Daniel Chiquito
0a24e106ba Increase threshold for zipfile test_many_opens
It turns out that there are many other tests that can impact
test_many_opens by leaving unclosed file handles. Rather than fix them
all, it is easier to simply increase the threshold for the problematic
test.
2024-03-21 13:37:46 -04:00
Daniel Chiquito
e6c73883ea Revert test skip 2024-03-21 13:36:28 -04:00
Daniel Chiquito
e315077630 Add TODO: RUSTPYTHON to skip reason 2024-03-21 11:31:03 -04:00
Daniel Chiquito
85c427b842 Reset exception in WithCleanupFinish (#5203)
Context managers have an `__exit__` function that returns a boolean-like
object. If the object is truthy, then exceptions are suppressed.

If an exception was thrown while resolving that boolean, it would leak
and live on in the error stack, getting tacked on to all future
exceptions. This caused several mysterious test failures which would
only trigger after this very specific event was tested in `test_with`.

The solution is to move a call to `vm.set_exception()` before
attempting the `try_to_bool()` which threw the error.

Minimal example to reproduce the bug:
```py
import sys
import traceback

class cm(object):
    def __init__(self):
        pass

    def __enter__(self):
        return 3

    def __exit__(self, a, b, c):
        class Bool:
            def __bool__(self):
                1 // 0
        return Bool()

try:
    with cm():
        raise Exception("Should NOT see this")
except ZeroDivisionError:
    print("exception caught, as expected")

print("There should now be no exception")
traceback.print_exc()
print(sys.exc_info())
```
2024-03-22 00:12:01 +09:00
Daniel Chiquito
5ee5531f32 Properly unload modules between tests (#5192)
There seems to have been a bug in the libregrtest code which unloaded
modules between tests. The previous state was calculated using
`sys.modules.keys()`, which is actually a mutable object that is updated
as the underlying `sys.modules` is updated. The result was that modules
were not unloaded between tests, which is the root cause for
`test_unittest` failing when run after `test_import` and
`test_importlib`.

This code is copied from 3.12. Ideally all of `libregrtest` should
probably be updated as it seems wildly out of date, but that's a lot
more work.
2024-03-21 21:51:57 +09:00
Jeong, YunWon
3737f2a091 make adding a single module simpler for interpreter users (#4792) 2024-03-21 21:48:29 +09:00
Daniel Chiquito
ac78517044 Skip TestScander.test_uninstantiable (#5204)
This test was marked as an expected failure. Because the garbage
collector is missing, that meant that the `os.scandir` object went
unclosed. This object was squatting on the file descriptors of all the
files contained in the test directory, which was breaking test_zipfile.
2024-03-21 14:44:03 +09:00
Jeong YunWon
12601d0b44 integrate sre_engine crate to workspace 2024-03-18 17:24:49 +09:00
Jeong YunWon
b3a606d9df Add 'vm/sre_engine/' from commit '21fc2059b70ebd5bf4a7c524c40e7d4347e065dc'
git-subtree-dir: vm/sre_engine
git-subtree-mainline: 426e582ba0
git-subtree-split: 21fc2059b7
2024-03-18 17:05:07 +09:00
Nikita Sobolev
426e582ba0 Remove incorrect @expectedFailures from test_cmd_line (#5201)
After you suggestion in https://github.com/python/cpython/issues/116504#issuecomment-1999239012 I went to take a look at `test_cmd_line` in RustPython (it was so long ago I contributed to this amazing project, so may thing had changed!), and I've noticed this.

This is a problem, here' the simplest demo:

```python
import unittest

class TestMe(unittest.TestCase):
    @unittest.expectedFailure
    def test_me(self):
        def run():
            raise ValueError

        with self.subTest(run=run):
            run()

if __name__ == '__main__':
    unittest.main()
```

This works as expected:

```
» ./python.exe ex.py
x
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK (expected failures=1)
```

This does not:

```python
import unittest

class TestMe(unittest.TestCase):
    def test_me(self):
        @unittest.expectedFailure
        def run():
            raise ValueError

        with self.subTest(run=run):
            run()

if __name__ == '__main__':
    unittest.main()
```

Produces:

```
» ./python.exe ex.py
E
======================================================================
ERROR: test_me (__main__.TestMe.test_me) (run=<function TestMe.test_me.<locals>.run at 0x1057a2150>)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/sobolev/Desktop/cpython2/ex.py", line 10, in test_me
    run()
    ~~~^^
  File "/Users/sobolev/Desktop/cpython2/ex.py", line 7, in run
    raise ValueError
ValueError

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)
```

So, I propose to remove these decorators, let's only keep `TODO` comments to indicate separate failures.
2024-03-15 22:15:45 +09:00
Kirill Podoprigora
92c8b371ae Update colorsys.py and test_colorsys.py to 3.12 (#5198) 2024-03-13 15:22:57 +09:00
Kirill Podoprigora
d8f2bd04ac Update cgitb.py to 3.12 (#5197) 2024-03-13 15:22:24 +09:00
Kirill Podoprigora
855fa1411f Update ftplib and test_ftplib to 3.12 (#5196) 2024-03-13 07:35:16 +09:00
Kirill Podoprigora
4e7b3bc8f2 Update pprint.py and test_pprint.py to 3.12 (#5195) 2024-03-12 22:36:10 +09:00
Kirill Podoprigora
83d1ad8a2c Update test_operator.py to 3.12 (#5194) 2024-03-12 22:35:21 +09:00
Kirill Podoprigora
7f02324dce Update Lib/test/test_hmac.py to 3.12 version (#5188) 2024-03-11 22:04:35 +09:00
Jeong, YunWon
d453026d19 Merge pull request #5191 from dchiquito/fix-failing-tests
Fix convenient failing tests
2024-03-11 15:03:39 +09:00
wellweek
2fde8e91e5 fix some typos (#5187)
Signed-off-by: wellweek <xiezitai@outlook.com>
2024-03-11 15:01:37 +09:00
Daniel Chiquito
23ebbd021b Skip test_format.test_locale
I had previously `test_locale` as expected to fail, as it did indeed
fail on my system due to unimplemented functionality. As it happens, it
passes in CI because the locale settings used there (`C`, I believe)
just happen to format integers the same with "%d" as "%n".

I mistakenly un-marked it because I thought I misunderstood the problem.
2024-03-10 22:53:39 -04:00
Daniel Chiquito
9b974bda0d Re-enable test_format.test_locale
Technically speaking, my system was misconfigured, leading me to disable
the test in the first place.

`test_locale` calls `locale.setlocale(locale.LC_ALL, '')`, which reads
the value of the `LANG` environment variable and uses that to look up
and reset all the locale settings. My system has `LANG=en_US.UTF-8`,
which is apparently not what this test was expecting. If `LANG` is unset
or set to `C`, the test passes, as it does in CI.
2024-03-10 22:30:41 -04:00
Daniel Chiquito
de7e4e49da Disable broken test_socket.py tests
There are a substantial number of socket tests that are disabled due to
`bind(): bad family` errors. It seems like RustPython only supports a
small subset of the required connection families, so the failing tests
are broken for the same reasons.
2024-03-09 18:22:57 -05:00
Daniel Chiquito
ead42beff6 Disable test_locale in test_format.py
See https://github.com/RustPython/RustPython/issues/5181
2024-03-09 18:22:57 -05:00
Daniel Chiquito
35229721ea Fix test_cmd_line.py
The failing test was unsetting `PYTHONPATH`, but neglecting to unset
`RUSTPYTHONPATH`, which obviously was not significant for the original
CPython test. Including `RUSTPYTHONPATH` in the test fixes it.
2024-03-09 18:22:57 -05:00
rrupy
2f8e5189d3 Use ast::Suite::parse instead of deprecated parse_program. (#5186) 2024-03-05 22:37:42 +09:00
Jeong, YunWon
4c8cd67db9 Merge pull request #5176 from Blues-star/csv
Update csv.py and test_csv.py from CPython v3.12
2024-03-05 22:37:09 +09:00
Blues-star
54247df801 implement more csv features
Co-authored-by: Jeong, YunWon <jeong@youknowone.org>
2024-03-05 15:10:35 +09:00
Blues-star
e4be47a08b Mark failing tests as expectedFailure 2024-03-05 15:10:35 +09:00
Blues-star
d2bf69e354 Update test_csv.py from CPython v3.12.0 2024-03-05 15:10:35 +09:00
Blues-star
88ee64d585 Update csv.py from CPython v3.12.0 2024-03-05 15:10:35 +09:00
Jeong, YunWon
d0c827ed38 Merge pull request #5184 from youknowone/symboltable
better symboltable error message
2024-02-29 22:19:46 +09:00
Jeong YunWon
d26766b7bc better symboltable error message 2024-02-29 21:30:31 +09:00
Jeong, YunWon
e6c6f966f7 Merge pull request #5180 from dchiquito/3.12-collections
Update `_collections_abc.py` and `test_collections.py` to 3.12.2
2024-02-26 13:20:36 +09:00
Daniel Chiquito
407f251866 Un-skip passing typing test
I missed that the typing test I disabled was on a base test class.
Moving the expected failure to the subclass allows the passing test to
pass.
2024-02-24 13:33:17 -05:00
Daniel Chiquito
6cd9e54427 Update test_ordered_dict.py to 3.12.2 2024-02-24 12:00:07 -05:00
Daniel Chiquito
c9ec4507ad Disable broken test_repr in test_typing.py
This should be resolved when `typing.py` and `test_typing.py` are
updated to 3.12.
2024-02-24 11:58:26 -05:00
Daniel Chiquito
9481df23e1 Disable test_Buffer
This test will not work until the `__buffer__` and `__release_buffer__`
methods are implemented on the appropriate builtin types, which is
outside the current scope.
2024-02-24 11:02:41 -05:00
Daniel Chiquito
0bd8c2504c Update test_collections.py to 3.12.2 2024-02-23 20:16:16 -05:00
Daniel Chiquito
bcb35919a4 Update collections/__init__.py to 3.12.2 2024-02-23 20:14:41 -05:00
Daniel Chiquito
5dc6d5582a Update _collections_abc.py to 3.12.2 2024-02-23 20:13:41 -05:00
Jeong, YunWon
a8ab7dd388 Merge pull request #5175 from Blues-star/copy
Update `copy.py` and `test_copy.py` from CPython v3.12
2024-02-21 12:02:54 +09:00
Jeong, YunWon
7c722c23a5 Merge pull request #5174 from Blues-star/code
Update code.py and test_code.py from CPython v3.12
2024-02-21 12:02:05 +09:00
Blues-star
83b8c3a3fc Update test_copy.py from CPython v3.12.0 2024-02-20 19:38:58 +08:00
Blues-star
36a36b200d Update copy.py from CPython v3.12.0 2024-02-20 19:37:42 +08:00
Jeong, YunWon
d297e73a55 Merge pull request #5172 from Blues-star/main
Update `configparser.py` and `test_configparser.py` from CPython v3.12
2024-02-20 18:26:05 +09:00
Blues-star
572053df82 est_co_lnotab_is_deprecated, 'test_invalid_bytecode' and est_code_hash_uses_bytecode test exceptions, add TODO 2024-02-19 20:30:06 +08:00
Blues-star
def5661728 Update test_code.py from CPython v3.12.0 2024-02-19 20:13:59 +08:00
Blues-star
078cd0d88c Update code.py from CPython v3.12.0 2024-02-19 20:04:09 +08:00
Blues-star
d061837467 Update test_configparser.py from CPython v3.12.0 2024-02-19 19:25:17 +08:00
Blues-star
defc3ac7f1 Update configparser.py from CPython v3.12.0 2024-02-19 19:22:38 +08:00
Dmitry Erlikh
97a0705d2e Fix Windows CI (#5168)
* pin openssl version for windows CI

* use cargo vcpkg

* install openssl with vcpkg

* use Swatinem/rust-cache right after dtolnay/rust-toolchain

* cargo install --target-dir=target cargo-vcpkg
---------

Co-authored-by: Dmitry Erlikh <d.erlikh@smartrecruiters.com>
2024-02-19 02:36:18 +09:00
Jeong, YunWon
a88c2fe000 Fix miri test failure (#5170) 2024-02-19 00:39:47 +09:00
Jeong, YunWon
2a62ef7344 Merge pull request #5169 from Blues-star/main
Update `calendar.py` and `test_calendar.py`  from CPython v3.12
2024-02-19 00:30:15 +09:00
Blues-star
258342e1ca Update test_calendar.py from CPython v3.12.0 2024-02-18 15:36:45 +08:00
Blues-star
69e8e4be43 Update calendar.py from CPython v3.12.0 2024-02-18 15:33:01 +08:00
Jeong, YunWon
1c93c1630b Merge pull request #5164 from dchiquito/frozenset-hash
Use CPython hash algorithm for frozenset
2024-02-10 14:55:24 +09:00
Jeong, YunWon
ec9f2cedd1 Merge pull request #5165 from dchiquito/unused-collections-bool
Removed unused __bool__ methods
2024-02-10 12:00:55 +09:00
Jeong, YunWon
10055772d8 Merge pull request #5163 from dchiquito/fix-benches
Fix benchmarks by replacing rust-cpython with pyo3

- Replace `cpython` and `python3-sys` dependencies with `pyo3`.
- Add `html_report` feature to `criterion`, it will be required in a
  future release.
- Remove `anyhow`. It was unused and cargo cleaned it up automatically.
- Use `compile` and `exec` from within the CPython context to
compile and execute benchmarks. There's probably more overhead to that than
the internal API had, but that overhead should be consistent per
benchmark.
2024-02-10 11:49:17 +09:00
Ikko Eltociear Ashimine
61e40de32b Fix typo in slot.rs (#5162)
overriden -> overridden
2024-02-10 11:43:40 +09:00
Daniel Chiquito
074d228a7a Use correct TODO syntax
Co-authored-by: fanninpm <luxverdans@sbcglobal.net>
2024-02-09 21:41:45 -05:00
Daniel Chiquito
bf461cdebc Use CPython hash algorithm for frozenset
The original hash algorithm just XOR'd all the hashes of the elements of
the set, which is problematic. The CPython algorithm is required to pass
the tests.

- Replace `PyFrozenSet::hash` with CPython's algorithm
- Remove unused `hash_iter_unorded` functions
- Add `frozenset` benchmark
- Enable tests
- Lower performance expectations on effectiveness test
- Adjust `slot::hash_wrapper` so that it doesn't rehash the computed
  hash value in the process of converting PyInt to PyHash.
2024-02-09 21:02:40 -05:00
Daniel Chiquito
ffc52ef87c Removed unused __bool__ methods
Python does not define `list().__bool__`, `dict().__bool__`, and
`str().__bool__`, and some tests were failing because they were
defined.
I could not find any references to them and deleting them doesn't
seem to break anything.
2024-02-09 20:58:17 -05:00
Daniel Chiquito
ea1f72e92d Replace rust-cpython with pyo3 in benches
Update the actual benchmark harnesses.

Because the internal APIs previously used are no longer available, I
opted to use `compile` and `exec` from within the CPython context to
compile and execute code. There's probably more overhead to that than
the internal API had, but that overhead should be consistent per
benchmark.

If anyone cares about hyperoptimizing benchmarks then they can optimize
the harness as well.
2024-02-09 16:12:47 -05:00
Daniel Chiquito
9693ad9b11 Replace rust-cpython with pyo3 in benchmarks
The benchmarks have been broken since Python 3.10 deprecated the API
they were using to parse and execute CPython baselines. Since then
rust-cpython has been deprecated in favor of pyo3.

- Replace `cpython` and `python3-sys` with `pyo3`.
- Add `html_report` feature to `criterion`, it will be required in a
  future release.
- Remove `anyhow`. It was unused and cargo cleaned it up automatically.
2024-02-09 15:15:22 -05:00
kingiler
bdf228eb42 Fix bug in binascii uu encoding. Pass more related unit tests. (#5160)
* Fix bug in binascii, passes more unit tests.

* Pass more additional tests due to this PR.
2024-02-09 22:42:39 +09:00
Jeong, YunWon
c4b1cc69be Merge pull request #5158 from dchiquito/fix-int-rounding
Fix integer rounding
2024-02-09 13:22:36 +09:00
Daniel Chiquito
36b9219e32 Bump ahash from 0.8.3 to 0.8.7
When building the project for the Miri CI step, the latest nightly
version apparently conflicts with the older `ahash` 0.8.3. Updating to
0.8.7 fixed the build for me locally.
2024-02-07 18:40:28 -05:00
Daniel Chiquito
43d7c71a68 Fix integer rounding
The [docs](https://docs.python.org/3/library/functions.html#round)
specify that calling `round` with a negative precision removes
significant digits, so that `round(12345, -2) == 12300`. The
implementation was simply returning the original integer.

Additionally, `round(a, b)` is implemented as `(a / 10^b) * 10^b`, using
half-even rounding during the division.
2024-02-07 14:19:17 -05:00
Alin-Ioan Alexandru
3eda1cf3b4 Deprecation warning fix for __complex__ (#5152) 2024-01-25 14:54:06 +09:00
deantvv
6917b4c2ca os: ns_to_sec rounding (#5150)
In cpython, they use `_PyTime_ROUND_FLOOR` to read time.
But in RustPython, we use `[Duration::from_secs_f64](https://doc.rust-lang.org/std/time/struct.Duration.html#method.try_from_secs_f64)` to read time.

Therefore, RustPython isn't affected by the rounding issue in the way
that cpython does. We can safely ignore the `0.5*1e-9` bit in
`ns_to_sec` function.
2024-01-23 20:09:22 +09:00
Kangzhi Shi
21fc2059b7 improve: fix double count on _count 2024-01-17 16:06:30 +02:00
Kangzhi Shi
10e51ba689 improve: use adjust_cursor reduce double calc 2024-01-17 16:06:30 +02:00
Kangzhi Shi
c93ea30b3b improve use StringCursor replace index based position 2024-01-17 16:06:30 +02:00
Kangzhi Shi
118a00c012 refactor _count general case 2024-01-17 16:06:30 +02:00
Kangzhi Shi
f9b2d10c71 improve search at_beginning 2024-01-17 16:05:11 +02:00
Kangzhi Shi
2fe129252f improve ctx in _match lazy create stack vec 2024-01-17 16:05:11 +02:00
Kangzhi Shi
17e1152de6 fix assert not mark 2024-01-17 16:05:11 +02:00
Kangzhi Shi
454aa4b654 fix count not advance 2024-01-17 16:05:11 +02:00
Kangzhi Shi
169368b7f0 fix some clippy 2024-01-17 16:05:11 +02:00
Kangzhi Shi
41bdcfe221 bump version to 0.5.0 2024-01-17 16:05:11 +02:00
Kangzhi Shi
003c45dbff remove unneccesary INFO logic on main dispatch 2024-01-17 16:05:11 +02:00
Kangzhi Shi
9378497346 clearup 2024-01-17 16:05:11 +02:00
Kangzhi Shi
99ed744c57 impl atomic group & possessive repeat 2024-01-17 16:05:11 +02:00
Kangzhi Shi
a9ed7de28d fix _count general case 2024-01-17 16:05:11 +02:00
Kangzhi Shi
39c0106e87 update to cpython 3.12 op code 2024-01-17 16:05:11 +02:00
Kangzhi Shi
9070e12e0d refactor _match with nest loop 2024-01-17 16:05:11 +02:00
Kangzhi Shi
d73cc5f58c update version and dependency 2024-01-17 16:05:11 +02:00
Kangzhi Shi
a777d22a53 fix _count 2024-01-17 16:05:11 +02:00
kenny the :/
aaae566231 Raise error on power with negative number (#5143) 2024-01-12 21:16:53 +09:00
NakanoMiku
28f0fa48a4 Fix abc error messages (#5140)
Co-authored-by: Jeong, YunWon <jeong@youknowone.org>
2024-01-11 17:48:56 +09:00
Jeong, YunWon
1983138c91 Merge pull request #5148 from coolreader18/upd-nix
Update nix and socket2
2024-01-10 03:12:13 +09:00
Jeong, YunWon
9cc571be95 Fix windows stdlib build 2024-01-09 20:53:58 +09:00
Noa
602015fca1 Update nix and socket2 2024-01-09 20:53:58 +09:00
Evan Rittenhouse
1ab133dae8 None.__ne__(None) should be NotImplemented (#5124) 2024-01-08 15:03:57 +09:00
Jeong, YunWon
317f449454 Merge pull request #5126 from RustPython/dependabot/cargo/openssl-0.10.60
Bump openssl from 0.10.55 to 0.10.60
2023-12-30 18:04:30 +09:00
Jeong, YunWon
32f662ae80 Bump openssl from 0.10.55 to 0.10.62 2023-12-30 17:14:08 +09:00
Jeong, YunWon
7236109d75 Merge pull request #5144 from youknowone/clippy
Fix 1.75 clippy warnings
2023-12-30 13:16:10 +09:00
Jeong YunWon
506c8a633e Fix redox and nightly 2023-12-30 12:53:57 +09:00
Jeong YunWon
a309cb5d2c Fix 1.75 clippy warnings 2023-12-30 11:48:40 +09:00
Jeong, YunWon
5001b2e572 Merge pull request #5142 from youknowone/windows-sys
Windows sys
2023-12-30 03:48:06 +09:00
Jeong, YunWon
756088e7fb more winapi to windows-sys 2023-12-29 00:40:04 +09:00
Jeong, YunWon
4729ca3af0 Drop winapi from rustpython-vm 2023-12-28 23:44:47 +09:00
Jeong, YunWon
cccfb08835 replace winbase winnt to windows-sys 2023-12-28 22:40:49 +09:00
Jeong, YunWon
d01909a524 replace handleapi to windows-sys 2023-12-28 22:28:49 +09:00
Jeong, YunWon
ee128eac7c replace errorhandling to windows-sys 2023-12-28 22:28:33 +09:00
Jeong, YunWon
6df9732965 replace wincon to windows-sys 2023-12-28 22:28:33 +09:00
Jeong, YunWon
8a84a479f1 remove processthreadsapi 2023-12-28 22:28:33 +09:00
Jeong, YunWon
7513017e21 replace sysinfoapi to windows-sys 2023-12-28 22:28:33 +09:00
Jeong, YunWon
adf0788895 bump up windows{-sys} 2023-12-28 22:28:33 +09:00
Jeong, YunWon
fc91cd8bc7 clean up winapi features (#5141) 2023-12-28 21:51:26 +09:00
Michał Moskal
459cad8407 update indexmap to 1.9.3 (#5128) 2023-12-28 13:08:08 +09:00
Jeong, YunWon
863a5b5a49 Merge pull request #5134 from NakanoMiku39/main
Update libraries and test files from CPython v3.12
2023-12-28 13:01:13 +09:00
NakanoMiku
1c6336b350 Merge branch 'RustPython:main' into main 2023-12-28 03:26:14 +08:00
Jeong, YunWon
fb12a4c219 Merge pull request #5136 from youknowone/fix-wasi-ci
Fix wasi CI
2023-12-28 02:50:30 +09:00
Jeong YunWon
16a3edd432 Fix wasi CI 2023-12-28 02:34:41 +09:00
Jeong, YunWon
ebc8f60e21 Merge pull request #5135 from youknowone/fix-malachite-version
Fix malachite-bigint version
2023-12-28 02:33:21 +09:00
Jeong YunWon
727f97fd48 Fix malachite-bigint version 2023-12-28 01:50:02 +09:00
NakanoMiku
de626b8627 Update test_file.py from CPython v3.12.0 2023-12-25 15:19:28 +08:00
NakanoMiku
f519ffdb18 Add asynchat.py and asyncore.py from CPython v3.12.0 2023-12-24 04:11:04 +08:00
NakanoMiku
57f9478d16 Add test_bz2.py from CPython v3.12.0 2023-12-24 04:07:12 +08:00
NakanoMiku
5700fa3953 Update argparse.py from CPython v3.12.0 2023-12-24 04:05:39 +08:00
NakanoMiku
44438bffbd Update argparse.py from CPython v3.12.0 2023-12-24 04:04:25 +08:00
NakanoMiku
dd0c393b45 Update test_bdb.py from CPython v3.12.0 2023-12-24 04:03:20 +08:00
NakanoMiku
a00a387735 Update bdb.py from CPython v3.12.0 2023-12-24 03:59:38 +08:00
NakanoMiku
b4ee044fa6 Update test_base64.py from CPython v3.12.0 2023-12-24 03:58:36 +08:00
NakanoMiku
df98264dd0 Update base64.py from CPython v3.12.0 2023-12-24 03:58:10 +08:00
NakanoMiku
b6c2179893 Update test_bisect.py from CPython v3.12.0 2023-12-24 03:56:28 +08:00
NakanoMiku
b5176fdbc0 Update bisect.py from CPython v3.12.0 2023-12-24 03:54:18 +08:00
NakanoMiku
79231ee399 Edit test_abc.py 2023-12-22 03:22:28 +08:00
NakanoMiku
c0f6a2f8c3 Update test_abc.py from CPython v3.12.0 2023-12-22 03:18:10 +08:00
NakanoMiku
99531514c8 Update abc.py from CPython v3.12.0 2023-12-22 03:15:47 +08:00
Jeong, YunWon
0fab6e6063 Merge pull request #5127 from NakanoMiku39/main
Update test files from CPython v3.12.0
2023-12-01 04:14:02 +09:00
NakanoMiku
784b5f1b1c Edit test_dictviews
ExpectedFailure added at line 282
2023-11-30 21:58:26 +08:00
NakanoMiku
3945e9a4ed Update test_epoll.py from CPython v3.12.0 2023-11-30 21:06:09 +08:00
NakanoMiku
ad3f0aecaf Update test_eof.py from CPython v3.12.0 2023-11-30 21:05:18 +08:00
NakanoMiku
1d76b762c7 Update test_eintr.py from CPython v3.12.0 2023-11-30 21:01:17 +08:00
NakanoMiku
f7c7398ba8 Update test_dynamic.py from CPython v3.12.0 2023-11-30 20:55:54 +08:00
NakanoMiku
186eac5095 Update test_dtrace.py from CPython v3.12.0 2023-11-30 20:55:02 +08:00
NakanoMiku
43ede611e3 Update test_dictviews.py from CPython v3.12.0 2023-11-30 20:53:15 +08:00
NakanoMiku
d5e4af7a4b Update test_dict.py from CPython v3.12.0 2023-11-30 20:51:40 +08:00
NakanoMiku
f1991c25bc Edit test_descrtut.py
ExpectedFailures at line 469-472
I'm not able to put a @unittest.expectedFailure for each tests so I commented them out
2023-11-30 20:48:58 +08:00
NakanoMiku
c4e2d6e379 Add test_descrtut.py from CPython v3.12.0 2023-11-30 20:46:29 +08:00
NakanoMiku
b11d554c11 Edit test_descr.py
Skip test at line 1861
ExpectedFailure at line 5104
2023-11-30 20:45:05 +08:00
NakanoMiku
b05d5920ab Update test_descr.py from CPython v3.12.0 2023-11-30 20:40:19 +08:00
NakanoMiku
926c4cef27 Update test_defaultdict.py from CPython v3.12.0 2023-11-30 20:27:46 +08:00
NakanoMiku
5c6b4cbd3c Update test_decorators.py from CPython v3.12.0 2023-11-30 20:27:09 +08:00
NakanoMiku
b93199f007 Update test_context.py from CPython v3.12.0 2023-11-30 20:11:24 +08:00
NakanoMiku
07f013dae2 Edit test_complex.py
ExpectedFailures added
2023-11-30 20:07:44 +08:00
NakanoMiku
fa3eae677d Update test_complex.py from CPython v3.12.0 2023-11-30 20:06:24 +08:00
NakanoMiku
c5364ca157 Update test_compare.py from CPython v3.12.0 2023-11-30 20:01:48 +08:00
NakanoMiku
c28cca97d1 Update test_code_module.py from CPython v3.12.0 2023-11-30 19:53:37 +08:00
NakanoMiku
9a61716f74 Edit test_cmd_line_script.py 2023-11-30 19:52:25 +08:00
NakanoMiku
700f2b9c12 Update test_cmd_line_script.py from CPython v3.12.0 2023-11-30 19:49:27 +08:00
NakanoMiku
999dcbdd1b Edit test_cmd_line.py 2023-11-30 19:48:22 +08:00
NakanoMiku
06bb68a6c6 Update test_cmd_line.py from CPython v3.12.0 2023-11-30 19:45:47 +08:00
ChenyG
dc4f6994fb Support slice hash (#5123)
* make slice object hashable

* Update test_slice.py from CPython v3.12

* remove TODO

* remove outdated tests
2023-11-25 13:11:17 +09:00
Jeong, YunWon
8fc2c39d62 Merge pull request #5122 from NakanoMiku39/main
Update test files from CPython v3.12.0
2023-11-23 23:10:36 +09:00
ChenyG
4d6a180638 Update ast, test_ast from CPython 3.12.0 (#5121) 2023-11-23 23:06:12 +09:00
NakanoMiku
460e9833f0 Edit Lib/test/test_cmath.py
ExpectedFailure added at line 628
2023-11-23 16:38:32 +08:00
NakanoMiku
c8256c5450 Update Lib/test/test_cmath.py from CPython v3.12.0 2023-11-23 16:36:59 +08:00
NakanoMiku
e06f5ccfe4 Update Lib/test/test_class.py from CPython v3.12.0 2023-11-23 16:31:27 +08:00
NakanoMiku
68dc5ca052 Edit Lib/test/test_c_locale_coercion.py
ExpectedFailure added at line 247
2023-11-23 16:21:42 +08:00
NakanoMiku
ac21576ab9 Add Lib/test/test_c_locale_coercion.py from CPython v3.12.0 2023-11-23 16:19:42 +08:00
NakanoMiku
b0ebe58636 Update Lib/test/test_bufio.py from CPython v3.12.0 2023-11-23 15:57:39 +08:00
NakanoMiku
7833fd9b12 Edit Lib/test/test_bool.py 2023-11-23 15:53:48 +08:00
NakanoMiku
81208637f5 Update Lib/test/test_bool.py from CPython v3.12.0 2023-11-23 15:51:02 +08:00
NakanoMiku
5303b33c8b Update Lib/test/test_bigmem.py from CPython v3.12.0 2023-11-23 15:42:40 +08:00
NakanoMiku
f54b80f88c Add Lib/test/test_bigaddrspace.py from CPython v3.12.0 2023-11-23 15:40:57 +08:00
Noa
068249196f Use new 1.74 features (#5118) 2023-11-17 13:01:05 +09:00
Noa
87cf891e50 Make PyMethodDef construction const (#5117)
* Make PyMethodDef construction const

* Remove iter_chain![]

Obsolete since arrays now impl IntoIterator
2023-11-15 12:52:47 +09:00
Jeong, YunWon
6d23daa480 Merge pull request #5115 from NakanoMiku39/main
Update test_opcodes.py, test_codeop.py and codeop.py from CPython v3.12
2023-11-10 01:45:00 -08:00
NakanoMiku39
ee9f6ae079 Edit Lib/test/test_codeop.py
cpython version: 3.12
2023-11-09 15:29:35 +08:00
NakanoMiku39
488bac7413 Update codeop.py from CPython v3.12 2023-11-09 15:20:52 +08:00
NakanoMiku39
8ff0872d41 Update test_codeop.py from CPython v3.12 2023-11-09 15:11:46 +08:00
NakanoMiku39
a96926cd77 Remove Lib/test/test_opcode.py
This file is a duplicate of test_codeop.py with a few changes
2023-11-09 15:06:08 +08:00
NakanoMiku39
06b9b4938d Add Lib/test/typinganndata folder
cpython version: 3.12
2023-11-09 15:05:04 +08:00
NakanoMiku39
b5e21ab136 Add Lib/test/test_opcodes.py
cpython version: 3.12
2023-11-09 14:59:59 +08:00
NakanoMiku
fe617431f4 Update test__locale.py and test_atexit.py from CPython v3.12 (#5114)
* Update test__locale.py from CPython v3.12

* Update Cargo.lock

* Update test_atexit.py from CPython v3.12
2023-11-08 22:39:56 -08:00
NakanoMiku39
99d992f708 Update test_atexit.py from CPython v3.12 2023-11-08 20:41:23 +08:00
NakanoMiku39
f682d184fb Update Cargo.lock 2023-11-08 20:18:46 +08:00
NakanoMiku39
2bfbfd7a03 Update test__locale.py from CPython v3.12 2023-11-08 19:25:01 +08:00
NakanoMiku
c32369bd27 Add Lib/test/test___all__.py (#5110)
* Add Lib/test/test___all__.py

cpython version: 3.12

* Edit Lib/test/test___all__.py

Add @unittest.expectedFailure for function test_all(self)

cpython version: 3.12
2023-11-07 23:37:26 -08:00
Steve Shi
b8f22e2967 bump malachite to 0.4.4 (#5069) 2023-11-07 22:43:49 -08:00
Tae-Geun Kim
b4b71e5a11 Bump puruspe to 0.2 (#5112) 2023-11-07 22:37:06 -08:00
Olivier Lemasle
4dacbc51e2 Remove outdated info about yanked crates (#5111)
The `rustpython-*` crates have been published since this message was added.
2023-11-06 21:12:21 -08:00
Jeong, YunWon
af884cb284 First step for Python 3.12 support (#5078)
* Mark 3.12

* Update importlib from Python 3.12.0

* Update test_importlib from Python3.12

* Mark failings tests from importlib

* Update test.support from Python3.12

* Fix unsupported parser feature

* mark failing test

* Update functools from Python 3.12

* manual type annotation

* slice behavior changed in 3.12

* empty unittest.main returns non-zero

* test_decimal from CPython 3.12

* Mark failing tests

* Update test_unicode from CPython 3.12

* Update test_functools from Python 3.12

* Update enum from Python 3.12

* enum

* Doc format changed

* Update test_module from CPython

---------

Co-authored-by: CPython developers <>
2023-10-22 19:19:05 -07:00
Jeong, YunWon
a75f26b922 Fake PEP-0695 with empty __type_params__ (#5098) 2023-10-22 18:01:06 -07:00
Jonas Zaddach
d32cb7efdd Add missing 'sysinfoapi' feature for winapi (#5094) 2023-10-19 18:04:11 -07:00
Dan Näsman
03576615e4 fix type_check (#5097) 2023-10-19 18:03:34 -07:00
Jeong, YunWon
bda7c5cf06 Fix examples/package_embed (#5096) 2023-10-18 15:27:51 -07:00
Dan Näsman
0e72ba8819 implement PyObject_Type and PyObject_TypeCheck (#5091) 2023-10-17 16:22:10 -07:00
Dan Näsman
4e6172b99d Add object protocol correspoinding to PyObject_GetAIter (#5090) 2023-10-16 10:39:29 -07:00
Amuthan Mannar
9241e2e5d5 [VM] Object pickling implementation for product object (python itertools) (#5089)
* Implemented __reduce__, __setstate__ in product object
2023-10-12 20:12:33 +09:00
Dan Näsman
830389f62c implement dir for ByObjectRef (#5088) 2023-10-09 15:16:38 +09:00
Jeong, YunWon
2c3dbb5eed Merge pull request #5081 from MannarAmuthan/bool-compare
Implemented compare operation for boolean types in JIT engine
2023-10-07 22:03:50 +09:00
Jeong, YunWon
f1b36233b6 Merge pull request #5086 from MannarAmuthan/float-int-ops
[JIT]  Binary operations for integers and floating-point numbers allowing mixed type calculations
2023-10-07 21:15:04 +09:00
Amuthan Mannar
eb83b729b2 Formatted code files 2023-10-07 21:05:27 +09:00
Amuthan Mannar
b69e6a910d Added missed tests in int_tests of jit 2023-10-07 21:05:27 +09:00
Amuthan Mannar
b9ee0b6b7a Implemented compare operation for boolean and int types 2023-10-07 21:05:27 +09:00
Amuthan Mannar
f8365ca6c3 Implemented compare operation for boolean types in JIT engine 2023-10-07 21:05:27 +09:00
Amuthan Mannar
c2f159b24a Formatted code files 2023-10-07 12:34:32 +05:30
Amuthan Mannar
54d5869457 Implement binary operations for integers and floating-point numbers, allowing mixed type calculations 2023-10-07 12:02:18 +05:30
Jeong YunWon
285ba765a7 0.4.2 with dependency update 2023-10-07 14:41:32 +09:00
Jeong, YunWon
91c0c8b002 Merge pull request #5085 from youknowone/windows
port winapi to windows-sys
2023-10-07 14:14:51 +09:00
Jeong, YunWon
4a61eba58e rustpython_vm::windows 2023-10-07 03:02:56 +09:00
Jeong, YunWon
58df09b492 GetLastError 2023-10-07 03:02:56 +09:00
Yaminyam
6313e4c9fb windows-sys attrs
Copied from https://github.com/RustPython/RustPython/pull/4086
2023-10-07 03:02:56 +09:00
Jeong, YunWon
0f3a9311e0 port winbase 2023-10-07 03:02:19 +09:00
Jeong, YunWon
987d50c092 port to windows-rs (#5080)
* Fix OpenSSL in windows CI

* bump windows-rs

* prepare windows-sys 0.48

* CloseHandle

* DuplicateHandle

* CreatePipe

* GetFileType

* GetExitCodeProcess

* TerminateProcess

* GetStdHandle

* GetCurrentProcess

* DeleteProcThreadAttributeList

* WaitForSingleObject

* CreateProcessW

* InitializeProcThreadAttributeList

* UpdateProcThreadAttribute

* clean up helpers
2023-10-07 03:01:42 +09:00
Jeong, YunWon
2fa88f94b6 Skip flaky test_enum::test_unique_composite (#5084) 2023-10-06 14:34:50 +09:00
Jeong, YunWon
7022512b83 retry windows ci openssl fix (#5082) 2023-10-06 14:34:35 +09:00
Jeong, YunWon
4135da42ac Fix clippy (#5083)
* Fix clippy

* Fix nightly clippy
2023-10-06 03:17:03 +09:00
Jeong, YunWon
d975c51b96 implement more warnings (#5077) 2023-10-04 23:42:37 +09:00
Jeong, YunWon
23bf5c42ca bump up and sync dependencies with rustpython-parser (#5075) 2023-10-04 23:42:07 +09:00
Jeong, YunWon
c3a8d5a9b5 Retry to fix win_lib_path again (#5076) 2023-10-03 23:19:31 +09:00
dvermd
0d0139b322 Update enum to CPython 3.11.5 (#5074)
part of: #4564
2023-10-03 22:32:38 +09:00
Jeong, YunWon
5384a4766a Merge pull request #5073 from dvermd/ftplib_311
Update ftplib to CPython 3.11.5
2023-10-01 20:51:26 +09:00
Jeong, YunWon
a12233e788 Merge pull request #5072 from dvermd/fractions_311
Update fractions to CPython 3.11.5
2023-10-01 20:50:57 +09:00
Jeong, YunWon
80e7186aa9 Merge pull request #5071 from dvermd/fileinput_311
Update fileinput to CPython 3.11.5
2023-10-01 20:50:33 +09:00
Jeong, YunWon
9ea4baa41c Merge pull request #5070 from dvermd/compileall_311
Update compileall to CPython 3.11.5
2023-10-01 20:50:08 +09:00
dvermd
0a76a9b115 Update ftplib to CPython 3.11.5
part_of: #4564
2023-10-01 07:12:00 +02:00
dvermd
fd98ab2084 Update fractions to CPython 3.11.5 2023-10-01 07:06:11 +02:00
dvermd
48d4c22362 Update fileinput to CPython 3.11.5 2023-10-01 07:04:53 +02:00
dvermd
10e4f715a5 Update compileall to CPython 3.11.5 2023-10-01 06:59:49 +02:00
Jelmer Vernooij
9031a0ac9f bump lz4_flex dependency to 0.11 (#5067) 2023-09-27 17:43:17 +09:00
Jeong, YunWon
37ce45fffb Fix windows CI error (#5068) 2023-09-27 17:42:54 +09:00
Jelmer Vernooij
39169de63a bump is-macro to 0.3 (#5066) 2023-09-23 01:06:47 +09:00
Caleb Cartwright
21cff29c31 ci: simplify rustfmt invocation (#5064) 2023-09-14 17:11:44 +09:00
Jeong, YunWon
589b3eb7c3 Merge pull request #5052 from youknowone/update-parser
Update parser & Release 0.3.0
2023-09-07 00:27:20 +09:00
Jeong YunWon
1208416b92 0.3.0 release 2023-09-06 18:49:55 +09:00
Jeong YunWon
e5cea3ad37 Update parser to 0.3.0 2023-09-05 17:18:39 +09:00
Reid00
8c11992e59 test: update test_difflib from CPython3.11.2 (#5063) 2023-09-05 17:18:00 +09:00
Reid00
b864e5da1f feat: Implement _imp._frozen_module_names (#5062) 2023-09-03 21:33:42 +09:00
Jeong, YunWon
77939d2ca5 Update platform from CPython 3.11.5 (#5060)
* Update platform and test from CPython 3.11.5

* sys.dllhandle (=0)

* Unmark fixed test of test_sysconfig

---------

Co-authored-by: CPython Developers <>
2023-09-01 13:55:28 +09:00
Jeong, YunWon
9cf18a8bdc feature encodings is dependency of stdlib (#5061) 2023-09-01 01:44:05 +09:00
Dominic Elm
c25aa1add4 Print version and info text when executing the shell (#5057) 2023-08-31 17:46:00 +09:00
Reid00
3900a086b8 update imp.py from CPython 3.11 (#5054) 2023-08-31 17:44:31 +09:00
Jeong, YunWon
64c66e00d6 Fix encodings related error messages to be less confusing (#5049)
* Move cwd setup to interpreter code

* rework import encodings failure message

* try import_encodings only when `path_list` is set

* std::mem::take instead of drain(..).collect()

* Add empty path_list warnings to import_encodings

* Prepend current working directory when !safe_path

Co-authored-by: fanninpm <fanninpm@miamioh.edu>
2023-08-30 21:45:36 +09:00
Jeong, YunWon
4ba2892168 Better tips for Interpreter & InterpreterConfig (#5047)
Co-authored-by: fanninpm <fanninpm@miamioh.edu>
2023-08-30 20:00:07 +09:00
Jeong, YunWon
aee68d20bb Fix freeze-stdlib + Interpreter::without_stdlib (#5051)
* Fix pylib invalidation config

* Fix Interpreter::without_stdlib with frozen-stdlib feature
2023-08-30 19:50:20 +09:00
Junho Lee
d4be55c2ea Add command line parameter -P (#4611)
* Add command line parameter -P

* Modify the value of safe_path to be set

---------

Co-authored-by: Jeong YunWon <jeong@youknowone.org>
2023-08-30 19:32:27 +09:00
Jeong, YunWon
e2f7d5b2f9 fix wasm prettier (#5055) 2023-08-30 18:25:27 +09:00
Reid00
9417eec81e feat: update test_hashlib fro CPython3.11 (#5053)
Co-authored-by: zhangbl <zhangbl@greatld.com>
2023-08-30 00:09:21 +09:00
Jeong, YunWon
d056201e5b Merge pull request #5040 from d3lm/delm/feat/wasi/set-current-dir
Add ability to initialize cwd from PWD when targeting WASI
2023-08-27 03:37:46 +09:00
Jeong, YunWon
93d731cc4b Merge pull request #5029 from LucaSforza/main
added __reduce__ method for itertools.permutations
2023-08-27 03:19:37 +09:00
Jeong, YunWon
d24013d582 Merge pull request #5045 from youknowone/debugger
Add with/without ssl targets to debugger configuration
2023-08-27 03:15:59 +09:00
Jeong, YunWon
31f0d9b282 Merge pull request #5035 from Reid00/main
Update test_code.py code.py from CPython v3.11
2023-08-27 03:05:15 +09:00
Jeong, YunWon
f7d4413c62 Merge pull request #5036 from dannasman/path_error_message
Path error message
2023-08-27 03:03:38 +09:00
Dominic Elm
3131d56298 fixup: add note 2023-08-27 02:30:07 +09:00
Dominic Elm
9c2355117c Add ability to initialize cwd from PWD when targeting WASI 2023-08-27 02:30:07 +09:00
LucaSforza
94029386ae added __reduce__ method for itertools.permutations 2023-08-27 02:13:44 +09:00
zhangbl
0fbe57f96b Update test_code.py code.py from CPython v3.11. 2023-08-27 02:13:11 +09:00
Dan Nasman
5f6059ef73 fix formatting 2023-08-27 02:12:27 +09:00
Dan Nasman
ba8d7b541f change conditional expression 2023-08-27 02:12:27 +09:00
Dan Nasman
b1238ab4eb change import_encodings error message 2023-08-27 02:12:27 +09:00
Jeong, YunWon
287f89aa04 Add with/without ssl targets to debugger configuration 2023-08-27 02:11:01 +09:00
Jeong, YunWon
d5a62848dd Merge pull request #5044 from youknowone/rust-1.72
Fix windows SSL failure & Update to Rust 1.72.0
2023-08-27 01:41:52 +09:00
Jeong, YunWon
03f954408a Fix windows SSL bug 2023-08-27 00:38:10 +09:00
Jeong, YunWon
d4d362a9ca Update to Rust 1.72.0 2023-08-27 00:38:10 +09:00
Jeong, YunWon
aea4d509c9 Fix win_lib_path to actually work 2023-08-26 23:23:14 +09:00
Zanie Blue
bdb0c8f645 Add parser support for PEP 695 (#5026)
* Add generated content for PEP 695 ASDL

* Bump RustPython/Parser to 704eb40108

* Add stubs for type aliases and parameters
2023-07-20 13:54:07 +09:00
Jeong, YunWon
c28cb3941f prettier *.js + fix miri build (#5028)
* prettier *.js

* bump up proc-macro2
2023-07-19 23:43:26 +09:00
Jim Fasarakis-Hilliard
d66f4d5315 Merge pull request #5025 from zanieb/internal/update-asdl
Delete stale ASDL update script
2023-07-11 23:45:27 +03:00
Zanie
b088787f7b Remove commented use of ASDL update script from CI workflow 2023-07-11 15:44:22 -05:00
Zanie
6363940d6c Delete stale ASDL update script 2023-07-11 15:27:16 -05:00
Bobby Palmer
a58ede99f2 add int_max_str_digits input from command line (#5021)
* added int_max_str_digits to settings struct

* changed vm init to use the value from settings

* added error handling and set the settings default to -1 which sets the value to 4300
2023-07-06 01:24:11 +09:00
Jim Fasarakis-Hilliard
7e66db0d43 Merge pull request #5020 from RustPython/dependabot/cargo/openssl-0.10.55
Bump openssl from 0.10.48 to 0.10.55
2023-06-22 02:52:01 +03:00
dependabot[bot]
179de9fb67 Bump openssl from 0.10.48 to 0.10.55
Bumps [openssl](https://github.com/sfackler/rust-openssl) from 0.10.48 to 0.10.55.
- [Release notes](https://github.com/sfackler/rust-openssl/releases)
- [Commits](https://github.com/sfackler/rust-openssl/compare/openssl-v0.10.48...openssl-v0.10.55)

---
updated-dependencies:
- dependency-name: openssl
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-06-21 22:55:38 +00:00
Bobby Palmer
500b0024f0 updated _collections_abc.py and test_collections.py to CPython 3.11.2 (#5016)
* updated _collections_abc.py and its tests in test_collections.py

* decorated failing tests in test_typing.py
2023-06-18 17:31:03 +03:00
Jim Fasarakis-Hilliard
4bec66e1c6 Pin Python version to 3.11.4 in CI runs. (#5015)
* Pin Python version to 3.11.4 in CI runs.

* Use quotation marks around version, yaml parsing issues.

Co-authored-by: fanninpm <fanninpm@miamioh.edu>

---------

Co-authored-by: fanninpm <fanninpm@miamioh.edu>
2023-06-18 14:36:50 +03:00
Dan Näsman
1cdc5d3294 Add get_int_max_str_digits and set_int_max_str_digits in sys. (#5014)
---------

Co-authored-by: DimitrisJim <d.f.hilliard@gmail.com>
2023-06-17 23:46:22 +03:00
Jeong, YunWon
3d2c51962b Update parser (#5012) 2023-06-18 02:38:42 +09:00
Jim Fasarakis-Hilliard
5c81649b19 Return SyntaxError with null byte input in eval. (#5013)
* Return SyntaxError with null byte input in eval.

* Special case MacOS for now, add a note for future sanity.
2023-06-17 16:58:28 +03:00
Jeong, YunWon
d64d33c809 Merge pull request #5009 from DimitrisJim/pylibs_upgrade_dos
Update more libraries from 3.11
2023-06-14 17:19:15 +09:00
DimitrisJim
b875057ae2 Fix test failures/successes for windows. 2023-06-14 01:53:08 +03:00
DimitrisJim
2e5b0330c7 Update pprint from Python 3.11 2023-06-14 01:19:14 +03:00
DimitrisJim
c7d3358582 Update numbers from Python 3.11 2023-06-14 01:13:52 +03:00
DimitrisJim
1df0a44958 Update nturl2path from Python 3.11 2023-06-14 01:07:12 +03:00
DimitrisJim
3c8fb93ab4 Update ntpath from Python 3.11 2023-06-14 00:50:38 +03:00
DimitrisJim
95ef76f7a0 Update nntplib to Python 3.11 2023-06-14 00:45:43 +03:00
DimitrisJim
9953597bb6 Update netrc to Python 3.11 2023-06-14 00:44:01 +03:00
DimitrisJim
91e206faa1 Update linecache to Python 3.11 2023-06-14 00:25:55 +03:00
DimitrisJim
06ed5cc8cf Update ipaddress from Python 3.11 2023-06-14 00:23:36 +03:00
DimitrisJim
483dfcdd2c Update imghdr from Python 3.11 2023-06-14 00:22:06 +03:00
DimitrisJim
a4330c3558 Update heapq from Python 3.11 2023-06-14 00:18:14 +03:00
Jeong, YunWon
a6a92128f0 Merge pull request #5007 from DimitrisJim/pylibs_upgrade_uno
Bump some Python libraries to 3.11
2023-06-13 11:38:00 +09:00
DimitrisJim
436be2b96a Upgrade colorsys from Python 3.11 2023-06-12 20:02:32 +03:00
DimitrisJim
89323dadc2 Update codeop from Python 3.11 2023-06-12 20:02:31 +03:00
DimitrisJim
7e3183ef16 Update chunk from Python 3.11 2023-06-12 20:02:31 +03:00
DimitrisJim
854d3e4d80 Update cgitb from Python 3.11 2023-06-12 20:02:31 +03:00
DimitrisJim
3150b4ded6 Update cgi.py from Python 3.11 2023-06-12 20:02:30 +03:00
DimitrisJim
1be262db40 Upgrade warnings.py from Python 3.11 2023-06-12 20:02:30 +03:00
DimitrisJim
3d76e1e1db Upgrade aifc.py from Python 3.11. 2023-06-12 20:02:29 +03:00
DimitrisJim
8af4c5fd7b Upgrade cmd.py and test_cmd.py from Python 3.11. 2023-06-12 20:02:29 +03:00
ilp-sys
8dbf7b27ef Fix test_path_like_objects (#4773)
Co-authored-by: DimitrisJim <d.f.hilliard@gmail.com>
2023-06-13 00:10:50 +09:00
Jim Fasarakis-Hilliard
2b4f607cbc Set correct output file (#5006) 2023-06-12 19:48:25 +09:00
Ivan Gromov
cbaed46fb4 Implement itertools.accumulate.__reduce__ and __setstate__ (#4434)
Co-authored-by: DimitrisJim <d.f.hilliard@gmail.com>
2023-06-12 19:26:03 +09:00
MisileLaboratory
9cab670bf8 Remove uneeded mutable variable declaration (#5004) 2023-06-12 12:19:43 +03:00
Jim Fasarakis-Hilliard
6fb15535ed Remove deprecated modules binhex and formatter. (#5003)
These modules have been deprecated as of Python 3.11 and can safely be removed from the standard library.
2023-06-11 18:55:28 +03:00
TheOnlyError
4721b7df92 Architecture overview (#2528)
* Add architecture document
---------

Co-authored-by: Eric van der Toorn <e.a.vandertoorn@student.tudelft.nl>
Co-authored-by: Pluriscient <erictoorn@gmail.com>
Co-authored-by: Jeong YunWon <youknowone@users.noreply.github.com>
Co-authored-by: DimitrisJim <d.f.hilliard@gmail.com>
Co-authored-by: fanninpm <fanninpm@miamioh.edu>
2023-06-11 18:35:15 +03:00
Jim Fasarakis-Hilliard
06c6244599 Fix failures in the cron ci. (#5002) 2023-06-11 14:23:17 +03:00
Jim Fasarakis-Hilliard
3a2a1d1b74 Remove unecessary unsafe, remove generated file in extra_snippets. (#4998) 2023-06-11 06:03:01 +03:00
Jim Fasarakis-Hilliard
531f4babff Temporarily skip configparser. (#4997)
* Temporarily skip configparser, socketserver tests.
2023-06-10 18:15:30 +03:00
Jim Fasarakis-Hilliard
0f2ea6a1f3 Merge pull request #4994 from LucaSforza/main
Added constructor for Settings called with_path
2023-06-10 13:35:44 +03:00
Jim Fasarakis-Hilliard
7069a1de0a Merge pull request #4993 from RustPython/dependabot/cargo/xml-rs-0.8.14
Bump xml-rs from 0.8.4 to 0.8.14
2023-06-08 14:29:54 +03:00
DimitrisJim
6f8f42b06a Remove expectedFailure decorator for test_xml_encodings. 2023-06-08 13:46:49 +03:00
DimitrisJim
8db7edb6d9 Bump xml-rs 2023-06-08 13:14:34 +03:00
Steve Shi
de0d323ad3 change dependency from num-bigint to malachite-bigint (#4952) 2023-06-08 14:28:39 +09:00
LucaSforza
01486830e3 Added constructor for Settings called with_path 2023-06-07 17:56:07 +02:00
Jeong, YunWon
7a3f965396 new ast layout for arguments (#4978) 2023-06-02 17:07:34 +09:00
Ankit Kumar Pandey
058f8c5500 Add fork support functions (#4972) 2023-06-01 15:29:16 +09:00
Patrick
0e24cf48c6 Fix abc error message (#4987)
* Print correct error message for abc instantiation

* Update dataclasses abc test
2023-05-23 16:43:52 +09:00
Patrick
e864e26592 update abc module (#4985) 2023-05-22 20:50:13 +09:00
Jeong, YunWon
59df92d700 Merge pull request #4981 from DimitrisJim/update_subprocess
Update subprocess to CPython 3.11
2023-05-20 15:22:31 +09:00
DimitrisJim
283a8046aa Mark failing tests. 2023-05-19 22:38:38 +03:00
Jim Fasarakis-Hilliard
9ba6027599 Merge pull request #4979 from patrickzbhe/del-local-scope
Check variables in local scope in DeleteFast
2023-05-19 21:52:18 +03:00
Patrick He
8312831bc9 fix python linting errors 2023-05-19 14:05:36 -04:00
Patrick He
2786831d32 add test 2023-05-19 13:34:33 -04:00
DimitrisJim
7366a41b1c Update subprocess from CPython 3.11 2023-05-19 19:54:59 +03:00
DimitrisJim
2b43d4817c Add additional arguments to fork_exec. 2023-05-19 19:54:38 +03:00
jinnarajin
050f0bd0ad weakref.__reversed__ fixed (#4780) 2023-05-19 23:43:07 +09:00
Jim Fasarakis-Hilliard
7c56313d2b Update DEVELOPMENT.md (#4980)
add a note on the fact we're using cspell, we can then allow it during ci ref: https://github.com/RustPython/RustPython/pull/4700
2023-05-19 23:27:36 +09:00
Patrick He
ab3862d7f4 formatting 2023-05-19 05:12:23 -04:00
Patrick He
f4b54082cf Check variables in scope in DeleteFast 2023-05-19 05:06:15 -04:00
Jeong, YunWon
ff7055749c Update parser to specialize ConversionFlag (#4976) 2023-05-17 10:47:56 +09:00
Luca Sforza
a1d9c649f6 Implemented __reduce__ method for types.SimpleNamespace (#4975) 2023-05-17 10:47:43 +09:00
Jim Fasarakis-Hilliard
1aeaf4afef Merge pull request #4912 from youknowone/issue-templates
Add more issue templates
2023-05-15 21:09:17 +03:00
Jeong, YunWon
5d3e12fca4 Reduce parser size and Avoid copying elements when parsing (#4974)
https://github.com/RustPython/Parser/pull/35
2023-05-16 01:35:50 +09:00
Luca Sforza
3ae6ce5216 Implemented __reduce__ method for the ImportError exception (#4973) 2023-05-15 20:51:20 +09:00
Jeong, YunWon
f5fc30ae63 Move range to each Node (#4969) 2023-05-15 17:00:23 +09:00
Micha Reiser
7a6000d181 Use format and cformat from rustpython-literal (#4968)
* Use `format` and `cformat` from `rustpython-literal`

* Remove unused dependencies, use `rustpython-format`
2023-05-13 02:58:02 +09:00
Jeong, YunWon
15fdf1ee5b Merge pull request #4967 from youknowone/fix-bytearray
Fix repr(bytearray) in debug
2023-05-12 20:49:50 +09:00
Jeong YunWon
4c2ab63185 Fix repr(bytearray) in debug 2023-05-12 06:52:11 +09:00
Jeong, YunWon
0a10e6cbea Merge pull request #4963 from matthew-hagemann/fix/silent-if-error-in-trace()
Set trace_func / profile_func to none on error
2023-05-12 05:51:50 +09:00
Matthew Hagemann
935e35bb40 Updating test to expect passing 2023-05-11 20:58:03 +02:00
Jeong, YunWon
698134caab Merge pull request #4965 from youknowone/wasmuuid
Allow uuid on wasm32 and wasi
2023-05-12 02:11:05 +09:00
Jeong YunWon
f12875027c Allow uuid on wasm32 and wasi 2023-05-12 01:42:20 +09:00
Jeong, YunWon
833371918d Merge pull request #4959 from bjoroen/report_overflow_error
Report overflow error
2023-05-11 17:13:34 +09:00
Matthew Hagemann
a5f08d77a7 Set trace_func / profile_func to none on error 2023-05-10 23:00:22 +02:00
Jeong, YunWon
f3e4d34092 Merge pull request #4962 from youknowone/fix-ast
Revert a few .as_str() calls by adding it to ast
2023-05-11 05:16:20 +09:00
Jeong YunWon
a43a8c546e Revert a few .as_str() calls by adding it to ast 2023-05-11 04:43:57 +09:00
Jeong, YunWon
b85e134477 Merge pull request #4961 from youknowone/byte-offset
location to byte offset (TextSize)
2023-05-10 21:26:28 +09:00
Jeong YunWon
4064c84cb4 ast::Identifier and ast::Int 2023-05-10 20:31:35 +09:00
Jeong YunWon
dc569e9921 Refactoring 2023-05-10 19:35:27 +09:00
Jeong YunWon
5337dedadf CI using exclude 2023-05-10 15:35:00 +09:00
Jeong YunWon
f1b261b4f8 Adapt byte-offset location 2023-05-10 04:13:30 +09:00
Jeong YunWon
a5ed4070bf remove lalrpop deps from cron-ci.yaml 2023-05-10 04:13:30 +09:00
Eirik Bjoroen
fa30d33c9b Added both errors, and used value error for year, month, day and overflow for hour, min and sec - test now passes 2023-05-08 20:52:23 +02:00
Eirik Bjoroen
3d9fb36cde Changed what error message is shown 2023-05-07 18:46:48 +02:00
Eirik Bjoroen
6603c46b41 Changed what type of error gets reported 2023-05-07 18:36:06 +02:00
Jeong YunWon
c7025f70d5 Remove PySymbol*Ref types 2023-05-08 01:11:13 +09:00
Jeong, YunWon
d03c7267d6 Merge pull request #4958 from RustPython/refactor-ast
Refactor ast to hold data as seperated type
2023-05-07 20:08:25 +09:00
Jeong YunWon
0af106c85c Refactor ast to hold data as seperated type 2023-05-07 19:31:31 +09:00
Jeong, YunWon
da15bb282e Merge pull request #4957 from youknowone/Located-start
more location -> start()
2023-05-06 20:30:51 +09:00
Jeong YunWon
ff808ad313 more location -> start() 2023-05-06 19:59:03 +09:00
Jeong, YunWon
0897385a30 Merge pull request #4956 from youknowone/remove-core-compiler-error
Remove compiler_core::CompileError
2023-05-06 19:14:57 +09:00
Jeong YunWon
bac1cdc55f Remove compiler_core::CompileError 2023-05-06 18:22:25 +09:00
Jeong, YunWon
91b9ed17c2 Merge pull request #4954 from youknowone/parser-repository
Split parser to new repository
2023-05-06 15:12:30 +09:00
Jeong YunWon
f2cfa5f0a7 Split parser to new repository
6b60f85cc4
2023-05-06 14:38:11 +09:00
Jeong, YunWon
306943a9f5 Merge pull request #4953 from youknowone/rustpython-literal
Remove rustpython-common dependency from parser
2023-05-05 23:06:07 +09:00
Jeong YunWon
ff5076b12c rustpython-literal 2023-05-05 21:49:12 +09:00
Jeong, YunWon
cea23d2b07 Merge pull request #4951 from youknowone/repr-quote
Port configurable repr quote from Ruff and refactoring
2023-05-05 20:45:40 +09:00
Jeong, YunWon
1dd5e84c9a Merge pull request #4873 from youknowone/method
Method overhaul with static PyMethodDef
2023-05-05 18:53:01 +09:00
Jeong YunWon
cfbdf7f924 Refactor common::bytes::repr using common::escape 2023-05-03 16:08:48 +09:00
Jeong YunWon
2080cc2067 Refactor common::str::repr using common::escape 2023-05-03 13:47:04 +09:00
Jeong YunWon
9cc8f2dfa9 repr as single quote default 2023-05-01 03:30:34 +09:00
Jeong YunWon
2315ce956e String repr quote option from Ruff project
Co-Authored-By: Charlie Marsh <charlie.r.marsh@gmail.com>
2023-05-01 03:08:13 +09:00
Jeong YunWon
e73603945d Rename PyTypeFlags::METHOD_{DESCR => DESCRIPTOR} 2023-04-30 17:45:05 +09:00
Jeong YunWon
98eef0804e Method overhaul with PyMethodDef 2023-04-30 17:33:23 +09:00
Christopher Gambrell
fa79055821 Format d bool support (#4948) 2023-04-30 16:29:04 +09:00
Kim Seungha
4a099ab6bc Make builtin method type same as that of builtin function 2023-04-30 13:35:43 +09:00
Jeong YunWon
9f58921a6f Rename IterNextIterable -> SelfIter by following CPython 2023-04-30 03:54:32 +09:00
Jeong YunWon
9b9dd1100a Add missing with(Iterable) 2023-04-30 03:54:32 +09:00
Jeong YunWon
2abf21b134 PyObject_SelfIter 2023-04-30 00:17:36 +09:00
Andrey Maltsev
f4b9bdfb29 Implement property name (#4944) 2023-04-29 18:51:33 +09:00
Jeong, YunWon
429d1d1cce Merge pull request #4876 from Phosphorescentt/4845
Update collections from Python 3.11.2
2023-04-29 04:32:46 +09:00
Jeong, YunWon
9d44f93192 Merge pull request #4943 from youknowone/use-ruff
Use ruff for CI
2023-04-29 01:20:45 +09:00
Jim Fasarakis-Hilliard
b8bca5a148 Merge pull request #4926 from youknowone/fromargs-cleanup
Remove Traverse from FromArgs
2023-04-28 13:22:10 +03:00
Jeong YunWon
8c1d923f3e Use ruff for CI 2023-04-28 18:57:53 +09:00
Joshua Mankelow
04749c13f4 Merge branch 'RustPython:main' into 4845 2023-04-28 10:56:26 +01:00
Jeong YunWon
e2ccb48463 Remove Traverse from FromArgs 2023-04-28 13:27:57 +09:00
Jeong, YunWon
02840593bc Merge pull request #4933 from youknowone/fix-wasm-stdlib
Fix wasm build stdlib
2023-04-28 13:26:34 +09:00
Jeong YunWon
8503e0de90 Fix wasm build stdlib 2023-04-28 11:50:53 +09:00
Jeong, YunWon
23fee2737c Merge pull request #4929 from astral-sh/add-start-end-to-located
Add `Located::start`, `Located::end` and impl `Deref`
2023-04-27 02:14:20 +09:00
Micha Reiser
0a94e1393a Add Located::start, Located::end and impl Deref 2023-04-26 10:24:34 -06:00
Jeong, YunWon
9287169201 Merge pull request #4928 from astral-sh/use-located-new
Use `Located::new` over struct initializer
2023-04-26 13:46:15 +09:00
Micha Reiser
8605fea678 Use Located::new over struct initializer 2023-04-25 18:10:13 -06:00
Jim Fasarakis-Hilliard
d9b6585804 Merge pull request #4927 from youknowone/rename-types
Rename method/member types
2023-04-25 06:42:18 +03:00
Jeong YunWon
30b8d7bdad MemberDescrObject -> PyMemberDescriptor 2023-04-25 04:54:51 +09:00
Jeong YunWon
436624b7a3 MemberDef -> PyMemberDef 2023-04-25 04:54:51 +09:00
Jeong, YunWon
268a39fc61 Merge pull request #4901 from youknowone/py-module-def
PyModuleDef and #[pymodule(with(...))]
2023-04-25 04:41:17 +09:00
Jeong YunWon
317f432a20 PyModuleDef and #[pymodule(with(...))] 2023-04-25 04:08:11 +09:00
Jeong, YunWon
cbcdcf4ad5 Merge pull request #4900 from youknowone/simpler-new-object
Simpler new object / small int range const
2023-04-25 04:07:33 +09:00
Jeong, YunWon
ce13fc5f03 Merge pull request #4898 from youknowone/pymodule-expose
module objects' type as PyModule
2023-04-25 04:00:52 +09:00
Jeong, YunWon
c16f813794 Merge pull request #4899 from youknowone/debug-friendly-tweaks
debug-friendly tweaks
2023-04-25 04:00:24 +09:00
Jeong, YunWon
6e9e15b929 Merge pull request #4885 from youknowone/super-init
super.__init__
2023-04-25 03:59:54 +09:00
Jeong, YunWon
776bda60ca Merge pull request #4886 from youknowone/pyexception
Merge define_exception into pyexception
2023-04-25 02:43:00 +09:00
Ankit Kumar Pandey
322aa6887a add support for os.fork and related functions (#4877) 2023-04-25 02:39:54 +09:00
Jeong YunWon
f256934f93 Merge define_exception into pyexception 2023-04-24 02:06:13 +09:00
Jeong YunWon
2658c5c539 super.__init__ 2023-04-24 02:04:07 +09:00
Jeong, YunWon
4e0890a50d Merge pull request #4835 from Masorubka1/test_hashlib.py
Update test_hashlib.py from Cpython v3.11.2 & refactor hashlib
2023-04-23 23:28:38 +09:00
Jeong YunWon
73e1eeb57e skip test_enum.test_unique_composite 2023-04-23 21:50:16 +09:00
Andrey Maltsev
514014f4fc add compare digest 2023-04-23 21:50:15 +09:00
Andrey Maltsev
508cf6b61d refactoring hashlib for tests 2023-04-23 21:48:02 +09:00
Andrey Maltsev
8782bf8cb0 Update test_hashlib.py from Cpython v3.11.2 2023-04-23 21:45:03 +09:00
discord9
94bdb6b97a feature: PyTraverse derive macro for traverse object's childrens(like CPython's tp_traverse) (#4872) 2023-04-23 21:43:49 +09:00
Leslie Zhai
2c90b128c3 Add loongarch64 support (#4914) 2023-04-23 17:37:16 +09:00
Jeong, YunWon
cf465bdd81 Merge pull request #4924 from itsankitkp/fix-sys-path-for-venv
Ensure correct site packages are visible to rustpython
2023-04-23 15:28:19 +09:00
Jeong, YunWon
05442bc667 Merge pull request #4913 from youknowone/buffer-as-object
Buffer arguments as PyObject
2023-04-23 12:38:40 +09:00
Jeong YunWon
aad1d848a6 Rewrite Python->RustPython on sysconfig.py
Co-Authored-By: Ankit Kumar Pandey <itsankitkp@gmail.com>
2023-04-23 12:26:06 +09:00
Jeong YunWon
09a1ce9b2b Add XXX: RUSTPYTHON 2023-04-23 12:26:06 +09:00
Ankit Kumar Pandey
d9c808ded8 Fix site and test_site for rustpython 2023-04-23 12:25:45 +09:00
Ankit Kumar Pandey
ba6757ad11 add CI for pip check inside venv 2023-04-23 12:23:29 +09:00
Jeong YunWon
e18dda3e08 Buffer arguments as PyObject 2023-04-23 10:57:24 +09:00
Lee Dogeon
1fa69ebcc9 sqlite: Throw TypeError when callable is not callable (#4923) 2023-04-22 07:10:44 +09:00
Ankit Kumar Pandey
f0db8329be rename os module's exit function to _exit as thats how it is defined in cpython (#4887) 2023-04-22 05:22:18 +09:00
Jeong, YunWon
6ce372cfb6 Merge pull request #4920 from itsankitkp/fix-windows-ssl-ci
fix wrong directory name for openssl setup
2023-04-22 05:18:37 +09:00
Ankit Kumar Pandey
7fc30b715a fix wrong directory name for openssl setup 2023-04-21 20:27:52 +05:30
Jeong, YunWon
07c07ad806 Merge pull request #4916 from youknowone/choco-no-progress
run choco with --no-progress
2023-04-21 21:16:15 +09:00
Jeong YunWon
d7e7001afd choco --no-progress to prevent spam 2023-04-21 21:15:10 +09:00
Jeong YunWon
df6831694b Adding more issue templates 2023-04-21 18:19:37 +09:00
Jeong, YunWon
14aa9d7108 Merge pull request #4897 from youknowone/reduce-stdlib-runtime
Reduce runtime of stdlib_collections_deque
2023-04-21 18:08:13 +09:00
Jeong, YunWon
3794c178be Merge pull request #4896 from youknowone/debugger-setup
RUST_BACKTRACE=1 for debugger
2023-04-20 22:49:28 +09:00
Jeong YunWon
560cd6ca6c Simpler new object / small int range const 2023-04-20 21:36:22 +09:00
Jeong YunWon
6996141448 debug-friendly tweaks 2023-04-20 21:21:50 +09:00
Jeong, YunWon
968f2ad7c9 Merge pull request #4849 from haxelion/feature/tp_repr_stdlib
Implemented tp_repr slots for more types
2023-04-20 21:02:55 +09:00
Jeong, YunWon
c05f23d99d Merge pull request #4888 from Masorubka1/test_poll.py
Add test_poll.py from Cpython v3.11.2
2023-04-20 21:02:27 +09:00
Jeong YunWon
19224505e8 module objects' type as PyModule 2023-04-20 20:48:57 +09:00
Jeong YunWon
06b0484ea3 Reduce runtime of stdlib_collections_deque 2023-04-20 17:33:36 +09:00
Jeong YunWon
9e4c8ad7d7 RUST_BACKTRACE=1 for debugger 2023-04-20 17:22:12 +09:00
Jeong, YunWon
e103adaa52 Merge pull request #4893 from Masorubka1/test_pulldom.py
Add test_pulldom.py from Cpython v3.11.2
2023-04-20 17:10:30 +09:00
Jeong YunWon
89e3587c15 Simplify repr implementation a bit 2023-04-20 16:58:29 +09:00
Jeong, YunWon
446285f348 Merge pull request #4892 from Masorubka1/test_property.py
Update test_property.py from Cpython v3.11.2
2023-04-20 16:12:12 +09:00
Joshua Mankelow
c03d3a58ec Removed RustPython specific @unittest.skip()
Added extra `unittest.expectedFailure` decorators
2023-04-20 15:38:19 +09:00
Andrey Maltsev
334c15e879 add required files 2023-04-20 15:36:28 +09:00
Jeong YunWon
c3d9a51d15 Update last fallback property error messages 2023-04-20 15:32:50 +09:00
Andrey Maltsev
fddbf0728a add annotation to skiped test 2023-04-20 15:27:41 +09:00
Joshua Mankelow
32aa9933be Updated __init__.py to be inline with CPython 3.11.2 2023-04-20 15:20:36 +09:00
Dmitry Erlikh
a354f7bba1 Properly handle failing test_os tests on windows (#4764) 2023-04-20 14:42:16 +09:00
Andrey Maltsev
b7811af211 Add test_pulldom.py from Cpython v3.11.2 2023-04-19 16:05:18 +00:00
Jeong, YunWon
471ec26873 Merge pull request #4790 from Snowapril/number-protocol-unary-ternary
Modify unary & ternary function fields in number protocol
2023-04-19 23:05:26 +09:00
Jeong YunWon
a4a6e8dabe Fix trivial suggestions 2023-04-19 22:18:19 +09:00
snowapril
4416b2a467 Refactor PyNumber methods based on review suggestions
Signed-off-by: snowapril <sinjihng@gmail.com>
2023-04-19 22:18:19 +09:00
snowapril
c69ff172e5 Replace PyInquiryFunc to PyUnaryFunc<bool>
Signed-off-by: snowapril <sinjihng@gmail.com>
2023-04-19 22:18:19 +09:00
snowapril
9c38e904dd Fix vm.ctx.none.as_obejct not to make clone
Signed-off-by: snowapril <sinjihng@gmail.com>
2023-04-19 22:18:19 +09:00
snowapril
fd4f6941bf Remove expectedFailures for each success tests
Signed-off-by: snowapril <sinjihng@gmail.com>
2023-04-19 22:18:19 +09:00
snowapril
9f28643ac7 Fix inplace_power in number slots
Signed-off-by: snowapril <sinjihng@gmail.com>
2023-04-19 22:18:19 +09:00
snowapril
9135d29346 Replace non-implemented type conversions in each PyNumber_Int, PyNumber_Index, PyNumber_Float
Signed-off-by: snowapril <sinjihng@gmail.com>
2023-04-19 22:18:19 +09:00
snowapril
96af524aad Fix PyInt PyNubmerMethods pow
Signed-off-by: snowapril <sinjihng@gmail.com>
2023-04-19 22:18:19 +09:00
snowapril
3a9a1c93dd Fix cargo formatting
Signed-off-by: snowapril <sinjihng@gmail.com>
2023-04-19 22:18:19 +09:00
snowapril
b0618b9c9a Modify inplace binop instruction to use inplace_power_no_mod
Signed-off-by: snowapril <sinjihng@gmail.com>
2023-04-19 22:18:19 +09:00
snowapril
c789490294 Fix each type's unary & ternary fields in PyNumberMethods def
Signed-off-by: snowapril <sinjihng@gmail.com>
2023-04-19 22:18:19 +09:00
snowapril
bf2fbfc52d Modify boolean field as PyNumberInquiryFunc
Signed-off-by: snowapril <sinjihng@gmail.com>
2023-04-19 22:18:19 +09:00
snowapril
3613edf321 Fix unary function to take PyNumber
Signed-off-by: snowapril <sinjihng@gmail.com>
2023-04-19 22:18:19 +09:00
snowapril
b06c0f8ff9 Modify original vm._pow usage
Signed-off-by: snowapril <sinjihng@gmail.com>
2023-04-19 22:18:19 +09:00
snowapril
05900fbabf Fix power, inplace_power as ternary function
Signed-off-by: snowapril <sinjihng@gmail.com>
2023-04-19 22:18:19 +09:00
snowapril
51ae26128b Modify unary functions in number protocol
Signed-off-by: snowapril <sinjihng@gmail.com>
2023-04-19 22:18:19 +09:00
Jeong, YunWon
1b66332f06 Merge pull request #4890 from Masorubka1/test_posix.py
Update test_posix.py from Cpython v3.11.2
2023-04-19 06:09:11 +09:00
Jeong, YunWon
7b4b325e9d Merge pull request #4891 from Masorubka1/test_pow.py
Update test_pow.py from Cpython v3.11.2
2023-04-19 05:11:56 +09:00
Andrey Maltsev
5c09cc37c9 Update test_property.py from Cpython v3.11.2 2023-04-18 19:54:49 +00:00
Jeong, YunWon
e96e0e113a Merge pull request #4889 from Masorubka1/test_popen.py
Update test_popen.py from Cpython v3.11.2
2023-04-19 04:43:32 +09:00
Andrey Maltsev
2d7ec43173 Update Lib/test/test_posix.py
Co-authored-by: fanninpm <fanninpm@miamioh.edu>
2023-04-18 22:36:03 +03:00
Andrey Maltsev
536da88df7 Update Lib/test/test_posix.py
Co-authored-by: fanninpm <fanninpm@miamioh.edu>
2023-04-18 22:25:32 +03:00
Andrey Maltsev
09b5838fda Update test_pow.py from Cpython v3.11.2 2023-04-18 19:17:57 +00:00
Andrey Maltsev
415fac82b2 Update test_posix.py from Cpython v3.11.2 2023-04-18 19:15:37 +00:00
Andrey Maltsev
eb30fb7330 Update test_popen.py from Cpython v3.11.2 2023-04-18 19:09:39 +00:00
Andrey Maltsev
be54e89ba6 Add test_poll.py from Cpython v3.11.2 2023-04-18 19:05:04 +00:00
Faisal Alatawi
5f17d281a6 Convert Context::empty_str to &'static PyStrInterned (#4882)
The goal of this commit is to convert Context::empty_str to &'static PyStrInterned and to fix issue #4869
2023-04-17 20:55:24 +09:00
Christopher Gambrell
d918f7e51b 'n' support for float format (#4865) 2023-04-16 00:06:20 +09:00
Ankit Kumar Pandey
cb6cf107ba handle panic if str is not defined in unraisablehook (#4864) 2023-04-15 23:47:19 +09:00
Boris Verkhovskiy
c9546c2419 Don't run whats_left with older CPython versions (#4847)
* Improve whats_left --signature output
2023-04-11 11:13:27 +09:00
Jeong, YunWon
2a1b2ed24d Merge pull request #4839 from Masorubka1/test_list.py
Update test_list.py from Cpython v3.11.2
2023-04-11 01:21:51 +09:00
Andrey Maltsev
889df336d6 fixes 2023-04-10 14:16:12 +00:00
Jim Fasarakis-Hilliard
12d288ffcd Merge pull request #4867 from youknowone/descr-name
DescrObject::name to PyStrInterned
2023-04-10 14:38:50 +03:00
Jeong, YunWon
4dd453f92c Merge pull request #4868 from coolreader18/upd-errno
Update errno crate to 0.3.1 for redox
2023-04-10 15:14:17 +09:00
Noa
e42be00054 Update errno crate to 0.3.1 for redox 2023-04-09 23:43:25 -05:00
Jeong YunWon
c3491ebdc3 DescrObject::name to PyStrInterned 2023-04-10 09:53:54 +09:00
Jim Fasarakis-Hilliard
f39f103dbe Merge pull request #4801 from Masorubka1/test_cmd_line
Update test_cmd_line from Cpython v3.11.2
2023-04-09 13:18:01 +03:00
Andrey Maltsev
cf06c25904 fix windows tests 2023-04-08 18:57:07 +00:00
Jeong, YunWon
dde92d1269 Merge pull request #4853 from youknowone/builtin-function-names
use interned str for builtin function names
2023-04-08 02:02:10 +09:00
Jim Fasarakis-Hilliard
46455e854e Merge pull request #4828 from Masorubka1/test_fstring
Update test_fstring.py from Cpython v3.11.2
2023-04-06 20:48:30 +03:00
Jeong, YunWon
473d391ea0 Merge pull request #4846 from Masorubka1/test_ordered_dict.py
Update test_ordered_dict.py from Cpython v3.11.2
2023-04-06 04:02:14 +09:00
Jeong, YunWon
1bc71b8f9e Merge pull request #4844 from Masorubka1/test_opcache.py
Update test_opcache.py from Cpython v3.11.2
2023-04-06 04:01:11 +09:00
Jeong, YunWon
4773ffba58 Merge pull request #4843 from Masorubka1/test_numeric_tower.py
Update test_numeric_tower.py from Cpython v3.11.2
2023-04-06 04:00:32 +09:00
Jeong, YunWon
b5c5e807ea Merge pull request #4842 from Masorubka1/test_module.py
Update test_module.py from Cpython v3.11.2
2023-04-06 03:59:46 +09:00
Jeong, YunWon
471dbace44 Merge pull request #4841 from Masorubka1/test_mmap.py
Update test_mmap.py from Cpython v3.11.2
2023-04-06 03:58:53 +09:00
Jeong, YunWon
507e1039a4 Merge pull request #4840 from Masorubka1/test_long.py
Update test_long.py from Cpython v3.11.2
2023-04-06 03:56:08 +09:00
Jeong, YunWon
a8d5070e98 Merge pull request #4838 from Masorubka1/test_largefile.py
Add test_largefile.py from Cpython v3.11.2
2023-04-06 03:54:02 +09:00
Jim Fasarakis-Hilliard
9e5e76080d Merge pull request #4854 from coolreader18/reenable-redox
Reenable redox ci
2023-04-05 21:45:28 +03:00
Noa
e40488b56e Reenable redox ci 2023-04-05 13:07:04 -05:00
Jeong, YunWon
e5629321db Merge pull request #4837 from Masorubka1/test_kqueue.py
Add test_kqueue.py from Cpython v3.11.2
2023-04-06 02:07:15 +09:00
Jeong, YunWon
4cb755d375 Merge pull request #4833 from Masorubka1/test_grp
Update test_grp.py from Cpython v3.11.2
2023-04-06 02:00:36 +09:00
Jeong, YunWon
9eb59946ce Merge pull request #4832 from Masorubka1/test_global
Update test_global.py from Cpython v3.11.2
2023-04-06 01:59:41 +09:00
Jeong, YunWon
a623616629 Merge pull request #4831 from Masorubka1/test_genericclass
Update test_genericclass.py from Cpython v3.11.2
2023-04-06 01:58:52 +09:00
Jeong YunWon
798b3bc158 expose slot_new_wrapper as its base type 2023-04-06 01:52:01 +09:00
Jeong YunWon
18044abbb6 use interned str for builtin function names 2023-04-06 01:23:36 +09:00
Andrey Maltsev
6e515b00ca Update test_fstring.py from Cpython v3.11.2 2023-04-06 00:45:50 +09:00
Charles Hubain
96a949189d Implemented tp_repr slots for more types 2023-04-06 00:42:01 +09:00
Andrey Maltsev
9bd4385002 Update test_ordered_dict.py from Cpython v3.11.2 2023-04-06 00:41:27 +09:00
Andrey Maltsev
f0a85acb63 Update test_opcache.py from Cpython v3.11.2 2023-04-06 00:40:40 +09:00
Andrey Maltsev
7fc0525577 Update test_numeric_tower.py from Cpython v3.11.2 2023-04-06 00:40:14 +09:00
Andrey Maltsev
18150fa70f Update test_module.py from Cpython v3.11.2 2023-04-06 00:39:55 +09:00
Andrey Maltsev
ffea61b540 Update test_mmap.py from Cpython v3.11.2 2023-04-06 00:39:38 +09:00
Andrey Maltsev
d6350c672a Update test_long.py from Cpython v3.11.2 2023-04-06 00:39:09 +09:00
Andrey Maltsev
6176e16275 Add test_largefile.py from Cpython v3.11.2 2023-04-06 00:38:36 +09:00
Andrey Maltsev
47e621aaf3 Add test_kqueue.py from Cpython v3.11.2 2023-04-06 00:38:18 +09:00
Andrey Maltsev
5e84692e7e Update test_grp.py from Cpython v3.11.2 2023-04-06 00:37:49 +09:00
Andrey Maltsev
6ab541ea4e Update test_global.py from Cpython v3.11.2 2023-04-06 00:37:37 +09:00
Andrey Maltsev
034f427638 Update test_genericclass.py from Cpython v3.11.2 2023-04-06 00:37:04 +09:00
Jeong, YunWon
143036aa0a Merge pull request #4851 from youknowone/redox-build
disable redox build
2023-04-06 00:36:00 +09:00
Jeong YunWon
ec001a067f temporary disable redox build 2023-04-06 00:01:43 +09:00
Jim Fasarakis-Hilliard
41239caa97 Merge pull request #4834 from Masorubka1/test_hash.py
Update test_hash.py from Cpython v3.11.2
2023-04-04 17:43:34 +03:00
Andrey Maltsev
30718a3be5 Update test_itertools.py from Cpython v3.11.2 (#4836)
* Update test_itertools.py from Cpython v3.11.2
2023-04-04 17:40:23 +03:00
Jim Fasarakis-Hilliard
ca44da8bdf Merge pull request #4830 from Masorubka1/test_future
Update test_future.py from CPython v3.11.2
2023-04-04 17:33:34 +03:00
Jim Fasarakis-Hilliard
4d6ca07596 Merge pull request #4829 from Masorubka1/test_funcattrs
Add test_funcattrs.py from Cpython v3.11.2
2023-04-04 17:32:13 +03:00
Andrey Maltsev
ba6a815711 Update test_list.py from Cpython v3.11.2 2023-04-04 09:56:10 +00:00
Andrey Maltsev
9ac77140b6 Update test_hash.py from Cpython v3.11.2 2023-04-04 09:14:07 +00:00
Andrey Maltsev
0bcfdd2c07 Update test_future.py from Cpython v3.11.2 2023-04-04 08:51:05 +00:00
Andrey Maltsev
b1f4606231 Add test_funcattrs.py from Cpython v3.11.2 2023-04-04 08:47:32 +00:00
Andrey Maltsev
8ec024a8fd fix windows 2023-04-04 08:10:56 +00:00
Andrey Maltsev
6c6290d20f Update test_class from Cpython v3.11.2 (#4800)
* Update test_class from Cpython v3.11.2
2023-04-04 10:34:06 +03:00
Jeong, YunWon
707fbcf649 Merge pull request #4812 from Masorubka1/test_dtrace
Update test_dtrace.py from Cpython v3.11.2
2023-04-03 04:06:09 +00:00
Jeong, YunWon
fea3cc2564 Merge pull request #4814 from Masorubka1/test_eintr
Update test_eintr.py from Cpython v3.11.2
2023-04-03 04:05:55 +00:00
Jeong, YunWon
1251750403 Merge pull request #4816 from Masorubka1/test_epoll
Update test_epoll.py from Cpython v3.11.2
2023-04-03 04:05:40 +00:00
Jeong, YunWon
40045ae02d Merge pull request #4817 from Masorubka1/test_exception_group
Add test_exception_group.py from Cpython v3.11.2
2023-04-03 04:05:20 +00:00
Jeong, YunWon
ae28139206 Merge pull request #4818 from Masorubka1/test_exception_hierarchy
Update test_exception_hierarchy.py from Cpython v3.11.2
2023-04-03 04:04:22 +00:00
Jeong, YunWon
c6b0e0cee3 Merge pull request #4819 from Masorubka1/test_exceptions
Update test_exceptions.py from Cpython v3.11.2
2023-04-03 04:03:46 +00:00
Jeong, YunWon
31644f1542 Merge pull request #4820 from Masorubka1/test_faulthandler
Update test_faulthandler.py from Cpython v3.11.2
2023-04-03 04:03:04 +00:00
Jeong, YunWon
aeba695431 Merge pull request #4821 from Masorubka1/test_fileio
Update test_fileio.py from Cpython v3.11.2
2023-04-03 04:02:49 +00:00
Andrey Maltsev
e7165e8b6e fix tests 2023-04-02 18:41:14 +00:00
Jim Fasarakis-Hilliard
392821700c Merge pull request #4802 from Masorubka1/test_cmd_line_script
Update test_cmd_line_script from Cpython v3.11.2
2023-04-02 17:18:58 +00:00
Jim Fasarakis-Hilliard
133e98f7e1 Merge pull request #4825 from Masorubka1/test_format.py
Update test_format.py from Cpython v3.11.2
2023-04-02 17:17:06 +00:00
Jim Fasarakis-Hilliard
52a0982948 Merge pull request #4813 from Masorubka1/test_dynamic
Update test_dynamic.py from Cpython v3.11.2
2023-04-02 16:48:30 +00:00
Jim Fasarakis-Hilliard
ad0116ac0b Merge pull request #4823 from Masorubka1/test_float
Update test_float.py from Cpython v3.11.2
2023-04-02 16:46:52 +00:00
Jim Fasarakis-Hilliard
05487c9f66 Merge pull request #4811 from Masorubka1/test_docxmlrpc
Update test_docxmlrpc.py from Cpython v3.11.2
2023-04-02 15:47:35 +00:00
Jim Fasarakis-Hilliard
e121f8e81b Merge pull request #4806 from Masorubka1/test_decorators
Update test_decorators from Cpython v3.11.2
2023-04-02 15:44:13 +00:00
Andrey Maltsev
94dc7bb279 Apply suggestions from code review
Co-authored-by: Jim Fasarakis-Hilliard <d.f.hilliard@gmail.com>
2023-04-02 18:25:21 +03:00
Jim Fasarakis-Hilliard
93613ec5e1 Merge pull request #4810 from Masorubka1/test_dictviews
Update test_dictviews.py from Cpython v3.11.2
2023-04-02 15:25:07 +00:00
Jim Fasarakis-Hilliard
5089cb045e Merge pull request #4808 from Masorubka1/test_devpoll
Update test_devpoll from Cpython v3.11.2
2023-04-02 15:24:40 +00:00
Andrey Maltsev
fa26b6815d wft 2023-04-02 15:23:40 +00:00
Andrey Maltsev
cde5527c9f fix mac os 2023-04-02 15:19:02 +00:00
Andrey Maltsev
0674aa7d8f Update test_format.py from Cpython v3.11.2 2023-04-02 14:57:54 +00:00
Jim Fasarakis-Hilliard
c56da459bc Merge pull request #4805 from Masorubka1/test_context
Update test_context from Cpython v3.11.2
2023-04-02 14:55:08 +00:00
Andrey Maltsev
77d9c8dcf0 Update test_float.py from Cpython v3.11.2 2023-04-02 14:51:38 +00:00
Jim Fasarakis-Hilliard
cae29ab96e Merge pull request #4804 from Masorubka1/test_compile
Update test_compile from Cpython v3.11.2
2023-04-02 14:50:48 +00:00
Andrey Maltsev
8bc32ebe5e Update test_fileio.py from Cpython v3.11.2 2023-04-02 14:46:20 +00:00
Andrey Maltsev
f461d4099c Update test_faulthandler.py from Cpython v3.11.2 2023-04-02 14:40:08 +00:00
fanninpm
32d16803a6 Merge pull request #4803 from Masorubka1/test_compare
Update test_compare from Cpython v3.11.2
2023-04-02 14:39:16 +00:00
Andrey Maltsev
302149e02d Update test_exceptions.py from Cpython v3.11.2 2023-04-02 14:35:18 +00:00
Andrey Maltsev
f2b4ed2893 Update test_exception_hierarchy.py from Cpython v3.11.2 2023-04-02 14:27:08 +00:00
Andrey Maltsev
73b1d4d877 Add test_exception_group.py from Cpython v3.11.2 2023-04-02 14:22:37 +00:00
Andrey Maltsev
14afeb0e53 Update test_epoll.py from Cpython v3.11.2 2023-04-02 14:04:28 +00:00
Andrey Maltsev
68ddbf82e0 Update test_eintr.py from Cpython v3.11.2 2023-04-02 13:57:34 +00:00
Andrey Maltsev
c020c6bbf7 Update test_dynamic.py from Cpython v3.11.2 2023-04-02 13:55:46 +00:00
Andrey Maltsev
b7ab716ee5 Update test_dtrace.py from Cpython v3.11.2 2023-04-02 13:53:14 +00:00
Andrey Maltsev
b47eccbff0 Update test_docxmlrpc.py from Cpython v3.11.2 2023-04-02 13:51:18 +00:00
Andrey Maltsev
f798d2df92 Update test_dictviews.py from Cpython v3.11.2 2023-04-02 13:47:49 +00:00
Andrey Maltsev
bf5aa56dc0 Update test_devpoll from Cpython v3.11.2 2023-04-02 13:42:27 +00:00
Andrey Maltsev
0609686803 Update test_decorators from Cpython v3.11.2 2023-04-02 13:16:01 +00:00
Andrey Maltsev
2a41320963 Update test_context from Cpython v3.11.2 2023-04-02 13:10:36 +00:00
Andrey Maltsev
f5d4c9fc1c Update test_compile from Cpython v3.11.2 2023-04-02 13:04:58 +00:00
Andrey Maltsev
23d6233d42 Update test_compare from Cpython v3.11.2 2023-04-02 12:59:17 +00:00
Andrey Maltsev
e169d97713 Update test_cmd_line_script from Cpython v3.11.2 2023-04-02 12:51:55 +00:00
Andrey Maltsev
1862d1d827 Update test_cmd_line from Cpython v3.11.2 2023-04-02 12:46:39 +00:00
Jim Fasarakis-Hilliard
07bad48dbc Merge pull request #4794 from RustPython/youknowone-patch-1
Add --features ssl to README
2023-03-31 05:31:36 +00:00
Jeong, YunWon
a05842d051 Add --features ssl to README 2023-03-31 12:08:01 +09:00
fanninpm
d8879486bd Merge pull request #4782 from youknowone/bytes-as-osstr
relocate FsPath from stdlib::os and rename PyPathLike to OsPath
2023-03-30 15:30:14 +00:00
Jeong, YunWon
e4292702aa Merge pull request #4789 from youknowone/site
Add test_site from CPython 3.11.2
2023-03-28 15:33:45 +00:00
Jeong YunWon
59f1e59ff1 Mark failing tests of test_site 2023-03-28 23:36:37 +09:00
CPython Developers
fa3010a50e Add test_site from CPython 3.11.2 2023-03-28 18:55:35 +09:00
Jeong, YunWon
11d700d0a7 Merge pull request #4066 from youknowone/site
Update site from CPython 3.10.6
2023-03-28 18:55:00 +09:00
Jeong, YunWon
5c351c8d17 Merge pull request #4788 from youknowone/skip-ci
remove labeled event from ci trigger to prevent re-run ci whenever label added
2023-03-28 18:37:16 +09:00
Jeong YunWon
f14ac7146c remove labeled event from ci trigger 2023-03-28 18:34:44 +09:00
Jeong YunWon
69280ee64f OsPath internal to OsString 2023-03-28 18:21:53 +09:00
Jeong YunWon
f66ab39a40 Update site from CPython 3.11.2 2023-03-28 18:04:03 +09:00
CPython Developers
0cea9346e1 Update site from CPython 3.10.6 2023-03-28 18:04:03 +09:00
Jeong, YunWon
1a7df66e10 Merge pull request #4781 from MegasKomnenos/io
Add encoding="locale" support for TextIOWrapper
2023-03-28 08:37:36 +00:00
MegasKomnenos
7e6c33152e Add encoding='locale' support for TextIOWrapper 2023-03-28 17:36:48 +09:00
MegasKomnenos
e48ca2892b Add utf8_mode field to Settings and make Flags to reference it for init
I also changed the type from u8 to bool. No difference functionality-wise.
2023-03-28 17:35:32 +09:00
Jeong YunWon
5d6ce6c986 OsPathOrFd calls __index__ 2023-03-28 17:01:18 +09:00
Jeong YunWon
7b08953c18 rename PyPathLike -> OsPath becasue it is not a python object 2023-03-28 17:01:18 +09:00
Jeong YunWon
3d1a74dda9 os::ffi_ext -> common::os::ffi 2023-03-28 17:01:18 +09:00
Jeong YunWon
877ba28d85 relocate FsPath -> vm::function::FsPath 2023-03-28 17:01:18 +09:00
Jeong YunWon
f68a80879e relocate bytes_as_osstr to common 2023-03-28 12:37:11 +09:00
fanninpm
7b0ab173dd Merge pull request #4771 from fanninpm/update-gh-actions
Update GH Actions to use Node 16
2023-03-27 23:10:38 +00:00
Padraic Fanning
4a5534f330 Add --openssl-legacy-provider to Node invocation 2023-03-27 18:38:13 -04:00
fanninpm
010640ccc8 Merge pull request #4732 from youknowone/into-exact-or
Fix str.__str__ and PyRef::into_exact_or for int() or str() pattern
2023-03-27 18:42:44 +00:00
Jeong, YunWon
f6c09da968 Merge pull request #4785 from youknowone/ci-merge-group
Add merge group to ci
2023-03-28 01:37:21 +09:00
Jeong, YunWon
2c21ecd1df Merge pull request #4783 from Snowapril/test_descr_modify
Add test_descr from cpython v3.11.2
2023-03-27 16:36:39 +00:00
Jeong, YunWon
e9bc79d5ff Lib/test/test_descr.py Fix comment to clarify cpython originated expectedFailure
Co-authored-by: fanninpm <fanninpm@miamioh.edu>
2023-03-28 01:36:12 +09:00
Jeong YunWon
44599e9e7e Add merge group to ci 2023-03-28 01:30:41 +09:00
snowapril
910fe62d92 Add expectedFailure and skips for the test cases that cause failure or crash
Signed-off-by: snowapril <sinjihng@gmail.com>
2023-03-27 23:27:22 +09:00
snowapril
5a1beaffb8 Add test_descr.py from cpython 3.11.2
Signed-off-by: snowapril <sinjihng@gmail.com>
2023-03-27 23:15:53 +09:00
Jeong, YunWon
bdf5434d7b Merge pull request #4772 from Brain-organizer/testreplacemodify
update string_tests and solve failing test
2023-03-27 20:01:56 +09:00
Jaeyoung Ahn
096a3dc645 Fix builtins (has,get,set)attr type error messages (#4776) 2023-03-27 15:22:11 +09:00
Jeong, YunWon
11aefa3e42 Merge pull request #4735 from youknowone/try_from_borrowed_object
apply TryFromBorrowed more
2023-03-27 15:11:57 +09:00
Yongjin
f7eb51204a Returns new string when (str|bytes).replace get empty original string 2023-03-27 15:05:45 +09:00
Yongjin
3fde8f3b27 update string_tests.py from python 3.11.2 2023-03-27 15:05:01 +09:00
MegasKomnenos
1795740517 Allow try_update_quantity_from_tuple to set left alignment when needed (#4766) 2023-03-26 21:01:44 +09:00
Jeong, YunWon
4783df5737 Merge pull request #4767 from jyj0816/weakref
Add Hash and Iter of WeakProxy.rs for test\testWeakref.py
2023-03-26 14:34:35 +09:00
Jeong, YunWon
b00e63e37e Merge pull request #4778 from JaeyoungAhn/test_print
added test_print.py and marked TODOs
2023-03-26 13:12:02 +09:00
Jeong, YunWon
322165f404 Merge pull request #4777 from MegasKomnenos/base64
make int.to_bytes arguments optional and update base64.py and test_base64.py from CPython v3.11.2
2023-03-26 13:10:51 +09:00
JaeyoungAhn
074e4b3144 add TODOs 2023-03-26 12:27:58 +09:00
JaeyoungAhn
bd51adebdf new test_print.py from cpython v.3.11.2 2023-03-26 12:26:46 +09:00
jyj
afffb13902 Add Hash of WeakProxy for test\test_Weakref.py's "test_proxy_hash "
Fix Hash of WeakProxy : remove "?" ending of the code

Fix weakproxy.rs for test_proxy_hash to solve error
2023-03-26 12:12:38 +09:00
Jeong, YunWon
afe5e96aee Merge pull request #4774 from MegasKomnenos/bisect
Update bisect.py and test_bisect.py from CPython v3.11.2
2023-03-26 11:47:53 +09:00
jyj
95f3c0d69d Add Iter of WeakProxy for test\test_weakref.py's "test_proxy_next" 2023-03-26 11:32:36 +09:00
MegasKomnenos
185111d197 Update base64.py and test_base64.py from CPython v3.11.2 2023-03-26 11:30:10 +09:00
MegasKomnenos
439b44ee93 Add default parameters to to_bytes based on v3.11 standard 2023-03-26 11:29:43 +09:00
MegasKomnenos
d39ddac486 Update bisect.py and test_bisect.py from CPython v3.11.2 2023-03-26 10:54:35 +09:00
Padraic Fanning
f3a8e98511 Update wasmerio/setup-wasmer@v1 to v2 2023-03-25 18:24:46 -04:00
Padraic Fanning
703ab1eee0 Update lalrpop 0.19.8 to 0.19.9 in GH Actions
cc #4768
2023-03-25 18:22:31 -04:00
Padraic Fanning
d3028e0227 Update baptiste0928/cargo-install@v1 to v2 2023-03-25 18:21:23 -04:00
Padraic Fanning
fe87ca8117 Update actions/setup-node@v1 to v3 2023-03-25 15:51:03 -04:00
Padraic Fanning
fa39705dcb Update actions/setup-python@v2 to v4 2023-03-25 15:50:09 -04:00
Padraic Fanning
e53c9d1540 Update andstor/file-existence-action@v1 to v2 2023-03-25 15:47:08 -04:00
Padraic Fanning
8d3ca534c4 Update actions/cache@v2 to v3 2023-03-25 15:45:46 -04:00
Jim Fasarakis-Hilliard
6f8a652653 Merge pull request #4768 from youknowone/lalrpop
Update lalrpop
2023-03-25 16:17:06 +02:00
Jim Fasarakis-Hilliard
cfed9f3958 Merge pull request #4765 from JaeyoungAhn/built_in
update test_builtin.py from cpython v3.11.2
2023-03-25 16:01:15 +02:00
JaeyoungAhn
44da572893 uncomment annotation 2023-03-25 22:19:15 +09:00
Jeong YunWon
57b0ca644e Update lalrpop 2023-03-25 22:18:02 +09:00
JaeyoungAhn
f750c0ca06 added back the skipIf for windows 2023-03-25 22:13:50 +09:00
Jim Fasarakis-Hilliard
25ca331dc8 Merge pull request #4761 from JaeyoungAhn/test_deque
updated test_deque.py from CPython v3.11.2
2023-03-25 14:53:53 +02:00
Jeong, YunWon
a209c1a7dc Merge pull request #4763 from JaeyoungAhn/test_dict
Update test_dict.py from CPython v3.11.2
2023-03-25 21:01:19 +09:00
JaeyoungAhn
0873e451a8 update test_builtin.py from cpython v3.11.2 2023-03-25 20:30:40 +09:00
JaeyoungAhn
0eea760443 Update test_builtin.py from CPython v3.11.2 2023-03-25 20:17:27 +09:00
JaeyoungAhn
62ae7339fe Update test_dict.py from CPython v3.11.2 2023-03-25 19:59:31 +09:00
MegasKomnenos
887ffd1116 Fix issue with cformat converting bytes to string for bytesinner (#4760) 2023-03-25 19:58:09 +09:00
Jeong, YunWon
d5975a4023 Merge pull request #4756 from JaeyoungAhn/modify_comparison
resolve unittest error in test_sort.py
2023-03-25 19:46:12 +09:00
JaeyoungAhn
057d6aa497 Fix tuple comparison to use eq for leading elements 2023-03-25 19:45:55 +09:00
JaeyoungAhn
395e7b390a updated test_deque.py from CPython v3.11.2 2023-03-25 17:37:56 +09:00
Jim Fasarakis-Hilliard
c36e3612e7 Merge pull request #4758 from jinnarajin/hehe
update test_cmath.py from cpython 3.11.2
2023-03-25 10:14:25 +02:00
Jim Fasarakis-Hilliard
8b6ef48046 Merge pull request #4757 from Brain-organizer/testlistcompsupdate
update test_listcomps.py from cpython 3.11.2
2023-03-25 10:10:42 +02:00
Jeong, YunWon
31c33c87c5 Merge pull request #4748 from jyj0816/random
Fix int.from_bytes and Update random.py and test/test_random.py from CPython v3.11.2
2023-03-25 17:06:50 +09:00
jyj
8c9a33b737 Update random/test_random from CPython v3.11.2 2023-03-25 17:05:45 +09:00
jyj
286810d25b Fix byteorder parameter of int.from_bytes to optional 2023-03-25 17:05:35 +09:00
Jeong, YunWon
8c5a279ba2 Merge pull request #4754 from Brain-organizer/testmathupdate
update test_math.py from cpython 3.11.2
2023-03-25 16:40:44 +09:00
Jeong, YunWon
c7081f90e8 Merge pull request #4755 from jinnarajin/test_update
update test_complex.py from cpython 3.11.2
2023-03-25 16:32:21 +09:00
JaeyoungAhn
6e921912d7 update test_deque from cpython v3.11.2 2023-03-25 16:32:13 +09:00
jinnarajin
c132c66ac6 update test_cmath.py from cpython 3.11.2 2023-03-25 16:17:32 +09:00
Jeong, YunWon
e3bf553dc3 Merge pull request #4746 from MegasKomnenos/test_bytes
Update test_bytes.py from CPython v3.11.2
2023-03-25 16:15:29 +09:00
Yongjin
7c3115ded6 update test_listcomps.py from cpython 3.11.2 2023-03-25 16:07:54 +09:00
Jeong, YunWon
db2fa98b52 Merge pull request #4753 from ilp-sys/test_eof
Add test_eof.py from Cpython v3.11.2
2023-03-25 16:02:14 +09:00
Jeong, YunWon
42ee90b4d5 Merge pull request #4752 from jinnarajin/updategetoff
update test_getopt.py from cpython 3.11.2
2023-03-25 16:01:23 +09:00
Jeong, YunWon
f5d2b869c0 Merge pull request #4744 from MegasKomnenos/main
Update glob.py and test/test_glob.py from CPython v3.11.2
2023-03-25 16:00:59 +09:00
jinnarajin
919a2fbfda update test_complex.py from cpython 3.11.2 2023-03-25 15:47:59 +09:00
Jeong, YunWon
09c2dc2e85 Merge pull request #4751 from ilp-sys/test_string
Update test_string.py from Cpython v3.11.2
2023-03-25 15:22:47 +09:00
Jeong, YunWon
a3c1082bb1 Merge pull request #4749 from JaeyoungAhn/test_sort
updated test_sort from cpython 3.11.2
2023-03-25 15:22:11 +09:00
Yongjin
3a13a7bfda update test_math.py from cpython 3.11.2 2023-03-25 15:21:41 +09:00
Jiwoo Ahn
1a698fae33 Add test_eof.py from Cpython v3.11.2 2023-03-25 14:50:04 +09:00
jinnarajin
0772f6fe16 update test_getopt.py from cpython 3.11.2 2023-03-25 14:42:57 +09:00
MegasKomnenos
f63e6f3e1b Flag/skip failing tests 2023-03-25 14:30:26 +09:00
Jiwoo Ahn
b636979127 Update test_string.py from Cpython v3.11.2 2023-03-25 14:26:56 +09:00
Jeong, YunWon
55bb584360 Merge pull request #4745 from ilp-sys/test_array_update
Update test_array.py from Cpython v3.11.2
2023-03-25 14:25:50 +09:00
Jeong, YunWon
5cce71db44 Merge pull request #4747 from JaeyoungAhn/test_update
update test from test_pwd cpython 3.11.2
2023-03-25 14:06:37 +09:00
MegasKomnenos
adb8d08995 Update test_bytes.py from CPython v3.11.2 2023-03-25 14:02:17 +09:00
JaeyoungAhn
6baddab4da marked failed case expected failure 2023-03-25 14:00:12 +09:00
MegasKomnenos
5e51f2a6ed Update glob.py and test/test_glob.py from CPython v3.11.2 2023-03-25 13:57:00 +09:00
JaeyoungAhn
701efe8903 update test_sort from cpython 3.11.2 2023-03-25 13:51:39 +09:00
Jeong, YunWon
791111b060 Merge pull request #4743 from RustPython/dependabot/cargo/openssl-0.10.48
Bump openssl from 0.10.45 to 0.10.48
2023-03-25 13:29:14 +09:00
JaeyoungAhn
999925744c update test from test_pwd cPython 3.11.2 2023-03-25 13:22:39 +09:00
Jiwoo Ahn
7188e0486a Update test_array.py from Cpython v3.11.2 2023-03-25 12:48:34 +09:00
dependabot[bot]
993c892ad9 Bump openssl from 0.10.45 to 0.10.48
Bumps [openssl](https://github.com/sfackler/rust-openssl) from 0.10.45 to 0.10.48.
- [Release notes](https://github.com/sfackler/rust-openssl/releases)
- [Commits](https://github.com/sfackler/rust-openssl/compare/openssl-v0.10.45...openssl-v0.10.48)

---
updated-dependencies:
- dependency-name: openssl
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-03-25 01:44:37 +00:00
Jeong, YunWon
92b1a5bf3e Merge pull request #4740 from youknowone/method-pyobject2
method calls to take &PyObject
2023-03-24 15:36:37 +09:00
Jeong, YunWon
4993925471 Merge pull request #4742 from qingshi163/cmpref
Improve Comparable take zelf as reference
2023-03-24 15:35:59 +09:00
Jeong, YunWon
11e69eab36 Merge pull request #4741 from dalinaum/test_unicode_file_functions.py
Update test_unicode_file_functions.py from CPython 3.11.2
2023-03-24 15:34:51 +09:00
Kangzhi Shi
e22ff3af36 improve Comparable take zelf as reference 2023-03-23 21:37:28 +02:00
LYK
7a5053dfb2 Update test_unicode_file_functions.py from CPython 3.11.2 2023-03-24 03:04:53 +09:00
Jeong YunWon
4d04e5522c Merge pull request #4711 from minhrongcon2000/fix/format-panic
Fix panic from test_int__format__locale
2023-03-23 16:38:31 +09:00
Jeong YunWon
ff1d4a1d4e Merge pull request #4710 from youknowone/contructor-baseobjet
Constructor for PyBaseObject
2023-03-23 15:56:47 +09:00
Jeong YunWon
7a6848595f Merge pull request #4697 from youknowone/remove-pyc
remove *.pyc during rustpython-pylib build
2023-03-23 15:56:25 +09:00
minhrongcon2000
d39b44c4d3 Fix panic from test_int__format__locale
fix #4588
2023-03-23 15:48:51 +09:00
Jeong YunWon
6a4ce0a620 broken test case of FormatSpec::format_int 2023-03-23 15:48:51 +09:00
Jeong YunWon
494c7bda58 method calls to take &PyObject 2023-03-22 22:09:14 +09:00
Jeong YunWon
8000235591 &PyObject::try_to_ref 2023-03-22 21:14:22 +09:00
Jeong YunWon
64e1df5cdb TryFromBorrowedObject for &str 2023-03-22 21:14:22 +09:00
Jeong YunWon
3817246915 Merge pull request #4738 from qingshi163/binopfunc
Refactor Number Protocol
2023-03-22 12:24:37 +09:00
Kangzhi Shi
c5ce44eaaa remove slots.as_number replaced with slots.number 2023-03-21 22:48:56 +02:00
Kangzhi Shi
75e11daa95 fix right_op_wrapper 2023-03-21 22:32:27 +02:00
Kangzhi Shi
91b06fbe03 fixup 2023-03-21 22:23:34 +02:00
Kangzhi Shi
fb7393e4bb impl create PyNumberSlots from PyNumberMethods 2023-03-21 22:17:14 +02:00
Kangzhi Shi
0a8c642e93 change as_number to static immutable 2023-03-21 21:51:45 +02:00
Jeong YunWon
6b848f5038 Fix str.__str__ 2023-03-22 04:46:40 +09:00
Jeong YunWon
eee6e78ec1 PyRef::into_exact_or 2023-03-22 04:46:40 +09:00
Kangzhi Shi
9c3ced06d5 remove PyNumberMethods from HeapTypeExt 2023-03-21 21:34:29 +02:00
Kangzhi Shi
6a45c3e25a move PyNumberSlots into protocol/number.rs 2023-03-21 21:32:16 +02:00
Kangzhi Shi
d6acd2ee7d refactor PyNumberBinaryFunc 2023-03-21 21:27:51 +02:00
Kangzhi Shi
ee78527a9e fix internal type slots 2023-03-21 20:27:45 +02:00
Jeong YunWon
b08f415f80 remove useless refcount 2023-03-22 02:38:18 +09:00
Jeong YunWon
80afab57d3 optimization with try_from_borrowed_object 2023-03-22 02:38:18 +09:00
Jeong YunWon
12297982c8 &PyObject::try_to_ref 2023-03-22 02:29:02 +09:00
Jeong YunWon
7da7929edf TryFromBorrowedObject for &str 2023-03-22 02:29:02 +09:00
Jeong YunWon
aad90154e5 Merge pull request #4731 from youknowone/class-ctx
PyPayload::class takes ctx instead of vm
2023-03-22 02:28:11 +09:00
Jeong YunWon
7b99df6c48 PyPayload::class takes ctx instead of vm 2023-03-20 21:03:40 +09:00
Jeong YunWon
70fb78d031 Merge pull request #4721 from youknowone/opt-getattr
type.__name__ & getattr optimization
2023-03-20 21:01:16 +09:00
Jeong YunWon
d41e46be14 remove is_typevar duplciation 2023-03-20 20:15:56 +09:00
Jeong YunWon
73364fa2db try &'static str for slots.name 2023-03-20 20:15:56 +09:00
Jeong YunWon
44f6446f6c End of IntoPyStrRef 2023-03-20 19:00:02 +09:00
Jeong YunWon
dfb37d9a44 setattro uses Py<PyStr> 2023-03-20 19:00:02 +09:00
Jeong YunWon
ab1de2942c type.__name__ & getattr optimization 2023-03-20 19:00:01 +09:00
Jeong YunWon
2ff3ae86d0 getattr to take &Py<PyStr> 2023-03-20 18:57:40 +09:00
TomZz
906dfd3b6c Fix windows+ssl build error with unmatched types (#4725) 2023-03-20 17:07:45 +09:00
Jim Fasarakis-Hilliard
a824e7b379 Merge pull request #4716 from youknowone/exception
Constructor/Initializer for BaseException
2023-03-19 22:56:20 +02:00
Jim Fasarakis-Hilliard
a2b358613b Merge pull request #4722 from youknowone/mmap
clean up mmap
2023-03-19 22:38:07 +02:00
Jeong YunWon
99b7a5b482 Merge pull request #4723 from coolreader18/redox-compat
Fix redox
2023-03-19 12:21:51 +09:00
Noa
725dac204f Fix redox 2023-03-18 21:17:23 -05:00
Jeong YunWon
ccc8f7ed37 clean up name() usage 2023-03-19 10:31:32 +09:00
Jeong YunWon
8d38bf8ded clean up mmap 2023-03-19 10:31:23 +09:00
Jeong YunWon
cdfc67661e Constructor/Initializer for BaseException 2023-03-19 10:26:25 +09:00
Jeong YunWon
4fba939726 Merge pull request #4706 from youknowone/py-type
#[pyclass] impl Py<PyType>
2023-03-19 07:49:50 +09:00
Jeong YunWon
cf2b48fc0c clean up memory 2023-03-19 05:51:43 +09:00
Jeong YunWon
962b3659c6 clean up genericalias 2023-03-19 05:51:43 +09:00
Jeong YunWon
717242636a clean up generator 2023-03-19 05:51:43 +09:00
Jeong YunWon
29055ab4a6 clean up enumerate 2023-03-19 05:51:43 +09:00
Jeong YunWon
29073cf711 clean up dict 2023-03-19 05:51:43 +09:00
Jeong YunWon
cf82d6f0a5 clean up descriptor 2023-03-19 05:51:43 +09:00
Jeong YunWon
8fd984a767 clean up coroutine 2023-03-19 05:51:43 +09:00
Jeong YunWon
1db415d897 clean up bytes 2023-03-19 05:51:43 +09:00
Jeong YunWon
914e14bb8d clean up bytearray 2023-03-19 05:51:43 +09:00
Jeong YunWon
c8eff6ec07 clean up asyncgen 2023-03-19 05:51:43 +09:00
Jeong YunWon
107a64003d clean up sqlite a bit 2023-03-19 05:51:43 +09:00
Jeong YunWon
47d2e7b658 clean up PyArray 2023-03-19 04:57:07 +09:00
Jeong YunWon
13b4fd9b9f {PyRef->Py}<PyWeak>::upgrade 2023-03-19 04:57:07 +09:00
Jeong YunWon
e95eafe02a clean up io.{String,Bytes}IO 2023-03-19 04:57:07 +09:00
Jeong YunWon
2cc83ed1fe clean up Frame 2023-03-19 04:57:07 +09:00
Jeong YunWon
b052d646ee clean up PyCode 2023-03-19 04:57:06 +09:00
Jeong YunWon
174ef52df7 \#[pyclass] impl Py<PyType> for 2023-03-19 04:57:06 +09:00
Jeong YunWon
1fda3ba969 impl PyNativeFuncInternal for &Py<Self> form 2023-03-19 04:57:06 +09:00
Jeong YunWon
f314328294 lifetime bounded TryFromBorrwoedObject 2023-03-19 04:57:06 +09:00
Jeong YunWon
0f24d66234 pyclass macro to recognize Py/PyRef pattern 2023-03-19 04:57:06 +09:00
Jeong YunWon
fd028253fe Merge pull request #4719 from RustPython/DimitrisJim-patch-1
Update anystr.rs
2023-03-19 04:55:56 +09:00
Jim Fasarakis-Hilliard
2e4ff723aa Update anystr.rs 2023-03-18 21:22:01 +02:00
Jeong YunWon
1218ddd5dc Merge pull request #4709 from r3m0t/str-splitlines
Fix str.splitlines to recognise unicode line terminators
2023-03-19 00:42:18 +09:00
Jeong YunWon
330eaf3ed6 Add TODO comment to remove py_bytes_splitlines 2023-03-19 00:42:04 +09:00
Jim Fasarakis-Hilliard
2758a0ec53 Merge pull request #4714 from youknowone/refactor-useless-complexity
Refactor unnecessary complexity
2023-03-18 17:39:39 +02:00
Jim Fasarakis-Hilliard
1f63fc4fea Merge pull request #4713 from youknowone/common-int
relocate a few int functions to common
2023-03-18 17:35:26 +02:00
Jeong YunWon
de481bcd67 human readable PyIterIter::next 2023-03-18 22:43:32 +09:00
Jeong YunWon
12c96d06e4 Remove useless and_then 2023-03-18 22:10:25 +09:00
Jeong YunWon
b572b91783 relocate a few int functions to common 2023-03-18 21:53:15 +09:00
Jeong YunWon
ad29fbdd55 apply early returning pattern 2023-03-18 21:51:07 +09:00
Jeong YunWon
f21ad9b879 Merge pull request #4689 from haxelion/feature/tp_repr
Implemented repr slot
2023-03-18 21:47:24 +09:00
Jeong YunWon
8851a246de Simplify Representable impls 2023-03-18 20:42:12 +09:00
Charles Hubain
23cfdfffba Implemented repr slot 2023-03-18 19:28:05 +09:00
Jeong YunWon
f5f0d1a43b Constructor for PyBaseObject 2023-03-18 13:19:07 +09:00
Tomer Chachamu
8bfbfe3e46 Fix str.splitlines to recognise unicode line terminators 2023-03-18 01:10:59 +00:00
Jeong YunWon
21615cb158 Merge pull request #4703 from tdub0/markupbase
Update _markupbase, html/parser for CPython v3.11.2
2023-03-18 03:30:30 +09:00
Patrick
b6b6766000 Add an architecture with libc::c_char == u8 build to CI (#4704)
* Add compilation check for c_char is u8 arch
* install gcc-aarch64-linux-gnu for check
2023-03-18 03:29:56 +09:00
tdub0
f37ec60456 Update _markupbase, html/parser from CPython v3.11.2 2023-03-17 06:47:47 -07:00
Jeong YunWon
b35861005e more cspell (#4702) 2023-03-17 14:02:24 +09:00
bearney74
123559478e fix fmean/fsum ValueError and test_inv_cdf 2023-03-17 01:18:47 +09:00
Jeong YunWon
bbd8fb2342 Merge pull request #4698 from youknowone/cspell
Update cspell
2023-03-16 23:15:37 +09:00
Jeong YunWon
310a83427c cspell derive 2023-03-16 22:42:03 +09:00
Jeong YunWon
0b6546aa8d skip openssl@3 installing for macos 2023-03-16 22:39:09 +09:00
Jeong YunWon
21afc1933a Update cspell for compiler 2023-03-16 22:39:09 +09:00
Jeong YunWon
e800af70bb update cspell dict 2023-03-16 22:08:14 +09:00
Jeong YunWon
40d36165a4 update cspell sqlite 2023-03-16 22:08:12 +09:00
Jeong YunWon
f10b76f087 remove *.pyc during rustpython-pylib build 2023-03-16 15:31:56 +09:00
Jim Fasarakis-Hilliard
2efc566b14 Merge pull request #4693 from youknowone/zlib-level
Refactor zlib using new `Level` type
2023-03-15 18:26:43 +02:00
Jeong YunWon
089c8b7b0a Refactor zlib using new Level type 2023-03-16 00:52:39 +09:00
Jeong YunWon
134d9f1e98 Merge pull request #4688 from tdub0/compression
Update _compression, gzip, and test_gzip for CPython v3.11.2
2023-03-16 00:52:21 +09:00
tdub0
7e0863ef81 Update _compression, gzip from CPython v3.11.2 2023-03-15 23:52:55 +09:00
Jeong YunWon
e96dd96864 Refactor zlib and add wbits to zlib.compress() 2023-03-15 23:52:51 +09:00
Jeong YunWon
8ff947e83a Merge pull request #4677 from youknowone/arg-index
apply ArgIndex
2023-03-14 00:36:19 +09:00
Jeong YunWon
d5fd7ff339 Merge pull request #4652 from minhrongcon2000/fix/locale-windows
Add locale implementation for windows
2023-03-14 00:36:05 +09:00
Jeong YunWon
8a223b8178 Fix unix localeconv to refer libc and additional clean up 2023-03-13 23:19:52 +09:00
minh.pham2000
19193cd2a4 Add locale implementation for windows 2023-03-13 23:19:52 +09:00
Jeong YunWon
c15f670f2c Merge pull request #4684 from RustPython/charlie/derive
Include Derive feature with optional Serde dependency
2023-03-13 13:47:36 +09:00
Jim Fasarakis-Hilliard
f583f9a2d4 Merge pull request #4683 from tdub0/test_types_modify
Update test/test_types from CPython 3.11.2
2023-03-12 21:10:23 +02:00
Charlie Marsh
70734a1f28 Include Derive feature with optional Serde dependency 2023-03-12 14:46:10 -04:00
tdub0
b458797979 fix todo for RustPython 2023-03-12 09:58:25 -07:00
tdub0
754b1df51d specify failure reason 2023-03-11 16:40:03 -07:00
tdub0
0da079c98c exp failure on new dunder sig test 2023-03-11 16:32:27 -07:00
tdub0
7be91fc92c Update test_types.py from CPython v3.11.2 2023-03-11 16:26:13 -07:00
Jeong YunWon
415cdb1ef9 Merge pull request #4681 from abyesilyurt/aziz/base_exc_group
Add BaseExceptionGroup
2023-03-11 02:31:50 +09:00
Jim Fasarakis-Hilliard
17c5361d43 Merge pull request #4554 from howjmay/co_stacksize
Add co_stacksize to code objects
2023-03-10 17:12:30 +02:00
Aziz Berkay Yesilyurt
6cf99b4415 Comment out ExceptionGroup
To be implemented
2023-03-10 15:54:51 +01:00
Aziz Berkay Yesilyurt
7a5d4ecdc9 update test_pickle.py from CPython 3.11 2023-03-10 15:54:29 +01:00
Aziz Berkay Yesilyurt
1a7792e06f add BaseExceptionGroup 2023-03-10 15:30:36 +01:00
Yang Hau
95e863cb6d Add co_stacksize to code objects 2023-03-10 21:28:56 +08:00
Jeong YunWon
9b55505cdf Merge pull request #4678 from dalinaum/test_unicodedata
Update test_unicodedata from CPython 3.11.2
2023-03-10 18:14:55 +09:00
LYK
b687960c5d Mark failing tests of test_unitcodedata 2023-03-10 17:01:47 +09:00
Jeong YunWon
10ccbc6a31 Remove PyPaylod::special_retrieve 2023-03-10 16:44:47 +09:00
Jeong YunWon
0e2e7e533a ArgPrimitiveIndex for zlib 2023-03-10 16:44:47 +09:00
Jeong YunWon
d71910cdc2 ArgSize for io.rs 2023-03-10 06:21:40 +09:00
Jeong YunWon
3cb7c61026 ArgIndex for builtin functions 2023-03-10 06:21:40 +09:00
Jeong YunWon
08c73444c4 ArgSize for tuple 2023-03-10 06:21:40 +09:00
Jeong YunWon
e686b64ff3 ArgSize for str 2023-03-10 06:21:40 +09:00
Jeong YunWon
7d1fae001b ArgSize for list 2023-03-10 06:21:40 +09:00
Jeong YunWon
f190cdb3ec ArgIndex for bytes/bytearray 2023-03-10 06:21:40 +09:00
Jeong YunWon
6275435567 <i>::try_from_object with try_index 2023-03-10 06:21:40 +09:00
Jeong YunWon
5a90b14099 ArgIndex for range/slice 2023-03-10 06:21:40 +09:00
Jeong YunWon
bca7c16435 ArgIndex for math 2023-03-10 06:21:40 +09:00
Jeong YunWon
925962505e Adapt ArgIndex in bisect 2023-03-10 06:21:39 +09:00
Jeong YunWon
e95529aca0 introduce ArgSize 2023-03-10 06:21:39 +09:00
Jeong YunWon
ed7bcf787c expose ArgIndex 2023-03-10 06:21:39 +09:00
Jeong YunWon
87728c4452 Merge pull request #4608 from coolreader18/bag-deser
Rework frozen modules and directly deserialize to CodeObject<Literal>
2023-03-10 05:19:23 +09:00
LYK
134f62a776 Update test_unicodedata from CPython 3.11.2 2023-03-10 04:06:03 +09:00
Jeong YunWon
8fb53964e4 Merge pull request #4676 from dalinaum/test_unpack
Update test/test_unpack.py from CPython 3.11.2
2023-03-10 04:03:40 +09:00
LYK
eb7daf1b74 Update test/test_unpack.py from CPython 3.11.2 2023-03-10 03:24:36 +09:00
Jeong YunWon
ea665cb743 Merge pull request #4615 from xiaozhiyan/binaryops-with-number-protocol
Improve: binary ops with Number Protocol
2023-03-10 02:47:01 +09:00
Jeong YunWon
2287720201 Mark flaky test_enum::TestFlag::test_unique_composite skip 2023-03-10 02:46:14 +09:00
Jeong YunWon
91b57a3f28 Fix clippy warnings 2023-03-10 02:05:52 +09:00
Jeong YunWon
97d69bd437 pin rust-toolchain 2023-03-10 01:49:29 +09:00
Jeong YunWon
3c4ac0e5a2 clean up PyNumber interfaces 2023-03-10 01:37:30 +09:00
Jeong YunWon
9822749c92 PyNumberBinaryOpSlot -> PyNumberBinaryOp
it is not a slot
2023-03-10 01:17:44 +09:00
Jeong YunWon
d9fc95c2a1 Fix sequence protocol concat 2023-03-10 01:17:43 +09:00
Jeong YunWon
788ccffb2b Make PyNumberMethods static 2023-03-10 01:17:28 +09:00
Zhiyan Xiao
1fea829760 Do not skip test_xml_dom_minicompat.EmptyNodeListTestCase.test_emptynodelist___radd__ 2023-03-10 00:10:09 +09:00
Zhiyan Xiao
53b89b704c Temp: Skip test_mmap.MmapTests.test_concat_repeat_exception 2023-03-10 00:10:02 +09:00
Zhiyan Xiao
a949c35228 Temp: Skip test_collections.TestChainMap.test_union_operators 2023-03-09 23:51:14 +09:00
Zhiyan Xiao
81fbcbc1b2 Temp: Implement Number Protocol for PyDictKeys and PyDictItems 2023-03-09 23:48:46 +09:00
Jeong YunWon
05317f1664 temporary fill up missing number protocols 2023-03-09 23:43:15 +09:00
Zhiyan Xiao
8a42a68a4f Temp: Implement Number Protocol for PyBool 2023-03-09 23:41:57 +09:00
Zhiyan Xiao
984db6798c Add PyNumberSlots to support right binary ops 2023-03-09 23:41:48 +09:00
Zhiyan Xiao
ba9c354e1b Add binary ops in Number Protocol to update_slot 2023-03-09 21:33:25 +09:00
Zhiyan Xiao
f1dc0a6fcb Improve: binary ops with Number Protocol 2023-03-09 21:33:25 +09:00
Karatus
13d715e2a5 Binary ops with number protocol (partially imported)
from #4139 https://github.com/RustPython/RustPython/pull/4139
2023-03-09 21:33:25 +09:00
Jeong YunWon
1fceeab0fc Merge pull request #4674 from carlosmiei/copy-update
Update copy.py from CPython 3.11
2023-03-09 21:19:47 +09:00
Jim Fasarakis-Hilliard
51020cd635 Merge pull request #4673 from youknowone/pin-rust-version
Pin our toolchain to Rust version 1.67.1
2023-03-09 13:23:09 +02:00
carlosmiei
4d05077ec3 Update test_copy.py from CPython 3.11 2023-03-09 10:59:44 +00:00
carlosmiei
5b6a4c4563 Update copy.py from CPython 3.11 2023-03-09 10:56:11 +00:00
Jeong YunWon
8e834cbdfc Rust version 1.67.1 2023-03-09 19:47:38 +09:00
John Pham
27233364bc Add init for array.rs (#4667) 2023-03-09 19:45:58 +09:00
Jeong YunWon
685532d44a Merge pull request #4671 from eltociear/patch-1
Fix typo in number.rs
2023-03-09 17:31:28 +09:00
Ikko Eltociear Ashimine
692c8036b7 Fix typo in number.rs
seperate -> separate
2023-03-09 16:02:21 +09:00
Jim Fasarakis-Hilliard
f9715c442a Merge pull request #4669 from carlosmiei/update-calendar
Update calendar.py from CPython 3.11
2023-03-08 23:37:34 +02:00
Jim Fasarakis-Hilliard
e73a1c3298 Merge pull request #4670 from carlosmiei/osx-support-test
Update test__osx_support.py from CPython 3.11
2023-03-08 23:36:57 +02:00
carlosmiei
129a6f187e Update test__osx_support.py from CPython 3.11 2023-03-08 19:01:33 +00:00
carlosmiei
7710ed00d3 Update test_calendar from CPython 3.11 2023-03-08 17:41:08 +00:00
carlosmiei
fcc17254f6 Update calendar.py from CPython 3.11 2023-03-08 17:40:05 +00:00
Jeong YunWon
6edd3705a6 Merge pull request #4666 from dalinaum/tests
Update test/test_userlist.py and test_userdict.py from CPython 3.11.2
2023-03-08 15:28:06 +09:00
LYK
58f988bf33 Update test_userdict.py from CPython 3.11.2
Only whitespace is different.
2023-03-08 14:37:00 +09:00
CPython Developers
34a0615ac1 Update test/test_userlist.py from CPython 3.11.2 2023-03-08 14:34:40 +09:00
Jeong YunWon
5a74f08c84 Merge pull request #4654 from youknowone/pycallble
Add protocol object PyCallable
2023-03-08 05:01:31 +09:00
Jeong YunWon
bdce56df30 Merge pull request #4656 from dalinaum/with-type-error
Change error type for bad objects in "with" and "async with"
2023-03-08 05:00:43 +09:00
Jeong YunWon
f4a7b652c7 Merge pull request #4664 from TheYkk/main
Remove strict type to allow cross compiling
2023-03-08 04:58:23 +09:00
Kaan Karakaya
cb479bf199 Remove strict type to allow cross compiling
Signed-off-by: Kaan Karakaya <yusufkaan142@gmail.com>
2023-03-07 21:46:53 +03:00
Jeong YunWon
b10421a9b4 Merge pull request #4637 from youknowone/refactor-asnumber
Refactor with AsNumber::number_downcast_exact
2023-03-08 02:17:43 +09:00
LYK
ef77e45427 Change error type for bad objects in "with" and "async with"
This patch does not cover all cases of `test_contextlib.py`

`bytecode::Instruction::SetupWith` and `BeforeAsyncWith`
raise a TypeError instead of an AttributeError.

Co-authored-by: Jeong YunWon <69878+youknowone@users.noreply.github.com>
2023-03-08 01:45:12 +09:00
Jeong YunWon
823b5288f9 Refactor AsNumber for PyInt 2023-03-08 01:37:58 +09:00
Jeong YunWon
c17951e3f6 Refactor AsNumber for PyFloat 2023-03-08 01:37:58 +09:00
Jeong YunWon
f0a29de87d AsNumber::number_downcast_exact useful for actual numbers 2023-03-08 01:37:58 +09:00
Jeong YunWon
b60271a6cf deprecate vm.invoke 2023-03-08 01:35:30 +09:00
Jeong YunWon
f00e6b69f2 Merge pull request #4659 from dalinaum/contextlib
Update contextlib.py and test_contextlib from CPython 3.11.2
2023-03-08 01:32:18 +09:00
Jeong YunWon
cc8515041b Merge pull request #4658 from dalinaum/test_userstring
Update test/test_userstring.py from CPython 3.11.2
2023-03-08 01:31:57 +09:00
Jeong YunWon
01512e27a0 Merge pull request #4657 from dalinaum/test_weakset
Update test/test_weakset.py from CPython 3.11.2
2023-03-08 00:41:09 +09:00
CPython Developers
301b79c688 Update contextlib.py and test_contextlib from CPython 3.11.2 2023-03-08 00:12:07 +09:00
LYK
98654634ca Update test/test_userstring.py from CPython 3.11.2 2023-03-07 23:58:26 +09:00
LYK
1812bd17a5 Mark a failing test of test/test_weakset.py 2023-03-07 23:49:18 +09:00
CPython Developers
7b6486cd30 Update test/test_weakset.py from CPython 3.11.2 2023-03-07 23:47:16 +09:00
seungha-kim
223fe14d85 Update to pass test for unhashable collections (#4640) 2023-03-07 23:45:33 +09:00
Jeong YunWon
5eb775462d vm.invoke -> obj.call 2023-03-07 20:06:42 +09:00
Jeong YunWon
ed60687f11 Replace all of callable operations to PyCallable 2023-03-07 19:44:46 +09:00
Jeong YunWon
66d9514e12 remove vm.is_callable 2023-03-07 19:28:21 +09:00
Jeong YunWon
37cc852bfc PyCallable 2023-03-07 19:28:13 +09:00
Jeong YunWon
10eb20e44b Merge pull request #4645 from youknowone/derive-trait-extend_slots
Add extend_slot support for protocol traits
2023-03-07 19:12:08 +09:00
Jeong YunWon
7744e95aea Merge pull request #4651 from minhrongcon2000/fix/add-arg-index
Add ArgIndex according to #4629
2023-03-07 15:19:38 +09:00
minh.pham2000
3f871f0805 Add ArgIndex according to #4629 2023-03-07 12:36:55 +07:00
minh.pham2000
db7b945262 Add ArgIndex according to #4629 2023-03-07 10:35:49 +07:00
Jeong YunWon
64aef5c13c Merge pull request #4649 from dalinaum/test/test_contextlib.py
Update test/test_contextlib.py from CPython 3.11.2
2023-03-07 12:13:44 +09:00
Jeong YunWon
3fe5a368ac Merge pull request #4648 from dalinaum/test_with
Update test/test_with.py from CPython 3.11.2
2023-03-07 12:11:11 +09:00
Jeong YunWon
f23a8480e7 Merge pull request #4647 from dalinaum/test_zlib
Update test/test_zlib.py from CPython 3.11.2
2023-03-07 12:08:37 +09:00
LYK
5d6a48b568 Mark failing tests of test_contextlib.py 2023-03-07 11:20:07 +09:00
CPython Developers
df48df5ede Update test/test_contextlib.py from CPython 3.11.2 2023-03-07 11:19:42 +09:00
LYK
51fcfe980f Mark failing tests of test/test_with.py
A TypeError is raised instead of an AttributeError in "with" and
"async with" statements for objects which do not support the context
manager or asynchronous context manager protocols correspondingly.

See also:
https://github.com/python/cpython/pull/26809
2023-03-07 05:02:10 +09:00
CPython Developers
e983506e23 Update test/test_with.py from CPython 3.11.2 2023-03-07 04:51:09 +09:00
Noa
456bc80697 Rework frozen modules and directly deserialize to CodeObject<Literal> 2023-03-06 13:45:33 -06:00
CPython Developers
5be22a073b Update test/test_zlib.py from CPython 3.11.2 2023-03-07 04:34:58 +09:00
Jeong YunWon
26a3ec9ae3 Merge pull request #4646 from dalinaum/winver
Add sys.winver
2023-03-07 00:19:00 +09:00
Jeong YunWon
8d6d47a5c6 Merge pull request #4642 from xiaozhiyan/fix-rsub-of-pyset-pyfrozenset
Fix `rsub` of `PySet` and `PyFrozenSet`
2023-03-06 23:48:50 +09:00
LYK
5adae77e39 Add sys.winver 2023-03-06 23:21:04 +09:00
Zhiyan Xiao
79840126b9 Add test cases for __sub__ and __rsub__ methods of set and frozenset 2023-03-06 22:46:07 +09:00
Zhiyan Xiao
05d0248b18 Fix rsub of PySet and PyFrozenSet 2023-03-06 22:45:48 +09:00
Jeong YunWon
2a61e1cace Add extend_slot support for protocol traits 2023-03-06 21:02:44 +09:00
Jeong YunWon
abf850af91 Merge pull request #4636 from youknowone/complex-asnumber
Fix AsNumber for PyComplex bugs
2023-03-06 17:38:06 +09:00
Jeong YunWon
0722db10df Merge pull request #4635 from youknowone/relocate-tests
Relocate and format extra_tests/snippets/builtin_{int,float.complex}.py
2023-03-06 17:29:08 +09:00
Jeong YunWon
1fece09687 Merge pull request #4644 from xiaozhiyan/implement-number-protocol-for-pydict
Implement Number Protocol for `PyDict`
2023-03-06 15:35:11 +09:00
Jeong YunWon
9f0db9a02a Merge pull request #4610 from haxelion/feature/hashlib_shake
Implementing shake_128 and shake_256 in the hashlib module
2023-03-06 14:59:04 +09:00
Zhiyan Xiao
6f499013f6 Implement Number Protocol for PyDict 2023-03-06 05:34:18 +09:00
Charles Hubain
29de419c87 Implementing shake_128 and shake_256 in the hashlib module. 2023-03-06 00:43:06 +09:00
Jeong YunWon
9bde9c9fbb Fix AsNumber for PyComplex bugs 2023-03-05 22:36:34 +09:00
black formatter
865c4984f6 format tests with black 2023-03-05 22:34:55 +09:00
Jeong YunWon
987ad12181 relocate int-unrelated tests to proper files 2023-03-05 22:34:55 +09:00
Jeong YunWon
003f3b4fe1 Merge pull request #4632 from dalinaum/test/test_buffer
Update test/test_buffer from CPython 3.11.2
2023-03-05 19:15:14 +09:00
Jeong YunWon
18f6a659f4 Merge pull request #4631 from dalinaum/test_bool
Update test/test_bool from cpython 3.11.2
2023-03-05 19:13:42 +09:00
CPython Developers
6e0de0d196 Update test/test_buffer from CPython 3.11.2 2023-03-05 16:07:25 +09:00
Jeong YunWon
4d464cc9fb Merge pull request #4627 from dalinaum/exception_hierarchy.txt
Update Lib/test/exception_hierarchy.txt from CPython 3.11.2
2023-03-05 11:07:06 +09:00
CPython Developers
4030807b9b Update Lib/test/exception_hierarchy.txt from CPython 3.11.2 2023-03-05 05:26:55 +09:00
Jim Fasarakis-Hilliard
174fbe56b0 Merge pull request #4626 from youknowone/bigmem
Fix test_bigmem glitches
2023-03-04 22:02:52 +02:00
Jeong YunWon
ab7971de42 Merge pull request #4624 from dalinaum/test_baseexception
Update test_baseexception.py from cpython 3.11.2
2023-03-05 04:06:34 +09:00
Jeong YunWon
65224213d0 test_bigmem fix expectedFailures not to always fall 2023-03-05 04:00:42 +09:00
Jeong YunWon
992f5aaed7 test_bigmem: removed outdated expected failures 2023-03-05 03:59:33 +09:00
Jeong YunWon
298b169dc0 Merge pull request #4625 from dalinaum/test_bigmem
Update test_bigmem from cpython 3.11.2
2023-03-05 03:52:17 +09:00
CPython Developers
3fd46e8cbe Update test/test_bool from cpython 3.11.2 2023-03-05 03:49:26 +09:00
LYK
9c10d4aa3f Mark failing tests of test_baseexception. 2023-03-05 03:21:18 +09:00
CPython Developers
a7b8768e56 Update test_baseexception.py from cpython 3.11.2 2023-03-05 03:19:40 +09:00
CPython Developers
5d482abb67 Update test_bigmem from cpython 3.11.2 2023-03-05 03:13:37 +09:00
Charlie Marsh
1871a1632e Treat match and case as soft keywords in lambda assignments (#4623) 2023-03-04 12:42:05 -05:00
Jeong YunWon
4e19be7e43 Merge pull request #4460 from evanrittenhouse/strict_mode_new
Implement `strict_mode` keyword for `binascii.a2b_base64()`
2023-03-04 17:44:53 +09:00
Jeong YunWon
ff973caa67 ToPyException for base64::DecodeError 2023-03-04 16:19:29 +09:00
Evan Rittenhouse
362be9f344 Add 'invalid base64-encoded string' error 2023-03-04 16:11:02 +09:00
Evan Rittenhouse
904e3a3492 Remove expected failure from test_base64invalid 2023-03-04 16:11:02 +09:00
Evan Rittenhouse
404c398b59 Implement strict_mode keyword for binascii.a2b_base64 2023-03-04 16:11:01 +09:00
Jeong YunWon
1f92212497 Merge pull request #4612 from youknowone/PyObject_Format
Add vm.format to call __format__ like PyObject_Format
2023-03-04 16:04:52 +09:00
Jeong YunWon
d7f65cbbcd Merge pull request #4622 from fanninpm/protocol-number-pystr
Add number protocol for PyStr
2023-03-04 14:06:42 +09:00
Padraic Fanning
b4124d0d92 Add number protocol for PyStr 2023-03-03 18:53:57 -05:00
John Pham
f3e9413b2d Add strxfrm and strcoll to locale mod (#4603) 2023-03-04 03:21:04 +09:00
Jim Fasarakis-Hilliard
d09527b75e Merge pull request #4621 from dalinaum/test_atexit
Update test_atexit from cpython 3.11.2
2023-03-03 13:11:21 +02:00
CPython Developers
ad05321d94 Update test_atexit from cpython 3.11.2 2023-03-03 16:38:41 +09:00
Jeong YunWon
822f6a9fa6 Merge pull request #4620 from RustPython/charlie/serde
Add optional serde dependency
2023-03-03 09:03:29 +09:00
Charlie Marsh
b846311173 Add optional serde dependency 2023-03-02 15:38:40 -05:00
Dan Näsman
cdf5634cf2 Implement as_sequence for array (#4585) 2023-03-03 04:29:52 +09:00
Jeong YunWon
2d02933d35 Merge pull request #4619 from xiaozhiyan/implement-number-protocol-for-pytype-pyset-pyfrozenset
Implement Number Protocol for `PyType`, `PySet` and `PyFrozenSet`
2023-03-03 04:22:20 +09:00
Jeong YunWon
11991772f8 leave fixme to wrong object.__str__ impl 2023-03-03 03:29:50 +09:00
Jeong YunWon
ab9573f108 object.__format__ the right way 2023-03-03 03:29:50 +09:00
Jeong YunWon
195673b01a Add vm.format to call __format__ like PyObject_Format 2023-03-03 03:29:50 +09:00
Jeong YunWon
c656cdd951 Merge pull request #4617 from RustPython/charlie/tag
Pin `youknowone/unicode_names2` to a specific revision
2023-03-03 03:28:06 +09:00
Zhiyan Xiao
1bec1d5f26 Implement Number Protocol for PySet and PyFrozenSet 2023-03-03 03:19:13 +09:00
Zhiyan Xiao
7a12d1e322 Implement Number Protocol for PyType 2023-03-03 03:19:13 +09:00
Charlie Marsh
25ddf1d1e9 Pin youknowone/unicode_names2 to a specific rev 2023-03-02 13:15:36 -05:00
fanninpm
6e4c2fe786 Merge pull request #4597 from youknowone/expected-failure-if
Add unittset.expectedFailureIf for RustPython
2023-03-02 09:49:52 -05:00
Jeong YunWon
a7c9856851 Merge pull request #4607 from youknowone/test-init
Add Lib/test/__init__.py
2023-03-02 15:34:45 +09:00
Jeong YunWon
78382209d3 Merge pull request #4606 from youknowone/to-u32
usize::to_u32 for compiler internal conversion
2023-03-02 15:01:59 +09:00
CPython Developers
c32bfa6eeb Add Lib/test/__init__.py 2023-03-02 15:01:12 +09:00
Jeong YunWon
815083d1e8 Add #![deny(clippy::cast_possible_truncation)] 2023-03-02 13:34:21 +09:00
Jeong YunWon
b432bb0cf3 usize::to_u32 for compiler internal conversion 2023-03-02 13:32:54 +09:00
Jeong YunWon
22bc2d24ab Merge pull request #4524 from coolreader18/no-serde
Custom marshal enc/decoding impl
2023-03-02 13:10:41 +09:00
Jeong YunWon
e5cf139b03 Merge pull request #4605 from coolreader18/no-hacky-gats
Switch hacky GAT-workarounds to use actual GATs
2023-03-02 12:58:26 +09:00
Noa
ec7b5861f9 Address review comments 2023-03-01 21:11:59 -06:00
Noa
58ac82b0a7 Switch hacky GAT-workarounds to use actual GATs 2023-03-01 20:48:12 -06:00
Noa
6d1f2986fe Custom marshal enc/decoding impl 2023-03-01 20:47:21 -06:00
Steve Shi
166959db41 Fix sys.getsizeof (#4604) 2023-03-02 05:56:34 +09:00
Jeong YunWon
367a948ff0 Adapt unittest.expectedFailureIf 2023-03-02 04:41:15 +09:00
Jeong YunWon
6d996834f3 unittest.expectedFailureIf 2023-03-01 22:31:19 +09:00
Carlos Gonçalves
e731e658ba feat(configparser): update to 3.11 (#4595) 2023-03-01 22:28:21 +09:00
Jeong YunWon
f62e8f594d Merge pull request #4601 from DimitrisJim/debug_kw
Disallow __debug__ as args, kwargs name.
2023-03-01 22:13:47 +09:00
Jeong YunWon
880ab910f7 Merge pull request #4602 from DimitrisJim/test_syntax_update
Update test_syntax from CPython 3.11.0
2023-03-01 22:11:11 +09:00
DimitrisJim
2da4b70e3a Mark failing tests. 2023-03-01 13:00:17 +02:00
DimitrisJim
f58014e2b3 Add test_syntax from CPython 3.11.0 2023-03-01 12:58:51 +02:00
Jim Fasarakis-Hilliard
dbfa61a156 Merge pull request #4599 from youknowone/test-import
Add test_import from CPython 3.11.2
2023-03-01 12:18:51 +02:00
DimitrisJim
8294d4ae8e Disallow __debug__ as args, kwargs name. 2023-03-01 12:15:41 +02:00
Jeong YunWon
8daffa76dd Mark failing tests of test_import 2023-03-01 18:07:36 +09:00
CPython Developers
d55f554d7b test_import from CPython 3.11.2 2023-03-01 18:06:50 +09:00
Jim Fasarakis-Hilliard
a0472e11a9 Merge pull request #4598 from DimitrisJim/fix_ensurepip
Fix ensurepip install issues and add a CI check to ensure it gets caught if broken.
2023-03-01 10:37:25 +02:00
DimitrisJim
c8d34fbe8c Mark failing test. Hardcoded value for library path means we can't succeed. 2023-03-01 00:57:10 +02:00
DimitrisJim
6ccb9814f8 Add CI check for ensurepip. 2023-03-01 00:03:53 +02:00
DimitrisJim
733813700e Change python to rustpython in sysconfig. 2023-03-01 00:03:40 +02:00
John Pham
af1d46f184 Add _locale module (#4558)
_locale.localeconv and _locale.setlocale

Co-authored-by: Jeong YunWon <jeong@youknowone.org>
2023-03-01 05:37:52 +09:00
Jeong YunWon
5f7fffbe81 Merge pull request #4596 from youknowone/format-clean
clean up common/src/format.rs
2023-03-01 05:36:36 +09:00
Jeong YunWon
71e11950d9 rename preconversor -> conversion to follow cpython name 2023-03-01 04:51:47 +09:00
Jeong YunWon
a1b74b0758 early error return of raw_magnitude_str 2023-03-01 04:19:28 +09:00
Jeong YunWon
f2515405b5 Merge pull request #4592 from carlosmiei/argparse
feat(argparse): update to 3.11
2023-03-01 03:52:03 +09:00
Jeong YunWon
3dc15623d5 Merge pull request #4591 from carlosmiei/antigravity
feat(antigravity): update to 3.11
2023-03-01 03:39:19 +09:00
carlosmiei
ffe1b6e03a update test 2023-02-28 17:50:29 +00:00
carlosmiei
7265921719 feat(argparse): update to 3.11 2023-02-28 17:49:39 +00:00
carlosmiei
58e24cea4b feat(antigravity): update to 3.11 2023-02-28 17:34:18 +00:00
Jeong YunWon
aa8336ee94 Merge pull request #4582 from youknowone/unicode_names2
Another temporary fix for #4566
2023-02-28 22:33:23 +09:00
Jeong YunWon
733ff9a05b Merge pull request #4590 from youknowone/ensurepip
Update ensurepip from cpython 3.11.2
2023-02-28 22:33:06 +09:00
CPython Developers
1204996378 Update ensurepip from cpython 3.11.2 2023-02-28 20:32:10 +09:00
DimitrisJim
c0f5266bc4 Use insta to verify values. 2023-02-28 20:00:47 +09:00
Jeong YunWon
1ca57407d3 Use git version of unicode_names2 to avoid alias search failure 2023-02-28 20:00:47 +09:00
Jim Fasarakis-Hilliard
e082d8cc75 Merge pull request #4586 from youknowone/trace
Update trace from CPython 3.11.2
2023-02-28 12:30:33 +02:00
Jeong YunWon
94237df7b5 Merge pull request #4587 from youknowone/crlf
Fix CR/LF of python libraries
2023-02-28 17:57:50 +09:00
CPython Developers
9377bef603 Update trace from CPython 3.11.2 2023-02-28 17:45:08 +09:00
Jeong YunWon
6ee827bd8a Fix CR/LF of python libraries 2023-02-28 17:11:41 +09:00
Jeong YunWon
351d464448 Merge pull request #4571 from youknowone/posix-pathconf_names
posix.pathconf_names
2023-02-27 15:06:51 +09:00
Jeong YunWon
fcacdb2791 macos test for pathconf_names 2023-02-27 14:23:22 +09:00
Jeong YunWon
e25b89d49a assert errno() == 0 before pathconf 2023-02-27 14:22:18 +09:00
Jeong YunWon
6385b82c2b Merge pull request #4579 from threeifbyair/array-4575
Satisfy safety conditions for std::slice::from_raw_parts. Fixes #4575.
2023-02-27 14:06:02 +09:00
Jeong YunWon
622b62cff1 Merge pull request #4584 from charliermarsh/charlie/adds
Use proper locations for sub-expressions in constant matches
2023-02-27 13:59:49 +09:00
Charlie Marsh
e206505061 Use proper locations for sub-expressions in constant matches 2023-02-26 23:10:38 -05:00
Alistair Bell
4955f05fa3 Modified fix for #4575 per @youknowone. Thanks, YunWon! 2023-02-26 09:24:01 -08:00
Jeong YunWon
258d1c0521 Merge pull request #4580 from DimitrisJim/load_debug
Always load __debug__ as a const.
2023-02-26 18:52:10 +09:00
Jeong YunWon
734b872e41 Merge pull request #4578 from RustPython/image-links
attach links to images
2023-02-26 18:46:44 +09:00
DimitrisJim
8405693325 Always load __debug__ as a const. 2023-02-26 09:58:27 +02:00
Alistair Bell
910574361c Satisfy safety conditions for std::slice::from_raw_parts. Fixes #4575. 2023-02-25 22:10:57 -08:00
Jim Fasarakis-Hilliard
0a8fa3a8ba attach links to images
closes #4577
2023-02-26 08:00:34 +02:00
Jeong YunWon
0094064f4a Merge pull request #4576 from jhowww/sys-platform-wasi
add wasm32-wasi as wasi in sys.platform
2023-02-26 06:25:21 +09:00
Cheng-Hao
7e37f8e3ed add wasm32-wasi as wasi in sys.platform 2023-02-25 12:40:08 -08:00
Jeong YunWon
945a53353f Merge pull request #4569 from youknowone/update-sys-311
Update sysconfig, test_sys and test_syslog from CPython 3.11
2023-02-25 23:01:48 +09:00
Jeong Yunwon
e3111729c6 Mark windows failing tests from test_sysconfig 2023-02-25 23:00:51 +09:00
CPython Developers
d41c786554 Update test_sysconfig from CPython 3.11.2 2023-02-25 23:00:51 +09:00
Jeong YunWon
c40e26ada9 Add dummy sys._vpath for windows 2023-02-25 23:00:51 +09:00
CPython Developers
9a8364566c cherry picking _imp.extension_suffixes()[0] fix from cpython main
fix #4123
2023-02-25 23:00:51 +09:00
Jeong YunWon
75aefb89fd Mark newly failing tests 2023-02-25 23:00:51 +09:00
CPython Developers
a3e60af35e Update test_syslog from CPython 3.11.2 2023-02-25 23:00:51 +09:00
Jeong YunWon
bc87fadc03 mark new failing tests 2023-02-25 23:00:51 +09:00
CPython Developers
76e295f606 Update sysconfig from CPython 3.11.2 2023-02-25 23:00:51 +09:00
CPython Developers
ad7544ce90 Update test_sys from CPython 3.11.2 2023-02-25 23:00:51 +09:00
Jeong YunWon
01a5355713 Update test_syslog from CPython 3.10.5 2023-02-25 23:00:51 +09:00
Jeong YunWon
abcb93d0bd test_syslog crlf -> lf 2023-02-25 23:00:50 +09:00
Jeong YunWon
e45b5b35e3 Update test_sys from CPython 3.10.5 2023-02-25 23:00:50 +09:00
CPython Developers
a6e44a349a Update sysconfig from CPython 3.10.5 2023-02-25 23:00:50 +09:00
Jeong YunWon
fe22f1e46a add sys.int_info new fields 2023-02-25 23:00:50 +09:00
Jeong YunWon
89bb079e50 add sys.flags new fields 2023-02-25 23:00:50 +09:00
Jeong YunWon
f2e32857cb Merge pull request #4561 from youknowone/update-importlib
Update importlib from CPython 3.11.2
2023-02-25 23:00:12 +09:00
Jeong YunWon
7492b46c43 mark success/fail tests for test_import 2023-02-25 22:59:57 +09:00
Jeong YunWon
ad20a404a9 Update __hello__ and __phello__ from cpython 2023-02-25 22:59:57 +09:00
CPython Developers
496006d629 Update importlib from CPython 3.11.2 2023-02-25 22:59:57 +09:00
Jeong YunWon
f2f8ecac73 Merge pull request #4559 from youknowone/importlib-requirements
Add (fake) frozenmodule __origname__ and _imp.find_frozen
2023-02-25 22:59:14 +09:00
Jeong YunWon
204643b8da Merge pull request #4574 from xiaozhiyan/implement-number-protocol-for-pybytearray
Implement Number Protocol for `PyByteArray`
2023-02-25 22:58:24 +09:00
Zhiyan Xiao
4f206c5f1e Implement Number Protocol for PyByteArray 2023-02-25 21:43:42 +09:00
Jeong YunWon
1e09c63282 try macos again 2023-02-25 19:09:07 +09:00
Jeong YunWon
c88248acb1 Refactor posix.pathconf_names 2023-02-25 19:08:55 +09:00
Jeong YunWon
547d752df5 Merge pull request #4567 from youknowone/unicodedata
clean up unicodedata
2023-02-25 17:55:06 +09:00
Jeong YunWon
8b40c2e372 clean up unicodedata 2023-02-25 17:16:03 +09:00
Jim Fasarakis-Hilliard
f51764f8d0 Merge pull request #3806 from ellie-ys/main
Update install rustup
2023-02-24 16:19:39 +02:00
Jim Fasarakis-Hilliard
64f95fec69 fix typo.
Co-authored-by: Jeong YunWon <69878+youknowone@users.noreply.github.com>
2023-02-24 16:19:30 +02:00
Jim Fasarakis-Hilliard
8ca35fdf4f Merge pull request #4562 from youknowone/async-windows
double windows sleep for syntax_async until it success
2023-02-24 16:04:30 +02:00
Jeong YunWon
d87921c12a Merge pull request #4560 from youknowone/unittest
Update unittest from CPython 3.11
2023-02-24 21:45:01 +09:00
Jeong YunWon
ae3de43a62 retry 10times in windows 2023-02-24 21:44:10 +09:00
Jeong YunWon
fbb5e78176 double windows sleep for syntax_async 2023-02-24 15:44:22 +09:00
Jeong YunWon
10de265b4f Add basic _imp.find_frozen 2023-02-24 14:34:50 +09:00
Jeong YunWon
4a357ee6e9 adjust successful/failing tests from unittest/test 2023-02-24 14:30:25 +09:00
CPython Developers
40b96bfffd Update unittest from CPython 3.11 2023-02-24 14:24:57 +09:00
Jeong YunWon
f53c68695d Add __origname__ to frozenlibs to fix importlib 2023-02-24 14:14:19 +09:00
Jeong YunWon
cfc2e3e228 Merge pull request #4537 from youknowone/lib-support
Update test.support from CPython 3.11.2
2023-02-24 10:59:04 +09:00
Jeong YunWon
25398fe622 adjust success/filure for test_utf8_mode 2023-02-24 02:07:44 +09:00
CPython Developers
a7c3740dee Update test_utf8_mode from 3.11.2 2023-02-24 02:06:13 +09:00
CPython Devleopers
7de056437f Update locale from CPython 3.11.2 2023-02-24 01:16:25 +09:00
Jeong YunWon
cfa9de4047 Mark newly failing tests 2023-02-24 01:16:25 +09:00
CPython Devleopers
c48fe567f7 Update test.support from CPython 3.11.2 2023-02-24 01:16:22 +09:00
Jeong YunWon
c4b6789666 Merge pull request #4557 from charliermarsh/charlie/star
Allow type variable tuple for *args
2023-02-24 00:54:38 +09:00
Jeong YunWon
6404c4bca8 Merge pull request #4555 from youknowone/imp-override-frozen-modules
Add _imp._override_frozen_modules_for_tests
2023-02-23 23:28:08 +09:00
Charlie Marsh
9423712696 Allow type variable tuple for *args 2023-02-23 08:39:59 -05:00
Jeong YunWon
5018b1a426 Merge pull request #4556 from youknowone/fix-async
Fix syntax_async test less fail in windows
2023-02-23 21:18:41 +09:00
Jeong YunWon
1336ca5d9d Fix syntax_async 2023-02-23 20:05:34 +09:00
Jeong YunWon
5e3046d86a Add _imp._override_frozen_modules_for_tests 2023-02-23 19:23:43 +09:00
Dimitris Fasarakis Hilliard
e45536ae93 Address feedback, clean up readme. 2023-02-23 11:15:40 +02:00
Jeong YunWon
48254acb4e Merge pull request #4165 from jopemachine/fix-test-prop
Fix `property.__set_name__` error message when arguments are invalid
2023-02-23 18:07:55 +09:00
Ellie
20b408ff3e Update install rustup 2023-02-23 10:38:47 +02:00
Jeong YunWon
4a579b5752 Merge pull request #4553 from howjmay/co_nlocals
Add co_nlocals to code object
2023-02-23 11:14:34 +09:00
Jeong YunWon
edf5995a1e Merge pull request #4552 from charliermarsh/charlie/loc
Limit match range to end of last statement
2023-02-23 02:11:42 +09:00
Jeong YunWon
6b2efdc9c3 Merge pull request #4543 from youknowone/flatten-parser
Flatten parser interface
2023-02-23 02:09:46 +09:00
Jeong YunWon
17f5aed6dd Merge pull request #4548 from youknowone/hash-tuple
Prepare to implement hash for tuple
2023-02-23 02:08:13 +09:00
Jim Fasarakis-Hilliard
fb44ded002 Merge pull request #4549 from youknowone/py311
Mark RustPython as Python 3.11
2023-02-22 18:38:12 +02:00
Yang Hau
fea711cebb Add co_nlocals to code object
Closes #4546
2023-02-23 00:29:35 +08:00
Charlie Marsh
57efe6f75d Limit match range to end of last statement 2023-02-22 11:25:50 -05:00
Jim Fasarakis-Hilliard
6d71f75817 Merge pull request #4551 from charliermarsh/charlie/trailing
Allow trailing commas in MappingPattern
2023-02-22 17:53:12 +02:00
Charlie Marsh
d570b1731e Allow trailing commas in MappingPattern 2023-02-22 10:02:41 -05:00
Jeong YunWon
8fc263f62f Python 3.11 missed 2023-02-22 22:18:15 +09:00
Narawit Rakket
3886f889ab Prepare to implement hash for tuple 2023-02-22 22:07:23 +09:00
Jeong YunWon
f96942bdc0 use super::* from tests submodules 2023-02-22 21:01:39 +09:00
Jeong YunWon
f628a3fd7e remove #[macro_use] 2023-02-22 20:41:27 +09:00
Jeong YunWon
f078f79c90 Flatten rustpython_parser interface 2023-02-22 20:32:31 +09:00
Jeong YunWon
af45d22fb7 make_tokenizer -> lex to integrate terms
we don't distinguish scanner or tokenizer from lexer
2023-02-22 20:28:15 +09:00
Jeong YunWon
c3a7d8c433 relocate feature-independent use 2023-02-22 20:28:03 +09:00
Jeong YunWon
6c7d81c325 Fix ModeParseError message 2023-02-22 20:28:03 +09:00
Jeong YunWon
a6f8c53924 Break down rustpython_parser::error module
because it doesn't share any common errors but specific error for each sub module
2023-02-22 20:28:01 +09:00
Jeong YunWon
6d5bbd913c Merge pull request #4542 from charliermarsh/charlie/expose
Always wrap token streams in `SoftKeywordTransformer`
2023-02-22 16:48:01 +09:00
Charlie Marsh
4e957d3484 Always wrap in SoftKeywordTransformer 2023-02-21 19:18:42 -05:00
Charlie Marsh
454ec3e74c Expose SoftKeywordTransformer on public API 2023-02-21 19:00:32 -05:00
Jim Fasarakis-Hilliard
ddf497623a Merge pull request #4519 from charliermarsh/charlie/match
Add support for match statements to parser
2023-02-21 19:43:28 +02:00
Jeong YunWon
746cb0493f Merge pull request #4508 from marvinmednick/pathconf_names
Pathconf names
2023-02-22 00:03:43 +09:00
Jeong YunWon
2e27587f15 Merge pull request #4530 from itsankitkp/handle-panic-strftime-new
Return arg in case of invalid param in strftime
2023-02-22 00:03:04 +09:00
Jeong YunWon
84c27f3694 Merge pull request #4535 from youknowone/fix-mul-and-union
Fix sequence mul types and add AsNumber for PyUnion
2023-02-22 00:02:16 +09:00
Jeong YunWon
42c0752370 less flaky syntax_async (#4536) 2023-02-21 23:17:11 +09:00
Dan Näsman
3615b1620e switch erprintln! to TODO comment (#4528) 2023-02-21 23:16:10 +09:00
Jeong YunWon
e42553867f Update extra_tests/snippets/stdlib_datetime.py 2023-02-21 23:14:53 +09:00
Jeong YunWon
955347e426 posix.pathconf_names only for linux 2023-02-21 19:45:04 +09:00
Marvin Mednick
468f1aa312 Implmentation for os.pathconf_names 2023-02-21 19:15:29 +09:00
Karatus
afb64c873a Add AsNumber for PyUnion 2023-02-21 19:02:35 +09:00
Karatus
8f17468f51 Fix mul/repeat index type to isize 2023-02-21 19:02:33 +09:00
Jeong YunWon
78b58122c1 Merge pull request #4533 from haxelion/bugfix/digest_block_size
Implemented the block_size attribute for PyHasher
2023-02-21 19:01:40 +09:00
Jeong YunWon
97ac351c5e Merge pull request #4534 from seungha-kim/graphlib
Copy graphlib module from cpython
2023-02-21 18:55:34 +09:00
Kim Seungha
c7f442425d Copy graphlib module from CPython 3.11 2023-02-21 18:11:19 +09:00
Charles Hubain
df0963446f Implemented the block_size attribute for PyHasher, fixing the 2 failing hmac tests 2023-02-21 16:52:15 +09:00
Jeong YunWon
ef873b4b60 Merge pull request #4531 from charliermarsh/charlie/exception-groups
Implement except* syntax
2023-02-21 13:20:18 +09:00
Jeong YunWon
6a267debf5 Merge pull request #4526 from haxelion/feature/hmac_3.11
Update hmac.py to CPython 3.11 and integrate test_hmac.py tests
2023-02-21 12:32:49 +09:00
Jeong YunWon
334bb30f9e Merge pull request #4532 from charliermarsh/charlie/star
Allow starred expressions in subscripts
2023-02-21 12:30:52 +09:00
Charlie Marsh
da9b2ee6b9 Implement except* syntax 2023-02-21 12:19:54 +09:00
Charlie Marsh
3ebf23e39b Update Python.asdl from CPython 3.11.1 2023-02-21 12:19:51 +09:00
Charles Hubain
b5f54f1624 Integrated hmac.py and test_hmac.py:
* fix typing issue on digest update function
* implemented copy for PyHasher object
* fixed two test_hashlib.py expected failure as a side-effect
2023-02-21 11:35:09 +09:00
Charles Hubain
33da5bf81f Updated Lib/hmac.py to cpython 3.11 version and included hmac test suite 2023-02-21 11:20:27 +09:00
Charlie Marsh
43e3fd9c2c Allow starred expressions in subscripts 2023-02-20 17:59:35 -05:00
Charlie Marsh
0201a5aadb Update compiler/parser/src/soft_keywords.rs
Co-authored-by: Jim Fasarakis-Hilliard <d.f.hilliard@gmail.com>
2023-02-20 15:03:39 -05:00
Jeong YunWon
fa38e9fa47 clean up soft-keyword transform 2023-02-20 15:03:39 -05:00
Charlie Marsh
382df8e714 Use muiltipeek 2023-02-20 15:03:39 -05:00
Charlie Marsh
c5db729191 Add support for match statements to parser 2023-02-20 15:03:39 -05:00
Jim Fasarakis-Hilliard
e0a294849a Merge pull request #4529 from haxelion/bugfix/fix_gethostbyname_ex
Ensure socket.gethostbyname_ex only returns IPv4 addresses
2023-02-20 18:11:26 +02:00
Ankit Kumar Pandey
fcfe8e1ac8 Return arg in case of invalid param in strftime
Chrono panics in case of unsupported formats, this patch handles such
cases and returns supplied format as a result.
2023-02-20 21:05:16 +05:30
Charles Hubain
4f1085beb3 Ensure socket.gethostbyname_ex only returns IPv4 addresses 2023-02-20 23:51:11 +09:00
Gyubong
e325a0003b Revert "Add SetNameArgs"
This reverts commit 2994e3f3444e4882512e3422cd63f6b913f19ec3.
2023-02-20 14:54:56 +09:00
Gyubong
e967e5ed11 Add SetNameArgs 2023-02-20 14:54:56 +09:00
Gyubong
d4aa062441 Fix set_name error message when arguments are invalid 2023-02-20 14:54:56 +09:00
Jeong YunWon
e5735cde67 Merge pull request #4517 from youknowone/cpython-3.11
trying to go with CPython 3.11
2023-02-20 13:50:10 +09:00
Jeong YunWon
75f3f3c8c4 Fix syntax_async.py for 3.11 2023-02-20 02:06:21 +09:00
Zhiyan Xiao
3b8d670c81 Add unicodedata.east_asian_width (#4523) 2023-02-20 01:38:02 +09:00
John Pham
8b7158f169 Make common::repr throw error instead of panic (#4520) 2023-02-19 22:09:54 +09:00
Jim Fasarakis-Hilliard
82c1a3a9ea Merge pull request #4521 from youknowone/update-ast
Update from CPython v3.10.9 to prepare match statement
2023-02-18 17:36:51 +02:00
CPython Developers
53d0f41ba5 Update from CPython v3.10.9 2023-02-18 22:52:47 +09:00
Jeong YunWon
a5091c56cb Merge pull request #4440 from MrrRaph/whats_left_utf8
Fixed UTF-8 encoding for `whats_left.py` and type mismatching in `stdlib/src/ssl.rs`
2023-02-18 18:45:23 +09:00
Jeong YunWon
daca1f38cf Fix cspell warnings 2023-02-18 14:02:18 +09:00
Jeong YunWon
5df8d29fe4 Add whats_left running to CI 2023-02-18 14:02:18 +09:00
Jeong YunWon
8c38a8381c os is frozen in 3.11 2023-02-18 07:09:42 +09:00
Jeong YunWon
d22446d1dd imp.py -> stdlib_imp.py 2023-02-18 07:09:42 +09:00
Jeong YunWon
c037e17220 Merge pull request #4518 from dannasman/unnecessary_tovec
Remove unnecessary to_vec() in bytes.rs
2023-02-18 06:56:10 +09:00
Dan Nasman
506e17ace2 remove unnecessary to_vec() 2023-02-17 22:37:40 +02:00
Jim Fasarakis-Hilliard
e88f09842d Merge pull request #4514 from youknowone/bytesinner-pub-super
narrow publicity of BytesInner::elements
2023-02-17 21:10:08 +02:00
Jeong YunWon
e79b1398b0 trying to go with CPython 3.11 2023-02-17 23:33:04 +09:00
MrrRaph
371b5e2a6e Fixed type mismatchs 2023-02-17 23:26:11 +09:00
MrrRaph
a4629db520 Added UTF-8 encoding when opening a file 2023-02-17 22:21:46 +09:00
Jeong YunWon
aaa364da54 narrow publicity of BytesInner::elements 2023-02-17 22:00:51 +09:00
Dan Näsman
a0c34dab54 Optimize bytes-like (l|r)strip (#4500) 2023-02-17 21:31:12 +09:00
Jeong YunWon
1d815464f6 Merge pull request #4513 from xiaozhiyan/optimize-pypydict-to_attributes-to-reuse-pyrefexact
Optimize Py<PyDict>::to_attributes() to reuse PyRefExact
2023-02-17 16:01:53 +09:00
Jeong YunWon
461f7491e8 Merge pull request #4512 from RustPython/upd-nix
Update nix & use its functions more
2023-02-17 16:00:36 +09:00
Noa
abe8cfdf1c Run cargo-update 2023-02-16 21:02:26 -06:00
Noa
d9e3f9ca68 Tidy up ssl a little 2023-02-16 21:02:26 -06:00
Noa
e08252cad7 Use nix for more things 2023-02-16 21:02:26 -06:00
Zhiyan Xiao
9206ad52b8 Optimize Py<PyDict>::to_attributes() to reuse PyRefExact 2023-02-17 07:23:19 +09:00
Jeong YunWon
57fd8c97b4 Add description for PyRefExact 2023-02-16 22:31:18 +09:00
Jeong YunWon
f9f665be62 PyObjectRef::downcast_exact returns PyRefExact 2023-02-16 22:20:16 +09:00
Jeong YunWon
3f1fb9d29d Merge pull request #4510 from DimitrisJim/unexpected_success
Fix unexpected success in test_future.
2023-02-16 21:39:15 +09:00
Dimitris Fasarakis Hilliard
8afe853b76 Fix unexpected success in test_future. 2023-02-16 10:37:35 +02:00
Jim Fasarakis-Hilliard
5ec62358b3 Merge pull request #4507 from howjmay/co_cellvars
Add co_cellvars to code object
2023-02-16 09:25:44 +02:00
Jim Fasarakis-Hilliard
dcda3e43b9 Merge pull request #4509 from minhrongcon2000/fix/add_co_freevars
Add co_freevars to code object
2023-02-16 09:24:23 +02:00
minh.pham2000
81fc6f16cf Fix code linting 2023-02-16 10:19:56 +07:00
minh.pham2000
07f4218bf4 Add co_freevars to code object 2023-02-16 10:10:15 +07:00
Yang Hau
529d223ee1 Add co_cellvars to code object
Closes #4501
2023-02-16 01:22:22 +08:00
Jim Fasarakis-Hilliard
3c4f04f3aa Merge pull request #4498 from youknowone/str-join
Fix str.join with str subclass
2023-02-15 19:07:11 +02:00
Jeong YunWon
1fd557d549 Merge pull request #4499 from jonathanslenders/fix-sqlite-aarch64
Fix aarch64 compatibility for sqlite.
2023-02-14 12:32:31 +09:00
Jonathan Slenders
9cac89347e Fix aarch64 compatibility for sqlite. 2023-02-13 20:53:39 +00:00
Jim Fasarakis-Hilliard
020372dde0 Merge pull request #4496 from youknowone/optimize-strip
optimize str.(l|r)strip
2023-02-13 18:31:23 +02:00
Jeong YunWon
bf49952275 Merge pull request #3769 from youknowone/wrap-index
wrap_index without abs
2023-02-14 00:55:26 +09:00
Jim Fasarakis-Hilliard
491bd0e74f Merge pull request #4495 from youknowone/fix-test-edit
Remove outdated TODOs from extra_tests/snippets
2023-02-13 15:25:14 +02:00
Jeong YunWon
5c43cab499 Fix str.join with str subclass 2023-02-13 22:05:53 +09:00
Jeong YunWon
317802a26e optimize str.(l|r)strip 2023-02-13 21:51:35 +09:00
Jeong YunWon
4bd090887e Remove completed TODO 2023-02-13 21:28:19 +09:00
Jeong YunWon
2fc0edf85b remove duplicated tests from tests/stdlib_math.py 2023-02-13 21:28:19 +09:00
Jeong YunWon
4192e5de2d extra_tests/snippets/{builtins => builtin_eval}.py 2023-02-13 21:28:19 +09:00
Jeong YunWon
443835096b Merge pull request #4490 from DimitrisJim/function_parser
Add tests, some comments, to function.rs.
2023-02-13 20:00:35 +09:00
Jeong YunWon
d3855a9158 Merge pull request #4491 from howjmay/typo
fix the typos
2023-02-13 20:00:09 +09:00
Jeong Yunwon
1d1ff8db7a wrap_index without abs 2023-02-13 18:19:40 +09:00
Yang Hau
937b0d3419 fix the typos 2023-02-13 16:25:24 +08:00
Jeong YunWon
96bfe406ee Merge pull request #4492 from DimitrisJim/doc_parser_uno
Document parser crate.
2023-02-13 17:25:14 +09:00
Jeong YunWon
271bfcbac7 Merge pull request #4489 from DimitrisJim/string_parser_refactor
Refactor: Join string and string_parser.
2023-02-13 17:18:54 +09:00
Dimitris Fasarakis Hilliard
5e40168829 Document parser crate. 2023-02-12 17:58:19 +02:00
Dimitris Fasarakis Hilliard
bc9e4abee5 Add tests, some comments, to function.rs. 2023-02-11 23:07:57 +02:00
Dimitris Fasarakis Hilliard
8d28a077fc Refactor: Join string and string_parser. 2023-02-11 18:05:06 +02:00
Jeong YunWon
61b48f1089 Merge pull request #4488 from charliermarsh/charlie/gen-exp-arg
Use entire range for generators-as-arguments
2023-02-11 05:45:25 +09:00
Jeong YunWon
d4207e2936 Add test_generator_expression_argument 2023-02-11 05:20:39 +09:00
Charlie Marsh
65a62b8d32 Use entire range for generators-as-arguments 2023-02-10 10:39:40 -05:00
Jim Fasarakis-Hilliard
cf54a780cf Merge pull request #4487 from DimitrisJim/mac_tests_no_ssl
Skip linking openSSL on mac runner for the rust tests.
2023-02-10 00:24:07 +02:00
Dimitris Fasarakis Hilliard
6d3cbf192e Skip linking ssl on mac runner. 2023-02-09 21:50:42 +02:00
Jim Fasarakis-Hilliard
d94d0ac720 Merge pull request #4486 from youknowone/simplify-example
Simplify examples/call_between_rust_and_python
2023-02-09 17:29:41 +02:00
Jeong YunWon
3942a08e0c Simplify examples/call_between_rust_and_python 2023-02-09 21:31:32 +09:00
Jeong YunWon
3092c17080 Merge pull request #4467 from youknowone/fix-ci
temporary skip rust tests for macOS to avoid CI failure
2023-02-09 17:09:37 +09:00
Jeong YunWon
b8a88719a7 skip run rust tests for macOS CI 2023-02-09 16:45:34 +09:00
Jeong YunWon
e22d91b8c4 bump up openssl and libffi 2023-02-09 15:52:45 +09:00
Jeong YunWon
5adc09150d update libffi 2023-02-09 15:51:28 +09:00
Jeong YunWon
f3a0127715 Try to fix mac build 2023-02-09 15:50:53 +09:00
Jeong YunWon
b8d95537e0 Merge pull request #4484 from DimitrisJim/newline_inline
Remove NewLineHandler, move handling inline.
2023-02-09 15:48:44 +09:00
Jeong YunWon
398c44e334 Merge pull request #4485 from RustPython/dependabot/cargo/openssl-src-111.25.01.1.1t
Bump openssl-src from 111.24.0+1.1.1s to 111.25.0+1.1.1t
2023-02-09 15:19:45 +09:00
dependabot[bot]
74284c3271 Bump openssl-src from 111.24.0+1.1.1s to 111.25.0+1.1.1t
Bumps [openssl-src](https://github.com/alexcrichton/openssl-src-rs) from 111.24.0+1.1.1s to 111.25.0+1.1.1t.
- [Release notes](https://github.com/alexcrichton/openssl-src-rs/releases)
- [Commits](https://github.com/alexcrichton/openssl-src-rs/commits)

---
updated-dependencies:
- dependency-name: openssl-src
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-02-08 22:31:06 +00:00
Dimitris Fasarakis Hilliard
1e9759679f Document lexer. 2023-02-07 21:43:57 +02:00
Dimitris Fasarakis Hilliard
e4096fb6f2 Move NewLineHandler inline, don't check each character twice. 2023-02-07 20:58:53 +02:00
Jeong YunWon
a3c372f602 Merge pull request #4482 from youknowone/workspace-deps
Use workspace dependencies to prevent version fragments
2023-02-06 16:30:13 +09:00
Jeong YunWon
42a22c280d use workspace dependencies 2023-02-06 15:30:38 +09:00
Jim Fasarakis-Hilliard
adc23253e4 Merge pull request #4480 from DimitrisJim/lexer_opt
Improve lexer performance by matching early on ascii identifiers.
2023-02-01 15:29:16 +02:00
Dimitris Fasarakis Hilliard
5025113da0 Hint that the unwrap should always succeed. 2023-02-01 12:17:33 +02:00
Nick Liu
5fb03b6891 use qualname in TypeErrors for functions (#4476) 2023-02-01 16:53:56 +09:00
Dimitris Fasarakis Hilliard
0fcac14fd7 Eat for comma. 2023-01-31 12:26:05 +02:00
Dimitris Fasarakis Hilliard
7edf3c86c3 Add initial capacities, use u32s for indents/spaces. 2023-01-31 12:26:05 +02:00
Dimitris Fasarakis Hilliard
ecac89521b Don't call is_emoji_presentation for each invocation of consume_normal 2023-01-31 12:26:05 +02:00
Dimitris Fasarakis Hilliard
51ba929fc6 Match on ascii start/continuation characters before calling functions. 2023-01-31 12:26:05 +02:00
Jim Fasarakis-Hilliard
b6e4471458 Merge pull request #4475 from discord9/pr_no_ctrl_c
feat: allow specifying an implementation flag so as to not set SIGINT handler
2023-01-31 12:24:01 +02:00
Discord9
763eaed2af feat: impl Detail option for not set SIG_INT 2023-01-31 16:32:40 +08:00
Jim Fasarakis-Hilliard
e1ab8a9b42 Fix clippy issues for rust 1.67 (#4478) 2023-01-31 09:46:19 +09:00
Discord9
daa9f116d0 feat: allow not set SIGINT handler 2023-01-28 16:07:04 +08:00
Jim Fasarakis-Hilliard
c0ab8e8256 Merge pull request #4464 from akx/bump-phf
Bump phf to 0.11 series
2023-01-28 00:13:53 +02:00
Aarni Koskela
6d927ef83d Bump phf to 0.11 series
string_cache is still using phf_shared 0.10.0 though
2023-01-25 19:58:43 +02:00
Jim Fasarakis-Hilliard
78e815c861 Merge pull request #4469 from moreal/array-register
Register `array.array` to `collections.abc.MutableSequence`
2023-01-25 18:53:29 +02:00
Lee Dogeon
8284a7cb89 Import from collections.abc instead of _collections_abc
Co-authored-by: Jim Fasarakis-Hilliard <d.f.hilliard@gmail.com>
2023-01-24 15:51:47 +09:00
Lee Dogeon
03f0e717a7 Correct typo
Co-authored-by: Jim Fasarakis-Hilliard <d.f.hilliard@gmail.com>
2023-01-24 15:37:28 +09:00
Moreal
778b8fdb2b Unmark resolved tests 2023-01-24 05:56:52 +09:00
Moreal
d0bf163641 Register array.array to collections.abc.MutableSequence 2023-01-24 05:56:34 +09:00
Jim Fasarakis-Hilliard
685a7e8ac5 Merge pull request #4466 from moreal/array-contains
Implement `array.array.__contains__`
2023-01-23 16:46:17 +02:00
Moreal
a866d881e5 Add extra_tests for array.__contains__ 2023-01-23 02:18:45 +09:00
Moreal
0527c4eae6 Implement array.array.__contains__ 2023-01-22 22:32:56 +09:00
Chia-Hsiang Cheng
bdd3beb257 Initialize ast when calling compile function (#4462) 2023-01-22 21:26:51 +09:00
Jeong YunWon
3b4b134328 Merge pull request #4465 from yt2b/fix_float_mod
Fix float mod
2023-01-22 20:53:17 +09:00
Jeong YunWon
4f38cb68e4 Merge pull request #4449 from harupy/fix-dict-spread-in-dict
Fix AST generated from a dict literal containing dict unpacking
2023-01-22 20:44:26 +09:00
yt2b
3d52769a47 Add tests 2023-01-22 14:44:35 +09:00
yt2b
8cb426120b Remove the decorator 2023-01-22 14:44:14 +09:00
yt2b
9e8e5a1e81 Fix mod_ 2023-01-22 14:43:58 +09:00
harupy
88e3c83138 Fix comment 2023-01-22 00:06:52 +09:00
Jeong YunWon
62aa942bf5 Merge pull request #4463 from charliermarsh/charlie/cformat
Implement lone-dot semantics for %-style format strings
2023-01-21 22:05:09 +09:00
Jeong YunWon
fc0b08042d refactor try_update_quantity_from_element 2023-01-21 11:57:22 +09:00
Jeong YunWon
225efcaf02 Merge pull request #4454 from moreal/sigaction
Correct exitcode when running through `subprocess.run`
2023-01-21 11:18:04 +09:00
Charlie Marsh
115357ddd1 Implement lone-dot semantics for %-style format strings 2023-01-20 13:23:26 -05:00
Moreal
a3507e7f58 Use unsafe only where necessary 2023-01-19 06:55:49 +09:00
Moreal
189a490e27 Unmark resolved tests 2023-01-18 19:42:15 +09:00
Moreal
52afc1ace5 Reuse flush_std 2023-01-18 14:33:37 +09:00
Moreal
f0c427b2d2 Correct exitcode when running through subprocess.run 2023-01-18 14:05:59 +09:00
Jeong YunWon
ff90fe52ee Merge pull request #4459 from andersk/elif-location
Fix end location for elif blocks
2023-01-18 13:36:44 +09:00
Jeong YunWon
02ee5bcb6a Merge pull request #4458 from fanninpm/test-ctypes
Add test_ctypes from CPython 3.11
2023-01-18 13:32:04 +09:00
Anders Kaseorg
16c1e214d6 Fix end location for elif blocks
Since we parse an `elif:` block as an `If` node, its location should
include its `orelse` node like it would for an `if:` block.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2023-01-17 22:39:44 -05:00
Padraic Fanning
019be59079 Add ctypes/test/*.py from CPython 3.11 2023-01-17 21:12:17 -05:00
Padraic Fanning
84caa50a08 Add test_ctypes from CPython 3.11 2023-01-17 20:36:07 -05:00
Jim Fasarakis-Hilliard
59b191c789 Merge pull request #4457 from harupy/remove-useless-string-from
Remove useless `String::from` in tests
2023-01-16 14:53:05 +02:00
harupy
6e7be1ec8d Remove useless String::from
Signed-off-by: harupy <hkawamura0130@gmail.com>
2023-01-16 21:27:57 +09:00
harupy
f4363c8a86 Revert "Simplify compile_dict"
This reverts commit b31b08a30e.
2023-01-16 01:27:54 +09:00
Jeong YunWon
211bdb0bb7 Merge pull request #4453 from yt2b/fix_error_message
Fix error message
2023-01-16 01:10:35 +09:00
Jeong YunWon
c3709e8b04 Merge pull request #4450 from yt2b/refactor_format_type
Refactor `FormatType`
2023-01-16 01:09:24 +09:00
Jeong YunWon
c79b1fa87a Merge pull request #4451 from notJoon/update-Lib-ctypes-3.11
add Lib/ctypes from cpython 3.11
2023-01-16 00:37:18 +09:00
harupy
964cc1b206 Rename test 2023-01-15 23:36:07 +09:00
harupy
b31b08a30e Simplify compile_dict 2023-01-15 23:20:00 +09:00
harupy
15228f4463 Simplify scan_expression 2023-01-15 22:08:19 +09:00
yt2b
e4102aae8d Remove the decorator 2023-01-15 21:42:25 +09:00
yt2b
ac4fc783ae Fix Error Message 2023-01-15 21:40:49 +09:00
not_joon
09ff71105a add Lib/ctypes from cpython 3.11 2023-01-15 17:08:30 +09:00
harupy
290736ae06 Improve test 2023-01-15 16:53:13 +09:00
harupy
ea9db0ebbe Add Option to Dict.keys field 2023-01-15 16:43:13 +09:00
harupy
82892f3e0e Remove commented-out code 2023-01-15 16:10:51 +09:00
yt2b
1640b6a7f3 Refactor FormatType 2023-01-15 15:19:26 +09:00
harupy
fdd3beaf58 Fix clippy errors 2023-01-15 15:04:15 +09:00
harupy
ae770996f6 Fix scan_expression and compile_dict 2023-01-15 14:54:58 +09:00
harupy
cdb1323515 Fix scan_expression and compile_dict 2023-01-15 13:22:05 +09:00
harupy
49b682edca Fix unparse 2023-01-15 13:11:55 +09:00
harupy
e1f41e83d1 Fix dict spreading in dict literal 2023-01-15 13:01:59 +09:00
Jim Fasarakis-Hilliard
acbc517b55 Merge pull request #4443 from bluetech/non-logical-newline-token-fixup
Fixup parse_tokens after "Add NonLogicalNewline token"
2023-01-14 12:49:04 +02:00
Jim Fasarakis-Hilliard
253b62f8b0 Merge pull request #4447 from burrbull/ci-update
update ci actions
2023-01-14 12:46:34 +02:00
Ran Benita
cb97a80350 Fixup parse_tokens after "Add NonLogicalNewline token"
I only updated `parse()` to ignore `NonLogicalNewline`, didn't notice
it's also needed in `parse_tokens()`.
2023-01-14 11:52:33 +02:00
Andrey Zgarbul
84897d4834 check caching 2023-01-14 12:39:17 +03:00
Andrey Zgarbul
a9696d8501 update ci actions 2023-01-14 12:03:43 +03:00
yt2b
8828fd17a0 Alternate float format doesn't work correctly. (#4446) 2023-01-14 16:48:03 +09:00
Noa
aa3d24ea2a Merge pull request #4346 from coolreader18/wordcode
Switch from 64-bit instruction enum to out-of-line arg values
2023-01-13 12:20:36 -06:00
Jeong YunWon
68d413e923 Merge pull request #4444 from messense/fix-format-spec-multi-byte
Fix panic when parsing a format spec with multi-byte character
2023-01-13 21:53:32 +09:00
messense
ee5a1c9002 Fix panic when parsing a format spec with multi-byte character 2023-01-13 14:40:20 +08:00
Noa
2861c874ed Switch from 64-bit instruction enum to out-of-line arg values 2023-01-12 23:05:17 -06:00
Jim Fasarakis-Hilliard
91a897dd74 Merge pull request #4442 from bluetech/non-logical-newline-token
lexer: Add `NonLogicalNewline` token
2023-01-12 19:18:23 +02:00
Ran Benita
e64faa781c Add NonLogicalNewline token
This token is completely ignored by the parser, but it's useful for
other users of the lexer, such as the Ruff linter. For example, the
token is helpful for a "trailing comma" lint.

The same idea exists in Python's `tokenize` module - there is a NEWLINE
token (logical newline), and a NL token (non-logical newline).

Fixes #4385.
2023-01-12 16:47:12 +02:00
Jeong YunWon
9bbe745a33 Merge pull request #4437 from not-my-profile/fix-parser-docs.rs
Fix docs.rs build for rustpython-parser
2023-01-12 19:02:21 +09:00
Martin Fischer
658d5a8b7e Fix docs.rs build for rustpython-parser
docs.rs failed to build the documentation of the recently released
rustpython-parser 0.2.0 because the build.rs script couldn't write the
parser.rs file because docs.rs builds the documentation in a sandbox
with a read-only filesystem.

This commit fixes this by writing the parser.rs file to the cargo output
directory instead, as recommended by the docs.rs documentation.[1]

Fixes #4436.

[1]: https://docs.rs/about/builds#read-only-directories
2023-01-11 09:58:10 +01:00
Kangzhi Shi
4e6b27144a introduce SearchIter 2022-08-15 21:35:08 +02:00
Kangzhi Shi
26a78dbaa4 update to 0.4.1 2022-08-15 21:33:51 +02:00
Kangzhi Shi
7e7b973481 clearup 2022-08-15 21:33:51 +02:00
Kangzhi Shi
646c8ac657 impl opinfo charset 2022-08-15 21:33:51 +02:00
Kangzhi Shi
236631141f impl opinfo literal 2022-08-15 21:33:51 +02:00
Kangzhi Shi
c4f10edc95 impl opinfo single literal 2022-08-15 21:33:51 +02:00
Kangzhi Shi
e42df1d859 update version to 0.4.0 2022-08-15 21:33:51 +02:00
Kangzhi Shi
18258000cd clearup 2022-08-15 21:33:51 +02:00
Kangzhi Shi
c494feb7f7 refactor split Marks 2022-08-15 21:33:51 +02:00
Kangzhi Shi
de8973d77a simplify lifetime 2022-08-15 21:33:51 +02:00
Kangzhi Shi
942063d4ef refactor benches 2022-08-15 21:33:51 +02:00
Kangzhi Shi
c15387e972 refactor tests 2022-08-15 21:33:51 +02:00
Kangzhi Shi
c31462d51b refactor split State with Request 2022-08-15 21:33:51 +02:00
Kangzhi Shi
8b1fcea7ec update version to 0.3.1 2022-08-15 21:33:51 +02:00
Kangzhi Shi
a48f5b07c5 impl op_info 2022-08-15 21:33:51 +02:00
Kangzhi Shi
ca20b5951d update version to 0.3.0 2022-08-07 08:02:49 +02:00
Kangzhi Shi
f3b30443aa fix lifetime 2022-08-07 08:02:49 +02:00
Kangzhi Shi
ccae898885 fix next_ctx bug 2022-08-07 08:02:49 +02:00
Kangzhi Shi
982d8f53f2 fix next_ctx 2022-08-07 08:02:49 +02:00
Kangzhi Shi
34bde45a2c pass compile 2022-08-07 08:02:49 +02:00
Kangzhi Shi
9058f28788 refactor trait StrDrive instead enum 2022-08-07 08:02:49 +02:00
Kangzhi Shi
bf57f289bf update version to 0.2.1 2022-07-29 10:54:20 +02:00
Kangzhi Shi
4007f82765 optimize max_until and min_until 2022-07-29 10:54:20 +02:00
Steve Shi
919e1d7933 Refactor and fix multiple max_until recusion (#10)
* wip refactor engine

* wip 2 refactor engine

* wip 3 refactor engine

* wip 3 refactor engine

* wip 4 refactor engine

* wip 5 refactor engine

* refactor seperate Stacks

* fix clippy

* fix pymatch and search restore _stacks

* fix toplevel

* fix marks panic

* fix double max_until repeat context

* clearup

* update version to 0.2.0
2022-07-26 20:38:03 +02:00
Kangzhi Shi
74ebdaf4e8 fix panic OpMinUntil return before restore repeat 2022-07-11 21:30:48 +02:00
Noah
c5871f4c2a Merge pull request #9 from qingshi163/master
Add benchmark and replace hashmap to btreemap
2021-05-01 13:34:35 -05:00
Kangzhi Shi
58981a41e9 optimize; replace hashmap with btreemap 2021-04-22 19:27:24 +02:00
Kangzhi Shi
86435b8a4b add benchmark 2021-04-22 17:15:03 +02:00
Steve Shi
980863366c Merge pull request #8 from qingshi163/master
optimize opcode with once execution and search with cached offset
2021-04-22 08:40:02 +02:00
Kangzhi Shi
7324feef89 optimize search cache the string offset 2021-04-21 11:13:58 +02:00
Kangzhi Shi
5bd6b672d0 optimize opcode that execute only once 2021-04-21 11:09:10 +02:00
Steve Shi
869d80a34b Merge pull request #7 from qingshi163/master
optimize count
2021-04-21 09:34:21 +02:00
Kangzhi Shi
a3c3573d67 optimize count 2021-04-21 09:30:54 +02:00
Steve Shi
b4f0b05455 Merge pull request #6 from qingshi163/zerowidth
fix zerowidth search
2021-04-20 15:17:15 +02:00
Kangzhi Shi
df8453d387 fix zerowidth search 2021-04-20 10:19:27 +02:00
Noah
73abbace85 Add explicit include for Cargo files 2021-04-16 10:54:49 -05:00
Noah
d2b48fdea2 Release 0.1.1
sre-engine@0.1.1

Generated by cargo-workspaces
2021-04-16 10:40:42 -05:00
Noah
caef94d851 Merge pull request #5 from qingshi163/master
fix test_string_boundaries
2021-04-16 07:46:35 -05:00
Kangzhi Shi
9728dd8699 fix test_string_boundaries 2021-04-16 09:35:11 +02:00
Noah
b497b2234d Add more info to Cargo.toml 2021-04-05 11:10:32 -05:00
Noah
211a66cf33 Merge pull request #4 from RustPython/codegen-inline
Have generate_tests.py generate Patterns inline in tests.rs
2021-04-05 10:59:54 -05:00
Noah
ca1346ee03 Have generate_tests.py generate Patterns inline in tests.rs 2021-04-01 22:05:45 -05:00
Noah
2a43d66e11 Fix clippy lint 2021-04-01 21:38:20 -05:00
Noah
65c477414b Merge pull request #3 from qingshi163/master
fix OpAssert positive lookbehind
2021-02-03 09:06:37 -06:00
Kangzhi Shi
39a648bce1 fix OpAssert positive lookbehind 2021-02-03 13:32:48 +02:00
Noah
11532c5be9 Merge pull request #2 from RustPython/gen-tests
Compile regex patterns for tests with a script
2021-02-02 07:32:14 -06:00
Noah
cc4441b50f Compile regex patterns for tests with a script 2021-02-01 14:27:11 -06:00
Noah
174c9326a4 Merge pull request #1 from qingshi163/master
add tests; fix OpAssert panic
2021-02-01 14:20:39 -06:00
Kangzhi Shi
2473c3e49f add tests; fix OpAssert panic 2021-02-01 20:25:45 +02:00
Noah
2592067f95 Add LICENSE 2021-01-27 19:30:31 -06:00
Noah
9c95994dab Modify to work outside of rustpython-vm 2021-01-27 19:30:28 -06:00
Noah
db5bd646b9 Initial commit to switch to new repo 2021-01-27 19:29:55 -06:00
Kangzhi Shi
4416158ece fix multiple bugs; pass tests 2021-01-22 16:40:33 +02:00
Kangzhi Shi
7f0dad7901 fix back_peek_char 2021-01-21 19:24:10 +02:00
Kangzhi Shi
6a792324b0 fix at_beginning 2021-01-21 16:14:23 +02:00
Kangzhi Shi
f2311b56fc fix op branch 2021-01-21 12:48:32 +02:00
Kangzhi Shi
84113cba2c fix zero width repeat 2021-01-20 16:33:30 +02:00
Kangzhi Shi
97000fc4e0 fix multiple bugs; skip crash tests 2021-01-20 08:29:58 +02:00
Kangzhi Shi
13a8b6cc4e add bytes support and refactor 2021-01-20 08:29:58 +02:00
Kangzhi Shi
76c95abbb5 impl Match.lastgroup 2021-01-20 08:29:58 +02:00
Kangzhi Shi
33ef823645 impl Match.groupdict 2021-01-20 08:29:57 +02:00
Kangzhi Shi
817eb66167 fix OpMinUntil 2021-01-20 08:29:57 +02:00
Kangzhi Shi
f05f6cb44d impl Pattern.sub 2021-01-20 08:29:57 +02:00
Kangzhi Shi
36433a9f4d fix Opcode::BIGCHARSET 2021-01-20 08:29:57 +02:00
Kangzhi Shi
db84f32981 fix Opcode::CHARSET 2021-01-20 08:29:57 +02:00
Kangzhi Shi
af1a53cb05 impl Match.groups() 2021-01-20 08:29:57 +02:00
Kangzhi Shi
8fba935bba OpMaxUntil zero-width protection 2021-01-20 08:29:57 +02:00
Kangzhi Shi
8c442f599b rework OpMaxUntil; restruct popping context; add tests; 2021-01-20 08:29:57 +02:00
Kangzhi Shi
04bb80f157 impl Match.group 2021-01-20 08:29:57 +02:00
Kangzhi Shi
f7287553e9 impl re.Match object 2021-01-20 08:29:57 +02:00
Kangzhi Shi
ae44580371 general case for count 2021-01-20 08:29:57 +02:00
Kangzhi Shi
fa2adaf2ff Impl OpRepeatONe 2021-01-20 08:29:57 +02:00
Kangzhi Shi
af7901dcb2 impl OpMinUntil 2021-01-20 08:29:57 +02:00
Kangzhi Shi
5a4459856c impl OpRepeat 2021-01-20 08:29:57 +02:00
Kangzhi Shi
93c2b8b555 impl OpBranch 2021-01-20 08:29:57 +02:00
Kangzhi Shi
0b2c8d1fa2 impl OpMaxUntil 2021-01-20 08:29:57 +02:00
Kangzhi Shi
312e5b8756 impl opcode groupref and assert_not 2021-01-20 08:29:57 +02:00
Kangzhi Shi
aa0f20b93e impl Pattern.fullmatch, Pattern.search 2021-01-20 08:29:57 +02:00
Kangzhi Shi
78485fd8df create _sre.Match 2021-01-20 08:29:57 +02:00
Kangzhi Shi
4e03fb361f upgrade sre_parse.py; impl marks count 2021-01-20 08:29:56 +02:00
Kangzhi Shi
82922bf0d7 upgrade re version; implement helper functions; 2021-01-20 08:29:56 +02:00
Kangzhi Shi
e1362ead3c WIP structure 2021-01-20 08:29:56 +02:00
Noah
c2ee9ca3e0 WIP - native _sre 2021-01-20 08:29:56 +02:00
1159 changed files with 150970 additions and 74667 deletions

View File

@@ -26,109 +26,263 @@
"words": [
// Rust
"ahash",
"bitflags",
"bidi",
"biguint",
"bindgen",
"cstring",
"bitflags",
"bstr",
"byteorder",
"chrono",
"peekable",
"consts",
"cstring",
"flate2",
"fract",
"hasher",
"idents",
"indexmap",
"insta",
"keccak",
"lalrpop",
"memmap",
"libc",
"libz",
"longlong",
"Manually",
"maplit",
"memmap",
"metas",
"modpow",
"nanos",
"objclass",
"peekable",
"powc",
"powf",
"prepended",
"punct",
"replacen",
"rsplitn",
"rustc",
"unistd",
"rustfmt",
"seekfrom",
"splitn",
"subsec",
"timsort",
"trai",
"ulonglong",
"unic",
"unistd",
"winapi",
"winsock",
// Python
"cformat",
"cpython",
"fspath",
"kwarg",
"kwargs",
"vararg",
"varargs",
"metaclass",
"metaclasses",
"fstring",
"fstrings",
"docstring",
"docstrings",
"fileencoding",
"linearization",
"linearize",
"PYTHONDEBUG",
"PYTHONINSPECT",
"PYTHONPATH",
"PYTHONHOME",
"PYTHONPATH",
"PYTHONVERBOSE",
"PYTHONOPTIMIZE",
"PYTHONWARNINGS",
"basicsize",
"itemsize",
"getattro",
"setattro",
"iternext",
"maxsplit",
"fdel",
"subclasscheck",
"qualname",
"eventmask",
"instanceof",
"abstractmethods",
"aiter",
"anext",
"rdiv",
"idiv",
"ndim",
"varnames",
"getweakrefs",
"getweakrefcount",
"stacklevel",
"MemoryView",
"warningregistry",
"arrayiterator",
"arraytype",
"asend",
"athrow",
"basicsize",
"cformat",
"classcell",
"closesocket",
"codepoint",
"codepoints",
"cpython",
"decompressor",
"defaultaction",
"unraisablehook",
"descr",
"xopts",
"dictcomp",
"dictitems",
"dictkeys",
"dictview",
"docstring",
"docstrings",
"dunder",
"eventmask",
"fdel",
"fget",
"fileencoding",
"fillchar",
"finallyhandler",
"frombytes",
"fromhex",
"fromunicode",
"fset",
"fspath",
"fstring",
"fstrings",
"genexpr",
"getattro",
"getformat",
"getnewargs",
"getweakrefcount",
"getweakrefs",
"hostnames",
"idiv",
"impls",
"infj",
"instancecheck",
"instanceof",
"isabstractmethod",
"itemiterator",
"itemsize",
"iternext",
"keyiterator",
"kwarg",
"kwargs",
"linearization",
"linearize",
"listcomp",
"mappingproxy",
"maxsplit",
"memoryview",
"memoryviewiterator",
"metaclass",
"metaclasses",
"metatype",
"mro",
"mros",
"nanj",
"ndigits",
"ndim",
"nonbytes",
"origname",
"posixsubprocess",
"pyexpat",
"PYTHONDEBUG",
"PYTHONHOME",
"PYTHONINSPECT",
"PYTHONOPTIMIZE",
"PYTHONPATH",
"PYTHONPATH",
"PYTHONVERBOSE",
"PYTHONWARNINGS",
"qualname",
"radd",
"rdiv",
"rdivmod",
"reconstructor",
"reversevalueiterator",
"rfloordiv",
"rlshift",
"rmod",
"rpow",
"rrshift",
"rsub",
"rtruediv",
"scproxy",
"setattro",
"setcomp",
"showwarnmsg",
"warnmsg",
"stacklevel",
"subclasscheck",
"subclasshook",
"unionable",
"unraisablehook",
"valueiterator",
"vararg",
"varargs",
"varnames",
"warningregistry",
"warnopts",
"weakproxy",
"mappingproxy",
"xopts",
// RustPython
"RustPython",
"baseclass",
"Bytecode",
"cfgs",
"codegen",
"dedentations",
"dedents",
"deduped",
"downcasted",
"dumpable",
"GetSet",
"internable",
"makeunicodedata",
"miri",
"notrace",
"pyarg",
"pyarg",
"pyargs",
"pygetset",
"pyobj",
"pystr",
"pyc",
"pyref",
"pyslot",
"PyFunction",
"PyMethod",
"PyClassMethod",
"PyStaticMethod",
"PyProperty",
"PyClass",
"pyimpl",
"pyarg",
"PyModule",
"PyAttr",
"PyResult",
"PyObject",
"pyc",
"PyClass",
"PyClassMethod",
"PyException",
"GetSet",
"zelf",
"wasi",
"Bytecode",
"PyFunction",
"pygetset",
"pyimpl",
"pymember",
"PyMethod",
"PyModule",
"pyname",
"pyobj",
"PyObject",
"pypayload",
"PyProperty",
"pyref",
"PyResult",
"pyslot",
"PyStaticMethod",
"pystr",
"pystruct",
"pystructseq",
"pytrace",
"reducelib",
"richcompare",
"makeunicodedata",
"unhashable",
"unraisable",
"RustPython",
"struc",
"tracebacks",
"typealiases",
"Unconstructible",
"posonlyargs",
"kwonlyargs",
"unhashable",
"uninit",
"miri"
"unraisable",
"wasi",
"zelf",
// cpython
"argtypes",
"asdl",
"asname",
"augassign",
"badsyntax",
"basetype",
"boolop",
"bxor",
"cellarg",
"cellvar",
"cellvars",
"cmpop",
"dictoffset",
"elts",
"excepthandler",
"finalbody",
"freevar",
"freevars",
"fromlist",
"heaptype",
"IMMUTABLETYPE",
"kwonlyarg",
"kwonlyargs",
"linearise",
"maxdepth",
"mult",
"nkwargs",
"orelse",
"patma",
"posonlyarg",
"posonlyargs",
"prec",
"stackdepth",
"unaryop",
"unparse",
"unparser",
"VARKEYWORDS",
"varkwarg",
"wbits",
"withitem",
"withs"
],
// flagWords - list of words to be always considered incorrect
"flagWords": [

2
.gitattributes vendored
View File

@@ -1,8 +1,6 @@
Lib/** linguist-vendored
Cargo.lock linguist-generated -merge
*.snap linguist-generated -merge
ast/src/ast_gen.rs linguist-generated -merge
vm/src/stdlib/ast/gen.rs linguist-generated -merge
compiler/parser/python.lalrpop text eol=LF
Lib/*.py text working-tree-encoding=UTF-8 eol=LF
**/*.rs text working-tree-encoding=UTF-8 eol=LF

16
.github/ISSUE_TEMPLATE/empty.md vendored Normal file
View File

@@ -0,0 +1,16 @@
---
name: Generic issue template
about: which is not covered by other templates
title: ''
labels:
assignees: ''
---
## Summary
<!-- Short description of the issue. -->
## Details
<!-- Whatever you want to share -->

View File

@@ -0,0 +1,16 @@
---
name: Feature request
about: Request a feature to use RustPython (as a Rust library)
title: ''
labels: C-enhancement
assignees: 'youknowone'
---
## Summary
<!-- Short description of the request. Please use incompatibility form to report missing features as Python interpreter -->
## Expected use case
<!-- By sharing detailed use case, we can understand the requirements better! If it will be used by open source projects, please also share the project URL. -->

24
.github/ISSUE_TEMPLATE/report-bug.md vendored Normal file
View File

@@ -0,0 +1,24 @@
---
name: Report bugs
about: Report a bug not related to CPython compatibility
title: ''
labels: C-bug
assignees: ''
---
## Summary
<!-- Short description of the bug -->
## Expected
<!-- What's the expected result? Using ``` ``` block is preferred for text. -->
## Actual
<!-- What's the actual result? Using ``` ``` block is preferred for text. -->
## Python Documentation
<!-- If applicable. -->

View File

@@ -2,7 +2,7 @@
name: Report incompatibility
about: Report an incompatibility between RustPython and CPython
title: ''
labels: feat
labels: C-compat
assignees: ''
---
@@ -11,6 +11,6 @@ assignees: ''
<!-- What Python feature is missing from RustPython? Give a short description of the feature and how you ran into its absence. -->
## Python Documentation
## Python Documentation or reference to CPython source code
<!-- Give a link to the feature in the CPython documentation (https://docs.python.org/3/) in order to assist in its implementation. -->

13
.github/dependabot.yml vendored Normal file
View File

@@ -0,0 +1,13 @@
# Keep GitHub Actions up to date with GitHub's Dependabot...
# https://docs.github.com/en/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot
# https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#package-ecosystem
version: 2
updates:
- package-ecosystem: github-actions
directory: /
groups:
github-actions:
patterns:
- "*" # Group all Actions updates into a single larger pull request
schedule:
interval: weekly

23
.github/workflows/ai-review.yml vendored Normal file
View File

@@ -0,0 +1,23 @@
name: PR Review
on:
pull_request:
types: [opened, synchronize]
permissions:
contents: read
pull-requests: write
jobs:
review:
runs-on: ubuntu-latest
steps:
- name: AI Code Review
uses: gitea-actions/ai-reviewer@v0.6
with:
access-token: ${{ secrets.ACCESS_TOKEN }}
full-context-model: "gpt-4o"
full-context-api-key: ${{ secrets.OPENAI_API_KEY }}
single-chunk-model: "claude-3-5-sonnet"
single-chunk-api-key: ${{ secrets.ANTHROPIC_API_KEY }}
exclude-files: "*.md,*.yaml"

View File

@@ -2,7 +2,8 @@ on:
push:
branches: [main, release]
pull_request:
types: [labeled, unlabeled, opened, synchronize, reopened]
types: [unlabeled, opened, synchronize, reopened]
merge_group:
name: CI
@@ -14,18 +15,24 @@ concurrency:
cancel-in-progress: true
env:
CARGO_ARGS: --no-default-features --features stdlib,zlib,importlib,encodings,ssl,jit
NON_WASM_PACKAGES: >-
-p rustpython-common
-p rustpython-compiler-core
-p rustpython-compiler
-p rustpython-codegen
-p rustpython-parser
-p rustpython-vm
-p rustpython-stdlib
-p rustpython-jit
-p rustpython-derive
-p rustpython
CARGO_ARGS: --no-default-features --features stdlib,zlib,importlib,encodings,sqlite,ssl
# Skip additional tests on Windows. They are checked on Linux and MacOS.
WINDOWS_SKIPS: >-
test_datetime
test_glob
test_importlib
test_io
test_os
test_pathlib
test_posixpath
test_venv
# configparser: https://github.com/RustPython/RustPython/issues/4995#issuecomment-1582397417
# socketserver: seems related to configparser crash.
MACOS_SKIPS: >-
test_configparser
test_socketserver
# PLATFORM_INDEPENDENT_TESTS are tests that do not depend on the underlying OS. They are currently
# only run on Linux to speed up the CI.
PLATFORM_INDEPENDENT_TESTS: >-
test_argparse
test_array
@@ -97,6 +104,8 @@ env:
test_unpack
test_weakref
test_yield_from
# Python version targeted by the CI.
PYTHON_VERSION: "3.12.3"
jobs:
rust_tests:
@@ -104,41 +113,47 @@ jobs:
env:
RUST_BACKTRACE: full
name: Run rust tests
needs: lalrpop
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
fail-fast: false
steps:
- uses: actions/checkout@v2
- name: Cache generated parser
uses: actions/cache@v2
with:
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- uses: Swatinem/rust-cache@v2
- name: Set up the Windows environment
shell: bash
run: |
choco install llvm openssl
echo "OPENSSL_DIR=C:\Program Files\OpenSSL-Win64" >>$GITHUB_ENV
cargo install --target-dir=target -v cargo-vcpkg
cargo vcpkg -v build
if: runner.os == 'Windows'
- name: Set up the Mac environment
run: brew install autoconf automake libtool
if: runner.os == 'macOS'
- uses: Swatinem/rust-cache@v1
- name: run clippy
run: cargo clippy ${{ env.CARGO_ARGS }} ${{ env.NON_WASM_PACKAGES }} -- -Dwarnings
run: cargo clippy ${{ env.CARGO_ARGS }} --workspace --exclude rustpython_wasm -- -Dwarnings
- name: run rust tests
run: cargo test --workspace --exclude rustpython_wasm --verbose --features threading ${{ env.CARGO_ARGS }} ${{ env.NON_WASM_PACKAGES }}
run: cargo test --workspace --exclude rustpython_wasm --verbose --features threading ${{ env.CARGO_ARGS }}
if: runner.os != 'macOS'
- name: run rust tests
run: cargo test --workspace --exclude rustpython_wasm --exclude rustpython-jit --verbose --features threading ${{ env.CARGO_ARGS }}
if: runner.os == 'macOS'
- name: check compilation without threading
run: cargo check ${{ env.CARGO_ARGS }}
- name: Test example projects
run:
cargo run --manifest-path example_projects/barebone/Cargo.toml
cargo run --manifest-path example_projects/frozen_stdlib/Cargo.toml
if: runner.os == 'Linux'
- name: prepare AppleSilicon build
uses: dtolnay/rust-toolchain@stable
with:
@@ -159,16 +174,9 @@ jobs:
exotic_targets:
if: ${{ !contains(github.event.pull_request.labels.*.name, 'skip:ci') }}
name: Ensure compilation on various targets
needs: lalrpop
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Cache generated parser
uses: actions/cache@v2
with:
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
target: i686-unknown-linux-gnu
@@ -185,6 +193,15 @@ jobs:
- name: Check compilation for android
run: cargo check --target aarch64-linux-android
- uses: dtolnay/rust-toolchain@stable
with:
target: aarch64-unknown-linux-gnu
- name: Install gcc-aarch64-linux-gnu
run: sudo apt install gcc-aarch64-linux-gnu
- name: Check compilation for aarch64 linux gnu
run: cargo check --target aarch64-unknown-linux-gnu
- uses: dtolnay/rust-toolchain@stable
with:
target: i686-unknown-linux-musl
@@ -199,13 +216,6 @@ jobs:
- name: Check compilation for freebsd
run: cargo check --target x86_64-unknown-freebsd
- uses: dtolnay/rust-toolchain@stable
with:
target: wasm32-unknown-unknown
- name: Check compilation for wasm32
run: cargo check --target wasm32-unknown-unknown --no-default-features
- uses: dtolnay/rust-toolchain@stable
with:
target: x86_64-unknown-freebsd
@@ -216,14 +226,13 @@ jobs:
- name: Prepare repository for redox compilation
run: bash scripts/redox/uncomment-cargo.sh
- name: Check compilation for Redox
if: false # FIXME: redoxer toolchain is from ~july 2021, edition2021 isn't stabilized
uses: coolreader18/redoxer-action@v1
with:
command: check
args: --ignore-rust-version
snippets_cpython:
if: ${{ !contains(github.event.pull_request.labels.*.name, 'skip:ci') }}
needs: lalrpop
env:
RUST_BACKTRACE: full
name: Run snippets and cpython tests
@@ -233,32 +242,30 @@ jobs:
os: [macos-latest, ubuntu-latest, windows-latest]
fail-fast: false
steps:
- uses: actions/checkout@v2
- name: Cache generated parser
uses: actions/cache@v2
with:
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: actions/setup-python@v2
- uses: Swatinem/rust-cache@v2
- uses: actions/setup-python@v5
with:
python-version: "3.10"
python-version: ${{ env.PYTHON_VERSION }}
- name: Set up the Windows environment
shell: bash
run: |
choco install llvm openssl
echo "OPENSSL_DIR=C:\Program Files\OpenSSL-Win64" >>$GITHUB_ENV
cargo install cargo-vcpkg
cargo vcpkg build
if: runner.os == 'Windows'
- name: Set up the Mac environment
run: brew install autoconf automake libtool
run: brew install autoconf automake libtool openssl@3
if: runner.os == 'macOS'
- uses: Swatinem/rust-cache@v1
- name: build rustpython
run: cargo build --release --verbose --features=threading ${{ env.CARGO_ARGS }}
- uses: actions/setup-python@v2
if: runner.os == 'macOS'
- name: build rustpython
run: cargo build --release --verbose --features=threading ${{ env.CARGO_ARGS }},jit
if: runner.os != 'macOS'
- uses: actions/setup-python@v5
with:
python-version: "3.10"
python-version: ${{ env.PYTHON_VERSION }}
- name: run snippets
run: python -m pip install -r requirements.txt && pytest -v
working-directory: ./extra_tests
@@ -266,113 +273,72 @@ jobs:
name: run cpython platform-independent tests
run:
target/release/rustpython -m test -j 1 -u all --slowest --fail-env-changed -v ${{ env.PLATFORM_INDEPENDENT_TESTS }}
- if: runner.os != 'Windows'
name: run cpython platform-dependent tests
- if: runner.os == 'Linux'
name: run cpython platform-dependent tests (Linux)
run: target/release/rustpython -m test -j 1 -u all --slowest --fail-env-changed -v -x ${{ env.PLATFORM_INDEPENDENT_TESTS }}
- if: runner.os == 'macOS'
name: run cpython platform-dependent tests (MacOS)
run: target/release/rustpython -m test -j 1 --slowest --fail-env-changed -v -x ${{ env.PLATFORM_INDEPENDENT_TESTS }} ${{ env.MACOS_SKIPS }}
- if: runner.os == 'Windows'
name: run cpython platform-dependent tests (windows partial - fixme)
run:
target/release/rustpython -m test -j 1 -u all --slowest --fail-env-changed -v -x ${{ env.PLATFORM_INDEPENDENT_TESTS }}
test_glob
test_importlib
test_io
test_iter
test_os
test_pathlib
test_posixpath
test_shutil
test_venv
target/release/rustpython -m test -j 1 --slowest --fail-env-changed -v -x ${{ env.PLATFORM_INDEPENDENT_TESTS }} ${{ env.WINDOWS_SKIPS }}
- if: runner.os != 'Windows'
name: check that --install-pip succeeds
run: |
mkdir site-packages
target/release/rustpython --install-pip ensurepip --user
lalrpop:
if: ${{ !contains(github.event.pull_request.labels.*.name, 'skip:ci') }}
name: Generate parser with lalrpop
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Cache generated parser
uses: actions/cache@v2
with:
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- name: Check if cached generated parser exists
id: generated_parser
uses: andstor/file-existence-action@v1
with:
files: "compiler/parser/python.rs"
- if: runner.os == 'Windows'
name: Force python.lalrpop to be lf # actions@checkout ignore .gitattributes
target/release/rustpython -m pip install six
- if: runner.os != 'Windows'
name: Check that ensurepip succeeds.
run: |
set file compiler/parser/python.lalrpop; ((Get-Content $file) -join "`n") + "`n" | Set-Content -NoNewline $file
- name: Install lalrpop
if: steps.generated_parser.outputs.files_exists == 'false'
uses: baptiste0928/cargo-install@v1
with:
crate: lalrpop
version: "0.19.8"
- name: Run lalrpop
if: steps.generated_parser.outputs.files_exists == 'false'
run: lalrpop compiler/parser/python.lalrpop
target/release/rustpython -m ensurepip
target/release/rustpython -c "import pip"
- if: runner.os != 'Windows'
name: Check if pip inside venv is functional
run: |
target/release/rustpython -m venv testvenv
testvenv/bin/rustpython -m pip install wheel
- name: Check whats_left is not broken
run: python -I whats_left.py
lint:
name: Check Rust code with rustfmt and clippy
needs: lalrpop
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Cache generated parser
uses: actions/cache@v2
with:
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- name: run rustfmt
run: cargo fmt --all -- --check
run: cargo fmt --check
- name: run clippy on wasm
run: cargo clippy --manifest-path=wasm/lib/Cargo.toml -- -Dwarnings
- uses: actions/setup-python@v2
- uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: install flake8
run: python -m pip install flake8
- name: run lint
run: flake8 . --count --exclude=./.*,./Lib,./vm/Lib,./benches/ --select=E9,F63,F7,F82 --show-source --statistics
python-version: ${{ env.PYTHON_VERSION }}
- name: install ruff
run: python -m pip install ruff==0.0.291 # astral-sh/ruff#7778
- name: run python lint
run: ruff extra_tests wasm examples --exclude='./.*',./Lib,./vm/Lib,./benches/ --select=E9,F63,F7,F82 --show-source
- name: install prettier
run: yarn global add prettier && echo "$(yarn global bin)" >>$GITHUB_PATH
- name: check wasm code with prettier
# prettier doesn't handle ignore files very well: https://github.com/prettier/prettier/issues/8506
run: cd wasm && git ls-files -z | xargs -0 prettier --check -u
- name: Check update_asdl.sh consistency
run: bash scripts/update_asdl.sh && git diff --exit-code
- name: Check whats_left is not broken
run: python -I whats_left.py
miri:
if: ${{ !contains(github.event.pull_request.labels.*.name, 'skip:ci') }}
name: Run tests under miri
needs: lalrpop
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Cache generated parser
uses: actions/cache@v2
with:
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly
components: miri
- uses: Swatinem/rust-cache@v1
- uses: Swatinem/rust-cache@v2
- name: Run tests under miri
# miri-ignore-leaks because the type-object circular reference means that there will always be
# a memory leak, at least until we have proper cyclic gc
@@ -381,46 +347,53 @@ jobs:
wasm:
if: ${{ !contains(github.event.pull_request.labels.*.name, 'skip:ci') }}
name: Check the WASM package and demo
needs: lalrpop
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Cache generated parser
uses: actions/cache@v2
with:
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v1
- uses: Swatinem/rust-cache@v2
- name: install wasm-pack
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- name: install geckodriver
run: |
wget https://github.com/mozilla/geckodriver/releases/download/v0.30.0/geckodriver-v0.30.0-linux64.tar.gz
wget https://github.com/mozilla/geckodriver/releases/download/v0.34.0/geckodriver-v0.34.0-linux64.tar.gz
mkdir geckodriver
tar -xzf geckodriver-v0.30.0-linux64.tar.gz -C geckodriver
- uses: actions/setup-python@v2
tar -xzf geckodriver-v0.34.0-linux64.tar.gz -C geckodriver
- uses: actions/setup-python@v5
with:
python-version: "3.10"
python-version: ${{ env.PYTHON_VERSION }}
- run: python -m pip install -r requirements.txt
working-directory: ./wasm/tests
- uses: actions/setup-node@v1
- uses: actions/setup-node@v4
- name: run test
run: |
export PATH=$PATH:`pwd`/../../geckodriver
npm install
npm run test
env:
NODE_OPTIONS: "--openssl-legacy-provider"
working-directory: ./wasm/demo
- uses: mwilliamson/setup-wabt-action@v3
with: { wabt-version: "1.0.30" }
- name: check wasm32-unknown without js
run: |
cargo build --release --manifest-path wasm/wasm-unknown-test/Cargo.toml --target wasm32-unknown-unknown --verbose
if wasm-objdump -xj Import target/wasm32-unknown-unknown/release/wasm_unknown_test.wasm; then
echo "ERROR: wasm32-unknown module expects imports from the host environment" >2
fi
- name: build notebook demo
if: github.ref == 'refs/heads/release'
run: |
npm install
npm run dist
mv dist ../demo/dist/notebook
env:
NODE_OPTIONS: "--openssl-legacy-provider"
working-directory: ./wasm/notebook
- name: Deploy demo to Github Pages
if: success() && github.ref == 'refs/heads/release'
uses: peaceiris/actions-gh-pages@v2
uses: peaceiris/actions-gh-pages@v4
env:
ACTIONS_DEPLOY_KEY: ${{ secrets.ACTIONS_DEMO_DEPLOY_KEY }}
PUBLISH_DIR: ./wasm/demo/dist
@@ -430,24 +403,21 @@ jobs:
wasm-wasi:
if: ${{ !contains(github.event.pull_request.labels.*.name, 'skip:ci') }}
name: Run snippets and cpython tests on wasm-wasi
needs: lalrpop
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Cache generated parser
uses: actions/cache@v2
with:
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
target: wasm32-wasi
- uses: Swatinem/rust-cache@v1
target: wasm32-wasip1
- uses: Swatinem/rust-cache@v2
- name: Setup Wasmer
uses: wasmerio/setup-wasmer@v1
uses: wasmerio/setup-wasmer@v3
- name: Install clang
run: sudo apt-get update && sudo apt-get install clang -y
- name: build rustpython
run: cargo build --release --target wasm32-wasi --features freeze-stdlib,stdlib --verbose
run: cargo build --release --target wasm32-wasip1 --features freeze-stdlib,stdlib --verbose
- name: run snippets
run: wasmer run --dir . target/wasm32-wasi/release/rustpython.wasm -- extra_tests/snippets/stdlib_random.py
run: wasmer run --dir `pwd` target/wasm32-wasip1/release/rustpython.wasm -- `pwd`/extra_tests/snippets/stdlib_random.py
- name: run cpython unittest
run: wasmer run --dir `pwd` target/wasm32-wasip1/release/rustpython.wasm -- `pwd`/Lib/test/test_int.py

View File

@@ -6,71 +6,43 @@ on:
name: Periodic checks/tasks
env:
CARGO_ARGS: --features ssl,jit
CARGO_ARGS: --no-default-features --features stdlib,zlib,importlib,encodings,ssl,jit
PYTHON_VERSION: "3.12.0"
jobs:
# codecov collects code coverage data from the rust tests, python snippets and python test suite.
# This is done using cargo-llvm-cov, which is a wrapper around llvm-cov.
codecov:
name: Collect code coverage data
needs: lalrpop
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Cache generated parser
uses: actions/cache@v2
with:
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: taiki-e/install-action@cargo-llvm-cov
- uses: actions/setup-python@v5
with:
components: llvm-tools-preview
python-version: ${{ env.PYTHON_VERSION }}
- run: sudo apt-get update && sudo apt-get -y install lcov
- run: cargo build --release --verbose ${{ env.CARGO_ARGS }}
env:
RUSTC_WRAPPER: './scripts/codecoverage-rustc-wrapper.sh'
- uses: actions/setup-python@v2
with:
python-version: "3.10"
- run: python -m pip install pytest
working-directory: ./extra_tests
- name: run snippets
run: LLVM_PROFILE_FILE="$PWD/snippet-%p.profraw" pytest -v
working-directory: ./extra_tests
- name: Run cargo-llvm-cov with Rust tests.
run: cargo llvm-cov --no-report --workspace --exclude rustpython_wasm --verbose --no-default-features --features stdlib,zlib,importlib,encodings,ssl,jit
- name: Run cargo-llvm-cov with Python snippets.
run: python scripts/cargo-llvm-cov.py
continue-on-error: true
- name: run cpython tests
run: |
alltests=($(target/release/rustpython -c 'from test.libregrtest.runtest import findtests; print(*findtests())'))
i=0
# chunk into chunks of 10 tests each. idk at this point
while subtests=("${alltests[@]:$i:10}"); [[ ${#subtests[@]} -ne 0 ]]; do
LLVM_PROFILE_FILE="$PWD/regrtest-%p.profraw" target/release/rustpython -m test -v "${subtests[@]}" || true
((i+=10))
done
- name: Run cargo-llvm-cov with Python test suite.
run: cargo llvm-cov --no-report run -- -m test -u all --slowest --fail-env-changed
continue-on-error: true
- name: prepare code coverage data
run: |
rusttool() {
local tool=$1; shift; "$(rustc --print target-libdir)/../bin/llvm-$tool" "$@"
}
rusttool profdata merge extra_tests/snippet-*.profraw regrtest-*.profraw --output codecov.profdata
rusttool cov export --instr-profile codecov.profdata target/release/rustpython --format lcov > codecov_tmp.lcov
lcov -e codecov_tmp.lcov "$PWD"/'*' -o codecov_tmp2.lcov
lcov -r codecov_tmp2.lcov "$PWD"/target/'*' -o codecov.lcov # remove LALRPOP-generated parser
- name: upload to Codecov
uses: codecov/codecov-action@v3
- name: Prepare code coverage data
run: cargo llvm-cov report --lcov --output-path='codecov.lcov'
- name: Upload to Codecov
uses: codecov/codecov-action@v5
with:
file: ./codecov.lcov
testdata:
name: Collect regression test data
needs: lalrpop
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Cache generated parser
uses: actions/cache@v2
with:
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: build rustpython
run: cargo build --release --verbose
@@ -97,16 +69,13 @@ jobs:
whatsleft:
name: Collect what is left data
needs: lalrpop
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Cache generated parser
uses: actions/cache@v2
with:
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: build rustpython
run: cargo build --release --verbose
- name: Collect what is left data
@@ -135,17 +104,11 @@ jobs:
benchmark:
name: Collect benchmark data
needs: lalrpop
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Cache generated parser
uses: actions/cache@v2
with:
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: actions/setup-python@v2
- uses: actions/setup-python@v5
with:
python-version: 3.9
- run: cargo install cargo-criterion
@@ -183,35 +146,3 @@ jobs:
if git -c user.name="Github Actions" -c user.email="actions@github.com" commit -m "Update benchmark results"; then
git push
fi
lalrpop:
name: Generate parser with lalrpop
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Cache generated parser
uses: actions/cache@v2
with:
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- name: Check if cached generated parser exists
id: generated_parser
uses: andstor/file-existence-action@v1
with:
files: "compiler/parser/python.rs"
- if: runner.os == 'Windows'
name: Force python.lalrpop to be lf # actions@checkout ignore .gitattributes
run: |
set file compiler/parser/python.lalrpop; ((Get-Content $file) -join "`n") + "`n" | Set-Content -NoNewline $file
- name: Install lalrpop
if: steps.generated_parser.outputs.files_exists == 'false'
uses: baptiste0928/cargo-install@v1
with:
crate: lalrpop
version: "0.19.8"
- name: Run lalrpop
if: steps.generated_parser.outputs.files_exists == 'false'
run: lalrpop compiler/parser/python.lalrpop

145
.github/workflows/release.yml vendored Normal file
View File

@@ -0,0 +1,145 @@
name: Release
on:
schedule:
# 9 AM UTC on every Monday
- cron: "0 9 * * Mon"
workflow_dispatch:
inputs:
pre-release:
type: boolean
description: Mark "Pre-Release"
required: false
default: true
permissions:
contents: write
env:
CARGO_ARGS: --no-default-features --features stdlib,zlib,importlib,encodings,sqlite,ssl
jobs:
build:
runs-on: ${{ matrix.platform.runner }}
strategy:
matrix:
platform:
- runner: ubuntu-latest
target: x86_64-unknown-linux-gnu
# - runner: ubuntu-latest
# target: i686-unknown-linux-gnu
# - runner: ubuntu-latest
# target: aarch64-unknown-linux-gnu
# - runner: ubuntu-latest
# target: armv7-unknown-linux-gnueabi
# - runner: ubuntu-latest
# target: s390x-unknown-linux-gnu
# - runner: ubuntu-latest
# target: powerpc64le-unknown-linux-gnu
- runner: macos-latest
target: aarch64-apple-darwin
# - runner: macos-latest
# target: x86_64-apple-darwin
- runner: windows-latest
target: x86_64-pc-windows-msvc
# - runner: windows-latest
# target: i686-pc-windows-msvc
# - runner: windows-latest
# target: aarch64-pc-windows-msvc
fail-fast: false
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: Set up Environment
shell: bash
run: rustup target add ${{ matrix.platform.target }}
- name: Set up Windows Environment
shell: bash
run: |
cargo install --target-dir=target -v cargo-vcpkg
cargo vcpkg -v build
if: runner.os == 'Windows'
- name: Set up MacOS Environment
run: brew install autoconf automake libtool
if: runner.os == 'macOS'
- name: Build RustPython
run: cargo build --release --target=${{ matrix.platform.target }} --verbose --features=threading ${{ env.CARGO_ARGS }}
if: runner.os == 'macOS'
- name: Build RustPython
run: cargo build --release --target=${{ matrix.platform.target }} --verbose --features=threading ${{ env.CARGO_ARGS }},jit
if: runner.os != 'macOS'
- name: Rename Binary
run: cp target/${{ matrix.platform.target }}/release/rustpython target/rustpython-release-${{ runner.os }}-${{ matrix.platform.target }}
if: runner.os != 'Windows'
- name: Rename Binary
run: cp target/${{ matrix.platform.target }}/release/rustpython.exe target/rustpython-release-${{ runner.os }}-${{ matrix.platform.target }}.exe
if: runner.os == 'Windows'
- name: Upload Binary Artifacts
uses: actions/upload-artifact@v4
with:
name: rustpython-release-${{ runner.os }}-${{ matrix.platform.target }}
path: target/rustpython-release-${{ runner.os }}-${{ matrix.platform.target }}*
build-wasm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: Set up Environment
shell: bash
run: rustup target add wasm32-wasip1
- name: Build RustPython
run: cargo build --target wasm32-wasip1 --no-default-features --features freeze-stdlib,stdlib --release
- name: Rename Binary
run: cp target/wasm32-wasip1/release/rustpython.wasm target/rustpython-release-wasm32-wasip1.wasm
- name: Upload Binary Artifacts
uses: actions/upload-artifact@v4
with:
name: rustpython-release-wasm32-wasip1
path: target/rustpython-release-wasm32-wasip1.wasm
release:
runs-on: ubuntu-latest
needs: [build, build-wasm]
steps:
- name: Download Binary Artifacts
uses: actions/download-artifact@v4
with:
path: bin
pattern: rustpython-release-*
merge-multiple: true
- name: List Binaries
run: |
ls -lah bin/
file bin/*
- name: Create Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
tag: ${{ github.ref_name }}
run: ${{ github.run_number }}
run: |
if [[ "${{ github.event.inputs.pre-release }}" == "false" ]]; then
RELEASE_TYPE_NAME=Release
PRERELEASE_ARG=
else
RELEASE_TYPE_NAME=Pre-Release
PRERELEASE_ARG=--prerelease
fi
today=$(date '+%Y-%m-%d')
gh release create "$today-$tag-$run" \
--repo="$GITHUB_REPOSITORY" \
--title="RustPython $RELEASE_TYPE_NAME $today-$tag #$run" \
--target="$tag" \
--generate-notes \
$PRERELEASE_ARG \
bin/rustpython-release-*

4
.gitignore vendored
View File

@@ -9,6 +9,8 @@ __pycache__
.vscode
wasm-pack.log
.idea/
.envrc
.python-version
flame-graph.html
flame.txt
@@ -19,5 +21,3 @@ flamescope.json
extra_tests/snippets/resources
extra_tests/not_impl.py
compiler/parser/python.rs

21
.vscode/launch.json vendored
View File

@@ -8,15 +8,24 @@
"type": "lldb",
"request": "launch",
"name": "Debug executable 'rustpython'",
"cargo": {
"args": [
"build",
"--package=rustpython"
],
},
"preLaunchTask": "Build RustPython Debug",
"program": "target/debug/rustpython",
"args": [],
"env": {
"RUST_BACKTRACE": "1"
},
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'rustpython' without SSL",
"preLaunchTask": "Build RustPython Debug without SSL",
"program": "target/debug/rustpython",
"args": [],
"env": {
"RUST_BACKTRACE": "1"
},
"cwd": "${workspaceFolder}"
},
{

20
.vscode/tasks.json vendored
View File

@@ -2,7 +2,7 @@
"version": "2.0.0",
"tasks": [
{
"label": "Build RustPython Debug",
"label": "Build RustPython Debug without SSL",
"type": "shell",
"command": "cargo",
"args": [
@@ -15,6 +15,22 @@
"kind": "build",
"isDefault": true,
},
}
},
{
"label": "Build RustPython Debug",
"type": "shell",
"command": "cargo",
"args": [
"build",
"--features=ssl"
],
"problemMatcher": [
"$rustc",
],
"group": {
"kind": "build",
"isDefault": true,
},
},
],
}

1851
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,62 +1,54 @@
# REDOX START
# cargo-features = ["edition2021"]
# REDOX END
[package]
name = "rustpython"
version = "0.2.0"
authors = ["RustPython Team"]
edition = "2021"
description = "A python interpreter written in rust."
repository = "https://github.com/RustPython/RustPython"
license = "MIT"
include = ["LICENSE", "Cargo.toml", "src/**/*.rs"]
[workspace]
resolver = "2"
members = [
"compiler", "compiler/ast", "compiler/core", "compiler/codegen", "compiler/parser",
".", "common", "derive", "jit", "vm", "pylib", "stdlib", "wasm/lib", "derive-impl",
]
version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
repository.workspace = true
license.workspace = true
[features]
default = ["threading", "stdlib", "zlib", "importlib", "encodings", "rustpython-parser/lalrpop"]
default = ["threading", "stdlib", "zlib", "importlib"]
importlib = ["rustpython-vm/importlib"]
encodings = ["rustpython-vm/encodings"]
stdlib = ["rustpython-stdlib", "rustpython-pylib"]
stdlib = ["rustpython-stdlib", "rustpython-pylib", "encodings"]
flame-it = ["rustpython-vm/flame-it", "flame", "flamescope"]
freeze-stdlib = ["rustpython-vm/freeze-stdlib", "rustpython-pylib?/freeze-stdlib"]
freeze-stdlib = ["stdlib", "rustpython-vm/freeze-stdlib", "rustpython-pylib?/freeze-stdlib"]
jit = ["rustpython-vm/jit"]
threading = ["rustpython-vm/threading", "rustpython-stdlib/threading"]
zlib = ["stdlib", "rustpython-stdlib/zlib"]
bz2 = ["stdlib", "rustpython-stdlib/bz2"]
sqlite = ["rustpython-stdlib/sqlite"]
ssl = ["rustpython-stdlib/ssl"]
ssl-vendor = ["rustpython-stdlib/ssl-vendor"]
ssl-vendor = ["ssl", "rustpython-stdlib/ssl-vendor"]
[dependencies]
rustpython-compiler = { path = "compiler", version = "0.2.0" }
rustpython-parser = { path = "compiler/parser", version = "0.2.0" }
rustpython-pylib = { path = "pylib", optional = true, default-features = false }
rustpython-stdlib = { path = "stdlib", optional = true, default-features = false }
rustpython-vm = { path = "vm", version = "0.2.0", default-features = false, features = ["compiler"] }
rustpython-compiler = { workspace = true }
rustpython-pylib = { workspace = true, optional = true }
rustpython-stdlib = { workspace = true, optional = true, features = ["compiler"] }
rustpython-vm = { workspace = true, features = ["compiler"] }
rustpython-parser = { workspace = true }
cfg-if = { workspace = true }
log = { workspace = true }
flame = { workspace = true, optional = true }
cfg-if = "1.0.0"
clap = "2.34"
dirs = { package = "dirs-next", version = "2.0.0" }
env_logger = { version = "0.9.0", default-features = false, features = ["atty", "termcolor"] }
flame = { version = "0.2.2", optional = true }
flamescope = { version = "0.1.2", optional = true }
libc = "0.2.133"
log = "0.4.16"
num-traits = "0.2.14"
atty = "0.2.14"
[target.'cfg(windows)'.dependencies]
libc = { workspace = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
rustyline = "10.0.0"
rustyline = { workspace = true }
[dev-dependencies]
cpython = "0.7.0"
criterion = "0.3.5"
python3-sys = "0.7.0"
criterion = { version = "0.3.5", features = ["html_reports"] }
pyo3 = { version = "0.22", features = ["auto-initialize"] }
[[bench]]
name = "execution"
@@ -87,5 +79,119 @@ opt-level = 3
lto = "thin"
[patch.crates-io]
# REDOX START, Uncommment when you want to compile/check with redoxer
# REDOX START, Uncomment when you want to compile/check with redoxer
# REDOX END
# Used only on Windows to build the vcpkg dependencies
[package.metadata.vcpkg]
git = "https://github.com/microsoft/vcpkg"
# The revision of the vcpkg repository to use
# https://github.com/microsoft/vcpkg/tags
rev = "2024.02.14"
[package.metadata.vcpkg.target]
x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md", dev-dependencies = ["openssl" ] }
[workspace]
resolver = "2"
members = [
"compiler", "compiler/core", "compiler/codegen",
".", "common", "derive", "jit", "vm", "vm/sre_engine", "pylib", "stdlib", "derive-impl",
"wasm/lib",
]
[workspace.package]
version = "0.4.0"
authors = ["RustPython Team"]
edition = "2021"
rust-version = "1.83.0"
repository = "https://github.com/RustPython/RustPython"
license = "MIT"
[workspace.dependencies]
rustpython-compiler-core = { path = "compiler/core", version = "0.4.0" }
rustpython-compiler = { path = "compiler", version = "0.4.0" }
rustpython-codegen = { path = "compiler/codegen", version = "0.4.0" }
rustpython-common = { path = "common", version = "0.4.0" }
rustpython-derive = { path = "derive", version = "0.4.0" }
rustpython-derive-impl = { path = "derive-impl", version = "0.4.0" }
rustpython-jit = { path = "jit", version = "0.4.0" }
rustpython-vm = { path = "vm", default-features = false, version = "0.4.0" }
rustpython-pylib = { path = "pylib", version = "0.4.0" }
rustpython-stdlib = { path = "stdlib", default-features = false, version = "0.4.0" }
rustpython-sre_engine = { path = "vm/sre_engine", version = "0.4.0" }
rustpython-doc = { git = "https://github.com/RustPython/__doc__", tag = "0.3.0", version = "0.3.0" }
# rustpython-literal = { version = "0.4.0" }
# rustpython-parser-core = { version = "0.4.0" }
# rustpython-parser = { version = "0.4.0" }
# rustpython-ast = { version = "0.4.0" }
# rustpython-format= { version = "0.4.0" }
rustpython-literal = { git = "https://github.com/RustPython/Parser.git", version = "0.4.0", rev = "4588ea5c3e6327009640e7c9c89eb6fa9220358e" }
rustpython-parser-core = { git = "https://github.com/RustPython/Parser.git", version = "0.4.0", rev = "4588ea5c3e6327009640e7c9c89eb6fa9220358e" }
rustpython-parser = { git = "https://github.com/RustPython/Parser.git", version = "0.4.0", rev = "4588ea5c3e6327009640e7c9c89eb6fa9220358e" }
rustpython-ast = { git = "https://github.com/RustPython/Parser.git", version = "0.4.0", rev = "4588ea5c3e6327009640e7c9c89eb6fa9220358e" }
rustpython-format = { git = "https://github.com/RustPython/Parser.git", version = "0.4.0", rev = "4588ea5c3e6327009640e7c9c89eb6fa9220358e" }
# rustpython-literal = { path = "../RustPython-parser/literal" }
# rustpython-parser-core = { path = "../RustPython-parser/core" }
# rustpython-parser = { path = "../RustPython-parser/parser" }
# rustpython-ast = { path = "../RustPython-parser/ast" }
# rustpython-format = { path = "../RustPython-parser/format" }
ahash = "0.8.11"
ascii = "1.0"
bitflags = "2.4.1"
bstr = "1"
cfg-if = "1.0"
chrono = "0.4.37"
crossbeam-utils = "0.8.19"
flame = "0.2.2"
getrandom = "0.2.12"
glob = "0.3"
hex = "0.4.3"
indexmap = { version = "2.2.6", features = ["std"] }
insta = "1.38.0"
itertools = "0.11.0"
is-macro = "0.3.0"
junction = "1.0.0"
libc = "0.2.153"
log = "0.4.16"
nix = { version = "0.29", features = ["fs", "user", "process", "term", "time", "signal", "ioctl", "socket", "sched", "zerocopy", "dir", "hostname", "net", "poll"] }
malachite-bigint = "0.2.2"
malachite-q = "<=0.4.18"
malachite-base = "<=0.4.18"
memchr = "2.7.2"
num-complex = "0.4.0"
num-integer = "0.1.44"
num-traits = "0.2"
num_enum = { version = "0.7", default-features = false }
once_cell = "1.19.0"
parking_lot = "0.12.1"
paste = "1.0.7"
rand = "0.8.5"
rustix = { version = "0.38", features = ["event"] }
rustyline = "14.0.0"
serde = { version = "1.0.133", default-features = false }
schannel = "0.1.22"
static_assertions = "1.1"
strum = "0.26"
strum_macros = "0.26"
syn = "1.0.109"
thiserror = "1.0"
thread_local = "1.1.4"
unicode_names2 = "1.2.0"
widestring = "1.1.0"
windows-sys = "0.52.0"
wasm-bindgen = "0.2.92"
# Lints
[workspace.lints.rust]
unsafe_code = "allow"
[workspace.lints.clippy]
perf = "warn"
style = "warn"
complexity = "warn"
suspicious = "warn"
correctness = "warn"

View File

@@ -19,13 +19,13 @@ The contents of the Development Guide include:
RustPython requires the following:
- Rust latest stable version (e.g 1.51.0 as of Apr 2 2021)
- Rust latest stable version (e.g 1.69.0 as of Apr 20 2023)
- To check Rust version: `rustc --version`
- If you have `rustup` on your system, enter to update to the latest
stable version: `rustup update stable`
- If you do not have Rust installed, use [rustup](https://rustup.rs/) to
do so.
- CPython version 3.10 or higher
- CPython version 3.12 or higher
- CPython can be installed by your operating system's package manager,
from the [Python website](https://www.python.org/downloads/), or
using a third-party distribution, such as
@@ -47,7 +47,10 @@ you can check yourself with `cargo clippy`.
Custom Python code (i.e. code not copied from CPython's standard library) should
follow the [PEP 8](https://www.python.org/dev/peps/pep-0008/) style. We also use
[flake8](http://flake8.pycqa.org/en/latest/) to check Python code style.
[ruff](https://beta.ruff.rs/docs/) to check Python code style.
In addition to language specific tools, [cspell](https://github.com/streetsidesoftware/cspell),
a code spell checker, is used in order to ensure correct spellings for code.
## Testing

6
Lib/__future__.py vendored
View File

@@ -33,7 +33,7 @@ in releases at or after that, modules no longer need
to use the feature in question, but may continue to use such imports.
MandatoryRelease may also be None, meaning that a planned feature got
dropped.
dropped or that the release version is undetermined.
Instances of class _Feature have two corresponding methods,
.getOptionalRelease() and .getMandatoryRelease().
@@ -96,7 +96,7 @@ class _Feature:
"""Return release in which this feature will become mandatory.
This is a 5-tuple, of the same form as sys.version_info, or, if
the feature was dropped, is None.
the feature was dropped, or the release date is undetermined, is None.
"""
return self.mandatory
@@ -143,5 +143,5 @@ generator_stop = _Feature((3, 5, 0, "beta", 1),
CO_FUTURE_GENERATOR_STOP)
annotations = _Feature((3, 7, 0, "beta", 1),
(3, 11, 0, "alpha", 0),
None,
CO_FUTURE_ANNOTATIONS)

16
Lib/__hello__.py vendored Normal file
View File

@@ -0,0 +1,16 @@
initialized = True
class TestFrozenUtf8_1:
"""\u00b6"""
class TestFrozenUtf8_2:
"""\u03c0"""
class TestFrozenUtf8_4:
"""\U0001f600"""
def main():
print("Hello world!")
if __name__ == '__main__':
main()

7
Lib/__phello__/__init__.py vendored Normal file
View File

@@ -0,0 +1,7 @@
initialized = True
def main():
print("Hello world!")
if __name__ == '__main__':
main()

7
Lib/__phello__/spam.py vendored Normal file
View File

@@ -0,0 +1,7 @@
initialized = True
def main():
print("Hello world!")
if __name__ == '__main__':
main()

View File

@@ -6,6 +6,32 @@
Unit tests are in test_collections.
"""
############ Maintenance notes #########################################
#
# ABCs are different from other standard library modules in that they
# specify compliance tests. In general, once an ABC has been published,
# new methods (either abstract or concrete) cannot be added.
#
# Though classes that inherit from an ABC would automatically receive a
# new mixin method, registered classes would become non-compliant and
# violate the contract promised by ``isinstance(someobj, SomeABC)``.
#
# Though irritating, the correct procedure for adding new abstract or
# mixin methods is to create a new ABC as a subclass of the previous
# ABC. For example, union(), intersection(), and difference() cannot
# be added to Set but could go into a new ABC that extends Set.
#
# Because they are so hard to change, new ABCs should have their APIs
# carefully thought through prior to publication.
#
# Since ABCMeta only checks for the presence of methods, it is possible
# to alter the signature of a method by adding optional arguments
# or changing parameters names. This is still a bit dubious but at
# least it won't cause isinstance() to return an incorrect result.
#
#
#######################################################################
from abc import ABCMeta, abstractmethod
import sys
@@ -23,7 +49,7 @@ __all__ = ["Awaitable", "Coroutine",
"Mapping", "MutableMapping",
"MappingView", "KeysView", "ItemsView", "ValuesView",
"Sequence", "MutableSequence",
"ByteString",
"ByteString", "Buffer",
]
# This module has been renamed from collections.abc to _collections_abc to
@@ -413,6 +439,21 @@ class Collection(Sized, Iterable, Container):
return NotImplemented
class Buffer(metaclass=ABCMeta):
__slots__ = ()
@abstractmethod
def __buffer__(self, flags: int, /) -> memoryview:
raise NotImplementedError
@classmethod
def __subclasshook__(cls, C):
if cls is Buffer:
return _check_methods(C, "__buffer__")
return NotImplemented
class _CallableGenericAlias(GenericAlias):
""" Represent `Callable[argtypes, resulttype]`.
@@ -430,25 +471,13 @@ class _CallableGenericAlias(GenericAlias):
raise TypeError(
"Callable must be used as Callable[[arg, ...], result].")
t_args, t_result = args
if isinstance(t_args, list):
if isinstance(t_args, (tuple, list)):
args = (*t_args, t_result)
elif not _is_param_expr(t_args):
raise TypeError(f"Expected a list of types, an ellipsis, "
f"ParamSpec, or Concatenate. Got {t_args}")
return super().__new__(cls, origin, args)
@property
def __parameters__(self):
params = []
for arg in self.__args__:
# Looks like a genericalias
if hasattr(arg, "__parameters__") and isinstance(arg.__parameters__, tuple):
params.extend(arg.__parameters__)
else:
if _is_typevarlike(arg):
params.append(arg)
return tuple(dict.fromkeys(params))
def __repr__(self):
if len(self.__args__) == 2 and _is_param_expr(self.__args__[0]):
return super().__repr__()
@@ -467,55 +496,18 @@ class _CallableGenericAlias(GenericAlias):
# rather than the default types.GenericAlias object. Most of the
# code is copied from typing's _GenericAlias and the builtin
# types.GenericAlias.
# A special case in PEP 612 where if X = Callable[P, int],
# then X[int, str] == X[[int, str]].
param_len = len(self.__parameters__)
if param_len == 0:
raise TypeError(f'{self} is not a generic class')
if not isinstance(item, tuple):
item = (item,)
if (param_len == 1 and _is_param_expr(self.__parameters__[0])
and item and not _is_param_expr(item[0])):
item = (list(item),)
item_len = len(item)
if item_len != param_len:
raise TypeError(f'Too {"many" if item_len > param_len else "few"}'
f' arguments for {self};'
f' actual {item_len}, expected {param_len}')
subst = dict(zip(self.__parameters__, item))
new_args = []
for arg in self.__args__:
if _is_typevarlike(arg):
if _is_param_expr(arg):
arg = subst[arg]
if not _is_param_expr(arg):
raise TypeError(f"Expected a list of types, an ellipsis, "
f"ParamSpec, or Concatenate. Got {arg}")
else:
arg = subst[arg]
# Looks like a GenericAlias
elif hasattr(arg, '__parameters__') and isinstance(arg.__parameters__, tuple):
subparams = arg.__parameters__
if subparams:
subargs = tuple(subst[x] for x in subparams)
arg = arg[subargs]
new_args.append(arg)
new_args = super().__getitem__(item).__args__
# args[0] occurs due to things like Z[[int, str, bool]] from PEP 612
if not isinstance(new_args[0], list):
if not isinstance(new_args[0], (tuple, list)):
t_result = new_args[-1]
t_args = new_args[:-1]
new_args = (t_args, t_result)
return _CallableGenericAlias(Callable, tuple(new_args))
def _is_typevarlike(arg):
obj = type(arg)
# looks like a TypeVar/ParamSpec
return (obj.__module__ == 'typing'
and obj.__name__ in {'ParamSpec', 'TypeVar'})
def _is_param_expr(obj):
"""Checks if obj matches either a list of types, ``...``, ``ParamSpec`` or
``_ConcatenateGenericAlias`` from typing.py
@@ -533,9 +525,8 @@ def _type_repr(obj):
Copied from :mod:`typing` since collections.abc
shouldn't depend on that module.
(Keep this roughly in sync with the typing version.)
"""
if isinstance(obj, GenericAlias):
return repr(obj)
if isinstance(obj, type):
if obj.__module__ == 'builtins':
return obj.__qualname__
@@ -868,7 +859,7 @@ class KeysView(MappingView, Set):
__slots__ = ()
@classmethod
def _from_iterable(self, it):
def _from_iterable(cls, it):
return set(it)
def __contains__(self, key):
@@ -886,7 +877,7 @@ class ItemsView(MappingView, Set):
__slots__ = ()
@classmethod
def _from_iterable(self, it):
def _from_iterable(cls, it):
return set(it)
def __contains__(self, item):
@@ -1064,10 +1055,10 @@ class Sequence(Reversible, Collection):
while stop is None or i < stop:
try:
v = self[i]
if v is value or v == value:
return i
except IndexError:
break
if v is value or v == value:
return i
i += 1
raise ValueError
@@ -1080,8 +1071,27 @@ Sequence.register(str)
Sequence.register(range)
Sequence.register(memoryview)
class _DeprecateByteStringMeta(ABCMeta):
def __new__(cls, name, bases, namespace, **kwargs):
if name != "ByteString":
import warnings
class ByteString(Sequence):
warnings._deprecated(
"collections.abc.ByteString",
remove=(3, 14),
)
return super().__new__(cls, name, bases, namespace, **kwargs)
def __instancecheck__(cls, instance):
import warnings
warnings._deprecated(
"collections.abc.ByteString",
remove=(3, 14),
)
return super().__instancecheck__(instance)
class ByteString(Sequence, metaclass=_DeprecateByteStringMeta):
"""This unifies bytes and bytearray.
XXX Should add all their methods.

12
Lib/_compression.py vendored
View File

@@ -1,7 +1,7 @@
"""Internal classes used by the gzip, lzma and bz2 modules"""
import io
import sys
BUFFER_SIZE = io.DEFAULT_BUFFER_SIZE # Compressed data read chunk size
@@ -110,6 +110,16 @@ class DecompressReader(io.RawIOBase):
self._pos += len(data)
return data
def readall(self):
chunks = []
# sys.maxsize means the max length of output buffer is unlimited,
# so that the whole input buffer can be decompressed within one
# .decompress() call.
while data := self.read(sys.maxsize):
chunks.append(data)
return b"".join(chunks)
# Rewind the file to the beginning of the data stream.
def _rewind(self):
self._fp.seek(0)

22
Lib/_dummy_os.py vendored
View File

@@ -5,22 +5,30 @@ A shim of the os module containing only simple path-related utilities
try:
from os import *
except ImportError:
import abc
import abc, sys
def __getattr__(name):
raise OSError("no os specific module found")
if name in {"_path_normpath", "__path__"}:
raise AttributeError(name)
if name.isupper():
return 0
def dummy(*args, **kwargs):
import io
return io.UnsupportedOperation(f"{name}: no os specific module found")
dummy.__name__ = f"dummy_{name}"
return dummy
def _shim():
import _dummy_os, sys
sys.modules['os'] = _dummy_os
sys.modules['os.path'] = _dummy_os.path
sys.modules['os'] = sys.modules['posix'] = sys.modules[__name__]
import posixpath as path
import sys
sys.modules['os.path'] = path
del sys
sep = path.sep
supports_dir_fd = set()
supports_effective_ids = set()
supports_fd = set()
supports_follow_symlinks = set()
def fspath(path):

View File

@@ -145,6 +145,9 @@ class LockType(object):
def locked(self):
return self.locked_status
def _at_fork_reinit(self):
self.locked_status = False
def __repr__(self):
return "<%s %s.%s object at %s>" % (
"locked" if self.locked_status else "unlocked",

35
Lib/_markupbase.py vendored
View File

@@ -29,10 +29,6 @@ class ParserBase:
raise RuntimeError(
"_markupbase.ParserBase must be subclassed")
def error(self, message):
raise NotImplementedError(
"subclasses of ParserBase must override error()")
def reset(self):
self.lineno = 1
self.offset = 0
@@ -131,12 +127,11 @@ class ParserBase:
# also in data attribute specifications of attlist declaration
# also link type declaration subsets in linktype declarations
# also link attribute specification lists in link declarations
self.error("unsupported '[' char in %s declaration" % decltype)
raise AssertionError("unsupported '[' char in %s declaration" % decltype)
else:
self.error("unexpected '[' char in declaration")
raise AssertionError("unexpected '[' char in declaration")
else:
self.error(
"unexpected %r char in declaration" % rawdata[j])
raise AssertionError("unexpected %r char in declaration" % rawdata[j])
if j < 0:
return j
return -1 # incomplete
@@ -156,7 +151,9 @@ class ParserBase:
# look for MS Office ]> ending
match= _msmarkedsectionclose.search(rawdata, i+3)
else:
self.error('unknown status keyword %r in marked section' % rawdata[i+3:j])
raise AssertionError(
'unknown status keyword %r in marked section' % rawdata[i+3:j]
)
if not match:
return -1
if report:
@@ -168,7 +165,7 @@ class ParserBase:
def parse_comment(self, i, report=1):
rawdata = self.rawdata
if rawdata[i:i+4] != '<!--':
self.error('unexpected call to parse_comment()')
raise AssertionError('unexpected call to parse_comment()')
match = _commentclose.search(rawdata, i+4)
if not match:
return -1
@@ -192,7 +189,9 @@ class ParserBase:
return -1
if s != "<!":
self.updatepos(declstartpos, j + 1)
self.error("unexpected char in internal subset (in %r)" % s)
raise AssertionError(
"unexpected char in internal subset (in %r)" % s
)
if (j + 2) == n:
# end of buffer; incomplete
return -1
@@ -209,8 +208,9 @@ class ParserBase:
return -1
if name not in {"attlist", "element", "entity", "notation"}:
self.updatepos(declstartpos, j + 2)
self.error(
"unknown declaration %r in internal subset" % name)
raise AssertionError(
"unknown declaration %r in internal subset" % name
)
# handle the individual names
meth = getattr(self, "_parse_doctype_" + name)
j = meth(j, declstartpos)
@@ -234,14 +234,14 @@ class ParserBase:
if rawdata[j] == ">":
return j
self.updatepos(declstartpos, j)
self.error("unexpected char after internal subset")
raise AssertionError("unexpected char after internal subset")
else:
return -1
elif c.isspace():
j = j + 1
else:
self.updatepos(declstartpos, j)
self.error("unexpected char %r in internal subset" % c)
raise AssertionError("unexpected char %r in internal subset" % c)
# end of buffer reached
return -1
@@ -387,8 +387,9 @@ class ParserBase:
return name.lower(), m.end()
else:
self.updatepos(declstartpos, i)
self.error("expected name token at %r"
% rawdata[declstartpos:declstartpos+20])
raise AssertionError(
"expected name token at %r" % rawdata[declstartpos:declstartpos+20]
)
# To be overridden -- handlers for unknown objects
def unknown_decl(self, data):

2643
Lib/_pydatetime.py vendored Normal file

File diff suppressed because it is too large Load Diff

27
Lib/_pydecimal.py vendored
View File

@@ -734,18 +734,23 @@ class Decimal(object):
"""
if isinstance(f, int): # handle integer inputs
return cls(f)
if not isinstance(f, float):
raise TypeError("argument must be int or float.")
if _math.isinf(f) or _math.isnan(f):
return cls(repr(f))
if _math.copysign(1.0, f) == 1.0:
sign = 0
sign = 0 if f >= 0 else 1
k = 0
coeff = str(abs(f))
elif isinstance(f, float):
if _math.isinf(f) or _math.isnan(f):
return cls(repr(f))
if _math.copysign(1.0, f) == 1.0:
sign = 0
else:
sign = 1
n, d = abs(f).as_integer_ratio()
k = d.bit_length() - 1
coeff = str(n*5**k)
else:
sign = 1
n, d = abs(f).as_integer_ratio()
k = d.bit_length() - 1
result = _dec_from_triple(sign, str(n*5**k), -k)
raise TypeError("argument must be int or float.")
result = _dec_from_triple(sign, coeff, -k)
if cls is Decimal:
return result
else:

90
Lib/_pyio.py vendored
View File

@@ -44,8 +44,9 @@ def text_encoding(encoding, stacklevel=2):
"""
A helper function to choose the text encoding.
When encoding is not None, just return it.
Otherwise, return the default text encoding (i.e. "locale").
When encoding is not None, this function returns it.
Otherwise, this function returns the default text encoding
(i.e. "locale" or "utf-8" depends on UTF-8 mode).
This function emits an EncodingWarning if *encoding* is None and
sys.flags.warn_default_encoding is true.
@@ -55,7 +56,10 @@ def text_encoding(encoding, stacklevel=2):
However, please consider using encoding="utf-8" for new APIs.
"""
if encoding is None:
encoding = "locale"
if sys.flags.utf8_mode:
encoding = "utf-8"
else:
encoding = "locale"
if sys.flags.warn_default_encoding:
import warnings
warnings.warn("'encoding' argument not specified.",
@@ -101,7 +105,6 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newline mode (deprecated)
========= ===============================================================
The default mode is 'rt' (open for reading text). For binary random
@@ -117,10 +120,6 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
returned as strings, the bytes having been first decoded using a
platform-dependent encoding or using the specified encoding if given.
'U' mode is deprecated and will raise an exception in future versions
of Python. It has no effect in Python 3. Use newline to control
universal newlines mode.
buffering is an optional integer used to set the buffering policy.
Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
line buffering (only usable in text mode), and an integer > 1 to indicate
@@ -206,7 +205,7 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
if errors is not None and not isinstance(errors, str):
raise TypeError("invalid errors: %r" % errors)
modes = set(mode)
if modes - set("axrwb+tU") or len(mode) > len(modes):
if modes - set("axrwb+t") or len(mode) > len(modes):
raise ValueError("invalid mode: %r" % mode)
creating = "x" in modes
reading = "r" in modes
@@ -215,13 +214,6 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
updating = "+" in modes
text = "t" in modes
binary = "b" in modes
if "U" in modes:
if creating or writing or appending or updating:
raise ValueError("mode U cannot be combined with 'x', 'w', 'a', or '+'")
import warnings
warnings.warn("'U' mode is deprecated",
DeprecationWarning, 2)
reading = True
if text and binary:
raise ValueError("can't have text and binary mode at once")
if creating + reading + writing + appending > 1:
@@ -311,22 +303,6 @@ except AttributeError:
open_code = _open_code_with_warning
def __getattr__(name):
if name == "OpenWrapper":
# bpo-43680: Until Python 3.9, _pyio.open was not a static method and
# builtins.open was set to OpenWrapper to not become a bound method
# when set to a class variable. _io.open is a built-in function whereas
# _pyio.open is a Python function. In Python 3.10, _pyio.open() is now
# a static method, and builtins.open() is now io.open().
import warnings
warnings.warn('OpenWrapper is deprecated, use open instead',
DeprecationWarning, stacklevel=2)
global OpenWrapper
OpenWrapper = open
return OpenWrapper
raise AttributeError(name)
# In normal operation, both `UnsupportedOperation`s should be bound to the
# same object.
try:
@@ -338,8 +314,7 @@ except AttributeError:
class IOBase(metaclass=abc.ABCMeta):
"""The abstract base class for all I/O classes, acting on streams of
bytes. There is no public constructor.
"""The abstract base class for all I/O classes.
This class provides dummy implementations for many methods that
derived classes can override selectively; the default implementations
@@ -1154,6 +1129,7 @@ class BufferedReader(_BufferedIOMixin):
do at most one raw read to satisfy it. We never return more
than self.buffer_size.
"""
self._checkClosed("peek of closed file")
with self._read_lock:
return self._peek_unlocked(size)
@@ -1172,6 +1148,7 @@ class BufferedReader(_BufferedIOMixin):
"""Reads up to size bytes, with at most one read() system call."""
# Returns up to size bytes. If at least one byte is buffered, we
# only return buffered bytes. Otherwise, we do one raw read.
self._checkClosed("read of closed file")
if size < 0:
size = self.buffer_size
if size == 0:
@@ -1189,6 +1166,8 @@ class BufferedReader(_BufferedIOMixin):
def _readinto(self, buf, read1):
"""Read data into *buf* with at most one system call."""
self._checkClosed("readinto of closed file")
# Need to create a memoryview object of type 'b', otherwise
# we may not be able to assign bytes to it, and slicing it
# would create a new object.
@@ -1233,11 +1212,13 @@ class BufferedReader(_BufferedIOMixin):
return written
def tell(self):
return _BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_pos
# GH-95782: Keep return value non-negative
return max(_BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_pos, 0)
def seek(self, pos, whence=0):
if whence not in valid_seek_flags:
raise ValueError("invalid whence value")
self._checkClosed("seek of closed file")
with self._read_lock:
if whence == 1:
pos -= len(self._read_buf) - self._read_pos
@@ -1845,7 +1826,7 @@ class TextIOBase(IOBase):
"""Base class for text I/O.
This class provides a character and line based interface to stream
I/O. There is no public constructor.
I/O.
"""
def read(self, size=-1):
@@ -1997,7 +1978,7 @@ class TextIOWrapper(TextIOBase):
r"""Character and line based layer over a BufferedIOBase object, buffer.
encoding gives the name of the encoding that the stream will be
decoded or encoded with. It defaults to locale.getpreferredencoding(False).
decoded or encoded with. It defaults to locale.getencoding().
errors determines the strictness of encoding and decoding (see the
codecs.register) and defaults to "strict".
@@ -2031,19 +2012,7 @@ class TextIOWrapper(TextIOBase):
encoding = text_encoding(encoding)
if encoding == "locale":
try:
encoding = os.device_encoding(buffer.fileno()) or "locale"
except (AttributeError, UnsupportedOperation):
pass
if encoding == "locale":
try:
import locale
except ImportError:
# Importing locale may fail if Python is being built
encoding = "utf-8"
else:
encoding = locale.getpreferredencoding(False)
encoding = self._get_locale_encoding()
if not isinstance(encoding, str):
raise ValueError("invalid encoding: %r" % encoding)
@@ -2176,6 +2145,8 @@ class TextIOWrapper(TextIOBase):
else:
if not isinstance(encoding, str):
raise TypeError("invalid encoding: %r" % encoding)
if encoding == "locale":
encoding = self._get_locale_encoding()
if newline is Ellipsis:
newline = self._readnl
@@ -2243,8 +2214,9 @@ class TextIOWrapper(TextIOBase):
self.buffer.write(b)
if self._line_buffering and (haslf or "\r" in s):
self.flush()
self._set_decoded_chars('')
self._snapshot = None
if self._snapshot is not None:
self._set_decoded_chars('')
self._snapshot = None
if self._decoder:
self._decoder.reset()
return length
@@ -2280,6 +2252,15 @@ class TextIOWrapper(TextIOBase):
self._decoded_chars_used += len(chars)
return chars
def _get_locale_encoding(self):
try:
import locale
except ImportError:
# Importing locale may fail if Python is being built
return "utf-8"
else:
return locale.getencoding()
def _rewind_decoded_chars(self, n):
"""Rewind the _decoded_chars buffer."""
if self._decoded_chars_used < n:
@@ -2549,8 +2530,9 @@ class TextIOWrapper(TextIOBase):
# Read everything.
result = (self._get_decoded_chars() +
decoder.decode(self.buffer.read(), final=True))
self._set_decoded_chars('')
self._snapshot = None
if self._snapshot is not None:
self._set_decoded_chars('')
self._snapshot = None
return result
else:
# Keep reading chunks until we have size characters to return.

View File

@@ -10,7 +10,6 @@ The objects used by the site module to add custom builtins.
import sys
class Quitter(object):
def __init__(self, name, eof):
self.name = name
@@ -48,7 +47,7 @@ class _Printer(object):
data = None
for filename in self.__filenames:
try:
with open(filename, "r") as fp:
with open(filename, encoding='utf-8') as fp:
data = fp.read()
break
except OSError:

565
Lib/_strptime.py vendored Normal file
View File

@@ -0,0 +1,565 @@
"""Strptime-related classes and functions.
CLASSES:
LocaleTime -- Discovers and stores locale-specific time information
TimeRE -- Creates regexes for pattern matching a string of text containing
time information
FUNCTIONS:
_getlang -- Figure out what language is being used for the locale
strptime -- Calculates the time struct represented by the passed-in string
"""
import time
import locale
import calendar
from re import compile as re_compile
from re import IGNORECASE
from re import escape as re_escape
from datetime import (date as datetime_date,
timedelta as datetime_timedelta,
timezone as datetime_timezone)
from _thread import allocate_lock as _thread_allocate_lock
__all__ = []
def _getlang():
# Figure out what the current language is set to.
return locale.getlocale(locale.LC_TIME)
class LocaleTime(object):
"""Stores and handles locale-specific information related to time.
ATTRIBUTES:
f_weekday -- full weekday names (7-item list)
a_weekday -- abbreviated weekday names (7-item list)
f_month -- full month names (13-item list; dummy value in [0], which
is added by code)
a_month -- abbreviated month names (13-item list, dummy value in
[0], which is added by code)
am_pm -- AM/PM representation (2-item list)
LC_date_time -- format string for date/time representation (string)
LC_date -- format string for date representation (string)
LC_time -- format string for time representation (string)
timezone -- daylight- and non-daylight-savings timezone representation
(2-item list of sets)
lang -- Language used by instance (2-item tuple)
"""
def __init__(self):
"""Set all attributes.
Order of methods called matters for dependency reasons.
The locale language is set at the offset and then checked again before
exiting. This is to make sure that the attributes were not set with a
mix of information from more than one locale. This would most likely
happen when using threads where one thread calls a locale-dependent
function while another thread changes the locale while the function in
the other thread is still running. Proper coding would call for
locks to prevent changing the locale while locale-dependent code is
running. The check here is done in case someone does not think about
doing this.
Only other possible issue is if someone changed the timezone and did
not call tz.tzset . That is an issue for the programmer, though,
since changing the timezone is worthless without that call.
"""
self.lang = _getlang()
self.__calc_weekday()
self.__calc_month()
self.__calc_am_pm()
self.__calc_timezone()
self.__calc_date_time()
if _getlang() != self.lang:
raise ValueError("locale changed during initialization")
if time.tzname != self.tzname or time.daylight != self.daylight:
raise ValueError("timezone changed during initialization")
def __calc_weekday(self):
# Set self.a_weekday and self.f_weekday using the calendar
# module.
a_weekday = [calendar.day_abbr[i].lower() for i in range(7)]
f_weekday = [calendar.day_name[i].lower() for i in range(7)]
self.a_weekday = a_weekday
self.f_weekday = f_weekday
def __calc_month(self):
# Set self.f_month and self.a_month using the calendar module.
a_month = [calendar.month_abbr[i].lower() for i in range(13)]
f_month = [calendar.month_name[i].lower() for i in range(13)]
self.a_month = a_month
self.f_month = f_month
def __calc_am_pm(self):
# Set self.am_pm by using time.strftime().
# The magic date (1999,3,17,hour,44,55,2,76,0) is not really that
# magical; just happened to have used it everywhere else where a
# static date was needed.
am_pm = []
for hour in (1, 22):
time_tuple = time.struct_time((1999,3,17,hour,44,55,2,76,0))
am_pm.append(time.strftime("%p", time_tuple).lower())
self.am_pm = am_pm
def __calc_date_time(self):
# Set self.date_time, self.date, & self.time by using
# time.strftime().
# Use (1999,3,17,22,44,55,2,76,0) for magic date because the amount of
# overloaded numbers is minimized. The order in which searches for
# values within the format string is very important; it eliminates
# possible ambiguity for what something represents.
time_tuple = time.struct_time((1999,3,17,22,44,55,2,76,0))
date_time = [None, None, None]
date_time[0] = time.strftime("%c", time_tuple).lower()
date_time[1] = time.strftime("%x", time_tuple).lower()
date_time[2] = time.strftime("%X", time_tuple).lower()
replacement_pairs = [('%', '%%'), (self.f_weekday[2], '%A'),
(self.f_month[3], '%B'), (self.a_weekday[2], '%a'),
(self.a_month[3], '%b'), (self.am_pm[1], '%p'),
('1999', '%Y'), ('99', '%y'), ('22', '%H'),
('44', '%M'), ('55', '%S'), ('76', '%j'),
('17', '%d'), ('03', '%m'), ('3', '%m'),
# '3' needed for when no leading zero.
('2', '%w'), ('10', '%I')]
replacement_pairs.extend([(tz, "%Z") for tz_values in self.timezone
for tz in tz_values])
for offset,directive in ((0,'%c'), (1,'%x'), (2,'%X')):
current_format = date_time[offset]
for old, new in replacement_pairs:
# Must deal with possible lack of locale info
# manifesting itself as the empty string (e.g., Swedish's
# lack of AM/PM info) or a platform returning a tuple of empty
# strings (e.g., MacOS 9 having timezone as ('','')).
if old:
current_format = current_format.replace(old, new)
# If %W is used, then Sunday, 2005-01-03 will fall on week 0 since
# 2005-01-03 occurs before the first Monday of the year. Otherwise
# %U is used.
time_tuple = time.struct_time((1999,1,3,1,1,1,6,3,0))
if '00' in time.strftime(directive, time_tuple):
U_W = '%W'
else:
U_W = '%U'
date_time[offset] = current_format.replace('11', U_W)
self.LC_date_time = date_time[0]
self.LC_date = date_time[1]
self.LC_time = date_time[2]
def __calc_timezone(self):
# Set self.timezone by using time.tzname.
# Do not worry about possibility of time.tzname[0] == time.tzname[1]
# and time.daylight; handle that in strptime.
try:
time.tzset()
except AttributeError:
pass
self.tzname = time.tzname
self.daylight = time.daylight
no_saving = frozenset({"utc", "gmt", self.tzname[0].lower()})
if self.daylight:
has_saving = frozenset({self.tzname[1].lower()})
else:
has_saving = frozenset()
self.timezone = (no_saving, has_saving)
class TimeRE(dict):
"""Handle conversion from format directives to regexes."""
def __init__(self, locale_time=None):
"""Create keys/values.
Order of execution is important for dependency reasons.
"""
if locale_time:
self.locale_time = locale_time
else:
self.locale_time = LocaleTime()
base = super()
base.__init__({
# The " [1-9]" part of the regex is to make %c from ANSI C work
'd': r"(?P<d>3[0-1]|[1-2]\d|0[1-9]|[1-9]| [1-9])",
'f': r"(?P<f>[0-9]{1,6})",
'H': r"(?P<H>2[0-3]|[0-1]\d|\d)",
'I': r"(?P<I>1[0-2]|0[1-9]|[1-9])",
'G': r"(?P<G>\d\d\d\d)",
'j': r"(?P<j>36[0-6]|3[0-5]\d|[1-2]\d\d|0[1-9]\d|00[1-9]|[1-9]\d|0[1-9]|[1-9])",
'm': r"(?P<m>1[0-2]|0[1-9]|[1-9])",
'M': r"(?P<M>[0-5]\d|\d)",
'S': r"(?P<S>6[0-1]|[0-5]\d|\d)",
'U': r"(?P<U>5[0-3]|[0-4]\d|\d)",
'w': r"(?P<w>[0-6])",
'u': r"(?P<u>[1-7])",
'V': r"(?P<V>5[0-3]|0[1-9]|[1-4]\d|\d)",
# W is set below by using 'U'
'y': r"(?P<y>\d\d)",
#XXX: Does 'Y' need to worry about having less or more than
# 4 digits?
'Y': r"(?P<Y>\d\d\d\d)",
'z': r"(?P<z>[+-]\d\d:?[0-5]\d(:?[0-5]\d(\.\d{1,6})?)?|(?-i:Z))",
'A': self.__seqToRE(self.locale_time.f_weekday, 'A'),
'a': self.__seqToRE(self.locale_time.a_weekday, 'a'),
'B': self.__seqToRE(self.locale_time.f_month[1:], 'B'),
'b': self.__seqToRE(self.locale_time.a_month[1:], 'b'),
'p': self.__seqToRE(self.locale_time.am_pm, 'p'),
'Z': self.__seqToRE((tz for tz_names in self.locale_time.timezone
for tz in tz_names),
'Z'),
'%': '%'})
base.__setitem__('W', base.__getitem__('U').replace('U', 'W'))
base.__setitem__('c', self.pattern(self.locale_time.LC_date_time))
base.__setitem__('x', self.pattern(self.locale_time.LC_date))
base.__setitem__('X', self.pattern(self.locale_time.LC_time))
def __seqToRE(self, to_convert, directive):
"""Convert a list to a regex string for matching a directive.
Want possible matching values to be from longest to shortest. This
prevents the possibility of a match occurring for a value that also
a substring of a larger value that should have matched (e.g., 'abc'
matching when 'abcdef' should have been the match).
"""
to_convert = sorted(to_convert, key=len, reverse=True)
for value in to_convert:
if value != '':
break
else:
return ''
regex = '|'.join(re_escape(stuff) for stuff in to_convert)
regex = '(?P<%s>%s' % (directive, regex)
return '%s)' % regex
def pattern(self, format):
"""Return regex pattern for the format string.
Need to make sure that any characters that might be interpreted as
regex syntax are escaped.
"""
processed_format = ''
# The sub() call escapes all characters that might be misconstrued
# as regex syntax. Cannot use re.escape since we have to deal with
# format directives (%m, etc.).
regex_chars = re_compile(r"([\\.^$*+?\(\){}\[\]|])")
format = regex_chars.sub(r"\\\1", format)
whitespace_replacement = re_compile(r'\s+')
format = whitespace_replacement.sub(r'\\s+', format)
while '%' in format:
directive_index = format.index('%')+1
processed_format = "%s%s%s" % (processed_format,
format[:directive_index-1],
self[format[directive_index]])
format = format[directive_index+1:]
return "%s%s" % (processed_format, format)
def compile(self, format):
"""Return a compiled re object for the format string."""
return re_compile(self.pattern(format), IGNORECASE)
_cache_lock = _thread_allocate_lock()
# DO NOT modify _TimeRE_cache or _regex_cache without acquiring the cache lock
# first!
_TimeRE_cache = TimeRE()
_CACHE_MAX_SIZE = 5 # Max number of regexes stored in _regex_cache
_regex_cache = {}
def _calc_julian_from_U_or_W(year, week_of_year, day_of_week, week_starts_Mon):
"""Calculate the Julian day based on the year, week of the year, and day of
the week, with week_start_day representing whether the week of the year
assumes the week starts on Sunday or Monday (6 or 0)."""
first_weekday = datetime_date(year, 1, 1).weekday()
# If we are dealing with the %U directive (week starts on Sunday), it's
# easier to just shift the view to Sunday being the first day of the
# week.
if not week_starts_Mon:
first_weekday = (first_weekday + 1) % 7
day_of_week = (day_of_week + 1) % 7
# Need to watch out for a week 0 (when the first day of the year is not
# the same as that specified by %U or %W).
week_0_length = (7 - first_weekday) % 7
if week_of_year == 0:
return 1 + day_of_week - first_weekday
else:
days_to_week = week_0_length + (7 * (week_of_year - 1))
return 1 + days_to_week + day_of_week
def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
"""Return a 2-tuple consisting of a time struct and an int containing
the number of microseconds based on the input string and the
format string."""
for index, arg in enumerate([data_string, format]):
if not isinstance(arg, str):
msg = "strptime() argument {} must be str, not {}"
raise TypeError(msg.format(index, type(arg)))
global _TimeRE_cache, _regex_cache
with _cache_lock:
locale_time = _TimeRE_cache.locale_time
if (_getlang() != locale_time.lang or
time.tzname != locale_time.tzname or
time.daylight != locale_time.daylight):
_TimeRE_cache = TimeRE()
_regex_cache.clear()
locale_time = _TimeRE_cache.locale_time
if len(_regex_cache) > _CACHE_MAX_SIZE:
_regex_cache.clear()
format_regex = _regex_cache.get(format)
if not format_regex:
try:
format_regex = _TimeRE_cache.compile(format)
# KeyError raised when a bad format is found; can be specified as
# \\, in which case it was a stray % but with a space after it
except KeyError as err:
bad_directive = err.args[0]
if bad_directive == "\\":
bad_directive = "%"
del err
raise ValueError("'%s' is a bad directive in format '%s'" %
(bad_directive, format)) from None
# IndexError only occurs when the format string is "%"
except IndexError:
raise ValueError("stray %% in format '%s'" % format) from None
_regex_cache[format] = format_regex
found = format_regex.match(data_string)
if not found:
raise ValueError("time data %r does not match format %r" %
(data_string, format))
if len(data_string) != found.end():
raise ValueError("unconverted data remains: %s" %
data_string[found.end():])
iso_year = year = None
month = day = 1
hour = minute = second = fraction = 0
tz = -1
gmtoff = None
gmtoff_fraction = 0
iso_week = week_of_year = None
week_of_year_start = None
# weekday and julian defaulted to None so as to signal need to calculate
# values
weekday = julian = None
found_dict = found.groupdict()
for group_key in found_dict.keys():
# Directives not explicitly handled below:
# c, x, X
# handled by making out of other directives
# U, W
# worthless without day of the week
if group_key == 'y':
year = int(found_dict['y'])
# Open Group specification for strptime() states that a %y
#value in the range of [00, 68] is in the century 2000, while
#[69,99] is in the century 1900
if year <= 68:
year += 2000
else:
year += 1900
elif group_key == 'Y':
year = int(found_dict['Y'])
elif group_key == 'G':
iso_year = int(found_dict['G'])
elif group_key == 'm':
month = int(found_dict['m'])
elif group_key == 'B':
month = locale_time.f_month.index(found_dict['B'].lower())
elif group_key == 'b':
month = locale_time.a_month.index(found_dict['b'].lower())
elif group_key == 'd':
day = int(found_dict['d'])
elif group_key == 'H':
hour = int(found_dict['H'])
elif group_key == 'I':
hour = int(found_dict['I'])
ampm = found_dict.get('p', '').lower()
# If there was no AM/PM indicator, we'll treat this like AM
if ampm in ('', locale_time.am_pm[0]):
# We're in AM so the hour is correct unless we're
# looking at 12 midnight.
# 12 midnight == 12 AM == hour 0
if hour == 12:
hour = 0
elif ampm == locale_time.am_pm[1]:
# We're in PM so we need to add 12 to the hour unless
# we're looking at 12 noon.
# 12 noon == 12 PM == hour 12
if hour != 12:
hour += 12
elif group_key == 'M':
minute = int(found_dict['M'])
elif group_key == 'S':
second = int(found_dict['S'])
elif group_key == 'f':
s = found_dict['f']
# Pad to always return microseconds.
s += "0" * (6 - len(s))
fraction = int(s)
elif group_key == 'A':
weekday = locale_time.f_weekday.index(found_dict['A'].lower())
elif group_key == 'a':
weekday = locale_time.a_weekday.index(found_dict['a'].lower())
elif group_key == 'w':
weekday = int(found_dict['w'])
if weekday == 0:
weekday = 6
else:
weekday -= 1
elif group_key == 'u':
weekday = int(found_dict['u'])
weekday -= 1
elif group_key == 'j':
julian = int(found_dict['j'])
elif group_key in ('U', 'W'):
week_of_year = int(found_dict[group_key])
if group_key == 'U':
# U starts week on Sunday.
week_of_year_start = 6
else:
# W starts week on Monday.
week_of_year_start = 0
elif group_key == 'V':
iso_week = int(found_dict['V'])
elif group_key == 'z':
z = found_dict['z']
if z == 'Z':
gmtoff = 0
else:
if z[3] == ':':
z = z[:3] + z[4:]
if len(z) > 5:
if z[5] != ':':
msg = f"Inconsistent use of : in {found_dict['z']}"
raise ValueError(msg)
z = z[:5] + z[6:]
hours = int(z[1:3])
minutes = int(z[3:5])
seconds = int(z[5:7] or 0)
gmtoff = (hours * 60 * 60) + (minutes * 60) + seconds
gmtoff_remainder = z[8:]
# Pad to always return microseconds.
gmtoff_remainder_padding = "0" * (6 - len(gmtoff_remainder))
gmtoff_fraction = int(gmtoff_remainder + gmtoff_remainder_padding)
if z.startswith("-"):
gmtoff = -gmtoff
gmtoff_fraction = -gmtoff_fraction
elif group_key == 'Z':
# Since -1 is default value only need to worry about setting tz if
# it can be something other than -1.
found_zone = found_dict['Z'].lower()
for value, tz_values in enumerate(locale_time.timezone):
if found_zone in tz_values:
# Deal with bad locale setup where timezone names are the
# same and yet time.daylight is true; too ambiguous to
# be able to tell what timezone has daylight savings
if (time.tzname[0] == time.tzname[1] and
time.daylight and found_zone not in ("utc", "gmt")):
break
else:
tz = value
break
# Deal with the cases where ambiguities arise
# don't assume default values for ISO week/year
if iso_year is not None:
if julian is not None:
raise ValueError("Day of the year directive '%j' is not "
"compatible with ISO year directive '%G'. "
"Use '%Y' instead.")
elif iso_week is None or weekday is None:
raise ValueError("ISO year directive '%G' must be used with "
"the ISO week directive '%V' and a weekday "
"directive ('%A', '%a', '%w', or '%u').")
elif iso_week is not None:
if year is None or weekday is None:
raise ValueError("ISO week directive '%V' must be used with "
"the ISO year directive '%G' and a weekday "
"directive ('%A', '%a', '%w', or '%u').")
else:
raise ValueError("ISO week directive '%V' is incompatible with "
"the year directive '%Y'. Use the ISO year '%G' "
"instead.")
leap_year_fix = False
if year is None:
if month == 2 and day == 29:
year = 1904 # 1904 is first leap year of 20th century
leap_year_fix = True
else:
year = 1900
# If we know the week of the year and what day of that week, we can figure
# out the Julian day of the year.
if julian is None and weekday is not None:
if week_of_year is not None:
week_starts_Mon = True if week_of_year_start == 0 else False
julian = _calc_julian_from_U_or_W(year, week_of_year, weekday,
week_starts_Mon)
elif iso_year is not None and iso_week is not None:
datetime_result = datetime_date.fromisocalendar(iso_year, iso_week, weekday + 1)
year = datetime_result.year
month = datetime_result.month
day = datetime_result.day
if julian is not None and julian <= 0:
year -= 1
yday = 366 if calendar.isleap(year) else 365
julian += yday
if julian is None:
# Cannot pre-calculate datetime_date() since can change in Julian
# calculation and thus could have different value for the day of
# the week calculation.
# Need to add 1 to result since first day of the year is 1, not 0.
julian = datetime_date(year, month, day).toordinal() - \
datetime_date(year, 1, 1).toordinal() + 1
else: # Assume that if they bothered to include Julian day (or if it was
# calculated above with year/week/weekday) it will be accurate.
datetime_result = datetime_date.fromordinal(
(julian - 1) +
datetime_date(year, 1, 1).toordinal())
year = datetime_result.year
month = datetime_result.month
day = datetime_result.day
if weekday is None:
weekday = datetime_date(year, month, day).weekday()
# Add timezone info
tzname = found_dict.get("Z")
if leap_year_fix:
# the caller didn't supply a year but asked for Feb 29th. We couldn't
# use the default of 1900 for computations. We set it back to ensure
# that February 29th is smaller than March 1st.
year = 1900
return (year, month, day,
hour, minute, second,
weekday, julian, tz, tzname, gmtoff), fraction, gmtoff_fraction
def _strptime_time(data_string, format="%a %b %d %H:%M:%S %Y"):
"""Return a time struct based on the input string and the
format string."""
tt = _strptime(data_string, format)[0]
return time.struct_time(tt[:time._STRUCT_TM_ITEMS])
def _strptime_datetime(cls, data_string, format="%a %b %d %H:%M:%S %Y"):
"""Return a class cls instance based on the input string and the
format string."""
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
tzname, gmtoff = tt[-2:]
args = tt[:6] + (fraction,)
if gmtoff is not None:
tzdelta = datetime_timedelta(seconds=gmtoff, microseconds=gmtoff_fraction)
if tzname:
tz = datetime_timezone(tzdelta, tzname)
else:
tz = datetime_timezone(tzdelta)
args += (tz,)
return cls(*args)

4
Lib/abc.py vendored
View File

@@ -18,7 +18,7 @@ def abstractmethod(funcobj):
class C(metaclass=ABCMeta):
@abstractmethod
def my_abstract_method(self, ...):
def my_abstract_method(self, arg1, arg2, argN):
...
"""
funcobj.__isabstractmethod__ = True
@@ -106,7 +106,7 @@ else:
implementations defined by the registering ABC be callable (not
even via super()).
"""
def __new__(mcls, name, bases, namespace, **kwargs):
def __new__(mcls, name, bases, namespace, /, **kwargs):
cls = super().__new__(mcls, name, bases, namespace, **kwargs)
_abc_init(cls)
return cls

65
Lib/aifc.py vendored
View File

@@ -138,7 +138,11 @@ import struct
import builtins
import warnings
__all__ = ["Error", "open", "openfp"]
__all__ = ["Error", "open"]
warnings._deprecated(__name__, remove=(3, 13))
class Error(Exception):
pass
@@ -251,7 +255,9 @@ def _write_float(f, x):
_write_ulong(f, himant)
_write_ulong(f, lomant)
from chunk import Chunk
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
from chunk import Chunk
from collections import namedtuple
_aifc_params = namedtuple('_aifc_params',
@@ -447,21 +453,33 @@ class Aifc_read:
#
def _alaw2lin(self, data):
import audioop
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=DeprecationWarning)
import audioop
return audioop.alaw2lin(data, 2)
def _ulaw2lin(self, data):
import audioop
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=DeprecationWarning)
import audioop
return audioop.ulaw2lin(data, 2)
def _adpcm2lin(self, data):
import audioop
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=DeprecationWarning)
import audioop
if not hasattr(self, '_adpcmstate'):
# first time
self._adpcmstate = None
data, self._adpcmstate = audioop.adpcm2lin(data, 2, self._adpcmstate)
return data
def _sowt2lin(self, data):
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=DeprecationWarning)
import audioop
return audioop.byteswap(data, 2)
def _read_comm_chunk(self, chunk):
self._nchannels = _read_short(chunk)
self._nframes = _read_long(chunk)
@@ -497,6 +515,8 @@ class Aifc_read:
self._convert = self._ulaw2lin
elif self._comptype in (b'alaw', b'ALAW'):
self._convert = self._alaw2lin
elif self._comptype in (b'sowt', b'SOWT'):
self._convert = self._sowt2lin
else:
raise Error('unsupported compression type')
self._sampwidth = 2
@@ -659,7 +679,7 @@ class Aifc_write:
if self._nframeswritten:
raise Error('cannot change parameters after starting to write')
if comptype not in (b'NONE', b'ulaw', b'ULAW',
b'alaw', b'ALAW', b'G722'):
b'alaw', b'ALAW', b'G722', b'sowt', b'SOWT'):
raise Error('unsupported compression type')
self._comptype = comptype
self._compname = compname
@@ -680,7 +700,7 @@ class Aifc_write:
if self._nframeswritten:
raise Error('cannot change parameters after starting to write')
if comptype not in (b'NONE', b'ulaw', b'ULAW',
b'alaw', b'ALAW', b'G722'):
b'alaw', b'ALAW', b'G722', b'sowt', b'SOWT'):
raise Error('unsupported compression type')
self.setnchannels(nchannels)
self.setsampwidth(sampwidth)
@@ -764,28 +784,43 @@ class Aifc_write:
#
def _lin2alaw(self, data):
import audioop
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=DeprecationWarning)
import audioop
return audioop.lin2alaw(data, 2)
def _lin2ulaw(self, data):
import audioop
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=DeprecationWarning)
import audioop
return audioop.lin2ulaw(data, 2)
def _lin2adpcm(self, data):
import audioop
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=DeprecationWarning)
import audioop
if not hasattr(self, '_adpcmstate'):
self._adpcmstate = None
data, self._adpcmstate = audioop.lin2adpcm(data, 2, self._adpcmstate)
return data
def _lin2sowt(self, data):
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=DeprecationWarning)
import audioop
return audioop.byteswap(data, 2)
def _ensure_header_written(self, datasize):
if not self._nframeswritten:
if self._comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):
if self._comptype in (b'ULAW', b'ulaw',
b'ALAW', b'alaw', b'G722',
b'sowt', b'SOWT'):
if not self._sampwidth:
self._sampwidth = 2
if self._sampwidth != 2:
raise Error('sample width must be 2 when compressing '
'with ulaw/ULAW, alaw/ALAW or G7.22 (ADPCM)')
'with ulaw/ULAW, alaw/ALAW, sowt/SOWT '
'or G7.22 (ADPCM)')
if not self._nchannels:
raise Error('# channels not specified')
if not self._sampwidth:
@@ -801,6 +836,8 @@ class Aifc_write:
self._convert = self._lin2ulaw
elif self._comptype in (b'alaw', b'ALAW'):
self._convert = self._lin2alaw
elif self._comptype in (b'sowt', b'SOWT'):
self._convert = self._lin2sowt
def _write_header(self, initlength):
if self._aifc and self._comptype != b'NONE':
@@ -920,10 +957,6 @@ def open(f, mode=None):
else:
raise Error("mode must be 'r', 'rb', 'w', or 'wb'")
def openfp(f, mode=None):
warnings.warn("aifc.openfp is deprecated since Python 3.7. "
"Use aifc.open instead.", DeprecationWarning, stacklevel=2)
return open(f, mode=mode)
if __name__ == '__main__':
import sys

4
Lib/antigravity.py vendored
View File

@@ -11,7 +11,7 @@ def geohash(latitude, longitude, datedow):
37.857713 -122.544543
'''
# http://xkcd.com/426/
h = hashlib.md5(datedow).hexdigest()
# https://xkcd.com/426/
h = hashlib.md5(datedow, usedforsecurity=False).hexdigest()
p, q = [('%f' % float.fromhex('0.' + x)) for x in (h[:16], h[16:32])]
print('%d%s %d%s' % (latitude, p[1:], longitude, q[1:]))

130
Lib/argparse.py vendored
View File

@@ -89,6 +89,8 @@ import os as _os
import re as _re
import sys as _sys
import warnings
from gettext import gettext as _, ngettext
SUPPRESS = '==SUPPRESS=='
@@ -151,6 +153,7 @@ def _copy_items(items):
# Formatting Help
# ===============
class HelpFormatter(object):
"""Formatter for generating usage messages and argument help strings.
@@ -342,21 +345,22 @@ class HelpFormatter(object):
def get_lines(parts, indent, prefix=None):
lines = []
line = []
indent_length = len(indent)
if prefix is not None:
line_len = len(prefix) - 1
else:
line_len = len(indent) - 1
line_len = indent_length - 1
for part in parts:
if line_len + 1 + len(part) > text_width and line:
lines.append(indent + ' '.join(line))
line = []
line_len = len(indent) - 1
line_len = indent_length - 1
line.append(part)
line_len += len(part) + 1
if line:
lines.append(indent + ' '.join(line))
if prefix is not None:
lines[0] = lines[0][len(indent):]
lines[0] = lines[0][indent_length:]
return lines
# if prog is short, follow it with optionals or positionals
@@ -400,10 +404,18 @@ class HelpFormatter(object):
except ValueError:
continue
else:
end = start + len(group._group_actions)
group_action_count = len(group._group_actions)
end = start + group_action_count
if actions[start:end] == group._group_actions:
suppressed_actions_count = 0
for action in group._group_actions:
group_actions.add(action)
if action.help is SUPPRESS:
suppressed_actions_count += 1
exposed_actions_count = group_action_count - suppressed_actions_count
if not group.required:
if start in inserts:
inserts[start] += ' ['
@@ -413,7 +425,7 @@ class HelpFormatter(object):
inserts[end] += ']'
else:
inserts[end] = ']'
else:
elif exposed_actions_count > 1:
if start in inserts:
inserts[start] += ' ('
else:
@@ -487,7 +499,6 @@ class HelpFormatter(object):
text = _re.sub(r'(%s) ' % open, r'\1', text)
text = _re.sub(r' (%s)' % close, r'\1', text)
text = _re.sub(r'%s *%s' % (open, close), r'', text)
text = _re.sub(r'\(([^|]*)\)', r'\1', text)
text = text.strip()
# return the text
@@ -693,8 +704,19 @@ class ArgumentDefaultsHelpFormatter(HelpFormatter):
"""
def _get_help_string(self, action):
"""
Add the default value to the option help message.
ArgumentDefaultsHelpFormatter and BooleanOptionalAction when it isn't
already present. This code will do that, detecting cornercases to
prevent duplicates or cases where it wouldn't make sense to the end
user.
"""
help = action.help
if '%(default)' not in action.help:
if help is None:
help = ''
if '%(default)' not in help:
if action.default is not SUPPRESS:
defaulting_nargs = [OPTIONAL, ZERO_OR_MORE]
if action.option_strings or action.nargs in defaulting_nargs:
@@ -702,6 +724,7 @@ class ArgumentDefaultsHelpFormatter(HelpFormatter):
return help
class MetavarTypeHelpFormatter(HelpFormatter):
"""Help message formatter which uses the argument 'type' as the default
metavar value (instead of the argument 'dest')
@@ -717,7 +740,6 @@ class MetavarTypeHelpFormatter(HelpFormatter):
return action.type.__name__
# =====================
# Options and Arguments
# =====================
@@ -752,7 +774,7 @@ class ArgumentError(Exception):
if self.argument_name is None:
format = '%(message)s'
else:
format = 'argument %(argument_name)s: %(message)s'
format = _('argument %(argument_name)s: %(message)s')
return format % dict(message=self.message,
argument_name=self.argument_name)
@@ -860,16 +882,20 @@ class Action(_AttributeHolder):
def __call__(self, parser, namespace, values, option_string=None):
raise NotImplementedError(_('.__call__() not defined'))
# FIXME: remove together with `BooleanOptionalAction` deprecated arguments.
_deprecated_default = object()
class BooleanOptionalAction(Action):
def __init__(self,
option_strings,
dest,
default=None,
type=None,
choices=None,
type=_deprecated_default,
choices=_deprecated_default,
required=False,
help=None,
metavar=None):
metavar=_deprecated_default):
_option_strings = []
for option_string in option_strings:
@@ -879,8 +905,23 @@ class BooleanOptionalAction(Action):
option_string = '--no-' + option_string[2:]
_option_strings.append(option_string)
if help is not None and default is not None and default is not SUPPRESS:
help += " (default: %(default)s)"
# We need `_deprecated` special value to ban explicit arguments that
# match default value. Like:
# parser.add_argument('-f', action=BooleanOptionalAction, type=int)
for field_name in ('type', 'choices', 'metavar'):
if locals()[field_name] is not _deprecated_default:
warnings._deprecated(
field_name,
"{name!r} is deprecated as of Python 3.12 and will be "
"removed in Python {remove}.",
remove=(3, 14))
if type is _deprecated_default:
type = None
if choices is _deprecated_default:
choices = None
if metavar is _deprecated_default:
metavar = None
super().__init__(
option_strings=_option_strings,
@@ -893,6 +934,7 @@ class BooleanOptionalAction(Action):
help=help,
metavar=metavar)
def __call__(self, parser, namespace, values, option_string=None):
if option_string in self.option_strings:
setattr(namespace, self.dest, not option_string.startswith('--no-'))
@@ -941,7 +983,7 @@ class _StoreConstAction(Action):
def __init__(self,
option_strings,
dest,
const,
const=None,
default=None,
required=False,
help=None,
@@ -1036,7 +1078,7 @@ class _AppendConstAction(Action):
def __init__(self,
option_strings,
dest,
const,
const=None,
default=None,
required=False,
help=None,
@@ -1168,6 +1210,13 @@ class _SubParsersAction(Action):
aliases = kwargs.pop('aliases', ())
if name in self._name_parser_map:
raise ArgumentError(self, _('conflicting subparser: %s') % name)
for alias in aliases:
if alias in self._name_parser_map:
raise ArgumentError(
self, _('conflicting subparser alias: %s') % alias)
# create a pseudo-action to hold the choice help
if 'help' in kwargs:
help = kwargs.pop('help')
@@ -1648,6 +1697,14 @@ class _ArgumentGroup(_ActionsContainer):
super(_ArgumentGroup, self)._remove_action(action)
self._group_actions.remove(action)
def add_argument_group(self, *args, **kwargs):
warnings.warn(
"Nesting argument groups is deprecated.",
category=DeprecationWarning,
stacklevel=2
)
return super().add_argument_group(*args, **kwargs)
class _MutuallyExclusiveGroup(_ArgumentGroup):
@@ -1668,6 +1725,14 @@ class _MutuallyExclusiveGroup(_ArgumentGroup):
self._container._remove_action(action)
self._group_actions.remove(action)
def add_mutually_exclusive_group(self, *args, **kwargs):
warnings.warn(
"Nesting mutually exclusive groups is deprecated.",
category=DeprecationWarning,
stacklevel=2
)
return super().add_mutually_exclusive_group(*args, **kwargs)
class ArgumentParser(_AttributeHolder, _ActionsContainer):
"""Object for parsing command line strings into Python objects.
@@ -1857,8 +1922,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
if self.exit_on_error:
try:
namespace, args = self._parse_known_args(args, namespace)
except ArgumentError:
err = _sys.exc_info()[1]
except ArgumentError as err:
self.error(str(err))
else:
namespace, args = self._parse_known_args(args, namespace)
@@ -1962,7 +2026,11 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
# arguments, try to parse more single-dash options out
# of the tail of the option string
chars = self.prefix_chars
if arg_count == 0 and option_string[1] not in chars:
if (
arg_count == 0
and option_string[1] not in chars
and explicit_arg != ''
):
action_tuples.append((action, [], option_string))
char = option_string[0]
option_string = char + explicit_arg[0]
@@ -2126,15 +2194,16 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
# replace arguments referencing files with the file content
else:
try:
with open(arg_string[1:]) as args_file:
with open(arg_string[1:],
encoding=_sys.getfilesystemencoding(),
errors=_sys.getfilesystemencodeerrors()) as args_file:
arg_strings = []
for arg_line in args_file.read().splitlines():
for arg in self.convert_arg_line_to_args(arg_line):
arg_strings.append(arg)
arg_strings = self._read_args_from_files(arg_strings)
new_arg_strings.extend(arg_strings)
except OSError:
err = _sys.exc_info()[1]
except OSError as err:
self.error(str(err))
# return the modified argument list
@@ -2441,9 +2510,11 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
not action.option_strings):
if action.default is not None:
value = action.default
self._check_value(action, value)
else:
# since arg_strings is always [] at this point
# there is no need to use self._check_value(action, value)
value = arg_strings
self._check_value(action, value)
# single argument or optional argument produces a single value
elif len(arg_strings) == 1 and action.nargs in [None, OPTIONAL]:
@@ -2484,9 +2555,8 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
result = type_func(arg_string)
# ArgumentTypeErrors indicate errors
except ArgumentTypeError:
name = getattr(action.type, '__name__', repr(action.type))
msg = str(_sys.exc_info()[1])
except ArgumentTypeError as err:
msg = str(err)
raise ArgumentError(action, msg)
# TypeErrors or ValueErrors also indicate errors
@@ -2557,9 +2627,11 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
def _print_message(self, message, file=None):
if message:
if file is None:
file = _sys.stderr
file.write(message)
file = file or _sys.stderr
try:
file.write(message)
except (AttributeError, OSError):
pass
# ===============
# Exiting methods

362
Lib/ast.py vendored
View File

@@ -25,9 +25,10 @@
:license: Python License.
"""
import sys
import re
from _ast import *
from contextlib import contextmanager, nullcontext
from enum import IntEnum, auto
from enum import IntEnum, auto, _simple_enum
def parse(source, filename='<unknown>', mode='exec', *,
@@ -40,12 +41,13 @@ def parse(source, filename='<unknown>', mode='exec', *,
flags = PyCF_ONLY_AST
if type_comments:
flags |= PyCF_TYPE_COMMENTS
if isinstance(feature_version, tuple):
major, minor = feature_version # Should be a 2-tuple.
assert major == 3
feature_version = minor
elif feature_version is None:
if feature_version is None:
feature_version = -1
elif isinstance(feature_version, tuple):
major, minor = feature_version # Should be a 2-tuple.
if major != 3:
raise ValueError(f"Unsupported major version: {major}")
feature_version = minor
# Else it should be an int giving the minor version for 3.x.
return compile(source, filename, mode, flags,
_feature_version=feature_version)
@@ -53,10 +55,12 @@ def parse(source, filename='<unknown>', mode='exec', *,
def literal_eval(node_or_string):
"""
Safely evaluate an expression node or a string containing a Python
Evaluate an expression node or a string containing only a Python
expression. The string or node provided may only consist of the following
Python literal structures: strings, bytes, numbers, tuples, lists, dicts,
sets, booleans, and None.
Caution: A complex expression can overflow the C stack and cause a crash.
"""
if isinstance(node_or_string, str):
node_or_string = parse(node_or_string.lstrip(" \t"), mode='eval')
@@ -234,6 +238,12 @@ def increment_lineno(node, n=1):
location in a file.
"""
for child in walk(node):
# TypeIgnore is a special case where lineno is not an attribute
# but rather a field of the node itself.
if isinstance(child, TypeIgnore):
child.lineno = getattr(child, 'lineno', 0) + n
continue
if 'lineno' in child._attributes:
child.lineno = getattr(child, 'lineno', 0) + n
if (
@@ -284,9 +294,7 @@ def get_docstring(node, clean=True):
if not(node.body and isinstance(node.body[0], Expr)):
return None
node = node.body[0].value
if isinstance(node, Str):
text = node.s
elif isinstance(node, Constant) and isinstance(node.value, str):
if isinstance(node, Constant) and isinstance(node.value, str):
text = node.value
else:
return None
@@ -296,28 +304,17 @@ def get_docstring(node, clean=True):
return text
def _splitlines_no_ff(source):
_line_pattern = re.compile(r"(.*?(?:\r\n|\n|\r|$))")
def _splitlines_no_ff(source, maxlines=None):
"""Split a string into lines ignoring form feed and other chars.
This mimics how the Python parser splits source code.
"""
idx = 0
lines = []
next_line = ''
while idx < len(source):
c = source[idx]
next_line += c
idx += 1
# Keep \r\n together
if c == '\r' and idx < len(source) and source[idx] == '\n':
next_line += '\n'
idx += 1
if c in '\r\n':
lines.append(next_line)
next_line = ''
if next_line:
lines.append(next_line)
for lineno, match in enumerate(_line_pattern.finditer(source), 1):
if maxlines is not None and lineno > maxlines:
break
lines.append(match[0])
return lines
@@ -351,7 +348,7 @@ def get_source_segment(source, node, *, padded=False):
except AttributeError:
return None
lines = _splitlines_no_ff(source)
lines = _splitlines_no_ff(source, maxlines=end_lineno+1)
if end_lineno == lineno:
return lines[lineno].encode()[col_offset:end_col_offset].decode()
@@ -500,20 +497,52 @@ class NodeTransformer(NodeVisitor):
return node
_DEPRECATED_VALUE_ALIAS_MESSAGE = (
"{name} is deprecated and will be removed in Python {remove}; use value instead"
)
_DEPRECATED_CLASS_MESSAGE = (
"{name} is deprecated and will be removed in Python {remove}; "
"use ast.Constant instead"
)
# If the ast module is loaded more than once, only add deprecated methods once
if not hasattr(Constant, 'n'):
# The following code is for backward compatibility.
# It will be removed in future.
def _getter(self):
def _n_getter(self):
"""Deprecated. Use value instead."""
import warnings
warnings._deprecated(
"Attribute n", message=_DEPRECATED_VALUE_ALIAS_MESSAGE, remove=(3, 14)
)
return self.value
def _setter(self, value):
def _n_setter(self, value):
import warnings
warnings._deprecated(
"Attribute n", message=_DEPRECATED_VALUE_ALIAS_MESSAGE, remove=(3, 14)
)
self.value = value
Constant.n = property(_getter, _setter)
Constant.s = property(_getter, _setter)
def _s_getter(self):
"""Deprecated. Use value instead."""
import warnings
warnings._deprecated(
"Attribute s", message=_DEPRECATED_VALUE_ALIAS_MESSAGE, remove=(3, 14)
)
return self.value
def _s_setter(self, value):
import warnings
warnings._deprecated(
"Attribute s", message=_DEPRECATED_VALUE_ALIAS_MESSAGE, remove=(3, 14)
)
self.value = value
Constant.n = property(_n_getter, _n_setter)
Constant.s = property(_s_getter, _s_setter)
class _ABC(type):
@@ -521,6 +550,13 @@ class _ABC(type):
cls.__doc__ = """Deprecated AST node class. Use ast.Constant instead"""
def __instancecheck__(cls, inst):
if cls in _const_types:
import warnings
warnings._deprecated(
f"ast.{cls.__qualname__}",
message=_DEPRECATED_CLASS_MESSAGE,
remove=(3, 14)
)
if not isinstance(inst, Constant):
return False
if cls in _const_types:
@@ -544,6 +580,10 @@ def _new(cls, *args, **kwargs):
if pos < len(args):
raise TypeError(f"{cls.__name__} got multiple values for argument {key!r}")
if cls in _const_types:
import warnings
warnings._deprecated(
f"ast.{cls.__qualname__}", message=_DEPRECATED_CLASS_MESSAGE, remove=(3, 14)
)
return Constant(*args, **kwargs)
return Constant.__new__(cls, *args, **kwargs)
@@ -566,10 +606,19 @@ class Ellipsis(Constant, metaclass=_ABC):
_fields = ()
def __new__(cls, *args, **kwargs):
if cls is Ellipsis:
if cls is _ast_Ellipsis:
import warnings
warnings._deprecated(
"ast.Ellipsis", message=_DEPRECATED_CLASS_MESSAGE, remove=(3, 14)
)
return Constant(..., *args, **kwargs)
return Constant.__new__(cls, *args, **kwargs)
# Keep another reference to Ellipsis in the global namespace
# so it can be referenced in Ellipsis.__new__
# (The original "Ellipsis" name is removed from the global namespace later on)
_ast_Ellipsis = Ellipsis
_const_types = {
Num: (int, float, complex),
Str: (str,),
@@ -636,10 +685,12 @@ class Param(expr_context):
# We unparse those infinities to INFSTR.
_INFSTR = "1e" + repr(sys.float_info.max_10_exp + 1)
class _Precedence(IntEnum):
@_simple_enum(IntEnum)
class _Precedence:
"""Precedence table that originated from python grammar."""
TUPLE = auto()
NAMED_EXPR = auto() # <target> := <expr1>
TUPLE = auto() # <expr1>, <expr2>
YIELD = auto() # 'yield', 'yield from'
TEST = auto() # 'if'-'else', 'lambda'
OR = auto() # 'or'
@@ -677,11 +728,11 @@ class _Unparser(NodeVisitor):
def __init__(self, *, _avoid_backslashes=False):
self._source = []
self._buffer = []
self._precedences = {}
self._type_ignores = {}
self._indent = 0
self._avoid_backslashes = _avoid_backslashes
self._in_try_star = False
def interleave(self, inter, f, seq):
"""Call f on each item in seq, calling inter() in between."""
@@ -716,18 +767,19 @@ class _Unparser(NodeVisitor):
self.maybe_newline()
self.write(" " * self._indent + text)
def write(self, text):
"""Append a piece of text"""
self._source.append(text)
def write(self, *text):
"""Add new source parts"""
self._source.extend(text)
def buffer_writer(self, text):
self._buffer.append(text)
@contextmanager
def buffered(self, buffer = None):
if buffer is None:
buffer = []
@property
def buffer(self):
value = "".join(self._buffer)
self._buffer.clear()
return value
original_source = self._source
self._source = buffer
yield buffer
self._source = original_source
@contextmanager
def block(self, *, extra = None):
@@ -837,7 +889,7 @@ class _Unparser(NodeVisitor):
self.traverse(node.value)
def visit_NamedExpr(self, node):
with self.require_parens(_Precedence.TUPLE, node):
with self.require_parens(_Precedence.NAMED_EXPR, node):
self.set_precedence(_Precedence.ATOM, node.target, node.value)
self.traverse(node.target)
self.write(" := ")
@@ -849,7 +901,7 @@ class _Unparser(NodeVisitor):
def visit_ImportFrom(self, node):
self.fill("from ")
self.write("." * node.level)
self.write("." * (node.level or 0))
if node.module:
self.write(node.module)
self.write(" import ")
@@ -858,6 +910,7 @@ class _Unparser(NodeVisitor):
def visit_Assign(self, node):
self.fill()
for target in node.targets:
self.set_precedence(_Precedence.TUPLE, target)
self.traverse(target)
self.write(" = ")
self.traverse(node.value)
@@ -950,7 +1003,7 @@ class _Unparser(NodeVisitor):
self.write(" from ")
self.traverse(node.cause)
def visit_Try(self, node):
def do_visit_try(self, node):
self.fill("try")
with self.block():
self.traverse(node.body)
@@ -965,8 +1018,24 @@ class _Unparser(NodeVisitor):
with self.block():
self.traverse(node.finalbody)
def visit_Try(self, node):
prev_in_try_star = self._in_try_star
try:
self._in_try_star = False
self.do_visit_try(node)
finally:
self._in_try_star = prev_in_try_star
def visit_TryStar(self, node):
prev_in_try_star = self._in_try_star
try:
self._in_try_star = True
self.do_visit_try(node)
finally:
self._in_try_star = prev_in_try_star
def visit_ExceptHandler(self, node):
self.fill("except")
self.fill("except*" if self._in_try_star else "except")
if node.type:
self.write(" ")
self.traverse(node.type)
@@ -982,6 +1051,8 @@ class _Unparser(NodeVisitor):
self.fill("@")
self.traverse(deco)
self.fill("class " + node.name)
if hasattr(node, "type_params"):
self._type_params_helper(node.type_params)
with self.delimit_if("(", ")", condition = node.bases or node.keywords):
comma = False
for e in node.bases:
@@ -1013,6 +1084,8 @@ class _Unparser(NodeVisitor):
self.traverse(deco)
def_str = fill_suffix + " " + node.name
self.fill(def_str)
if hasattr(node, "type_params"):
self._type_params_helper(node.type_params)
with self.delimit("(", ")"):
self.traverse(node.args)
if node.returns:
@@ -1021,6 +1094,30 @@ class _Unparser(NodeVisitor):
with self.block(extra=self.get_type_comment(node)):
self._write_docstring_and_traverse_body(node)
def _type_params_helper(self, type_params):
if type_params is not None and len(type_params) > 0:
with self.delimit("[", "]"):
self.interleave(lambda: self.write(", "), self.traverse, type_params)
def visit_TypeVar(self, node):
self.write(node.name)
if node.bound:
self.write(": ")
self.traverse(node.bound)
def visit_TypeVarTuple(self, node):
self.write("*" + node.name)
def visit_ParamSpec(self, node):
self.write("**" + node.name)
def visit_TypeAlias(self, node):
self.fill("type ")
self.traverse(node.name)
self._type_params_helper(node.type_params)
self.write(" = ")
self.traverse(node.value)
def visit_For(self, node):
self._for_helper("for ", node)
@@ -1029,6 +1126,7 @@ class _Unparser(NodeVisitor):
def _for_helper(self, fill, node):
self.fill(fill)
self.set_precedence(_Precedence.TUPLE, node.target)
self.traverse(node.target)
self.write(" in ")
self.traverse(node.iter)
@@ -1125,71 +1223,81 @@ class _Unparser(NodeVisitor):
def visit_JoinedStr(self, node):
self.write("f")
if self._avoid_backslashes:
self._fstring_JoinedStr(node, self.buffer_writer)
self._write_str_avoiding_backslashes(self.buffer)
return
# If we don't need to avoid backslashes globally (i.e., we only need
# to avoid them inside FormattedValues), it's cosmetically preferred
# to use escaped whitespace. That is, it's preferred to use backslashes
# for cases like: f"{x}\n". To accomplish this, we keep track of what
# in our buffer corresponds to FormattedValues and what corresponds to
# Constant parts of the f-string, and allow escapes accordingly.
buffer = []
fstring_parts = []
for value in node.values:
meth = getattr(self, "_fstring_" + type(value).__name__)
meth(value, self.buffer_writer)
buffer.append((self.buffer, isinstance(value, Constant)))
new_buffer = []
quote_types = _ALL_QUOTES
for value, is_constant in buffer:
# Repeatedly narrow down the list of possible quote_types
value, quote_types = self._str_literal_helper(
value, quote_types=quote_types,
escape_special_whitespace=is_constant
with self.buffered() as buffer:
self._write_fstring_inner(value)
fstring_parts.append(
("".join(buffer), isinstance(value, Constant))
)
new_buffer.append(value)
value = "".join(new_buffer)
new_fstring_parts = []
quote_types = list(_ALL_QUOTES)
fallback_to_repr = False
for value, is_constant in fstring_parts:
if is_constant:
value, new_quote_types = self._str_literal_helper(
value,
quote_types=quote_types,
escape_special_whitespace=True,
)
if set(new_quote_types).isdisjoint(quote_types):
fallback_to_repr = True
break
quote_types = new_quote_types
elif "\n" in value:
quote_types = [q for q in quote_types if q in _MULTI_QUOTES]
assert quote_types
new_fstring_parts.append(value)
if fallback_to_repr:
# If we weren't able to find a quote type that works for all parts
# of the JoinedStr, fallback to using repr and triple single quotes.
quote_types = ["'''"]
new_fstring_parts.clear()
for value, is_constant in fstring_parts:
if is_constant:
value = repr('"' + value) # force repr to use single quotes
expected_prefix = "'\""
assert value.startswith(expected_prefix), repr(value)
value = value[len(expected_prefix):-1]
new_fstring_parts.append(value)
value = "".join(new_fstring_parts)
quote_type = quote_types[0]
self.write(f"{quote_type}{value}{quote_type}")
def _write_fstring_inner(self, node):
if isinstance(node, JoinedStr):
# for both the f-string itself, and format_spec
for value in node.values:
self._write_fstring_inner(value)
elif isinstance(node, Constant) and isinstance(node.value, str):
value = node.value.replace("{", "{{").replace("}", "}}")
self.write(value)
elif isinstance(node, FormattedValue):
self.visit_FormattedValue(node)
else:
raise ValueError(f"Unexpected node inside JoinedStr, {node!r}")
def visit_FormattedValue(self, node):
self.write("f")
self._fstring_FormattedValue(node, self.buffer_writer)
self._write_str_avoiding_backslashes(self.buffer)
def unparse_inner(inner):
unparser = type(self)()
unparser.set_precedence(_Precedence.TEST.next(), inner)
return unparser.visit(inner)
def _fstring_JoinedStr(self, node, write):
for value in node.values:
meth = getattr(self, "_fstring_" + type(value).__name__)
meth(value, write)
def _fstring_Constant(self, node, write):
if not isinstance(node.value, str):
raise ValueError("Constants inside JoinedStr should be a string.")
value = node.value.replace("{", "{{").replace("}", "}}")
write(value)
def _fstring_FormattedValue(self, node, write):
write("{")
unparser = type(self)(_avoid_backslashes=True)
unparser.set_precedence(_Precedence.TEST.next(), node.value)
expr = unparser.visit(node.value)
if expr.startswith("{"):
write(" ") # Separate pair of opening brackets as "{ {"
if "\\" in expr:
raise ValueError("Unable to avoid backslash in f-string expression part")
write(expr)
if node.conversion != -1:
conversion = chr(node.conversion)
if conversion not in "sra":
raise ValueError("Unknown f-string conversion.")
write(f"!{conversion}")
if node.format_spec:
write(":")
meth = getattr(self, "_fstring_" + type(node.format_spec).__name__)
meth(node.format_spec, write)
write("}")
with self.delimit("{", "}"):
expr = unparse_inner(node.value)
if expr.startswith("{"):
# Separate pair of opening brackets as "{ {"
self.write(" ")
self.write(expr)
if node.conversion != -1:
self.write(f"!{chr(node.conversion)}")
if node.format_spec:
self.write(":")
self._write_fstring_inner(node.format_spec)
def visit_Name(self, node):
self.write(node.id)
@@ -1312,7 +1420,11 @@ class _Unparser(NodeVisitor):
)
def visit_Tuple(self, node):
with self.delimit("(", ")"):
with self.delimit_if(
"(",
")",
len(node.elts) == 0 or self.get_precedence(node) > _Precedence.TUPLE
):
self.items_view(self.traverse, node.elts)
unop = {"Invert": "~", "Not": "not", "UAdd": "+", "USub": "-"}
@@ -1328,7 +1440,7 @@ class _Unparser(NodeVisitor):
operator_precedence = self.unop_precedence[operator]
with self.require_parens(operator_precedence, node):
self.write(operator)
# factor prefixes (+, -, ~) shouldn't be seperated
# factor prefixes (+, -, ~) shouldn't be separated
# from the value they belong, (e.g: +1 instead of + 1)
if operator_precedence is not _Precedence.FACTOR:
self.write(" ")
@@ -1453,20 +1565,17 @@ class _Unparser(NodeVisitor):
self.traverse(e)
def visit_Subscript(self, node):
def is_simple_tuple(slice_value):
# when unparsing a non-empty tuple, the parentheses can be safely
# omitted if there aren't any elements that explicitly requires
# parentheses (such as starred expressions).
def is_non_empty_tuple(slice_value):
return (
isinstance(slice_value, Tuple)
and slice_value.elts
and not any(isinstance(elt, Starred) for elt in slice_value.elts)
)
self.set_precedence(_Precedence.ATOM, node.value)
self.traverse(node.value)
with self.delimit("[", "]"):
if is_simple_tuple(node.slice):
if is_non_empty_tuple(node.slice):
# parentheses can be omitted if the tuple isn't empty
self.items_view(self.traverse, node.slice.elts)
else:
self.traverse(node.slice)
@@ -1563,8 +1672,11 @@ class _Unparser(NodeVisitor):
def visit_Lambda(self, node):
with self.require_parens(_Precedence.TEST, node):
self.write("lambda ")
self.traverse(node.args)
self.write("lambda")
with self.buffered() as buffer:
self.traverse(node.args)
if buffer:
self.write(" ", *buffer)
self.write(": ")
self.set_precedence(_Precedence.TEST, node.body)
self.traverse(node.body)
@@ -1673,6 +1785,22 @@ def unparse(ast_obj):
return unparser.visit(ast_obj)
_deprecated_globals = {
name: globals().pop(name)
for name in ('Num', 'Str', 'Bytes', 'NameConstant', 'Ellipsis')
}
def __getattr__(name):
if name in _deprecated_globals:
globals()[name] = value = _deprecated_globals[name]
import warnings
warnings._deprecated(
f"ast.{name}", message=_DEPRECATED_CLASS_MESSAGE, remove=(3, 14)
)
return value
raise AttributeError(f"module 'ast' has no attribute '{name}'")
def main():
import argparse

View File

@@ -1,22 +1,14 @@
"""The asyncio package, tracking PEP 3156."""
# flake8: noqa
import sys
import selectors
# XXX RustPython TODO: _overlapped
if sys.platform == 'win32' and False:
# Similar thing for _overlapped.
try:
from . import _overlapped
except ImportError:
import _overlapped # Will also be exported.
# This relies on each of the submodules having an __all__ variable.
from .base_events import *
from .coroutines import *
from .events import *
from .exceptions import *
from .futures import *
from .locks import *
from .protocols import *
@@ -25,11 +17,15 @@ from .queues import *
from .streams import *
from .subprocess import *
from .tasks import *
from .taskgroups import *
from .timeouts import *
from .threads import *
from .transports import *
__all__ = (base_events.__all__ +
coroutines.__all__ +
events.__all__ +
exceptions.__all__ +
futures.__all__ +
locks.__all__ +
protocols.__all__ +
@@ -38,6 +34,9 @@ __all__ = (base_events.__all__ +
streams.__all__ +
subprocess.__all__ +
tasks.__all__ +
taskgroups.__all__ +
threads.__all__ +
timeouts.__all__ +
transports.__all__)
if sys.platform == 'win32': # pragma: no cover

125
Lib/asyncio/__main__.py vendored Normal file
View File

@@ -0,0 +1,125 @@
import ast
import asyncio
import code
import concurrent.futures
import inspect
import sys
import threading
import types
import warnings
from . import futures
class AsyncIOInteractiveConsole(code.InteractiveConsole):
def __init__(self, locals, loop):
super().__init__(locals)
self.compile.compiler.flags |= ast.PyCF_ALLOW_TOP_LEVEL_AWAIT
self.loop = loop
def runcode(self, code):
future = concurrent.futures.Future()
def callback():
global repl_future
global repl_future_interrupted
repl_future = None
repl_future_interrupted = False
func = types.FunctionType(code, self.locals)
try:
coro = func()
except SystemExit:
raise
except KeyboardInterrupt as ex:
repl_future_interrupted = True
future.set_exception(ex)
return
except BaseException as ex:
future.set_exception(ex)
return
if not inspect.iscoroutine(coro):
future.set_result(coro)
return
try:
repl_future = self.loop.create_task(coro)
futures._chain_future(repl_future, future)
except BaseException as exc:
future.set_exception(exc)
loop.call_soon_threadsafe(callback)
try:
return future.result()
except SystemExit:
raise
except BaseException:
if repl_future_interrupted:
self.write("\nKeyboardInterrupt\n")
else:
self.showtraceback()
class REPLThread(threading.Thread):
def run(self):
try:
banner = (
f'asyncio REPL {sys.version} on {sys.platform}\n'
f'Use "await" directly instead of "asyncio.run()".\n'
f'Type "help", "copyright", "credits" or "license" '
f'for more information.\n'
f'{getattr(sys, "ps1", ">>> ")}import asyncio'
)
console.interact(
banner=banner,
exitmsg='exiting asyncio REPL...')
finally:
warnings.filterwarnings(
'ignore',
message=r'^coroutine .* was never awaited$',
category=RuntimeWarning)
loop.call_soon_threadsafe(loop.stop)
if __name__ == '__main__':
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
repl_locals = {'asyncio': asyncio}
for key in {'__name__', '__package__',
'__loader__', '__spec__',
'__builtins__', '__file__'}:
repl_locals[key] = locals()[key]
console = AsyncIOInteractiveConsole(repl_locals, loop)
repl_future = None
repl_future_interrupted = False
try:
import readline # NoQA
except ImportError:
pass
repl_thread = REPLThread()
repl_thread.daemon = True
repl_thread.start()
while True:
try:
loop.run_forever()
except KeyboardInterrupt:
if repl_future and not repl_future.done():
repl_future.cancel()
repl_future_interrupted = True
continue
else:
break

File diff suppressed because it is too large Load Diff

View File

@@ -1,18 +1,8 @@
__all__ = []
__all__ = ()
import concurrent.futures._base
import reprlib
from . import events
Error = concurrent.futures._base.Error
CancelledError = concurrent.futures.CancelledError
TimeoutError = concurrent.futures.TimeoutError
class InvalidStateError(Error):
"""The operation is not allowed in this state."""
from . import format_helpers
# States for Future.
_PENDING = 'PENDING'
@@ -38,17 +28,17 @@ def _format_callbacks(cb):
cb = ''
def format_cb(callback):
return events._format_callback_source(callback, ())
return format_helpers._format_callback_source(callback, ())
if size == 1:
cb = format_cb(cb[0])
cb = format_cb(cb[0][0])
elif size == 2:
cb = '{}, {}'.format(format_cb(cb[0]), format_cb(cb[1]))
cb = '{}, {}'.format(format_cb(cb[0][0]), format_cb(cb[1][0]))
elif size > 2:
cb = '{}, <{} more>, {}'.format(format_cb(cb[0]),
cb = '{}, <{} more>, {}'.format(format_cb(cb[0][0]),
size - 2,
format_cb(cb[-1]))
return 'cb=[%s]' % cb
format_cb(cb[-1][0]))
return f'cb=[{cb}]'
def _future_repr_info(future):
@@ -57,15 +47,21 @@ def _future_repr_info(future):
info = [future._state.lower()]
if future._state == _FINISHED:
if future._exception is not None:
info.append('exception={!r}'.format(future._exception))
info.append(f'exception={future._exception!r}')
else:
# use reprlib to limit the length of the output, especially
# for very long strings
result = reprlib.repr(future._result)
info.append('result={}'.format(result))
info.append(f'result={result}')
if future._callbacks:
info.append(_format_callbacks(future._callbacks))
if future._source_traceback:
frame = future._source_traceback[-1]
info.append('created at %s:%s' % (frame[0], frame[1]))
info.append(f'created at {frame[0]}:{frame[1]}')
return info
@reprlib.recursive_repr()
def _future_repr(future):
info = ' '.join(_future_repr_info(future))
return f'<{future.__class__.__name__} {info}>'

View File

@@ -2,10 +2,8 @@ import collections
import subprocess
import warnings
from . import compat
from . import protocols
from . import transports
from .coroutines import coroutine
from .log import logger
@@ -59,9 +57,9 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
if self._closed:
info.append('closed')
if self._pid is not None:
info.append('pid=%s' % self._pid)
info.append(f'pid={self._pid}')
if self._returncode is not None:
info.append('returncode=%s' % self._returncode)
info.append(f'returncode={self._returncode}')
elif self._pid is not None:
info.append('running')
else:
@@ -69,19 +67,19 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
stdin = self._pipes.get(0)
if stdin is not None:
info.append('stdin=%s' % stdin.pipe)
info.append(f'stdin={stdin.pipe}')
stdout = self._pipes.get(1)
stderr = self._pipes.get(2)
if stdout is not None and stderr is stdout:
info.append('stdout=stderr=%s' % stdout.pipe)
info.append(f'stdout=stderr={stdout.pipe}')
else:
if stdout is not None:
info.append('stdout=%s' % stdout.pipe)
info.append(f'stdout={stdout.pipe}')
if stderr is not None:
info.append('stderr=%s' % stderr.pipe)
info.append(f'stderr={stderr.pipe}')
return '<%s>' % ' '.join(info)
return '<{}>'.format(' '.join(info))
def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs):
raise NotImplementedError
@@ -105,12 +103,13 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
continue
proto.pipe.close()
if (self._proc is not None
# the child process finished?
and self._returncode is None
# the child process finished but the transport was not notified yet?
and self._proc.poll() is None
):
if (self._proc is not None and
# has the child process finished?
self._returncode is None and
# the child process has finished, but the
# transport hasn't been notified yet?
self._proc.poll() is None):
if self._loop.get_debug():
logger.warning('Close running child process: kill %r', self)
@@ -121,15 +120,10 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
# Don't clear the _proc reference yet: _post_init() may still run
# On Python 3.3 and older, objects with a destructor part of a reference
# cycle are never destroyed. It's not more the case on Python 3.4 thanks
# to the PEP 442.
if compat.PY34:
def __del__(self):
if not self._closed:
warnings.warn("unclosed transport %r" % self, ResourceWarning,
source=self)
self.close()
def __del__(self, _warn=warnings.warn):
if not self._closed:
_warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
self.close()
def get_pid(self):
return self._pid
@@ -159,26 +153,25 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
self._check_proc()
self._proc.kill()
@coroutine
def _connect_pipes(self, waiter):
async def _connect_pipes(self, waiter):
try:
proc = self._proc
loop = self._loop
if proc.stdin is not None:
_, pipe = yield from loop.connect_write_pipe(
_, pipe = await loop.connect_write_pipe(
lambda: WriteSubprocessPipeProto(self, 0),
proc.stdin)
self._pipes[0] = pipe
if proc.stdout is not None:
_, pipe = yield from loop.connect_read_pipe(
_, pipe = await loop.connect_read_pipe(
lambda: ReadSubprocessPipeProto(self, 1),
proc.stdout)
self._pipes[1] = pipe
if proc.stderr is not None:
_, pipe = yield from loop.connect_read_pipe(
_, pipe = await loop.connect_read_pipe(
lambda: ReadSubprocessPipeProto(self, 2),
proc.stderr)
self._pipes[2] = pipe
@@ -189,7 +182,9 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
for callback, data in self._pending_calls:
loop.call_soon(callback, *data)
self._pending_calls = None
except Exception as exc:
except (SystemExit, KeyboardInterrupt):
raise
except BaseException as exc:
if waiter is not None and not waiter.cancelled():
waiter.set_exception(exc)
else:
@@ -213,24 +208,17 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
assert returncode is not None, returncode
assert self._returncode is None, self._returncode
if self._loop.get_debug():
logger.info('%r exited with return code %r',
self, returncode)
logger.info('%r exited with return code %r', self, returncode)
self._returncode = returncode
if self._proc.returncode is None:
# asyncio uses a child watcher: copy the status into the Popen
# object. On Python 3.6, it is required to avoid a ResourceWarning.
self._proc.returncode = returncode
self._call(self._protocol.process_exited)
self._try_finish()
# wake up futures waiting for wait()
for waiter in self._exit_waiters:
if not waiter.cancelled():
waiter.set_result(returncode)
self._exit_waiters = None
@coroutine
def _wait(self):
async def _wait(self):
"""Wait until the process exit and return the process return code.
This method is a coroutine."""
@@ -239,7 +227,7 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
waiter = self._loop.create_future()
self._exit_waiters.append(waiter)
return (yield from waiter)
return await waiter
def _try_finish(self):
assert not self._finished
@@ -254,6 +242,11 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
try:
self._protocol.connection_lost(exc)
finally:
# wake up futures waiting for wait()
for waiter in self._exit_waiters:
if not waiter.cancelled():
waiter.set_result(self._returncode)
self._exit_waiters = None
self._loop = None
self._proc = None
self._protocol = None
@@ -271,8 +264,7 @@ class WriteSubprocessPipeProto(protocols.BaseProtocol):
self.pipe = transport
def __repr__(self):
return ('<%s fd=%s pipe=%r>'
% (self.__class__.__name__, self.fd, self.pipe))
return f'<{self.__class__.__name__} fd={self.fd} pipe={self.pipe!r}>'
def connection_lost(self, exc):
self.disconnected = True

View File

@@ -1,4 +1,5 @@
import linecache
import reprlib
import traceback
from . import base_futures
@@ -8,25 +9,42 @@ from . import coroutines
def _task_repr_info(task):
info = base_futures._future_repr_info(task)
if task._must_cancel:
if task.cancelling() and not task.done():
# replace status
info[0] = 'cancelling'
coro = coroutines._format_coroutine(task._coro)
info.insert(1, 'coro=<%s>' % coro)
info.insert(1, 'name=%r' % task.get_name())
if task._fut_waiter is not None:
info.insert(2, 'wait_for=%r' % task._fut_waiter)
info.insert(2, f'wait_for={task._fut_waiter!r}')
if task._coro:
coro = coroutines._format_coroutine(task._coro)
info.insert(2, f'coro=<{coro}>')
return info
@reprlib.recursive_repr()
def _task_repr(task):
info = ' '.join(_task_repr_info(task))
return f'<{task.__class__.__name__} {info}>'
def _task_get_stack(task, limit):
frames = []
try:
# 'async def' coroutines
if hasattr(task._coro, 'cr_frame'):
# case 1: 'async def' coroutines
f = task._coro.cr_frame
except AttributeError:
elif hasattr(task._coro, 'gi_frame'):
# case 2: legacy coroutines
f = task._coro.gi_frame
elif hasattr(task._coro, 'ag_frame'):
# case 3: async generators
f = task._coro.ag_frame
else:
# case 4: unknown objects
f = None
if f is not None:
while f is not None:
if limit is not None:
@@ -61,15 +79,15 @@ def _task_print_stack(task, limit, file):
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
extracted_list.append((filename, lineno, name, line))
exc = task._exception
if not extracted_list:
print('No stack for %r' % task, file=file)
print(f'No stack for {task!r}', file=file)
elif exc is not None:
print('Traceback for %r (most recent call last):' % task,
file=file)
print(f'Traceback for {task!r} (most recent call last):', file=file)
else:
print('Stack for %r (most recent call last):' % task,
file=file)
print(f'Stack for {task!r} (most recent call last):', file=file)
traceback.print_list(extracted_list, file=file)
if exc is not None:
for line in traceback.format_exception_only(exc.__class__, exc):

18
Lib/asyncio/compat.py vendored
View File

@@ -1,18 +0,0 @@
"""Compatibility helpers for the different Python versions."""
import sys
PY34 = sys.version_info >= (3, 4)
PY35 = sys.version_info >= (3, 5)
PY352 = sys.version_info >= (3, 5, 2)
def flatten_list_bytes(list_of_data):
"""Concatenate a sequence of bytes-like objects."""
if not PY34:
# On Python 3.3 and older, bytes.join() doesn't handle
# memoryview.
list_of_data = (
bytes(data) if isinstance(data, memoryview) else data
for data in list_of_data)
return b''.join(list_of_data)

View File

@@ -1,7 +1,41 @@
"""Constants."""
# Contains code from https://github.com/MagicStack/uvloop/tree/v0.16.0
# SPDX-License-Identifier: PSF-2.0 AND (MIT OR Apache-2.0)
# SPDX-FileCopyrightText: Copyright (c) 2015-2021 MagicStack Inc. http://magic.io
import enum
# After the connection is lost, log warnings after this many write()s.
LOG_THRESHOLD_FOR_CONNLOST_WRITES = 5
# Seconds to wait before retrying accept().
ACCEPT_RETRY_DELAY = 1
# Number of stack entries to capture in debug mode.
# The larger the number, the slower the operation in debug mode
# (see extract_stack() in format_helpers.py).
DEBUG_STACK_DEPTH = 10
# Number of seconds to wait for SSL handshake to complete
# The default timeout matches that of Nginx.
SSL_HANDSHAKE_TIMEOUT = 60.0
# Number of seconds to wait for SSL shutdown to complete
# The default timeout mimics lingering_time
SSL_SHUTDOWN_TIMEOUT = 30.0
# Used in sendfile fallback code. We use fallback for platforms
# that don't support sendfile, or for TLS connections.
SENDFILE_FALLBACK_READBUFFER_SIZE = 1024 * 256
FLOW_CONTROL_HIGH_WATER_SSL_READ = 256 # KiB
FLOW_CONTROL_HIGH_WATER_SSL_WRITE = 512 # KiB
# Default timeout for joining the threads in the threadpool
THREAD_JOIN_TIMEOUT = 300
# The enum should be here to break circular dependencies between
# base_events and sslproto
class _SendfileMode(enum.Enum):
UNSUPPORTED = enum.auto()
TRY_NATIVE = enum.auto()
FALLBACK = enum.auto()

View File

@@ -1,249 +1,16 @@
__all__ = ['coroutine',
'iscoroutinefunction', 'iscoroutine']
__all__ = 'iscoroutinefunction', 'iscoroutine'
import functools
import collections.abc
import inspect
import opcode
import os
import sys
import traceback
import types
from . import compat
from . import events
from . import base_futures
from .log import logger
# Opcode of "yield from" instruction
_YIELD_FROM = opcode.opmap['YIELD_FROM']
# If you set _DEBUG to true, @coroutine will wrap the resulting
# generator objects in a CoroWrapper instance (defined below). That
# instance will log a message when the generator is never iterated
# over, which may happen when you forget to use "yield from" with a
# coroutine call. Note that the value of the _DEBUG flag is taken
# when the decorator is used, so to be of any use it must be set
# before you define your coroutines. A downside of using this feature
# is that tracebacks show entries for the CoroWrapper.__next__ method
# when _DEBUG is true.
_DEBUG = (not sys.flags.ignore_environment and
bool(os.environ.get('PYTHONASYNCIODEBUG')))
try:
_types_coroutine = types.coroutine
_types_CoroutineType = types.CoroutineType
except AttributeError:
# Python 3.4
_types_coroutine = None
_types_CoroutineType = None
try:
_inspect_iscoroutinefunction = inspect.iscoroutinefunction
except AttributeError:
# Python 3.4
_inspect_iscoroutinefunction = lambda func: False
try:
from collections.abc import Coroutine as _CoroutineABC, \
Awaitable as _AwaitableABC
except ImportError:
_CoroutineABC = _AwaitableABC = None
# Check for CPython issue #21209
def has_yield_from_bug():
class MyGen:
def __init__(self):
self.send_args = None
def __iter__(self):
return self
def __next__(self):
return 42
def send(self, *what):
self.send_args = what
return None
def yield_from_gen(gen):
yield from gen
value = (1, 2, 3)
gen = MyGen()
coro = yield_from_gen(gen)
next(coro)
coro.send(value)
return gen.send_args != (value,)
_YIELD_FROM_BUG = has_yield_from_bug()
del has_yield_from_bug
def debug_wrapper(gen):
# This function is called from 'sys.set_coroutine_wrapper'.
# We only wrap here coroutines defined via 'async def' syntax.
# Generator-based coroutines are wrapped in @coroutine
# decorator.
return CoroWrapper(gen, None)
class CoroWrapper:
# Wrapper for coroutine object in _DEBUG mode.
def __init__(self, gen, func=None):
assert inspect.isgenerator(gen) or inspect.iscoroutine(gen), gen
self.gen = gen
self.func = func # Used to unwrap @coroutine decorator
self._source_traceback = traceback.extract_stack(sys._getframe(1))
self.__name__ = getattr(gen, '__name__', None)
self.__qualname__ = getattr(gen, '__qualname__', None)
def __repr__(self):
coro_repr = _format_coroutine(self)
if self._source_traceback:
frame = self._source_traceback[-1]
coro_repr += ', created at %s:%s' % (frame[0], frame[1])
return '<%s %s>' % (self.__class__.__name__, coro_repr)
def __iter__(self):
return self
def __next__(self):
return self.gen.send(None)
if _YIELD_FROM_BUG:
# For for CPython issue #21209: using "yield from" and a custom
# generator, generator.send(tuple) unpacks the tuple instead of passing
# the tuple unchanged. Check if the caller is a generator using "yield
# from" to decide if the parameter should be unpacked or not.
def send(self, *value):
frame = sys._getframe()
caller = frame.f_back
assert caller.f_lasti >= 0
if caller.f_code.co_code[caller.f_lasti] != _YIELD_FROM:
value = value[0]
return self.gen.send(value)
else:
def send(self, value):
return self.gen.send(value)
def throw(self, type, value=None, traceback=None):
return self.gen.throw(type, value, traceback)
def close(self):
return self.gen.close()
@property
def gi_frame(self):
return self.gen.gi_frame
@property
def gi_running(self):
return self.gen.gi_running
@property
def gi_code(self):
return self.gen.gi_code
if compat.PY35:
def __await__(self):
cr_await = getattr(self.gen, 'cr_await', None)
if cr_await is not None:
raise RuntimeError(
"Cannot await on coroutine {!r} while it's "
"awaiting for {!r}".format(self.gen, cr_await))
return self
@property
def gi_yieldfrom(self):
return self.gen.gi_yieldfrom
@property
def cr_await(self):
return self.gen.cr_await
@property
def cr_running(self):
return self.gen.cr_running
@property
def cr_code(self):
return self.gen.cr_code
@property
def cr_frame(self):
return self.gen.cr_frame
def __del__(self):
# Be careful accessing self.gen.frame -- self.gen might not exist.
gen = getattr(self, 'gen', None)
frame = getattr(gen, 'gi_frame', None)
if frame is None:
frame = getattr(gen, 'cr_frame', None)
if frame is not None and frame.f_lasti == -1:
msg = '%r was never yielded from' % self
tb = getattr(self, '_source_traceback', ())
if tb:
tb = ''.join(traceback.format_list(tb))
msg += ('\nCoroutine object created at '
'(most recent call last):\n')
msg += tb.rstrip()
logger.error(msg)
def coroutine(func):
"""Decorator to mark coroutines.
If the coroutine is not yielded from before it is destroyed,
an error message is logged.
"""
if _inspect_iscoroutinefunction(func):
# In Python 3.5 that's all we need to do for coroutines
# defiend with "async def".
# Wrapping in CoroWrapper will happen via
# 'sys.set_coroutine_wrapper' function.
return func
if inspect.isgeneratorfunction(func):
coro = func
else:
@functools.wraps(func)
def coro(*args, **kw):
res = func(*args, **kw)
if (base_futures.isfuture(res) or inspect.isgenerator(res) or
isinstance(res, CoroWrapper)):
res = yield from res
elif _AwaitableABC is not None:
# If 'func' returns an Awaitable (new in 3.5) we
# want to run it.
try:
await_meth = res.__await__
except AttributeError:
pass
else:
if isinstance(res, _AwaitableABC):
res = yield from await_meth()
return res
if not _DEBUG:
if _types_coroutine is None:
wrapper = coro
else:
wrapper = _types_coroutine(coro)
else:
@functools.wraps(func)
def wrapper(*args, **kwds):
w = CoroWrapper(coro(*args, **kwds), func=func)
if w._source_traceback:
del w._source_traceback[-1]
# Python < 3.5 does not implement __qualname__
# on generator objects, so we set it manually.
# We use getattr as some callables (such as
# functools.partial may lack __qualname__).
w.__name__ = getattr(func, '__name__', None)
w.__qualname__ = getattr(func, '__qualname__', None)
return w
wrapper._is_coroutine = _is_coroutine # For iscoroutinefunction().
return wrapper
def _is_debug_mode():
# See: https://docs.python.org/3/library/asyncio-dev.html#asyncio-debug-mode.
return sys.flags.dev_mode or (not sys.flags.ignore_environment and
bool(os.environ.get('PYTHONASYNCIODEBUG')))
# A marker for iscoroutinefunction.
@@ -252,93 +19,91 @@ _is_coroutine = object()
def iscoroutinefunction(func):
"""Return True if func is a decorated coroutine function."""
return (getattr(func, '_is_coroutine', None) is _is_coroutine or
_inspect_iscoroutinefunction(func))
return (inspect.iscoroutinefunction(func) or
getattr(func, '_is_coroutine', None) is _is_coroutine)
_COROUTINE_TYPES = (types.GeneratorType, CoroWrapper)
if _CoroutineABC is not None:
_COROUTINE_TYPES += (_CoroutineABC,)
if _types_CoroutineType is not None:
# Prioritize native coroutine check to speed-up
# asyncio.iscoroutine.
_COROUTINE_TYPES = (_types_CoroutineType,) + _COROUTINE_TYPES
# Prioritize native coroutine check to speed-up
# asyncio.iscoroutine.
_COROUTINE_TYPES = (types.CoroutineType, collections.abc.Coroutine)
_iscoroutine_typecache = set()
def iscoroutine(obj):
"""Return True if obj is a coroutine object."""
return isinstance(obj, _COROUTINE_TYPES)
if type(obj) in _iscoroutine_typecache:
return True
if isinstance(obj, _COROUTINE_TYPES):
# Just in case we don't want to cache more than 100
# positive types. That shouldn't ever happen, unless
# someone stressing the system on purpose.
if len(_iscoroutine_typecache) < 100:
_iscoroutine_typecache.add(type(obj))
return True
else:
return False
def _format_coroutine(coro):
assert iscoroutine(coro)
if not hasattr(coro, 'cr_code') and not hasattr(coro, 'gi_code'):
# Most likely a built-in type or a Cython coroutine.
def get_name(coro):
# Coroutines compiled with Cython sometimes don't have
# proper __qualname__ or __name__. While that is a bug
# in Cython, asyncio shouldn't crash with an AttributeError
# in its __repr__ functions.
if hasattr(coro, '__qualname__') and coro.__qualname__:
coro_name = coro.__qualname__
elif hasattr(coro, '__name__') and coro.__name__:
coro_name = coro.__name__
else:
# Stop masking Cython bugs, expose them in a friendly way.
coro_name = f'<{type(coro).__name__} without __name__>'
return f'{coro_name}()'
# Built-in types might not have __qualname__ or __name__.
coro_name = getattr(
coro, '__qualname__',
getattr(coro, '__name__', type(coro).__name__))
coro_name = '{}()'.format(coro_name)
running = False
def is_running(coro):
try:
running = coro.cr_running
return coro.cr_running
except AttributeError:
try:
running = coro.gi_running
return coro.gi_running
except AttributeError:
pass
return False
if running:
return '{} running'.format(coro_name)
coro_code = None
if hasattr(coro, 'cr_code') and coro.cr_code:
coro_code = coro.cr_code
elif hasattr(coro, 'gi_code') and coro.gi_code:
coro_code = coro.gi_code
coro_name = get_name(coro)
if not coro_code:
# Built-in types might not have __qualname__ or __name__.
if is_running(coro):
return f'{coro_name} running'
else:
return coro_name
coro_name = None
if isinstance(coro, CoroWrapper):
func = coro.func
coro_name = coro.__qualname__
if coro_name is not None:
coro_name = '{}()'.format(coro_name)
else:
func = coro
if coro_name is None:
coro_name = events._format_callback(func, (), {})
try:
coro_code = coro.gi_code
except AttributeError:
coro_code = coro.cr_code
try:
coro_frame = None
if hasattr(coro, 'gi_frame') and coro.gi_frame:
coro_frame = coro.gi_frame
except AttributeError:
elif hasattr(coro, 'cr_frame') and coro.cr_frame:
coro_frame = coro.cr_frame
filename = coro_code.co_filename
# If Cython's coroutine has a fake code object without proper
# co_filename -- expose that.
filename = coro_code.co_filename or '<empty co_filename>'
lineno = 0
if (isinstance(coro, CoroWrapper) and
not inspect.isgeneratorfunction(coro.func) and
coro.func is not None):
source = events._get_function_source(coro.func)
if source is not None:
filename, lineno = source
if coro_frame is None:
coro_repr = ('%s done, defined at %s:%s'
% (coro_name, filename, lineno))
else:
coro_repr = ('%s running, defined at %s:%s'
% (coro_name, filename, lineno))
elif coro_frame is not None:
if coro_frame is not None:
lineno = coro_frame.f_lineno
coro_repr = ('%s running at %s:%s'
% (coro_name, filename, lineno))
coro_repr = f'{coro_name} running at {filename}:{lineno}'
else:
lineno = coro_code.co_firstlineno
coro_repr = ('%s done, defined at %s:%s'
% (coro_name, filename, lineno))
coro_repr = f'{coro_name} done, defined at {filename}:{lineno}'
return coro_repr

469
Lib/asyncio/events.py vendored
View File

@@ -1,96 +1,50 @@
"""Event loop and event loop policy."""
__all__ = ['AbstractEventLoopPolicy',
'AbstractEventLoop', 'AbstractServer',
'Handle', 'TimerHandle',
'get_event_loop_policy', 'set_event_loop_policy',
'get_event_loop', 'set_event_loop', 'new_event_loop',
'get_child_watcher', 'set_child_watcher',
'_set_running_loop', 'get_running_loop',
'_get_running_loop',
]
# Contains code from https://github.com/MagicStack/uvloop/tree/v0.16.0
# SPDX-License-Identifier: PSF-2.0 AND (MIT OR Apache-2.0)
# SPDX-FileCopyrightText: Copyright (c) 2015-2021 MagicStack Inc. http://magic.io
import functools
import inspect
import reprlib
__all__ = (
'AbstractEventLoopPolicy',
'AbstractEventLoop', 'AbstractServer',
'Handle', 'TimerHandle',
'get_event_loop_policy', 'set_event_loop_policy',
'get_event_loop', 'set_event_loop', 'new_event_loop',
'get_child_watcher', 'set_child_watcher',
'_set_running_loop', 'get_running_loop',
'_get_running_loop',
)
import contextvars
import os
import signal
import socket
import subprocess
import sys
import threading
import traceback
from asyncio import compat
def _get_function_source(func):
if compat.PY34:
func = inspect.unwrap(func)
elif hasattr(func, '__wrapped__'):
func = func.__wrapped__
if inspect.isfunction(func):
code = func.__code__
return (code.co_filename, code.co_firstlineno)
if isinstance(func, functools.partial):
return _get_function_source(func.func)
if compat.PY34 and isinstance(func, functools.partialmethod):
return _get_function_source(func.func)
return None
def _format_args_and_kwargs(args, kwargs):
"""Format function arguments and keyword arguments.
Special case for a single parameter: ('hello',) is formatted as ('hello').
"""
# use reprlib to limit the length of the output
items = []
if args:
items.extend(reprlib.repr(arg) for arg in args)
if kwargs:
items.extend('{}={}'.format(k, reprlib.repr(v))
for k, v in kwargs.items())
return '(' + ', '.join(items) + ')'
def _format_callback(func, args, kwargs, suffix=''):
if isinstance(func, functools.partial):
suffix = _format_args_and_kwargs(args, kwargs) + suffix
return _format_callback(func.func, func.args, func.keywords, suffix)
if hasattr(func, '__qualname__'):
func_repr = getattr(func, '__qualname__')
elif hasattr(func, '__name__'):
func_repr = getattr(func, '__name__')
else:
func_repr = repr(func)
func_repr += _format_args_and_kwargs(args, kwargs)
if suffix:
func_repr += suffix
return func_repr
def _format_callback_source(func, args):
func_repr = _format_callback(func, args, None)
source = _get_function_source(func)
if source:
func_repr += ' at %s:%s' % source
return func_repr
from . import format_helpers
class Handle:
"""Object returned by callback registration methods."""
__slots__ = ('_callback', '_args', '_cancelled', '_loop',
'_source_traceback', '_repr', '__weakref__')
'_source_traceback', '_repr', '__weakref__',
'_context')
def __init__(self, callback, args, loop):
def __init__(self, callback, args, loop, context=None):
if context is None:
context = contextvars.copy_context()
self._context = context
self._loop = loop
self._callback = callback
self._args = args
self._cancelled = False
self._repr = None
if self._loop.get_debug():
self._source_traceback = traceback.extract_stack(sys._getframe(1))
self._source_traceback = format_helpers.extract_stack(
sys._getframe(1))
else:
self._source_traceback = None
@@ -99,17 +53,21 @@ class Handle:
if self._cancelled:
info.append('cancelled')
if self._callback is not None:
info.append(_format_callback_source(self._callback, self._args))
info.append(format_helpers._format_callback_source(
self._callback, self._args))
if self._source_traceback:
frame = self._source_traceback[-1]
info.append('created at %s:%s' % (frame[0], frame[1]))
info.append(f'created at {frame[0]}:{frame[1]}')
return info
def __repr__(self):
if self._repr is not None:
return self._repr
info = self._repr_info()
return '<%s>' % ' '.join(info)
return '<{}>'.format(' '.join(info))
def get_context(self):
return self._context
def cancel(self):
if not self._cancelled:
@@ -122,12 +80,18 @@ class Handle:
self._callback = None
self._args = None
def cancelled(self):
return self._cancelled
def _run(self):
try:
self._callback(*self._args)
except Exception as exc:
cb = _format_callback_source(self._callback, self._args)
msg = 'Exception in callback {}'.format(cb)
self._context.run(self._callback, *self._args)
except (SystemExit, KeyboardInterrupt):
raise
except BaseException as exc:
cb = format_helpers._format_callback_source(
self._callback, self._args)
msg = f'Exception in callback {cb}'
context = {
'message': msg,
'exception': exc,
@@ -144,9 +108,8 @@ class TimerHandle(Handle):
__slots__ = ['_scheduled', '_when']
def __init__(self, when, callback, args, loop):
assert when is not None
super().__init__(callback, args, loop)
def __init__(self, when, callback, args, loop, context=None):
super().__init__(callback, args, loop, context)
if self._source_traceback:
del self._source_traceback[-1]
self._when = when
@@ -155,27 +118,31 @@ class TimerHandle(Handle):
def _repr_info(self):
info = super()._repr_info()
pos = 2 if self._cancelled else 1
info.insert(pos, 'when=%s' % self._when)
info.insert(pos, f'when={self._when}')
return info
def __hash__(self):
return hash(self._when)
def __lt__(self, other):
return self._when < other._when
if isinstance(other, TimerHandle):
return self._when < other._when
return NotImplemented
def __le__(self, other):
if self._when < other._when:
return True
return self.__eq__(other)
if isinstance(other, TimerHandle):
return self._when < other._when or self.__eq__(other)
return NotImplemented
def __gt__(self, other):
return self._when > other._when
if isinstance(other, TimerHandle):
return self._when > other._when
return NotImplemented
def __ge__(self, other):
if self._when > other._when:
return True
return self.__eq__(other)
if isinstance(other, TimerHandle):
return self._when > other._when or self.__eq__(other)
return NotImplemented
def __eq__(self, other):
if isinstance(other, TimerHandle):
@@ -185,26 +152,60 @@ class TimerHandle(Handle):
self._cancelled == other._cancelled)
return NotImplemented
def __ne__(self, other):
equal = self.__eq__(other)
return NotImplemented if equal is NotImplemented else not equal
def cancel(self):
if not self._cancelled:
self._loop._timer_handle_cancelled(self)
super().cancel()
def when(self):
"""Return a scheduled callback time.
The time is an absolute timestamp, using the same time
reference as loop.time().
"""
return self._when
class AbstractServer:
"""Abstract server returned by create_server()."""
def close(self):
"""Stop serving. This leaves existing connections open."""
return NotImplemented
raise NotImplementedError
def wait_closed(self):
def get_loop(self):
"""Get the event loop the Server object is attached to."""
raise NotImplementedError
def is_serving(self):
"""Return True if the server is accepting connections."""
raise NotImplementedError
async def start_serving(self):
"""Start accepting connections.
This method is idempotent, so it can be called when
the server is already being serving.
"""
raise NotImplementedError
async def serve_forever(self):
"""Start accepting connections until the coroutine is cancelled.
The server is closed when the coroutine is cancelled.
"""
raise NotImplementedError
async def wait_closed(self):
"""Coroutine to wait until service is closed."""
return NotImplemented
raise NotImplementedError
async def __aenter__(self):
return self
async def __aexit__(self, *exc):
self.close()
await self.wait_closed()
class AbstractEventLoop:
@@ -250,23 +251,27 @@ class AbstractEventLoop:
"""
raise NotImplementedError
def shutdown_asyncgens(self):
async def shutdown_asyncgens(self):
"""Shutdown all active asynchronous generators."""
raise NotImplementedError
async def shutdown_default_executor(self):
"""Schedule the shutdown of the default executor."""
raise NotImplementedError
# Methods scheduling callbacks. All these return Handles.
def _timer_handle_cancelled(self, handle):
"""Notification that a TimerHandle has been cancelled."""
raise NotImplementedError
def call_soon(self, callback, *args):
return self.call_later(0, callback, *args)
def call_soon(self, callback, *args, context=None):
return self.call_later(0, callback, *args, context=context)
def call_later(self, delay, callback, *args):
def call_later(self, delay, callback, *args, context=None):
raise NotImplementedError
def call_at(self, when, callback, *args):
def call_at(self, when, callback, *args, context=None):
raise NotImplementedError
def time(self):
@@ -277,12 +282,12 @@ class AbstractEventLoop:
# Method scheduling a coroutine object: create a task.
def create_task(self, coro):
def create_task(self, coro, *, name=None, context=None):
raise NotImplementedError
# Methods for interacting with threads.
def call_soon_threadsafe(self, callback, *args):
def call_soon_threadsafe(self, callback, *args, context=None):
raise NotImplementedError
def run_in_executor(self, executor, func, *args):
@@ -293,21 +298,31 @@ class AbstractEventLoop:
# Network I/O methods returning Futures.
def getaddrinfo(self, host, port, *, family=0, type=0, proto=0, flags=0):
async def getaddrinfo(self, host, port, *,
family=0, type=0, proto=0, flags=0):
raise NotImplementedError
def getnameinfo(self, sockaddr, flags=0):
async def getnameinfo(self, sockaddr, flags=0):
raise NotImplementedError
def create_connection(self, protocol_factory, host=None, port=None, *,
ssl=None, family=0, proto=0, flags=0, sock=None,
local_addr=None, server_hostname=None):
async def create_connection(
self, protocol_factory, host=None, port=None,
*, ssl=None, family=0, proto=0,
flags=0, sock=None, local_addr=None,
server_hostname=None,
ssl_handshake_timeout=None,
ssl_shutdown_timeout=None,
happy_eyeballs_delay=None, interleave=None):
raise NotImplementedError
def create_server(self, protocol_factory, host=None, port=None, *,
family=socket.AF_UNSPEC, flags=socket.AI_PASSIVE,
sock=None, backlog=100, ssl=None, reuse_address=None,
reuse_port=None):
async def create_server(
self, protocol_factory, host=None, port=None,
*, family=socket.AF_UNSPEC,
flags=socket.AI_PASSIVE, sock=None, backlog=100,
ssl=None, reuse_address=None, reuse_port=None,
ssl_handshake_timeout=None,
ssl_shutdown_timeout=None,
start_serving=True):
"""A coroutine which creates a TCP server bound to host and port.
The return value is a Server object which can be used to stop
@@ -315,8 +330,8 @@ class AbstractEventLoop:
If host is an empty string or None all interfaces are assumed
and a list of multiple sockets will be returned (most likely
one for IPv4 and another one for IPv6). The host parameter can also be a
sequence (e.g. list) of hosts to bind to.
one for IPv4 and another one for IPv6). The host parameter can also be
a sequence (e.g. list) of hosts to bind to.
family can be set to either AF_INET or AF_INET6 to force the
socket to use IPv4 or IPv6. If not set it will be determined
@@ -342,22 +357,62 @@ class AbstractEventLoop:
the same port as other existing endpoints are bound to, so long as
they all set this flag when being created. This option is not
supported on Windows.
ssl_handshake_timeout is the time in seconds that an SSL server
will wait for completion of the SSL handshake before aborting the
connection. Default is 60s.
ssl_shutdown_timeout is the time in seconds that an SSL server
will wait for completion of the SSL shutdown procedure
before aborting the connection. Default is 30s.
start_serving set to True (default) causes the created server
to start accepting connections immediately. When set to False,
the user should await Server.start_serving() or Server.serve_forever()
to make the server to start accepting connections.
"""
raise NotImplementedError
def create_unix_connection(self, protocol_factory, path, *,
ssl=None, sock=None,
server_hostname=None):
async def sendfile(self, transport, file, offset=0, count=None,
*, fallback=True):
"""Send a file through a transport.
Return an amount of sent bytes.
"""
raise NotImplementedError
def create_unix_server(self, protocol_factory, path, *,
sock=None, backlog=100, ssl=None):
async def start_tls(self, transport, protocol, sslcontext, *,
server_side=False,
server_hostname=None,
ssl_handshake_timeout=None,
ssl_shutdown_timeout=None):
"""Upgrade a transport to TLS.
Return a new transport that *protocol* should start using
immediately.
"""
raise NotImplementedError
async def create_unix_connection(
self, protocol_factory, path=None, *,
ssl=None, sock=None,
server_hostname=None,
ssl_handshake_timeout=None,
ssl_shutdown_timeout=None):
raise NotImplementedError
async def create_unix_server(
self, protocol_factory, path=None, *,
sock=None, backlog=100, ssl=None,
ssl_handshake_timeout=None,
ssl_shutdown_timeout=None,
start_serving=True):
"""A coroutine which creates a UNIX Domain Socket server.
The return value is a Server object, which can be used to stop
the service.
path is a str, representing a file systsem path to bind the
path is a str, representing a file system path to bind the
server socket to.
sock can optionally be specified in order to use a preexisting
@@ -368,14 +423,40 @@ class AbstractEventLoop:
ssl can be set to an SSLContext to enable SSL over the
accepted connections.
ssl_handshake_timeout is the time in seconds that an SSL server
will wait for the SSL handshake to complete (defaults to 60s).
ssl_shutdown_timeout is the time in seconds that an SSL server
will wait for the SSL shutdown to finish (defaults to 30s).
start_serving set to True (default) causes the created server
to start accepting connections immediately. When set to False,
the user should await Server.start_serving() or Server.serve_forever()
to make the server to start accepting connections.
"""
raise NotImplementedError
def create_datagram_endpoint(self, protocol_factory,
local_addr=None, remote_addr=None, *,
family=0, proto=0, flags=0,
reuse_address=None, reuse_port=None,
allow_broadcast=None, sock=None):
async def connect_accepted_socket(
self, protocol_factory, sock,
*, ssl=None,
ssl_handshake_timeout=None,
ssl_shutdown_timeout=None):
"""Handle an accepted connection.
This is used by servers that accept connections outside of
asyncio, but use asyncio to handle connections.
This method is a coroutine. When completed, the coroutine
returns a (transport, protocol) pair.
"""
raise NotImplementedError
async def create_datagram_endpoint(self, protocol_factory,
local_addr=None, remote_addr=None, *,
family=0, proto=0, flags=0,
reuse_address=None, reuse_port=None,
allow_broadcast=None, sock=None):
"""A coroutine which creates a datagram endpoint.
This method will try to establish the endpoint in the background.
@@ -383,8 +464,8 @@ class AbstractEventLoop:
protocol_factory must be a callable returning a protocol instance.
socket family AF_INET or socket.AF_INET6 depending on host (or
family if specified), socket type SOCK_DGRAM.
socket family AF_INET, socket.AF_INET6 or socket.AF_UNIX depending on
host (or family if specified), socket type SOCK_DGRAM.
reuse_address tells the kernel to reuse a local socket in
TIME_WAIT state, without waiting for its natural timeout to
@@ -408,7 +489,7 @@ class AbstractEventLoop:
# Pipes and subprocesses.
def connect_read_pipe(self, protocol_factory, pipe):
async def connect_read_pipe(self, protocol_factory, pipe):
"""Register read pipe in event loop. Set the pipe to non-blocking mode.
protocol_factory should instantiate object with Protocol interface.
@@ -418,10 +499,10 @@ class AbstractEventLoop:
# The reason to accept file-like object instead of just file descriptor
# is: we need to own pipe and close it at transport finishing
# Can got complicated errors if pass f.fileno(),
# close fd in pipe transport then close f and vise versa.
# close fd in pipe transport then close f and vice versa.
raise NotImplementedError
def connect_write_pipe(self, protocol_factory, pipe):
async def connect_write_pipe(self, protocol_factory, pipe):
"""Register write pipe in event loop.
protocol_factory should instantiate object with BaseProtocol interface.
@@ -431,17 +512,21 @@ class AbstractEventLoop:
# The reason to accept file-like object instead of just file descriptor
# is: we need to own pipe and close it at transport finishing
# Can got complicated errors if pass f.fileno(),
# close fd in pipe transport then close f and vise versa.
# close fd in pipe transport then close f and vice versa.
raise NotImplementedError
def subprocess_shell(self, protocol_factory, cmd, *, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
**kwargs):
async def subprocess_shell(self, protocol_factory, cmd, *,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
**kwargs):
raise NotImplementedError
def subprocess_exec(self, protocol_factory, *args, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
**kwargs):
async def subprocess_exec(self, protocol_factory, *args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
**kwargs):
raise NotImplementedError
# Ready-based callback registration methods.
@@ -463,16 +548,32 @@ class AbstractEventLoop:
# Completion based I/O methods returning Futures.
def sock_recv(self, sock, nbytes):
async def sock_recv(self, sock, nbytes):
raise NotImplementedError
def sock_sendall(self, sock, data):
async def sock_recv_into(self, sock, buf):
raise NotImplementedError
def sock_connect(self, sock, address):
async def sock_recvfrom(self, sock, bufsize):
raise NotImplementedError
def sock_accept(self, sock):
async def sock_recvfrom_into(self, sock, buf, nbytes=0):
raise NotImplementedError
async def sock_sendall(self, sock, data):
raise NotImplementedError
async def sock_sendto(self, sock, data, address):
raise NotImplementedError
async def sock_connect(self, sock, address):
raise NotImplementedError
async def sock_accept(self, sock):
raise NotImplementedError
async def sock_sendfile(self, sock, file, offset=0, count=None,
*, fallback=None):
raise NotImplementedError
# Signal handling.
@@ -520,7 +621,7 @@ class AbstractEventLoopPolicy:
def get_event_loop(self):
"""Get the event loop for the current context.
Returns an event loop object implementing the BaseEventLoop interface,
Returns an event loop object implementing the AbstractEventLoop interface,
or raises an exception in case no event loop has been set for the
current context and the current policy does not specify to create one.
@@ -571,23 +672,43 @@ class BaseDefaultEventLoopPolicy(AbstractEventLoopPolicy):
self._local = self._Local()
def get_event_loop(self):
"""Get the event loop.
"""Get the event loop for the current context.
This may be None or an instance of EventLoop.
Returns an instance of EventLoop or raises an exception.
"""
if (self._local._loop is None and
not self._local._set_called and
isinstance(threading.current_thread(), threading._MainThread)):
not self._local._set_called and
threading.current_thread() is threading.main_thread()):
stacklevel = 2
try:
f = sys._getframe(1)
except AttributeError:
pass
else:
# Move up the call stack so that the warning is attached
# to the line outside asyncio itself.
while f:
module = f.f_globals.get('__name__')
if not (module == 'asyncio' or module.startswith('asyncio.')):
break
f = f.f_back
stacklevel += 1
import warnings
warnings.warn('There is no current event loop',
DeprecationWarning, stacklevel=stacklevel)
self.set_event_loop(self.new_event_loop())
if self._local._loop is None:
raise RuntimeError('There is no current event loop in thread %r.'
% threading.current_thread().name)
return self._local._loop
def set_event_loop(self, loop):
"""Set the event loop."""
self._local._set_called = True
assert loop is None or isinstance(loop, AbstractEventLoop)
if loop is not None and not isinstance(loop, AbstractEventLoop):
raise TypeError(f"loop must be an instance of AbstractEventLoop or None, not '{type(loop).__name__}'")
self._local._loop = loop
def new_event_loop(self):
@@ -611,7 +732,9 @@ _lock = threading.Lock()
# A TLS for the running event loop, used by _get_running_loop.
class _RunningLoop(threading.local):
_loop = None
loop_pid = (None, None)
_running_loop = _RunningLoop()
@@ -633,7 +756,10 @@ def _get_running_loop():
This is a low-level function intended to be used by event loops.
This function is thread-specific.
"""
return _running_loop._loop
# NOTE: this function is implemented in C (see _asynciomodule.c)
running_loop, pid = _running_loop.loop_pid
if running_loop is not None and pid == os.getpid():
return running_loop
def _set_running_loop(loop):
@@ -642,7 +768,8 @@ def _set_running_loop(loop):
This is a low-level function intended to be used by event loops.
This function is thread-specific.
"""
_running_loop._loop = loop
# NOTE: this function is implemented in C (see _asynciomodule.c)
_running_loop.loop_pid = (loop, os.getpid())
def _init_event_loop_policy():
@@ -665,7 +792,8 @@ def set_event_loop_policy(policy):
If policy is None, the default policy is restored."""
global _event_loop_policy
assert policy is None or isinstance(policy, AbstractEventLoopPolicy)
if policy is not None and not isinstance(policy, AbstractEventLoopPolicy):
raise TypeError(f"policy must be an instance of AbstractEventLoopPolicy or None, not '{type(policy).__name__}'")
_event_loop_policy = policy
@@ -678,6 +806,7 @@ def get_event_loop():
If there is no running event loop set, the function will return
the result of `get_event_loop_policy().get_event_loop()` call.
"""
# NOTE: this function is implemented in C (see _asynciomodule.c)
current_loop = _get_running_loop()
if current_loop is not None:
return current_loop
@@ -703,3 +832,37 @@ def set_child_watcher(watcher):
"""Equivalent to calling
get_event_loop_policy().set_child_watcher(watcher)."""
return get_event_loop_policy().set_child_watcher(watcher)
# Alias pure-Python implementations for testing purposes.
_py__get_running_loop = _get_running_loop
_py__set_running_loop = _set_running_loop
_py_get_running_loop = get_running_loop
_py_get_event_loop = get_event_loop
try:
# get_event_loop() is one of the most frequently called
# functions in asyncio. Pure Python implementation is
# about 4 times slower than C-accelerated.
from _asyncio import (_get_running_loop, _set_running_loop,
get_running_loop, get_event_loop)
except ImportError:
pass
else:
# Alias C implementations for testing purposes.
_c__get_running_loop = _get_running_loop
_c__set_running_loop = _set_running_loop
_c_get_running_loop = get_running_loop
_c_get_event_loop = get_event_loop
if hasattr(os, 'fork'):
def on_fork():
# Reset the loop and wakeupfd in the forked child process.
if _event_loop_policy is not None:
_event_loop_policy._local = BaseDefaultEventLoopPolicy._Local()
_set_running_loop(None)
signal.set_wakeup_fd(-1)
os.register_at_fork(after_in_child=on_fork)

62
Lib/asyncio/exceptions.py vendored Normal file
View File

@@ -0,0 +1,62 @@
"""asyncio exceptions."""
__all__ = ('BrokenBarrierError',
'CancelledError', 'InvalidStateError', 'TimeoutError',
'IncompleteReadError', 'LimitOverrunError',
'SendfileNotAvailableError')
class CancelledError(BaseException):
"""The Future or Task was cancelled."""
TimeoutError = TimeoutError # make local alias for the standard exception
class InvalidStateError(Exception):
"""The operation is not allowed in this state."""
class SendfileNotAvailableError(RuntimeError):
"""Sendfile syscall is not available.
Raised if OS does not support sendfile syscall for given socket or
file type.
"""
class IncompleteReadError(EOFError):
"""
Incomplete read error. Attributes:
- partial: read bytes string before the end of stream was reached
- expected: total number of expected bytes (or None if unknown)
"""
def __init__(self, partial, expected):
r_expected = 'undefined' if expected is None else repr(expected)
super().__init__(f'{len(partial)} bytes read on a total of '
f'{r_expected} expected bytes')
self.partial = partial
self.expected = expected
def __reduce__(self):
return type(self), (self.partial, self.expected)
class LimitOverrunError(Exception):
"""Reached the buffer limit while looking for a separator.
Attributes:
- consumed: total number of to be consumed bytes.
"""
def __init__(self, message, consumed):
super().__init__(message)
self.consumed = consumed
def __reduce__(self):
return type(self), (self.args[0], self.consumed)
class BrokenBarrierError(RuntimeError):
"""Barrier is broken by barrier.abort() call."""

76
Lib/asyncio/format_helpers.py vendored Normal file
View File

@@ -0,0 +1,76 @@
import functools
import inspect
import reprlib
import sys
import traceback
from . import constants
def _get_function_source(func):
func = inspect.unwrap(func)
if inspect.isfunction(func):
code = func.__code__
return (code.co_filename, code.co_firstlineno)
if isinstance(func, functools.partial):
return _get_function_source(func.func)
if isinstance(func, functools.partialmethod):
return _get_function_source(func.func)
return None
def _format_callback_source(func, args):
func_repr = _format_callback(func, args, None)
source = _get_function_source(func)
if source:
func_repr += f' at {source[0]}:{source[1]}'
return func_repr
def _format_args_and_kwargs(args, kwargs):
"""Format function arguments and keyword arguments.
Special case for a single parameter: ('hello',) is formatted as ('hello').
"""
# use reprlib to limit the length of the output
items = []
if args:
items.extend(reprlib.repr(arg) for arg in args)
if kwargs:
items.extend(f'{k}={reprlib.repr(v)}' for k, v in kwargs.items())
return '({})'.format(', '.join(items))
def _format_callback(func, args, kwargs, suffix=''):
if isinstance(func, functools.partial):
suffix = _format_args_and_kwargs(args, kwargs) + suffix
return _format_callback(func.func, func.args, func.keywords, suffix)
if hasattr(func, '__qualname__') and func.__qualname__:
func_repr = func.__qualname__
elif hasattr(func, '__name__') and func.__name__:
func_repr = func.__name__
else:
func_repr = repr(func)
func_repr += _format_args_and_kwargs(args, kwargs)
if suffix:
func_repr += suffix
return func_repr
def extract_stack(f=None, limit=None):
"""Replacement for traceback.extract_stack() that only does the
necessary work for asyncio debug mode.
"""
if f is None:
f = sys._getframe().f_back
if limit is None:
# Limit the amount of work to a reasonable amount, as extract_stack()
# can be called for each coroutine and future in debug mode.
limit = constants.DEBUG_STACK_DEPTH
stack = traceback.StackSummary.extract(traceback.walk_stack(f),
limit=limit,
lookup_lines=False)
stack.reverse()
return stack

298
Lib/asyncio/futures.py vendored
View File

@@ -1,21 +1,21 @@
"""A Future class similar to the one in PEP 3148."""
__all__ = ['CancelledError', 'TimeoutError', 'InvalidStateError',
'Future', 'wrap_future', 'isfuture']
__all__ = (
'Future', 'wrap_future', 'isfuture',
)
import concurrent.futures
import contextvars
import logging
import sys
import traceback
from types import GenericAlias
from . import base_futures
from . import compat
from . import events
from . import exceptions
from . import format_helpers
CancelledError = base_futures.CancelledError
InvalidStateError = base_futures.InvalidStateError
TimeoutError = base_futures.TimeoutError
isfuture = base_futures.isfuture
@@ -27,96 +27,18 @@ _FINISHED = base_futures._FINISHED
STACK_DEBUG = logging.DEBUG - 1 # heavy-duty debugging
class _TracebackLogger:
"""Helper to log a traceback upon destruction if not cleared.
This solves a nasty problem with Futures and Tasks that have an
exception set: if nobody asks for the exception, the exception is
never logged. This violates the Zen of Python: 'Errors should
never pass silently. Unless explicitly silenced.'
However, we don't want to log the exception as soon as
set_exception() is called: if the calling code is written
properly, it will get the exception and handle it properly. But
we *do* want to log it if result() or exception() was never called
-- otherwise developers waste a lot of time wondering why their
buggy code fails silently.
An earlier attempt added a __del__() method to the Future class
itself, but this backfired because the presence of __del__()
prevents garbage collection from breaking cycles. A way out of
this catch-22 is to avoid having a __del__() method on the Future
class itself, but instead to have a reference to a helper object
with a __del__() method that logs the traceback, where we ensure
that the helper object doesn't participate in cycles, and only the
Future has a reference to it.
The helper object is added when set_exception() is called. When
the Future is collected, and the helper is present, the helper
object is also collected, and its __del__() method will log the
traceback. When the Future's result() or exception() method is
called (and a helper object is present), it removes the helper
object, after calling its clear() method to prevent it from
logging.
One downside is that we do a fair amount of work to extract the
traceback from the exception, even when it is never logged. It
would seem cheaper to just store the exception object, but that
references the traceback, which references stack frames, which may
reference the Future, which references the _TracebackLogger, and
then the _TracebackLogger would be included in a cycle, which is
what we're trying to avoid! As an optimization, we don't
immediately format the exception; we only do the work when
activate() is called, which call is delayed until after all the
Future's callbacks have run. Since usually a Future has at least
one callback (typically set by 'yield from') and usually that
callback extracts the callback, thereby removing the need to
format the exception.
PS. I don't claim credit for this solution. I first heard of it
in a discussion about closing files when they are collected.
"""
__slots__ = ('loop', 'source_traceback', 'exc', 'tb')
def __init__(self, future, exc):
self.loop = future._loop
self.source_traceback = future._source_traceback
self.exc = exc
self.tb = None
def activate(self):
exc = self.exc
if exc is not None:
self.exc = None
self.tb = traceback.format_exception(exc.__class__, exc,
exc.__traceback__)
def clear(self):
self.exc = None
self.tb = None
def __del__(self):
if self.tb:
msg = 'Future/Task exception was never retrieved\n'
if self.source_traceback:
src = ''.join(traceback.format_list(self.source_traceback))
msg += 'Future/Task created at (most recent call last):\n'
msg += '%s\n' % src.rstrip()
msg += ''.join(self.tb).rstrip()
self.loop.call_exception_handler({'message': msg})
class Future:
"""This class is *almost* compatible with concurrent.futures.Future.
Differences:
- This class is not thread-safe.
- result() and exception() do not take a timeout argument and
raise an exception when the future isn't done yet.
- Callbacks registered with add_done_callback() are always called
via the event loop's call_soon_threadsafe().
via the event loop's call_soon().
- This class is not compatible with the wait() and as_completed()
methods in the concurrent.futures package.
@@ -130,6 +52,9 @@ class Future:
_exception = None
_loop = None
_source_traceback = None
_cancel_message = None
# A saved CancelledError for later chaining as an exception context.
_cancelled_exc = None
# This field is used for a dual purpose:
# - Its presence is a marker to declare that a class implements
@@ -137,12 +62,12 @@ class Future:
# The value must also be not-None, to enable a subclass to declare
# that it is not compatible by setting this to None.
# - It is set by __iter__() below so that Task._step() can tell
# the difference between `yield from Future()` (correct) vs.
# the difference between
# `await Future()` or`yield from Future()` (correct) vs.
# `yield Future()` (incorrect).
_asyncio_future_blocking = False
_log_traceback = False # Used for Python 3.4 and later
_tb_logger = None # Used for Python 3.3 only
__log_traceback = False
def __init__(self, *, loop=None):
"""Initialize the future.
@@ -157,50 +82,83 @@ class Future:
self._loop = loop
self._callbacks = []
if self._loop.get_debug():
self._source_traceback = traceback.extract_stack(sys._getframe(1))
_repr_info = base_futures._future_repr_info
self._source_traceback = format_helpers.extract_stack(
sys._getframe(1))
def __repr__(self):
return '<%s %s>' % (self.__class__.__name__, ' '.join(self._repr_info()))
return base_futures._future_repr(self)
# On Python 3.3 and older, objects with a destructor part of a reference
# cycle are never destroyed. It's not more the case on Python 3.4 thanks
# to the PEP 442.
if compat.PY34:
def __del__(self):
if not self._log_traceback:
# set_exception() was not called, or result() or exception()
# has consumed the exception
return
exc = self._exception
context = {
'message': ('%s exception was never retrieved'
% self.__class__.__name__),
'exception': exc,
'future': self,
}
if self._source_traceback:
context['source_traceback'] = self._source_traceback
self._loop.call_exception_handler(context)
def __del__(self):
if not self.__log_traceback:
# set_exception() was not called, or result() or exception()
# has consumed the exception
return
exc = self._exception
context = {
'message':
f'{self.__class__.__name__} exception was never retrieved',
'exception': exc,
'future': self,
}
if self._source_traceback:
context['source_traceback'] = self._source_traceback
self._loop.call_exception_handler(context)
def __class_getitem__(cls, type):
return cls
__class_getitem__ = classmethod(GenericAlias)
def cancel(self):
@property
def _log_traceback(self):
return self.__log_traceback
@_log_traceback.setter
def _log_traceback(self, val):
if val:
raise ValueError('_log_traceback can only be set to False')
self.__log_traceback = False
def get_loop(self):
"""Return the event loop the Future is bound to."""
loop = self._loop
if loop is None:
raise RuntimeError("Future object is not initialized.")
return loop
def _make_cancelled_error(self):
"""Create the CancelledError to raise if the Future is cancelled.
This should only be called once when handling a cancellation since
it erases the saved context exception value.
"""
if self._cancelled_exc is not None:
exc = self._cancelled_exc
self._cancelled_exc = None
return exc
if self._cancel_message is None:
exc = exceptions.CancelledError()
else:
exc = exceptions.CancelledError(self._cancel_message)
exc.__context__ = self._cancelled_exc
# Remove the reference since we don't need this anymore.
self._cancelled_exc = None
return exc
def cancel(self, msg=None):
"""Cancel the future and schedule callbacks.
If the future is already done or cancelled, return False. Otherwise,
change the future's state to cancelled, schedule the callbacks and
return True.
"""
self.__log_traceback = False
if self._state != _PENDING:
return False
self._state = _CANCELLED
self._schedule_callbacks()
self._cancel_message = msg
self.__schedule_callbacks()
return True
def _schedule_callbacks(self):
def __schedule_callbacks(self):
"""Internal: Ask the event loop to call all callbacks.
The callbacks are scheduled to be called as soon as possible. Also
@@ -211,8 +169,8 @@ class Future:
return
self._callbacks[:] = []
for callback in callbacks:
self._loop.call_soon(callback, self)
for callback, ctx in callbacks:
self._loop.call_soon(callback, self, context=ctx)
def cancelled(self):
"""Return True if the future was cancelled."""
@@ -236,15 +194,13 @@ class Future:
the future is done and has an exception set, this exception is raised.
"""
if self._state == _CANCELLED:
raise CancelledError
exc = self._make_cancelled_error()
raise exc
if self._state != _FINISHED:
raise InvalidStateError('Result is not ready.')
self._log_traceback = False
if self._tb_logger is not None:
self._tb_logger.clear()
self._tb_logger = None
raise exceptions.InvalidStateError('Result is not ready.')
self.__log_traceback = False
if self._exception is not None:
raise self._exception
raise self._exception.with_traceback(self._exception_tb)
return self._result
def exception(self):
@@ -256,16 +212,14 @@ class Future:
InvalidStateError.
"""
if self._state == _CANCELLED:
raise CancelledError
exc = self._make_cancelled_error()
raise exc
if self._state != _FINISHED:
raise InvalidStateError('Exception is not set.')
self._log_traceback = False
if self._tb_logger is not None:
self._tb_logger.clear()
self._tb_logger = None
raise exceptions.InvalidStateError('Exception is not set.')
self.__log_traceback = False
return self._exception
def add_done_callback(self, fn):
def add_done_callback(self, fn, *, context=None):
"""Add a callback to be run when the future becomes done.
The callback is called with a single argument - the future object. If
@@ -273,9 +227,11 @@ class Future:
scheduled with call_soon.
"""
if self._state != _PENDING:
self._loop.call_soon(fn, self)
self._loop.call_soon(fn, self, context=context)
else:
self._callbacks.append(fn)
if context is None:
context = contextvars.copy_context()
self._callbacks.append((fn, context))
# New method not in PEP 3148.
@@ -284,7 +240,9 @@ class Future:
Returns the number of callbacks removed.
"""
filtered_callbacks = [f for f in self._callbacks if f != fn]
filtered_callbacks = [(f, ctx)
for (f, ctx) in self._callbacks
if f != fn]
removed_count = len(self._callbacks) - len(filtered_callbacks)
if removed_count:
self._callbacks[:] = filtered_callbacks
@@ -299,10 +257,10 @@ class Future:
InvalidStateError.
"""
if self._state != _PENDING:
raise InvalidStateError('{}: {!r}'.format(self._state, self))
raise exceptions.InvalidStateError(f'{self._state}: {self!r}')
self._result = result
self._state = _FINISHED
self._schedule_callbacks()
self.__schedule_callbacks()
def set_exception(self, exception):
"""Mark the future done and set an exception.
@@ -311,38 +269,45 @@ class Future:
InvalidStateError.
"""
if self._state != _PENDING:
raise InvalidStateError('{}: {!r}'.format(self._state, self))
raise exceptions.InvalidStateError(f'{self._state}: {self!r}')
if isinstance(exception, type):
exception = exception()
if type(exception) is StopIteration:
raise TypeError("StopIteration interacts badly with generators "
"and cannot be raised into a Future")
self._exception = exception
self._exception_tb = exception.__traceback__
self._state = _FINISHED
self._schedule_callbacks()
if compat.PY34:
self._log_traceback = True
else:
self._tb_logger = _TracebackLogger(self, exception)
# Arrange for the logger to be activated after all callbacks
# have had a chance to call result() or exception().
self._loop.call_soon(self._tb_logger.activate)
self.__schedule_callbacks()
self.__log_traceback = True
def __iter__(self):
def __await__(self):
if not self.done():
self._asyncio_future_blocking = True
yield self # This tells Task to wait for completion.
assert self.done(), "yield from wasn't used with future"
if not self.done():
raise RuntimeError("await wasn't used with future")
return self.result() # May raise too.
if compat.PY35:
__await__ = __iter__ # make compatible with 'await' expression
__iter__ = __await__ # make compatible with 'yield from'.
# Needed for testing purposes.
_PyFuture = Future
def _get_loop(fut):
# Tries to call Future.get_loop() if it's available.
# Otherwise fallbacks to using the old '_loop' property.
try:
get_loop = fut.get_loop
except AttributeError:
pass
else:
return get_loop()
return fut._loop
def _set_result_unless_cancelled(fut, result):
"""Helper setting the result only if the future was not cancelled."""
if fut.cancelled():
@@ -350,6 +315,18 @@ def _set_result_unless_cancelled(fut, result):
fut.set_result(result)
def _convert_future_exc(exc):
exc_class = type(exc)
if exc_class is concurrent.futures.CancelledError:
return exceptions.CancelledError(*exc.args)
elif exc_class is concurrent.futures.TimeoutError:
return exceptions.TimeoutError(*exc.args)
elif exc_class is concurrent.futures.InvalidStateError:
return exceptions.InvalidStateError(*exc.args)
else:
return exc
def _set_concurrent_future_state(concurrent, source):
"""Copy state from a future to a concurrent.futures.Future."""
assert source.done()
@@ -359,7 +336,7 @@ def _set_concurrent_future_state(concurrent, source):
return
exception = source.exception()
if exception is not None:
concurrent.set_exception(exception)
concurrent.set_exception(_convert_future_exc(exception))
else:
result = source.result()
concurrent.set_result(result)
@@ -379,7 +356,7 @@ def _copy_future_state(source, dest):
else:
exception = source.exception()
if exception is not None:
dest.set_exception(exception)
dest.set_exception(_convert_future_exc(exception))
else:
result = source.result()
dest.set_result(result)
@@ -398,8 +375,8 @@ def _chain_future(source, destination):
if not isfuture(destination) and not isinstance(destination,
concurrent.futures.Future):
raise TypeError('A future is required for destination argument')
source_loop = source._loop if isfuture(source) else None
dest_loop = destination._loop if isfuture(destination) else None
source_loop = _get_loop(source) if isfuture(source) else None
dest_loop = _get_loop(destination) if isfuture(destination) else None
def _set_state(future, other):
if isfuture(future):
@@ -415,9 +392,14 @@ def _chain_future(source, destination):
source_loop.call_soon_threadsafe(source.cancel)
def _call_set_state(source):
if (destination.cancelled() and
dest_loop is not None and dest_loop.is_closed()):
return
if dest_loop is None or dest_loop is source_loop:
_set_state(destination, source)
else:
if dest_loop.is_closed():
return
dest_loop.call_soon_threadsafe(_set_state, destination, source)
destination.add_done_callback(_call_check_cancel)
@@ -429,7 +411,7 @@ def wrap_future(future, *, loop=None):
if isfuture(future):
return future
assert isinstance(future, concurrent.futures.Future), \
'concurrent.futures.Future is expected, got {!r}'.format(future)
f'concurrent.futures.Future is expected, got {future!r}'
if loop is None:
loop = events.get_event_loop()
new_future = loop.create_future()

456
Lib/asyncio/locks.py vendored
View File

@@ -1,92 +1,26 @@
"""Synchronization primitives."""
__all__ = ['Lock', 'Event', 'Condition', 'Semaphore', 'BoundedSemaphore']
__all__ = ('Lock', 'Event', 'Condition', 'Semaphore',
'BoundedSemaphore', 'Barrier')
import collections
import enum
from . import compat
from . import events
from . import futures
from .coroutines import coroutine
from . import exceptions
from . import mixins
class _ContextManager:
"""Context manager.
This enables the following idiom for acquiring and releasing a
lock around a block:
with (yield from lock):
<block>
while failing loudly when accidentally using:
with lock:
<block>
"""
def __init__(self, lock):
self._lock = lock
def __enter__(self):
class _ContextManagerMixin:
async def __aenter__(self):
await self.acquire()
# We have no use for the "as ..." clause in the with
# statement for locks.
return None
def __exit__(self, *args):
try:
self._lock.release()
finally:
self._lock = None # Crudely prevent reuse.
async def __aexit__(self, exc_type, exc, tb):
self.release()
class _ContextManagerMixin:
def __enter__(self):
raise RuntimeError(
'"yield from" should be used as context manager expression')
def __exit__(self, *args):
# This must exist because __enter__ exists, even though that
# always raises; that's how the with-statement works.
pass
@coroutine
def __iter__(self):
# This is not a coroutine. It is meant to enable the idiom:
#
# with (yield from lock):
# <block>
#
# as an alternative to:
#
# yield from lock.acquire()
# try:
# <block>
# finally:
# lock.release()
yield from self.acquire()
return _ContextManager(self)
if compat.PY35:
def __await__(self):
# To make "with await lock" work.
yield from self.acquire()
return _ContextManager(self)
@coroutine
def __aenter__(self):
yield from self.acquire()
# We have no use for the "as ..." clause in the with
# statement for locks.
return None
@coroutine
def __aexit__(self, exc_type, exc, tb):
self.release()
class Lock(_ContextManagerMixin):
class Lock(_ContextManagerMixin, mixins._LoopBoundMixin):
"""Primitive lock objects.
A primitive lock is a synchronization primitive that is not owned
@@ -108,16 +42,16 @@ class Lock(_ContextManagerMixin):
release() call resets the state to unlocked; first coroutine which
is blocked in acquire() is being processed.
acquire() is a coroutine and should be called with 'yield from'.
acquire() is a coroutine and should be called with 'await'.
Locks also support the context management protocol. '(yield from lock)'
should be used as the context manager expression.
Locks also support the asynchronous context management protocol.
'async with lock' statement should be used.
Usage:
lock = Lock()
...
yield from lock
await lock.acquire()
try:
...
finally:
@@ -127,57 +61,65 @@ class Lock(_ContextManagerMixin):
lock = Lock()
...
with (yield from lock):
async with lock:
...
Lock objects can be tested for locking state:
if not lock.locked():
yield from lock
await lock.acquire()
else:
# lock is acquired
...
"""
def __init__(self, *, loop=None):
self._waiters = collections.deque()
def __init__(self):
self._waiters = None
self._locked = False
if loop is not None:
self._loop = loop
else:
self._loop = events.get_event_loop()
def __repr__(self):
res = super().__repr__()
extra = 'locked' if self._locked else 'unlocked'
if self._waiters:
extra = '{},waiters:{}'.format(extra, len(self._waiters))
return '<{} [{}]>'.format(res[1:-1], extra)
extra = f'{extra}, waiters:{len(self._waiters)}'
return f'<{res[1:-1]} [{extra}]>'
def locked(self):
"""Return True if lock is acquired."""
return self._locked
@coroutine
def acquire(self):
async def acquire(self):
"""Acquire a lock.
This method blocks until the lock is unlocked, then sets it to
locked and returns True.
"""
if not self._locked and all(w.cancelled() for w in self._waiters):
if (not self._locked and (self._waiters is None or
all(w.cancelled() for w in self._waiters))):
self._locked = True
return True
fut = self._loop.create_future()
if self._waiters is None:
self._waiters = collections.deque()
fut = self._get_loop().create_future()
self._waiters.append(fut)
# Finally block should be called before the CancelledError
# handling as we don't want CancelledError to call
# _wake_up_first() and attempt to wake up itself.
try:
yield from fut
self._locked = True
return True
finally:
self._waiters.remove(fut)
try:
await fut
finally:
self._waiters.remove(fut)
except exceptions.CancelledError:
if not self._locked:
self._wake_up_first()
raise
self._locked = True
return True
def release(self):
"""Release a lock.
@@ -192,16 +134,27 @@ class Lock(_ContextManagerMixin):
"""
if self._locked:
self._locked = False
# Wake up the first waiter who isn't cancelled.
for fut in self._waiters:
if not fut.done():
fut.set_result(True)
break
self._wake_up_first()
else:
raise RuntimeError('Lock is not acquired.')
def _wake_up_first(self):
"""Wake up the first waiter if it isn't done."""
if not self._waiters:
return
try:
fut = next(iter(self._waiters))
except StopIteration:
return
class Event:
# .done() necessarily means that a waiter will wake up later on and
# either take the lock, or, if it was cancelled and lock wasn't
# taken already, will hit this again and wake up a new waiter.
if not fut.done():
fut.set_result(True)
class Event(mixins._LoopBoundMixin):
"""Asynchronous equivalent to threading.Event.
Class implementing event objects. An event manages a flag that can be set
@@ -210,20 +163,16 @@ class Event:
false.
"""
def __init__(self, *, loop=None):
def __init__(self):
self._waiters = collections.deque()
self._value = False
if loop is not None:
self._loop = loop
else:
self._loop = events.get_event_loop()
def __repr__(self):
res = super().__repr__()
extra = 'set' if self._value else 'unset'
if self._waiters:
extra = '{},waiters:{}'.format(extra, len(self._waiters))
return '<{} [{}]>'.format(res[1:-1], extra)
extra = f'{extra}, waiters:{len(self._waiters)}'
return f'<{res[1:-1]} [{extra}]>'
def is_set(self):
"""Return True if and only if the internal flag is true."""
@@ -247,8 +196,7 @@ class Event:
to true again."""
self._value = False
@coroutine
def wait(self):
async def wait(self):
"""Block until the internal flag is true.
If the internal flag is true on entry, return True
@@ -258,16 +206,16 @@ class Event:
if self._value:
return True
fut = self._loop.create_future()
fut = self._get_loop().create_future()
self._waiters.append(fut)
try:
yield from fut
await fut
return True
finally:
self._waiters.remove(fut)
class Condition(_ContextManagerMixin):
class Condition(_ContextManagerMixin, mixins._LoopBoundMixin):
"""Asynchronous equivalent to threading.Condition.
This class implements condition variable objects. A condition variable
@@ -277,16 +225,9 @@ class Condition(_ContextManagerMixin):
A new Lock object is created and used as the underlying lock.
"""
def __init__(self, lock=None, *, loop=None):
if loop is not None:
self._loop = loop
else:
self._loop = events.get_event_loop()
def __init__(self, lock=None):
if lock is None:
lock = Lock(loop=self._loop)
elif lock._loop is not self._loop:
raise ValueError("loop argument must agree with lock")
lock = Lock()
self._lock = lock
# Export the lock's locked(), acquire() and release() methods.
@@ -300,11 +241,10 @@ class Condition(_ContextManagerMixin):
res = super().__repr__()
extra = 'locked' if self.locked() else 'unlocked'
if self._waiters:
extra = '{},waiters:{}'.format(extra, len(self._waiters))
return '<{} [{}]>'.format(res[1:-1], extra)
extra = f'{extra}, waiters:{len(self._waiters)}'
return f'<{res[1:-1]} [{extra}]>'
@coroutine
def wait(self):
async def wait(self):
"""Wait until notified.
If the calling coroutine has not acquired the lock when this
@@ -320,25 +260,28 @@ class Condition(_ContextManagerMixin):
self.release()
try:
fut = self._loop.create_future()
fut = self._get_loop().create_future()
self._waiters.append(fut)
try:
yield from fut
await fut
return True
finally:
self._waiters.remove(fut)
finally:
# Must reacquire lock even if wait is cancelled
cancelled = False
while True:
try:
yield from self.acquire()
await self.acquire()
break
except futures.CancelledError:
pass
except exceptions.CancelledError:
cancelled = True
@coroutine
def wait_for(self, predicate):
if cancelled:
raise exceptions.CancelledError
async def wait_for(self, predicate):
"""Wait until a predicate becomes true.
The predicate should be a callable which result will be
@@ -347,7 +290,7 @@ class Condition(_ContextManagerMixin):
"""
result = predicate()
while not result:
yield from self.wait()
await self.wait()
result = predicate()
return result
@@ -384,7 +327,7 @@ class Condition(_ContextManagerMixin):
self.notify(len(self._waiters))
class Semaphore(_ContextManagerMixin):
class Semaphore(_ContextManagerMixin, mixins._LoopBoundMixin):
"""A Semaphore implementation.
A semaphore manages an internal counter which is decremented by each
@@ -399,37 +342,25 @@ class Semaphore(_ContextManagerMixin):
ValueError is raised.
"""
def __init__(self, value=1, *, loop=None):
def __init__(self, value=1):
if value < 0:
raise ValueError("Semaphore initial value must be >= 0")
self._waiters = None
self._value = value
self._waiters = collections.deque()
if loop is not None:
self._loop = loop
else:
self._loop = events.get_event_loop()
def __repr__(self):
res = super().__repr__()
extra = 'locked' if self.locked() else 'unlocked,value:{}'.format(
self._value)
extra = 'locked' if self.locked() else f'unlocked, value:{self._value}'
if self._waiters:
extra = '{},waiters:{}'.format(extra, len(self._waiters))
return '<{} [{}]>'.format(res[1:-1], extra)
def _wake_up_next(self):
while self._waiters:
waiter = self._waiters.popleft()
if not waiter.done():
waiter.set_result(None)
return
extra = f'{extra}, waiters:{len(self._waiters)}'
return f'<{res[1:-1]} [{extra}]>'
def locked(self):
"""Returns True if semaphore can not be acquired immediately."""
return self._value == 0
"""Returns True if semaphore cannot be acquired immediately."""
return self._value == 0 or (
any(not w.cancelled() for w in (self._waiters or ())))
@coroutine
def acquire(self):
async def acquire(self):
"""Acquire a semaphore.
If the internal counter is larger than zero on entry,
@@ -438,28 +369,53 @@ class Semaphore(_ContextManagerMixin):
called release() to make it larger than 0, and then return
True.
"""
while self._value <= 0:
fut = self._loop.create_future()
self._waiters.append(fut)
if not self.locked():
self._value -= 1
return True
if self._waiters is None:
self._waiters = collections.deque()
fut = self._get_loop().create_future()
self._waiters.append(fut)
# Finally block should be called before the CancelledError
# handling as we don't want CancelledError to call
# _wake_up_first() and attempt to wake up itself.
try:
try:
yield from fut
except:
# See the similar code in Queue.get.
fut.cancel()
if self._value > 0 and not fut.cancelled():
self._wake_up_next()
raise
self._value -= 1
await fut
finally:
self._waiters.remove(fut)
except exceptions.CancelledError:
if not fut.cancelled():
self._value += 1
self._wake_up_next()
raise
if self._value > 0:
self._wake_up_next()
return True
def release(self):
"""Release a semaphore, incrementing the internal counter by one.
When it was zero on entry and another coroutine is waiting for it to
become larger than zero again, wake up that coroutine.
"""
self._value += 1
self._wake_up_next()
def _wake_up_next(self):
"""Wake up the first waiter that isn't done."""
if not self._waiters:
return
for fut in self._waiters:
if not fut.done():
self._value -= 1
fut.set_result(True)
return
class BoundedSemaphore(Semaphore):
"""A bounded semaphore implementation.
@@ -468,11 +424,163 @@ class BoundedSemaphore(Semaphore):
above the initial value.
"""
def __init__(self, value=1, *, loop=None):
def __init__(self, value=1):
self._bound_value = value
super().__init__(value, loop=loop)
super().__init__(value)
def release(self):
if self._value >= self._bound_value:
raise ValueError('BoundedSemaphore released too many times')
super().release()
class _BarrierState(enum.Enum):
FILLING = 'filling'
DRAINING = 'draining'
RESETTING = 'resetting'
BROKEN = 'broken'
class Barrier(mixins._LoopBoundMixin):
"""Asyncio equivalent to threading.Barrier
Implements a Barrier primitive.
Useful for synchronizing a fixed number of tasks at known synchronization
points. Tasks block on 'wait()' and are simultaneously awoken once they
have all made their call.
"""
def __init__(self, parties):
"""Create a barrier, initialised to 'parties' tasks."""
if parties < 1:
raise ValueError('parties must be > 0')
self._cond = Condition() # notify all tasks when state changes
self._parties = parties
self._state = _BarrierState.FILLING
self._count = 0 # count tasks in Barrier
def __repr__(self):
res = super().__repr__()
extra = f'{self._state.value}'
if not self.broken:
extra += f', waiters:{self.n_waiting}/{self.parties}'
return f'<{res[1:-1]} [{extra}]>'
async def __aenter__(self):
# wait for the barrier reaches the parties number
# when start draining release and return index of waited task
return await self.wait()
async def __aexit__(self, *args):
pass
async def wait(self):
"""Wait for the barrier.
When the specified number of tasks have started waiting, they are all
simultaneously awoken.
Returns an unique and individual index number from 0 to 'parties-1'.
"""
async with self._cond:
await self._block() # Block while the barrier drains or resets.
try:
index = self._count
self._count += 1
if index + 1 == self._parties:
# We release the barrier
await self._release()
else:
await self._wait()
return index
finally:
self._count -= 1
# Wake up any tasks waiting for barrier to drain.
self._exit()
async def _block(self):
# Block until the barrier is ready for us,
# or raise an exception if it is broken.
#
# It is draining or resetting, wait until done
# unless a CancelledError occurs
await self._cond.wait_for(
lambda: self._state not in (
_BarrierState.DRAINING, _BarrierState.RESETTING
)
)
# see if the barrier is in a broken state
if self._state is _BarrierState.BROKEN:
raise exceptions.BrokenBarrierError("Barrier aborted")
async def _release(self):
# Release the tasks waiting in the barrier.
# Enter draining state.
# Next waiting tasks will be blocked until the end of draining.
self._state = _BarrierState.DRAINING
self._cond.notify_all()
async def _wait(self):
# Wait in the barrier until we are released. Raise an exception
# if the barrier is reset or broken.
# wait for end of filling
# unless a CancelledError occurs
await self._cond.wait_for(lambda: self._state is not _BarrierState.FILLING)
if self._state in (_BarrierState.BROKEN, _BarrierState.RESETTING):
raise exceptions.BrokenBarrierError("Abort or reset of barrier")
def _exit(self):
# If we are the last tasks to exit the barrier, signal any tasks
# waiting for the barrier to drain.
if self._count == 0:
if self._state in (_BarrierState.RESETTING, _BarrierState.DRAINING):
self._state = _BarrierState.FILLING
self._cond.notify_all()
async def reset(self):
"""Reset the barrier to the initial state.
Any tasks currently waiting will get the BrokenBarrier exception
raised.
"""
async with self._cond:
if self._count > 0:
if self._state is not _BarrierState.RESETTING:
#reset the barrier, waking up tasks
self._state = _BarrierState.RESETTING
else:
self._state = _BarrierState.FILLING
self._cond.notify_all()
async def abort(self):
"""Place the barrier into a 'broken' state.
Useful in case of error. Any currently waiting tasks and tasks
attempting to 'wait()' will have BrokenBarrierError raised.
"""
async with self._cond:
self._state = _BarrierState.BROKEN
self._cond.notify_all()
@property
def parties(self):
"""Return the number of tasks required to trip the barrier."""
return self._parties
@property
def n_waiting(self):
"""Return the number of tasks currently waiting at the barrier."""
if self._state is _BarrierState.FILLING:
return self._count
return 0
@property
def broken(self):
"""Return True if the barrier is in a broken state."""
return self._state is _BarrierState.BROKEN

21
Lib/asyncio/mixins.py vendored Normal file
View File

@@ -0,0 +1,21 @@
"""Event loop mixins."""
import threading
from . import events
_global_lock = threading.Lock()
class _LoopBoundMixin:
_loop = None
def _get_loop(self):
loop = events._get_running_loop()
if self._loop is None:
with _global_lock:
if self._loop is None:
self._loop = loop
if loop is not self._loop:
raise RuntimeError(f'{self!r} is bound to a different event loop')
return loop

View File

@@ -4,20 +4,45 @@ A proactor is a "notify-on-completion" multiplexer. Currently a
proactor is only implemented on Windows with IOCP.
"""
__all__ = ['BaseProactorEventLoop']
__all__ = 'BaseProactorEventLoop',
import io
import os
import socket
import warnings
import signal
import threading
import collections
from . import base_events
from . import compat
from . import constants
from . import futures
from . import exceptions
from . import protocols
from . import sslproto
from . import transports
from . import trsock
from .log import logger
def _set_socket_extra(transport, sock):
transport._extra['socket'] = trsock.TransportSocket(sock)
try:
transport._extra['sockname'] = sock.getsockname()
except socket.error:
if transport._loop.get_debug():
logger.warning(
"getsockname() failed on %r", sock, exc_info=True)
if 'peername' not in transport._extra:
try:
transport._extra['peername'] = sock.getpeername()
except socket.error:
# UDP sockets may not have a peer name
transport._extra['peername'] = None
class _ProactorBasePipeTransport(transports._FlowControlMixin,
transports.BaseTransport):
"""Base class for pipe and socket transports."""
@@ -27,7 +52,7 @@ class _ProactorBasePipeTransport(transports._FlowControlMixin,
super().__init__(extra, loop)
self._set_extra(sock)
self._sock = sock
self._protocol = protocol
self.set_protocol(protocol)
self._server = server
self._buffer = None # None or bytearray.
self._read_fut = None
@@ -35,6 +60,7 @@ class _ProactorBasePipeTransport(transports._FlowControlMixin,
self._pending_write = 0
self._conn_lost = 0
self._closing = False # Set when close() called.
self._called_connection_lost = False
self._eof_written = False
if self._server is not None:
self._server._attach()
@@ -51,17 +77,16 @@ class _ProactorBasePipeTransport(transports._FlowControlMixin,
elif self._closing:
info.append('closing')
if self._sock is not None:
info.append('fd=%s' % self._sock.fileno())
info.append(f'fd={self._sock.fileno()}')
if self._read_fut is not None:
info.append('read=%s' % self._read_fut)
info.append(f'read={self._read_fut!r}')
if self._write_fut is not None:
info.append("write=%r" % self._write_fut)
info.append(f'write={self._write_fut!r}')
if self._buffer:
bufsize = len(self._buffer)
info.append('write_bufsize=%s' % bufsize)
info.append(f'write_bufsize={len(self._buffer)}')
if self._eof_written:
info.append('EOF written')
return '<%s>' % ' '.join(info)
return '<{}>'.format(' '.join(info))
def _set_extra(self, sock):
self._extra['pipe'] = sock
@@ -86,31 +111,33 @@ class _ProactorBasePipeTransport(transports._FlowControlMixin,
self._read_fut.cancel()
self._read_fut = None
# On Python 3.3 and older, objects with a destructor part of a reference
# cycle are never destroyed. It's not more the case on Python 3.4 thanks
# to the PEP 442.
if compat.PY34:
def __del__(self):
if self._sock is not None:
warnings.warn("unclosed transport %r" % self, ResourceWarning,
source=self)
self.close()
def __del__(self, _warn=warnings.warn):
if self._sock is not None:
_warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
self._sock.close()
def _fatal_error(self, exc, message='Fatal error on pipe transport'):
if isinstance(exc, base_events._FATAL_ERROR_IGNORE):
if self._loop.get_debug():
logger.debug("%r: %s", self, message, exc_info=True)
else:
self._loop.call_exception_handler({
'message': message,
'exception': exc,
'transport': self,
'protocol': self._protocol,
})
self._force_close(exc)
try:
if isinstance(exc, OSError):
if self._loop.get_debug():
logger.debug("%r: %s", self, message, exc_info=True)
else:
self._loop.call_exception_handler({
'message': message,
'exception': exc,
'transport': self,
'protocol': self._protocol,
})
finally:
self._force_close(exc)
def _force_close(self, exc):
if self._closing:
if self._empty_waiter is not None and not self._empty_waiter.done():
if exc is None:
self._empty_waiter.set_result(None)
else:
self._empty_waiter.set_exception(exc)
if self._closing and self._called_connection_lost:
return
self._closing = True
self._conn_lost += 1
@@ -125,6 +152,8 @@ class _ProactorBasePipeTransport(transports._FlowControlMixin,
self._loop.call_soon(self._call_connection_lost, exc)
def _call_connection_lost(self, exc):
if self._called_connection_lost:
return
try:
self._protocol.connection_lost(exc)
finally:
@@ -132,7 +161,7 @@ class _ProactorBasePipeTransport(transports._FlowControlMixin,
# end then it may fail with ERROR_NETNAME_DELETED if we
# just close our end. First calling shutdown() seems to
# cure it, but maybe using DisconnectEx() would be better.
if hasattr(self._sock, 'shutdown'):
if hasattr(self._sock, 'shutdown') and self._sock.fileno() != -1:
self._sock.shutdown(socket.SHUT_RDWR)
self._sock.close()
self._sock = None
@@ -140,6 +169,7 @@ class _ProactorBasePipeTransport(transports._FlowControlMixin,
if server is not None:
server._detach()
self._server = None
self._called_connection_lost = True
def get_write_buffer_size(self):
size = self._pending_write
@@ -153,53 +183,127 @@ class _ProactorReadPipeTransport(_ProactorBasePipeTransport,
"""Transport for read pipes."""
def __init__(self, loop, sock, protocol, waiter=None,
extra=None, server=None):
extra=None, server=None, buffer_size=65536):
self._pending_data_length = -1
self._paused = True
super().__init__(loop, sock, protocol, waiter, extra, server)
self._paused = False
self._data = bytearray(buffer_size)
self._loop.call_soon(self._loop_reading)
self._paused = False
def is_reading(self):
return not self._paused and not self._closing
def pause_reading(self):
if self._closing:
raise RuntimeError('Cannot pause_reading() when closing')
if self._paused:
raise RuntimeError('Already paused')
if self._closing or self._paused:
return
self._paused = True
# bpo-33694: Don't cancel self._read_fut because cancelling an
# overlapped WSASend() loss silently data with the current proactor
# implementation.
#
# If CancelIoEx() fails with ERROR_NOT_FOUND, it means that WSASend()
# completed (even if HasOverlappedIoCompleted() returns 0), but
# Overlapped.cancel() currently silently ignores the ERROR_NOT_FOUND
# error. Once the overlapped is ignored, the IOCP loop will ignores the
# completion I/O event and so not read the result of the overlapped
# WSARecv().
if self._loop.get_debug():
logger.debug("%r pauses reading", self)
def resume_reading(self):
if not self._paused:
raise RuntimeError('Not paused')
self._paused = False
if self._closing:
if self._closing or not self._paused:
return
self._loop.call_soon(self._loop_reading, self._read_fut)
self._paused = False
if self._read_fut is None:
self._loop.call_soon(self._loop_reading, None)
length = self._pending_data_length
self._pending_data_length = -1
if length > -1:
# Call the protocol method after calling _loop_reading(),
# since the protocol can decide to pause reading again.
self._loop.call_soon(self._data_received, self._data[:length], length)
if self._loop.get_debug():
logger.debug("%r resumes reading", self)
def _loop_reading(self, fut=None):
if self._paused:
return
data = None
def _eof_received(self):
if self._loop.get_debug():
logger.debug("%r received EOF", self)
try:
keep_open = self._protocol.eof_received()
except (SystemExit, KeyboardInterrupt):
raise
except BaseException as exc:
self._fatal_error(
exc, 'Fatal error: protocol.eof_received() call failed.')
return
if not keep_open:
self.close()
def _data_received(self, data, length):
if self._paused:
# Don't call any protocol method while reading is paused.
# The protocol will be called on resume_reading().
assert self._pending_data_length == -1
self._pending_data_length = length
return
if length == 0:
self._eof_received()
return
if isinstance(self._protocol, protocols.BufferedProtocol):
try:
protocols._feed_data_to_buffered_proto(self._protocol, data)
except (SystemExit, KeyboardInterrupt):
raise
except BaseException as exc:
self._fatal_error(exc,
'Fatal error: protocol.buffer_updated() '
'call failed.')
return
else:
self._protocol.data_received(data)
def _loop_reading(self, fut=None):
length = -1
data = None
try:
if fut is not None:
assert self._read_fut is fut or (self._read_fut is None and
self._closing)
self._read_fut = None
data = fut.result() # deliver data later in "finally" clause
if fut.done():
# deliver data later in "finally" clause
length = fut.result()
if length == 0:
# we got end-of-file so no need to reschedule a new read
return
# It's a new slice so make it immutable so protocols upstream don't have problems
data = bytes(memoryview(self._data)[:length])
else:
# the future will be replaced by next proactor.recv call
fut.cancel()
if self._closing:
# since close() has been called we ignore any read data
data = None
return
if data == b'':
# we got end-of-file so no need to reschedule a new read
return
# bpo-33694: buffer_updated() has currently no fast path because of
# a data loss issue caused by overlapped WSASend() cancellation.
# reschedule a new read
self._read_fut = self._loop._proactor.recv(self._sock, 4096)
if not self._paused:
# reschedule a new read
self._read_fut = self._loop._proactor.recv_into(self._sock, self._data)
except ConnectionAbortedError as exc:
if not self._closing:
self._fatal_error(exc, 'Fatal read error on pipe transport')
@@ -210,32 +314,36 @@ class _ProactorReadPipeTransport(_ProactorBasePipeTransport,
self._force_close(exc)
except OSError as exc:
self._fatal_error(exc, 'Fatal read error on pipe transport')
except futures.CancelledError:
except exceptions.CancelledError:
if not self._closing:
raise
else:
self._read_fut.add_done_callback(self._loop_reading)
if not self._paused:
self._read_fut.add_done_callback(self._loop_reading)
finally:
if data:
self._protocol.data_received(data)
elif data is not None:
if self._loop.get_debug():
logger.debug("%r received EOF", self)
keep_open = self._protocol.eof_received()
if not keep_open:
self.close()
if length > -1:
self._data_received(data, length)
class _ProactorBaseWritePipeTransport(_ProactorBasePipeTransport,
transports.WriteTransport):
"""Transport for write pipes."""
_start_tls_compatible = True
def __init__(self, *args, **kw):
super().__init__(*args, **kw)
self._empty_waiter = None
def write(self, data):
if not isinstance(data, (bytes, bytearray, memoryview)):
raise TypeError('data argument must be byte-ish (%r)',
type(data))
raise TypeError(
f"data argument must be a bytes-like object, "
f"not {type(data).__name__}")
if self._eof_written:
raise RuntimeError('write_eof() already called')
if self._empty_waiter is not None:
raise RuntimeError('unable to write; sendfile is in progress')
if not data:
return
@@ -267,6 +375,10 @@ class _ProactorBaseWritePipeTransport(_ProactorBasePipeTransport,
def _loop_writing(self, f=None, data=None):
try:
if f is not None and self._write_fut is None and self._closing:
# XXX most likely self._force_close() has been called, and
# it has set self._write_fut to None.
return
assert f is self._write_fut
self._write_fut = None
self._pending_write = 0
@@ -295,6 +407,8 @@ class _ProactorBaseWritePipeTransport(_ProactorBasePipeTransport,
self._maybe_pause_protocol()
else:
self._write_fut.add_done_callback(self._loop_writing)
if self._empty_waiter is not None and self._write_fut is None:
self._empty_waiter.set_result(None)
except ConnectionResetError as exc:
self._force_close(exc)
except OSError as exc:
@@ -309,6 +423,17 @@ class _ProactorBaseWritePipeTransport(_ProactorBasePipeTransport,
def abort(self):
self._force_close(None)
def _make_empty_waiter(self):
if self._empty_waiter is not None:
raise RuntimeError("Empty waiter is already set")
self._empty_waiter = self._loop.create_future()
if self._write_fut is None:
self._empty_waiter.set_result(None)
return self._empty_waiter
def _reset_empty_waiter(self):
self._empty_waiter = None
class _ProactorWritePipeTransport(_ProactorBaseWritePipeTransport):
def __init__(self, *args, **kw):
@@ -332,6 +457,138 @@ class _ProactorWritePipeTransport(_ProactorBaseWritePipeTransport):
self.close()
class _ProactorDatagramTransport(_ProactorBasePipeTransport,
transports.DatagramTransport):
max_size = 256 * 1024
def __init__(self, loop, sock, protocol, address=None,
waiter=None, extra=None):
self._address = address
self._empty_waiter = None
self._buffer_size = 0
# We don't need to call _protocol.connection_made() since our base
# constructor does it for us.
super().__init__(loop, sock, protocol, waiter=waiter, extra=extra)
# The base constructor sets _buffer = None, so we set it here
self._buffer = collections.deque()
self._loop.call_soon(self._loop_reading)
def _set_extra(self, sock):
_set_socket_extra(self, sock)
def get_write_buffer_size(self):
return self._buffer_size
def abort(self):
self._force_close(None)
def sendto(self, data, addr=None):
if not isinstance(data, (bytes, bytearray, memoryview)):
raise TypeError('data argument must be bytes-like object (%r)',
type(data))
if not data:
return
if self._address is not None and addr not in (None, self._address):
raise ValueError(
f'Invalid address: must be None or {self._address}')
if self._conn_lost and self._address:
if self._conn_lost >= constants.LOG_THRESHOLD_FOR_CONNLOST_WRITES:
logger.warning('socket.sendto() raised exception.')
self._conn_lost += 1
return
# Ensure that what we buffer is immutable.
self._buffer.append((bytes(data), addr))
self._buffer_size += len(data)
if self._write_fut is None:
# No current write operations are active, kick one off
self._loop_writing()
# else: A write operation is already kicked off
self._maybe_pause_protocol()
def _loop_writing(self, fut=None):
try:
if self._conn_lost:
return
assert fut is self._write_fut
self._write_fut = None
if fut:
# We are in a _loop_writing() done callback, get the result
fut.result()
if not self._buffer or (self._conn_lost and self._address):
# The connection has been closed
if self._closing:
self._loop.call_soon(self._call_connection_lost, None)
return
data, addr = self._buffer.popleft()
self._buffer_size -= len(data)
if self._address is not None:
self._write_fut = self._loop._proactor.send(self._sock,
data)
else:
self._write_fut = self._loop._proactor.sendto(self._sock,
data,
addr=addr)
except OSError as exc:
self._protocol.error_received(exc)
except Exception as exc:
self._fatal_error(exc, 'Fatal write error on datagram transport')
else:
self._write_fut.add_done_callback(self._loop_writing)
self._maybe_resume_protocol()
def _loop_reading(self, fut=None):
data = None
try:
if self._conn_lost:
return
assert self._read_fut is fut or (self._read_fut is None and
self._closing)
self._read_fut = None
if fut is not None:
res = fut.result()
if self._closing:
# since close() has been called we ignore any read data
data = None
return
if self._address is not None:
data, addr = res, self._address
else:
data, addr = res
if self._conn_lost:
return
if self._address is not None:
self._read_fut = self._loop._proactor.recv(self._sock,
self.max_size)
else:
self._read_fut = self._loop._proactor.recvfrom(self._sock,
self.max_size)
except OSError as exc:
self._protocol.error_received(exc)
except exceptions.CancelledError:
if not self._closing:
raise
else:
if self._read_fut is not None:
self._read_fut.add_done_callback(self._loop_reading)
finally:
if data:
self._protocol.datagram_received(data, addr)
class _ProactorDuplexPipeTransport(_ProactorReadPipeTransport,
_ProactorBaseWritePipeTransport,
transports.Transport):
@@ -349,21 +606,15 @@ class _ProactorSocketTransport(_ProactorReadPipeTransport,
transports.Transport):
"""Transport for connected sockets."""
_sendfile_compatible = constants._SendfileMode.TRY_NATIVE
def __init__(self, loop, sock, protocol, waiter=None,
extra=None, server=None):
super().__init__(loop, sock, protocol, waiter, extra, server)
base_events._set_nodelay(sock)
def _set_extra(self, sock):
self._extra['socket'] = sock
try:
self._extra['sockname'] = sock.getsockname()
except (socket.error, AttributeError):
if self._loop.get_debug():
logger.warning("getsockname() failed on %r",
sock, exc_info=True)
if 'peername' not in self._extra:
try:
self._extra['peername'] = sock.getpeername()
except (socket.error, AttributeError):
if self._loop.get_debug():
logger.warning("getpeername() failed on %r",
sock, exc_info=True)
_set_socket_extra(self, sock)
def can_write_eof(self):
return True
@@ -387,26 +638,35 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
self._accept_futures = {} # socket file descriptor => Future
proactor.set_loop(self)
self._make_self_pipe()
if threading.current_thread() is threading.main_thread():
# wakeup fd can only be installed to a file descriptor from the main thread
signal.set_wakeup_fd(self._csock.fileno())
def _make_socket_transport(self, sock, protocol, waiter=None,
extra=None, server=None):
return _ProactorSocketTransport(self, sock, protocol, waiter,
extra, server)
def _make_ssl_transport(self, rawsock, protocol, sslcontext, waiter=None,
*, server_side=False, server_hostname=None,
extra=None, server=None):
if not sslproto._is_sslproto_available():
raise NotImplementedError("Proactor event loop requires Python 3.5"
" or newer (ssl.MemoryBIO) to support "
"SSL")
ssl_protocol = sslproto.SSLProtocol(self, protocol, sslcontext, waiter,
server_side, server_hostname)
def _make_ssl_transport(
self, rawsock, protocol, sslcontext, waiter=None,
*, server_side=False, server_hostname=None,
extra=None, server=None,
ssl_handshake_timeout=None,
ssl_shutdown_timeout=None):
ssl_protocol = sslproto.SSLProtocol(
self, protocol, sslcontext, waiter,
server_side, server_hostname,
ssl_handshake_timeout=ssl_handshake_timeout,
ssl_shutdown_timeout=ssl_shutdown_timeout)
_ProactorSocketTransport(self, rawsock, ssl_protocol,
extra=extra, server=server)
return ssl_protocol._app_transport
def _make_datagram_transport(self, sock, protocol,
address=None, waiter=None, extra=None):
return _ProactorDatagramTransport(self, sock, protocol, address,
waiter, extra)
def _make_duplex_pipe_transport(self, sock, protocol, waiter=None,
extra=None):
return _ProactorDuplexPipeTransport(self,
@@ -428,6 +688,8 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
if self.is_closed():
return
if threading.current_thread() is threading.main_thread():
signal.set_wakeup_fd(-1)
# Call these methods before closing the event loop (before calling
# BaseEventLoop.close), because they can schedule callbacks with
# call_soon(), which is forbidden when the event loop is closed.
@@ -440,20 +702,73 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
# Close the event loop
super().close()
def sock_recv(self, sock, n):
return self._proactor.recv(sock, n)
async def sock_recv(self, sock, n):
return await self._proactor.recv(sock, n)
def sock_sendall(self, sock, data):
return self._proactor.send(sock, data)
async def sock_recv_into(self, sock, buf):
return await self._proactor.recv_into(sock, buf)
def sock_connect(self, sock, address):
return self._proactor.connect(sock, address)
async def sock_recvfrom(self, sock, bufsize):
return await self._proactor.recvfrom(sock, bufsize)
def sock_accept(self, sock):
return self._proactor.accept(sock)
async def sock_recvfrom_into(self, sock, buf, nbytes=0):
if not nbytes:
nbytes = len(buf)
def _socketpair(self):
raise NotImplementedError
return await self._proactor.recvfrom_into(sock, buf, nbytes)
async def sock_sendall(self, sock, data):
return await self._proactor.send(sock, data)
async def sock_sendto(self, sock, data, address):
return await self._proactor.sendto(sock, data, 0, address)
async def sock_connect(self, sock, address):
return await self._proactor.connect(sock, address)
async def sock_accept(self, sock):
return await self._proactor.accept(sock)
async def _sock_sendfile_native(self, sock, file, offset, count):
try:
fileno = file.fileno()
except (AttributeError, io.UnsupportedOperation) as err:
raise exceptions.SendfileNotAvailableError("not a regular file")
try:
fsize = os.fstat(fileno).st_size
except OSError:
raise exceptions.SendfileNotAvailableError("not a regular file")
blocksize = count if count else fsize
if not blocksize:
return 0 # empty file
blocksize = min(blocksize, 0xffff_ffff)
end_pos = min(offset + count, fsize) if count else fsize
offset = min(offset, fsize)
total_sent = 0
try:
while True:
blocksize = min(end_pos - offset, blocksize)
if blocksize <= 0:
return total_sent
await self._proactor.sendfile(sock, file, offset, blocksize)
offset += blocksize
total_sent += blocksize
finally:
if total_sent > 0:
file.seek(offset)
async def _sendfile_native(self, transp, file, offset, count):
resume_reading = transp.is_reading()
transp.pause_reading()
await transp._make_empty_waiter()
try:
return await self.sock_sendfile(transp._sock, file, offset, count,
fallback=False)
finally:
transp._reset_empty_waiter()
if resume_reading:
transp.resume_reading()
def _close_self_pipe(self):
if self._self_reading_future is not None:
@@ -467,21 +782,30 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
def _make_self_pipe(self):
# A self-socket, really. :-)
self._ssock, self._csock = self._socketpair()
self._ssock, self._csock = socket.socketpair()
self._ssock.setblocking(False)
self._csock.setblocking(False)
self._internal_fds += 1
self.call_soon(self._loop_self_reading)
def _loop_self_reading(self, f=None):
try:
if f is not None:
f.result() # may raise
if self._self_reading_future is not f:
# When we scheduled this Future, we assigned it to
# _self_reading_future. If it's not there now, something has
# tried to cancel the loop while this callback was still in the
# queue (see windows_events.ProactorEventLoop.run_forever). In
# that case stop here instead of continuing to schedule a new
# iteration.
return
f = self._proactor.recv(self._ssock, 4096)
except futures.CancelledError:
except exceptions.CancelledError:
# _close_self_pipe() has been called, stop waiting for data
return
except Exception as exc:
except (SystemExit, KeyboardInterrupt):
raise
except BaseException as exc:
self.call_exception_handler({
'message': 'Error on reading from the event loop self pipe',
'exception': exc,
@@ -492,10 +816,27 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
f.add_done_callback(self._loop_self_reading)
def _write_to_self(self):
self._csock.send(b'\0')
# This may be called from a different thread, possibly after
# _close_self_pipe() has been called or even while it is
# running. Guard for self._csock being None or closed. When
# a socket is closed, send() raises OSError (with errno set to
# EBADF, but let's not rely on the exact error code).
csock = self._csock
if csock is None:
return
try:
csock.send(b'\0')
except OSError:
if self._debug:
logger.debug("Fail to write a null byte into the "
"self-pipe socket",
exc_info=True)
def _start_serving(self, protocol_factory, sock,
sslcontext=None, server=None, backlog=100):
sslcontext=None, server=None, backlog=100,
ssl_handshake_timeout=None,
ssl_shutdown_timeout=None):
def loop(f=None):
try:
@@ -508,7 +849,9 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
if sslcontext is not None:
self._make_ssl_transport(
conn, protocol, sslcontext, server_side=True,
extra={'peername': addr}, server=server)
extra={'peername': addr}, server=server,
ssl_handshake_timeout=ssl_handshake_timeout,
ssl_shutdown_timeout=ssl_shutdown_timeout)
else:
self._make_socket_transport(
conn, protocol,
@@ -521,13 +864,13 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
self.call_exception_handler({
'message': 'Accept failed on a socket',
'exception': exc,
'socket': sock,
'socket': trsock.TransportSocket(sock),
})
sock.close()
elif self._debug:
logger.debug("Accept failed on socket %r",
sock, exc_info=True)
except futures.CancelledError:
except exceptions.CancelledError:
sock.close()
else:
self._accept_futures[sock.fileno()] = f
@@ -545,6 +888,8 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
self._accept_futures.clear()
def _stop_serving(self, sock):
self._stop_accept_futures()
future = self._accept_futures.pop(sock.fileno(), None)
if future:
future.cancel()
self._proactor._stop_serving(sock)
sock.close()

View File

@@ -1,7 +1,9 @@
"""Abstract Protocol class."""
"""Abstract Protocol base classes."""
__all__ = ['BaseProtocol', 'Protocol', 'DatagramProtocol',
'SubprocessProtocol']
__all__ = (
'BaseProtocol', 'Protocol', 'DatagramProtocol',
'SubprocessProtocol', 'BufferedProtocol',
)
class BaseProtocol:
@@ -14,6 +16,8 @@ class BaseProtocol:
write-only transport like write pipe
"""
__slots__ = ()
def connection_made(self, transport):
"""Called when a connection is made.
@@ -85,6 +89,8 @@ class Protocol(BaseProtocol):
* CL: connection_lost()
"""
__slots__ = ()
def data_received(self, data):
"""Called when some data is received.
@@ -100,9 +106,64 @@ class Protocol(BaseProtocol):
"""
class BufferedProtocol(BaseProtocol):
"""Interface for stream protocol with manual buffer control.
Event methods, such as `create_server` and `create_connection`,
accept factories that return protocols that implement this interface.
The idea of BufferedProtocol is that it allows to manually allocate
and control the receive buffer. Event loops can then use the buffer
provided by the protocol to avoid unnecessary data copies. This
can result in noticeable performance improvement for protocols that
receive big amounts of data. Sophisticated protocols can allocate
the buffer only once at creation time.
State machine of calls:
start -> CM [-> GB [-> BU?]]* [-> ER?] -> CL -> end
* CM: connection_made()
* GB: get_buffer()
* BU: buffer_updated()
* ER: eof_received()
* CL: connection_lost()
"""
__slots__ = ()
def get_buffer(self, sizehint):
"""Called to allocate a new receive buffer.
*sizehint* is a recommended minimal size for the returned
buffer. When set to -1, the buffer size can be arbitrary.
Must return an object that implements the
:ref:`buffer protocol <bufferobjects>`.
It is an error to return a zero-sized buffer.
"""
def buffer_updated(self, nbytes):
"""Called when the buffer was updated with the received data.
*nbytes* is the total number of bytes that were written to
the buffer.
"""
def eof_received(self):
"""Called when the other end calls write_eof() or equivalent.
If this returns a false value (including None), the transport
will close itself. If it returns a true value, closing the
transport is up to the protocol.
"""
class DatagramProtocol(BaseProtocol):
"""Interface for datagram protocol."""
__slots__ = ()
def datagram_received(self, data, addr):
"""Called when some datagram is received."""
@@ -116,6 +177,8 @@ class DatagramProtocol(BaseProtocol):
class SubprocessProtocol(BaseProtocol):
"""Interface for protocol for subprocess calls."""
__slots__ = ()
def pipe_data_received(self, fd, data):
"""Called when the subprocess writes data into stdout/stderr pipe.
@@ -132,3 +195,22 @@ class SubprocessProtocol(BaseProtocol):
def process_exited(self):
"""Called when subprocess has exited."""
def _feed_data_to_buffered_proto(proto, data):
data_len = len(data)
while data_len:
buf = proto.get_buffer(data_len)
buf_len = len(buf)
if not buf_len:
raise RuntimeError('get_buffer() returned an empty buffer')
if buf_len >= data_len:
buf[:data_len] = data
proto.buffer_updated(data_len)
return
else:
buf[:buf_len] = data[:buf_len]
proto.buffer_updated(buf_len)
data = data[buf_len:]
data_len = len(data)

90
Lib/asyncio/queues.py vendored
View File

@@ -1,35 +1,28 @@
"""Queues"""
__all__ = ['Queue', 'PriorityQueue', 'LifoQueue', 'QueueFull', 'QueueEmpty']
__all__ = ('Queue', 'PriorityQueue', 'LifoQueue', 'QueueFull', 'QueueEmpty')
import collections
import heapq
from types import GenericAlias
from . import compat
from . import events
from . import locks
from .coroutines import coroutine
from . import mixins
class QueueEmpty(Exception):
"""Exception raised when Queue.get_nowait() is called on a Queue object
which is empty.
"""
"""Raised when Queue.get_nowait() is called on an empty Queue."""
pass
class QueueFull(Exception):
"""Exception raised when the Queue.put_nowait() method is called on a Queue
object which is full.
"""
"""Raised when the Queue.put_nowait() method is called on a full Queue."""
pass
class Queue:
class Queue(mixins._LoopBoundMixin):
"""A queue, useful for coordinating producer and consumer coroutines.
If maxsize is less than or equal to zero, the queue size is infinite. If it
is an integer greater than 0, then "yield from put()" will block when the
is an integer greater than 0, then "await put()" will block when the
queue reaches maxsize, until an item is removed by get().
Unlike the standard library Queue, you can reliably know this Queue's size
@@ -37,11 +30,7 @@ class Queue:
interrupted between calling qsize() and doing an operation on the Queue.
"""
def __init__(self, maxsize=0, *, loop=None):
if loop is None:
self._loop = events.get_event_loop()
else:
self._loop = loop
def __init__(self, maxsize=0):
self._maxsize = maxsize
# Futures.
@@ -49,7 +38,7 @@ class Queue:
# Futures.
self._putters = collections.deque()
self._unfinished_tasks = 0
self._finished = locks.Event(loop=self._loop)
self._finished = locks.Event()
self._finished.set()
self._init(maxsize)
@@ -75,25 +64,23 @@ class Queue:
break
def __repr__(self):
return '<{} at {:#x} {}>'.format(
type(self).__name__, id(self), self._format())
return f'<{type(self).__name__} at {id(self):#x} {self._format()}>'
def __str__(self):
return '<{} {}>'.format(type(self).__name__, self._format())
return f'<{type(self).__name__} {self._format()}>'
def __class_getitem__(cls, type):
return cls
__class_getitem__ = classmethod(GenericAlias)
def _format(self):
result = 'maxsize={!r}'.format(self._maxsize)
result = f'maxsize={self._maxsize!r}'
if getattr(self, '_queue', None):
result += ' _queue={!r}'.format(list(self._queue))
result += f' _queue={list(self._queue)!r}'
if self._getters:
result += ' _getters[{}]'.format(len(self._getters))
result += f' _getters[{len(self._getters)}]'
if self._putters:
result += ' _putters[{}]'.format(len(self._putters))
result += f' _putters[{len(self._putters)}]'
if self._unfinished_tasks:
result += ' tasks={}'.format(self._unfinished_tasks)
result += f' tasks={self._unfinished_tasks}'
return result
def qsize(self):
@@ -120,22 +107,26 @@ class Queue:
else:
return self.qsize() >= self._maxsize
@coroutine
def put(self, item):
async def put(self, item):
"""Put an item into the queue.
Put an item into the queue. If the queue is full, wait until a free
slot is available before adding item.
This method is a coroutine.
"""
while self.full():
putter = self._loop.create_future()
putter = self._get_loop().create_future()
self._putters.append(putter)
try:
yield from putter
await putter
except:
putter.cancel() # Just in case putter is not done yet.
try:
# Clean self._putters from canceled putters.
self._putters.remove(putter)
except ValueError:
# The putter could be removed from self._putters by a
# previous get_nowait call.
pass
if not self.full() and not putter.cancelled():
# We were woken up by get_nowait(), but can't take
# the call. Wake up the next in line.
@@ -155,21 +146,25 @@ class Queue:
self._finished.clear()
self._wakeup_next(self._getters)
@coroutine
def get(self):
async def get(self):
"""Remove and return an item from the queue.
If queue is empty, wait until an item is available.
This method is a coroutine.
"""
while self.empty():
getter = self._loop.create_future()
getter = self._get_loop().create_future()
self._getters.append(getter)
try:
yield from getter
await getter
except:
getter.cancel() # Just in case getter is not done yet.
try:
# Clean self._getters from canceled getters.
self._getters.remove(getter)
except ValueError:
# The getter could be removed from self._getters by a
# previous put_nowait call.
pass
if not self.empty() and not getter.cancelled():
# We were woken up by put_nowait(), but can't take
# the call. Wake up the next in line.
@@ -208,8 +203,7 @@ class Queue:
if self._unfinished_tasks == 0:
self._finished.set()
@coroutine
def join(self):
async def join(self):
"""Block until all items in the queue have been gotten and processed.
The count of unfinished tasks goes up whenever an item is added to the
@@ -218,7 +212,7 @@ class Queue:
When the count of unfinished tasks drops to zero, join() unblocks.
"""
if self._unfinished_tasks > 0:
yield from self._finished.wait()
await self._finished.wait()
class PriorityQueue(Queue):
@@ -248,9 +242,3 @@ class LifoQueue(Queue):
def _get(self):
return self._queue.pop()
if not compat.PY35:
JoinableQueue = Queue
"""Deprecated alias for Queue."""
__all__.append('JoinableQueue')

187
Lib/asyncio/runners.py vendored
View File

@@ -1,16 +1,168 @@
__all__ = ['run']
__all__ = ('Runner', 'run')
import contextvars
import enum
import functools
import threading
import signal
from . import coroutines
from . import events
from . import exceptions
from . import tasks
from . import constants
class _State(enum.Enum):
CREATED = "created"
INITIALIZED = "initialized"
CLOSED = "closed"
def run(main, *, debug=False):
"""Run a coroutine.
class Runner:
"""A context manager that controls event loop life cycle.
The context manager always creates a new event loop,
allows to run async functions inside it,
and properly finalizes the loop at the context manager exit.
If debug is True, the event loop will be run in debug mode.
If loop_factory is passed, it is used for new event loop creation.
asyncio.run(main(), debug=True)
is a shortcut for
with asyncio.Runner(debug=True) as runner:
runner.run(main())
The run() method can be called multiple times within the runner's context.
This can be useful for interactive console (e.g. IPython),
unittest runners, console tools, -- everywhere when async code
is called from existing sync framework and where the preferred single
asyncio.run() call doesn't work.
"""
# Note: the class is final, it is not intended for inheritance.
def __init__(self, *, debug=None, loop_factory=None):
self._state = _State.CREATED
self._debug = debug
self._loop_factory = loop_factory
self._loop = None
self._context = None
self._interrupt_count = 0
self._set_event_loop = False
def __enter__(self):
self._lazy_init()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
def close(self):
"""Shutdown and close event loop."""
if self._state is not _State.INITIALIZED:
return
try:
loop = self._loop
_cancel_all_tasks(loop)
loop.run_until_complete(loop.shutdown_asyncgens())
loop.run_until_complete(
loop.shutdown_default_executor(constants.THREAD_JOIN_TIMEOUT))
finally:
if self._set_event_loop:
events.set_event_loop(None)
loop.close()
self._loop = None
self._state = _State.CLOSED
def get_loop(self):
"""Return embedded event loop."""
self._lazy_init()
return self._loop
def run(self, coro, *, context=None):
"""Run a coroutine inside the embedded event loop."""
if not coroutines.iscoroutine(coro):
raise ValueError("a coroutine was expected, got {!r}".format(coro))
if events._get_running_loop() is not None:
# fail fast with short traceback
raise RuntimeError(
"Runner.run() cannot be called from a running event loop")
self._lazy_init()
if context is None:
context = self._context
task = self._loop.create_task(coro, context=context)
if (threading.current_thread() is threading.main_thread()
and signal.getsignal(signal.SIGINT) is signal.default_int_handler
):
sigint_handler = functools.partial(self._on_sigint, main_task=task)
try:
signal.signal(signal.SIGINT, sigint_handler)
except ValueError:
# `signal.signal` may throw if `threading.main_thread` does
# not support signals (e.g. embedded interpreter with signals
# not registered - see gh-91880)
sigint_handler = None
else:
sigint_handler = None
self._interrupt_count = 0
try:
return self._loop.run_until_complete(task)
except exceptions.CancelledError:
if self._interrupt_count > 0:
uncancel = getattr(task, "uncancel", None)
if uncancel is not None and uncancel() == 0:
raise KeyboardInterrupt()
raise # CancelledError
finally:
if (sigint_handler is not None
and signal.getsignal(signal.SIGINT) is sigint_handler
):
signal.signal(signal.SIGINT, signal.default_int_handler)
def _lazy_init(self):
if self._state is _State.CLOSED:
raise RuntimeError("Runner is closed")
if self._state is _State.INITIALIZED:
return
if self._loop_factory is None:
self._loop = events.new_event_loop()
if not self._set_event_loop:
# Call set_event_loop only once to avoid calling
# attach_loop multiple times on child watchers
events.set_event_loop(self._loop)
self._set_event_loop = True
else:
self._loop = self._loop_factory()
if self._debug is not None:
self._loop.set_debug(self._debug)
self._context = contextvars.copy_context()
self._state = _State.INITIALIZED
def _on_sigint(self, signum, frame, main_task):
self._interrupt_count += 1
if self._interrupt_count == 1 and not main_task.done():
main_task.cancel()
# wakeup loop if it is blocked by select() with long timeout
self._loop.call_soon_threadsafe(lambda: None)
return
raise KeyboardInterrupt()
def run(main, *, debug=None, loop_factory=None):
"""Execute the coroutine and return the result.
This function runs the passed coroutine, taking care of
managing the asyncio event loop and finalizing asynchronous
generators.
managing the asyncio event loop, finalizing asynchronous
generators and closing the default executor.
This function cannot be called when another asyncio event loop is
running in the same thread.
@@ -21,6 +173,10 @@ def run(main, *, debug=False):
It should be used as a main entry point for asyncio programs, and should
ideally only be called once.
The executor is given a timeout duration of 5 minutes to shutdown.
If the executor hasn't finished within that duration, a warning is
emitted and the executor is closed.
Example:
async def main():
@@ -30,24 +186,12 @@ def run(main, *, debug=False):
asyncio.run(main())
"""
if events._get_running_loop() is not None:
# fail fast with short traceback
raise RuntimeError(
"asyncio.run() cannot be called from a running event loop")
if not coroutines.iscoroutine(main):
raise ValueError("a coroutine was expected, got {!r}".format(main))
loop = events.new_event_loop()
try:
events.set_event_loop(loop)
loop.set_debug(debug)
return loop.run_until_complete(main)
finally:
try:
_cancel_all_tasks(loop)
loop.run_until_complete(loop.shutdown_asyncgens())
finally:
events.set_event_loop(None)
loop.close()
with Runner(debug=debug, loop_factory=loop_factory) as runner:
return runner.run(main)
def _cancel_all_tasks(loop):
@@ -58,8 +202,7 @@ def _cancel_all_tasks(loop):
for task in to_cancel:
task.cancel()
loop.run_until_complete(
tasks.gather(*to_cancel, loop=loop, return_exceptions=True))
loop.run_until_complete(tasks.gather(*to_cancel, return_exceptions=True))
for task in to_cancel:
if task.cancelled():

File diff suppressed because it is too large Load Diff

1136
Lib/asyncio/sslproto.py vendored

File diff suppressed because it is too large Load Diff

149
Lib/asyncio/staggered.py vendored Normal file
View File

@@ -0,0 +1,149 @@
"""Support for running coroutines in parallel with staggered start times."""
__all__ = 'staggered_race',
import contextlib
import typing
from . import events
from . import exceptions as exceptions_mod
from . import locks
from . import tasks
async def staggered_race(
coro_fns: typing.Iterable[typing.Callable[[], typing.Awaitable]],
delay: typing.Optional[float],
*,
loop: events.AbstractEventLoop = None,
) -> typing.Tuple[
typing.Any,
typing.Optional[int],
typing.List[typing.Optional[Exception]]
]:
"""Run coroutines with staggered start times and take the first to finish.
This method takes an iterable of coroutine functions. The first one is
started immediately. From then on, whenever the immediately preceding one
fails (raises an exception), or when *delay* seconds has passed, the next
coroutine is started. This continues until one of the coroutines complete
successfully, in which case all others are cancelled, or until all
coroutines fail.
The coroutines provided should be well-behaved in the following way:
* They should only ``return`` if completed successfully.
* They should always raise an exception if they did not complete
successfully. In particular, if they handle cancellation, they should
probably reraise, like this::
try:
# do work
except asyncio.CancelledError:
# undo partially completed work
raise
Args:
coro_fns: an iterable of coroutine functions, i.e. callables that
return a coroutine object when called. Use ``functools.partial`` or
lambdas to pass arguments.
delay: amount of time, in seconds, between starting coroutines. If
``None``, the coroutines will run sequentially.
loop: the event loop to use.
Returns:
tuple *(winner_result, winner_index, exceptions)* where
- *winner_result*: the result of the winning coroutine, or ``None``
if no coroutines won.
- *winner_index*: the index of the winning coroutine in
``coro_fns``, or ``None`` if no coroutines won. If the winning
coroutine may return None on success, *winner_index* can be used
to definitively determine whether any coroutine won.
- *exceptions*: list of exceptions returned by the coroutines.
``len(exceptions)`` is equal to the number of coroutines actually
started, and the order is the same as in ``coro_fns``. The winning
coroutine's entry is ``None``.
"""
# TODO: when we have aiter() and anext(), allow async iterables in coro_fns.
loop = loop or events.get_running_loop()
enum_coro_fns = enumerate(coro_fns)
winner_result = None
winner_index = None
exceptions = []
running_tasks = []
async def run_one_coro(
previous_failed: typing.Optional[locks.Event]) -> None:
# Wait for the previous task to finish, or for delay seconds
if previous_failed is not None:
with contextlib.suppress(exceptions_mod.TimeoutError):
# Use asyncio.wait_for() instead of asyncio.wait() here, so
# that if we get cancelled at this point, Event.wait() is also
# cancelled, otherwise there will be a "Task destroyed but it is
# pending" later.
await tasks.wait_for(previous_failed.wait(), delay)
# Get the next coroutine to run
try:
this_index, coro_fn = next(enum_coro_fns)
except StopIteration:
return
# Start task that will run the next coroutine
this_failed = locks.Event()
next_task = loop.create_task(run_one_coro(this_failed))
running_tasks.append(next_task)
assert len(running_tasks) == this_index + 2
# Prepare place to put this coroutine's exceptions if not won
exceptions.append(None)
assert len(exceptions) == this_index + 1
try:
result = await coro_fn()
except (SystemExit, KeyboardInterrupt):
raise
except BaseException as e:
exceptions[this_index] = e
this_failed.set() # Kickstart the next coroutine
else:
# Store winner's results
nonlocal winner_index, winner_result
assert winner_index is None
winner_index = this_index
winner_result = result
# Cancel all other tasks. We take care to not cancel the current
# task as well. If we do so, then since there is no `await` after
# here and CancelledError are usually thrown at one, we will
# encounter a curious corner case where the current task will end
# up as done() == True, cancelled() == False, exception() ==
# asyncio.CancelledError. This behavior is specified in
# https://bugs.python.org/issue30048
for i, t in enumerate(running_tasks):
if i != this_index:
t.cancel()
first_task = loop.create_task(run_one_coro(None))
running_tasks.append(first_task)
try:
# Wait for a growing list of tasks to all finish: poor man's version of
# curio's TaskGroup or trio's nursery
done_count = 0
while done_count != len(running_tasks):
done, _ = await tasks.wait(running_tasks)
done_count = len(done)
# If run_one_coro raises an unhandled exception, it's probably a
# programming error, and I want to see it.
if __debug__:
for d in done:
if d.done() and not d.cancelled() and d.exception():
raise d.exception()
return winner_result, winner_index, exceptions
finally:
# Make sure no tasks are left running if we leave this function
for t in running_tasks:
t.cancel()

411
Lib/asyncio/streams.py vendored
View File

@@ -1,55 +1,30 @@
"""Stream-related things."""
__all__ = ['StreamReader', 'StreamWriter', 'StreamReaderProtocol',
'open_connection', 'start_server',
'IncompleteReadError',
'LimitOverrunError',
]
__all__ = (
'StreamReader', 'StreamWriter', 'StreamReaderProtocol',
'open_connection', 'start_server')
import collections
import socket
import sys
import warnings
import weakref
if hasattr(socket, 'AF_UNIX'):
__all__.extend(['open_unix_connection', 'start_unix_server'])
__all__ += ('open_unix_connection', 'start_unix_server')
from . import coroutines
from . import compat
from . import events
from . import exceptions
from . import format_helpers
from . import protocols
from .coroutines import coroutine
from .log import logger
from .tasks import sleep
_DEFAULT_LIMIT = 2 ** 16
_DEFAULT_LIMIT = 2 ** 16 # 64 KiB
class IncompleteReadError(EOFError):
"""
Incomplete read error. Attributes:
- partial: read bytes string before the end of stream was reached
- expected: total number of expected bytes (or None if unknown)
"""
def __init__(self, partial, expected):
super().__init__("%d bytes read on a total of %r expected bytes"
% (len(partial), expected))
self.partial = partial
self.expected = expected
class LimitOverrunError(Exception):
"""Reached the buffer limit while looking for a separator.
Attributes:
- consumed: total number of to be consumed bytes.
"""
def __init__(self, message, consumed):
super().__init__(message)
self.consumed = consumed
@coroutine
def open_connection(host=None, port=None, *,
loop=None, limit=_DEFAULT_LIMIT, **kwds):
async def open_connection(host=None, port=None, *,
limit=_DEFAULT_LIMIT, **kwds):
"""A wrapper for create_connection() returning a (reader, writer) pair.
The reader returned is a StreamReader instance; the writer is a
@@ -67,19 +42,17 @@ def open_connection(host=None, port=None, *,
StreamReaderProtocol classes, just copy the code -- there's
really nothing special here except some convenience.)
"""
if loop is None:
loop = events.get_event_loop()
loop = events.get_running_loop()
reader = StreamReader(limit=limit, loop=loop)
protocol = StreamReaderProtocol(reader, loop=loop)
transport, _ = yield from loop.create_connection(
transport, _ = await loop.create_connection(
lambda: protocol, host, port, **kwds)
writer = StreamWriter(transport, protocol, reader, loop)
return reader, writer
@coroutine
def start_server(client_connected_cb, host=None, port=None, *,
loop=None, limit=_DEFAULT_LIMIT, **kwds):
async def start_server(client_connected_cb, host=None, port=None, *,
limit=_DEFAULT_LIMIT, **kwds):
"""Start a socket server, call back for each client connected.
The first parameter, `client_connected_cb`, takes two parameters:
@@ -94,15 +67,13 @@ def start_server(client_connected_cb, host=None, port=None, *,
positional host and port, with various optional keyword arguments
following. The return value is the same as loop.create_server().
Additional optional keyword arguments are loop (to set the event loop
instance to use) and limit (to set the buffer limit passed to the
StreamReader).
Additional optional keyword argument is limit (to set the buffer
limit passed to the StreamReader).
The return value is the same as loop.create_server(), i.e. a
Server object which can be used to stop the service.
"""
if loop is None:
loop = events.get_event_loop()
loop = events.get_running_loop()
def factory():
reader = StreamReader(limit=limit, loop=loop)
@@ -110,31 +81,28 @@ def start_server(client_connected_cb, host=None, port=None, *,
loop=loop)
return protocol
return (yield from loop.create_server(factory, host, port, **kwds))
return await loop.create_server(factory, host, port, **kwds)
if hasattr(socket, 'AF_UNIX'):
# UNIX Domain Sockets are supported on this platform
@coroutine
def open_unix_connection(path=None, *,
loop=None, limit=_DEFAULT_LIMIT, **kwds):
async def open_unix_connection(path=None, *,
limit=_DEFAULT_LIMIT, **kwds):
"""Similar to `open_connection` but works with UNIX Domain Sockets."""
if loop is None:
loop = events.get_event_loop()
loop = events.get_running_loop()
reader = StreamReader(limit=limit, loop=loop)
protocol = StreamReaderProtocol(reader, loop=loop)
transport, _ = yield from loop.create_unix_connection(
transport, _ = await loop.create_unix_connection(
lambda: protocol, path, **kwds)
writer = StreamWriter(transport, protocol, reader, loop)
return reader, writer
@coroutine
def start_unix_server(client_connected_cb, path=None, *,
loop=None, limit=_DEFAULT_LIMIT, **kwds):
async def start_unix_server(client_connected_cb, path=None, *,
limit=_DEFAULT_LIMIT, **kwds):
"""Similar to `start_server` but works with UNIX Domain Sockets."""
if loop is None:
loop = events.get_event_loop()
loop = events.get_running_loop()
def factory():
reader = StreamReader(limit=limit, loop=loop)
@@ -142,14 +110,14 @@ if hasattr(socket, 'AF_UNIX'):
loop=loop)
return protocol
return (yield from loop.create_unix_server(factory, path, **kwds))
return await loop.create_unix_server(factory, path, **kwds)
class FlowControlMixin(protocols.Protocol):
"""Reusable flow control logic for StreamWriter.drain().
This implements the protocol methods pause_writing(),
resume_reading() and connection_lost(). If the subclass overrides
resume_writing() and connection_lost(). If the subclass overrides
these it must call the super methods.
StreamWriter.drain() must wait for _drain_helper() coroutine.
@@ -161,7 +129,7 @@ class FlowControlMixin(protocols.Protocol):
else:
self._loop = loop
self._paused = False
self._drain_waiter = None
self._drain_waiters = collections.deque()
self._connection_lost = False
def pause_writing(self):
@@ -176,39 +144,37 @@ class FlowControlMixin(protocols.Protocol):
if self._loop.get_debug():
logger.debug("%r resumes writing", self)
waiter = self._drain_waiter
if waiter is not None:
self._drain_waiter = None
for waiter in self._drain_waiters:
if not waiter.done():
waiter.set_result(None)
def connection_lost(self, exc):
self._connection_lost = True
# Wake up the writer if currently paused.
# Wake up the writer(s) if currently paused.
if not self._paused:
return
waiter = self._drain_waiter
if waiter is None:
return
self._drain_waiter = None
if waiter.done():
return
if exc is None:
waiter.set_result(None)
else:
waiter.set_exception(exc)
@coroutine
def _drain_helper(self):
for waiter in self._drain_waiters:
if not waiter.done():
if exc is None:
waiter.set_result(None)
else:
waiter.set_exception(exc)
async def _drain_helper(self):
if self._connection_lost:
raise ConnectionResetError('Connection lost')
if not self._paused:
return
waiter = self._drain_waiter
assert waiter is None or waiter.cancelled()
waiter = self._loop.create_future()
self._drain_waiter = waiter
yield from waiter
self._drain_waiters.append(waiter)
try:
await waiter
finally:
self._drain_waiters.remove(waiter)
def _get_close_waiter(self, stream):
raise NotImplementedError
class StreamReaderProtocol(FlowControlMixin, protocols.Protocol):
@@ -220,40 +186,110 @@ class StreamReaderProtocol(FlowControlMixin, protocols.Protocol):
call inappropriate methods of the protocol.)
"""
_source_traceback = None
def __init__(self, stream_reader, client_connected_cb=None, loop=None):
super().__init__(loop=loop)
self._stream_reader = stream_reader
if stream_reader is not None:
self._stream_reader_wr = weakref.ref(stream_reader)
self._source_traceback = stream_reader._source_traceback
else:
self._stream_reader_wr = None
if client_connected_cb is not None:
# This is a stream created by the `create_server()` function.
# Keep a strong reference to the reader until a connection
# is established.
self._strong_reader = stream_reader
self._reject_connection = False
self._stream_writer = None
self._task = None
self._transport = None
self._client_connected_cb = client_connected_cb
self._over_ssl = False
self._closed = self._loop.create_future()
@property
def _stream_reader(self):
if self._stream_reader_wr is None:
return None
return self._stream_reader_wr()
def _replace_writer(self, writer):
loop = self._loop
transport = writer.transport
self._stream_writer = writer
self._transport = transport
self._over_ssl = transport.get_extra_info('sslcontext') is not None
def connection_made(self, transport):
self._stream_reader.set_transport(transport)
if self._reject_connection:
context = {
'message': ('An open stream was garbage collected prior to '
'establishing network connection; '
'call "stream.close()" explicitly.')
}
if self._source_traceback:
context['source_traceback'] = self._source_traceback
self._loop.call_exception_handler(context)
transport.abort()
return
self._transport = transport
reader = self._stream_reader
if reader is not None:
reader.set_transport(transport)
self._over_ssl = transport.get_extra_info('sslcontext') is not None
if self._client_connected_cb is not None:
self._stream_writer = StreamWriter(transport, self,
self._stream_reader,
reader,
self._loop)
res = self._client_connected_cb(self._stream_reader,
res = self._client_connected_cb(reader,
self._stream_writer)
if coroutines.iscoroutine(res):
self._loop.create_task(res)
def callback(task):
if task.cancelled():
transport.close()
return
exc = task.exception()
if exc is not None:
self._loop.call_exception_handler({
'message': 'Unhandled exception in client_connected_cb',
'exception': exc,
'transport': transport,
})
transport.close()
self._task = self._loop.create_task(res)
self._task.add_done_callback(callback)
self._strong_reader = None
def connection_lost(self, exc):
if self._stream_reader is not None:
reader = self._stream_reader
if reader is not None:
if exc is None:
self._stream_reader.feed_eof()
reader.feed_eof()
else:
self._stream_reader.set_exception(exc)
reader.set_exception(exc)
if not self._closed.done():
if exc is None:
self._closed.set_result(None)
else:
self._closed.set_exception(exc)
super().connection_lost(exc)
self._stream_reader = None
self._stream_reader_wr = None
self._stream_writer = None
self._task = None
self._transport = None
def data_received(self, data):
self._stream_reader.feed_data(data)
reader = self._stream_reader
if reader is not None:
reader.feed_data(data)
def eof_received(self):
self._stream_reader.feed_eof()
reader = self._stream_reader
if reader is not None:
reader.feed_eof()
if self._over_ssl:
# Prevent a warning in SSLProtocol.eof_received:
# "returning true from eof_received()
@@ -261,6 +297,20 @@ class StreamReaderProtocol(FlowControlMixin, protocols.Protocol):
return False
return True
def _get_close_waiter(self, stream):
return self._closed
def __del__(self):
# Prevent reports about unhandled exceptions.
# Better than self._closed._log_traceback = False hack
try:
closed = self._closed
except AttributeError:
pass # failed constructor
else:
if closed.done() and not closed.cancelled():
closed.exception()
class StreamWriter:
"""Wraps a Transport.
@@ -279,12 +329,14 @@ class StreamWriter:
assert reader is None or isinstance(reader, StreamReader)
self._reader = reader
self._loop = loop
self._complete_fut = self._loop.create_future()
self._complete_fut.set_result(None)
def __repr__(self):
info = [self.__class__.__name__, 'transport=%r' % self._transport]
info = [self.__class__.__name__, f'transport={self._transport!r}']
if self._reader is not None:
info.append('reader=%r' % self._reader)
return '<%s>' % ' '.join(info)
info.append(f'reader={self._reader!r}')
return '<{}>'.format(' '.join(info))
@property
def transport(self):
@@ -305,36 +357,68 @@ class StreamWriter:
def close(self):
return self._transport.close()
def is_closing(self):
return self._transport.is_closing()
async def wait_closed(self):
await self._protocol._get_close_waiter(self)
def get_extra_info(self, name, default=None):
return self._transport.get_extra_info(name, default)
@coroutine
def drain(self):
async def drain(self):
"""Flush the write buffer.
The intended use is to write
w.write(data)
yield from w.drain()
await w.drain()
"""
if self._reader is not None:
exc = self._reader.exception()
if exc is not None:
raise exc
if self._transport is not None:
if self._transport.is_closing():
# Yield to the event loop so connection_lost() may be
# called. Without this, _drain_helper() would return
# immediately, and code that calls
# write(...); yield from drain()
# in a loop would never call connection_lost(), so it
# would not see an error when the socket is closed.
yield
yield from self._protocol._drain_helper()
if self._transport.is_closing():
# Wait for protocol.connection_lost() call
# Raise connection closing error if any,
# ConnectionResetError otherwise
# Yield to the event loop so connection_lost() may be
# called. Without this, _drain_helper() would return
# immediately, and code that calls
# write(...); await drain()
# in a loop would never call connection_lost(), so it
# would not see an error when the socket is closed.
await sleep(0)
await self._protocol._drain_helper()
async def start_tls(self, sslcontext, *,
server_hostname=None,
ssl_handshake_timeout=None,
ssl_shutdown_timeout=None):
"""Upgrade an existing stream-based connection to TLS."""
server_side = self._protocol._client_connected_cb is not None
protocol = self._protocol
await self.drain()
new_transport = await self._loop.start_tls( # type: ignore
self._transport, protocol, sslcontext,
server_side=server_side, server_hostname=server_hostname,
ssl_handshake_timeout=ssl_handshake_timeout,
ssl_shutdown_timeout=ssl_shutdown_timeout)
self._transport = new_transport
protocol._replace_writer(self)
def __del__(self):
if not self._transport.is_closing():
if self._loop.is_closed():
warnings.warn("loop is closed", ResourceWarning)
else:
self.close()
warnings.warn(f"unclosed {self!r}", ResourceWarning)
class StreamReader:
_source_traceback = None
def __init__(self, limit=_DEFAULT_LIMIT, loop=None):
# The line length limit is a security feature;
# it also doubles as half the buffer limit.
@@ -353,24 +437,27 @@ class StreamReader:
self._exception = None
self._transport = None
self._paused = False
if self._loop.get_debug():
self._source_traceback = format_helpers.extract_stack(
sys._getframe(1))
def __repr__(self):
info = ['StreamReader']
if self._buffer:
info.append('%d bytes' % len(self._buffer))
info.append(f'{len(self._buffer)} bytes')
if self._eof:
info.append('eof')
if self._limit != _DEFAULT_LIMIT:
info.append('l=%d' % self._limit)
info.append(f'limit={self._limit}')
if self._waiter:
info.append('w=%r' % self._waiter)
info.append(f'waiter={self._waiter!r}')
if self._exception:
info.append('e=%r' % self._exception)
info.append(f'exception={self._exception!r}')
if self._transport:
info.append('t=%r' % self._transport)
info.append(f'transport={self._transport!r}')
if self._paused:
info.append('paused')
return '<%s>' % ' '.join(info)
return '<{}>'.format(' '.join(info))
def exception(self):
return self._exception
@@ -431,8 +518,7 @@ class StreamReader:
else:
self._paused = True
@coroutine
def _wait_for_data(self, func_name):
async def _wait_for_data(self, func_name):
"""Wait until feed_data() or feed_eof() is called.
If stream was paused, automatically resume it.
@@ -442,8 +528,9 @@ class StreamReader:
# would have an unexpected behaviour. It would not possible to know
# which coroutine would get the next data.
if self._waiter is not None:
raise RuntimeError('%s() called while another coroutine is '
'already waiting for incoming data' % func_name)
raise RuntimeError(
f'{func_name}() called while another coroutine is '
f'already waiting for incoming data')
assert not self._eof, '_wait_for_data after EOF'
@@ -455,12 +542,11 @@ class StreamReader:
self._waiter = self._loop.create_future()
try:
yield from self._waiter
await self._waiter
finally:
self._waiter = None
@coroutine
def readline(self):
async def readline(self):
"""Read chunk of data from the stream until newline (b'\n') is found.
On success, return chunk that ends with newline. If only partial
@@ -479,10 +565,10 @@ class StreamReader:
sep = b'\n'
seplen = len(sep)
try:
line = yield from self.readuntil(sep)
except IncompleteReadError as e:
line = await self.readuntil(sep)
except exceptions.IncompleteReadError as e:
return e.partial
except LimitOverrunError as e:
except exceptions.LimitOverrunError as e:
if self._buffer.startswith(sep, e.consumed):
del self._buffer[:e.consumed + seplen]
else:
@@ -491,8 +577,7 @@ class StreamReader:
raise ValueError(e.args[0])
return line
@coroutine
def readuntil(self, separator=b'\n'):
async def readuntil(self, separator=b'\n'):
"""Read data from the stream until ``separator`` is found.
On success, the data and separator will be removed from the
@@ -558,7 +643,7 @@ class StreamReader:
# see upper comment for explanation.
offset = buflen + 1 - seplen
if offset > self._limit:
raise LimitOverrunError(
raise exceptions.LimitOverrunError(
'Separator is not found, and chunk exceed the limit',
offset)
@@ -569,13 +654,13 @@ class StreamReader:
if self._eof:
chunk = bytes(self._buffer)
self._buffer.clear()
raise IncompleteReadError(chunk, None)
raise exceptions.IncompleteReadError(chunk, None)
# _wait_for_data() will resume reading if stream was paused.
yield from self._wait_for_data('readuntil')
await self._wait_for_data('readuntil')
if isep > self._limit:
raise LimitOverrunError(
raise exceptions.LimitOverrunError(
'Separator is found, but chunk is longer than limit', isep)
chunk = self._buffer[:isep + seplen]
@@ -583,20 +668,20 @@ class StreamReader:
self._maybe_resume_transport()
return bytes(chunk)
@coroutine
def read(self, n=-1):
async def read(self, n=-1):
"""Read up to `n` bytes from the stream.
If n is not provided, or set to -1, read until EOF and return all read
bytes. If the EOF was received and the internal buffer is empty, return
an empty bytes object.
If `n` is not provided or set to -1,
read until EOF, then return all read bytes.
If EOF was received and the internal buffer is empty,
return an empty bytes object.
If n is zero, return empty bytes object immediately.
If `n` is 0, return an empty bytes object immediately.
If n is positive, this function try to read `n` bytes, and may return
less or equal bytes than requested, but at least one byte. If EOF was
received before any byte is read, this function returns empty byte
object.
If `n` is positive, return at most `n` available bytes
as soon as at least 1 byte is available in the internal buffer.
If EOF is received before any byte is read, return an empty
bytes object.
Returned value is not limited with limit, configured at stream
creation.
@@ -618,24 +703,23 @@ class StreamReader:
# bytes. So just call self.read(self._limit) until EOF.
blocks = []
while True:
block = yield from self.read(self._limit)
block = await self.read(self._limit)
if not block:
break
blocks.append(block)
return b''.join(blocks)
if not self._buffer and not self._eof:
yield from self._wait_for_data('read')
await self._wait_for_data('read')
# This will work right even if buffer is less than n bytes
data = bytes(self._buffer[:n])
data = bytes(memoryview(self._buffer)[:n])
del self._buffer[:n]
self._maybe_resume_transport()
return data
@coroutine
def readexactly(self, n):
async def readexactly(self, n):
"""Read exactly `n` bytes.
Raise an IncompleteReadError if EOF is reached before `n` bytes can be
@@ -663,33 +747,24 @@ class StreamReader:
if self._eof:
incomplete = bytes(self._buffer)
self._buffer.clear()
raise IncompleteReadError(incomplete, n)
raise exceptions.IncompleteReadError(incomplete, n)
yield from self._wait_for_data('readexactly')
await self._wait_for_data('readexactly')
if len(self._buffer) == n:
data = bytes(self._buffer)
self._buffer.clear()
else:
data = bytes(self._buffer[:n])
data = bytes(memoryview(self._buffer)[:n])
del self._buffer[:n]
self._maybe_resume_transport()
return data
if compat.PY35:
@coroutine
def __aiter__(self):
return self
def __aiter__(self):
return self
@coroutine
def __anext__(self):
val = yield from self.readline()
if val == b'':
raise StopAsyncIteration
return val
if compat.PY352:
# In Python 3.5.2 and greater, __aiter__ should return
# the asynchronous iterator directly.
def __aiter__(self):
return self
async def __anext__(self):
val = await self.readline()
if val == b'':
raise StopAsyncIteration
return val

View File

@@ -1,4 +1,4 @@
__all__ = ['create_subprocess_exec', 'create_subprocess_shell']
__all__ = 'create_subprocess_exec', 'create_subprocess_shell'
import subprocess
@@ -6,7 +6,6 @@ from . import events
from . import protocols
from . import streams
from . import tasks
from .coroutines import coroutine
from .log import logger
@@ -24,16 +23,19 @@ class SubprocessStreamProtocol(streams.FlowControlMixin,
self._limit = limit
self.stdin = self.stdout = self.stderr = None
self._transport = None
self._process_exited = False
self._pipe_fds = []
self._stdin_closed = self._loop.create_future()
def __repr__(self):
info = [self.__class__.__name__]
if self.stdin is not None:
info.append('stdin=%r' % self.stdin)
info.append(f'stdin={self.stdin!r}')
if self.stdout is not None:
info.append('stdout=%r' % self.stdout)
info.append(f'stdout={self.stdout!r}')
if self.stderr is not None:
info.append('stderr=%r' % self.stderr)
return '<%s>' % ' '.join(info)
info.append(f'stderr={self.stderr!r}')
return '<{}>'.format(' '.join(info))
def connection_made(self, transport):
self._transport = transport
@@ -43,12 +45,14 @@ class SubprocessStreamProtocol(streams.FlowControlMixin,
self.stdout = streams.StreamReader(limit=self._limit,
loop=self._loop)
self.stdout.set_transport(stdout_transport)
self._pipe_fds.append(1)
stderr_transport = transport.get_pipe_transport(2)
if stderr_transport is not None:
self.stderr = streams.StreamReader(limit=self._limit,
loop=self._loop)
self.stderr.set_transport(stderr_transport)
self._pipe_fds.append(2)
stdin_transport = transport.get_pipe_transport(0)
if stdin_transport is not None:
@@ -73,6 +77,13 @@ class SubprocessStreamProtocol(streams.FlowControlMixin,
if pipe is not None:
pipe.close()
self.connection_lost(exc)
if exc is None:
self._stdin_closed.set_result(None)
else:
self._stdin_closed.set_exception(exc)
# Since calling `wait_closed()` is not mandatory,
# we shouldn't log the traceback if this is not awaited.
self._stdin_closed._log_traceback = False
return
if fd == 1:
reader = self.stdout
@@ -80,15 +91,28 @@ class SubprocessStreamProtocol(streams.FlowControlMixin,
reader = self.stderr
else:
reader = None
if reader != None:
if reader is not None:
if exc is None:
reader.feed_eof()
else:
reader.set_exception(exc)
if fd in self._pipe_fds:
self._pipe_fds.remove(fd)
self._maybe_close_transport()
def process_exited(self):
self._transport.close()
self._transport = None
self._process_exited = True
self._maybe_close_transport()
def _maybe_close_transport(self):
if len(self._pipe_fds) == 0 and self._process_exited:
self._transport.close()
self._transport = None
def _get_close_waiter(self, stream):
if stream is self.stdin:
return self._stdin_closed
class Process:
@@ -102,18 +126,15 @@ class Process:
self.pid = transport.get_pid()
def __repr__(self):
return '<%s %s>' % (self.__class__.__name__, self.pid)
return f'<{self.__class__.__name__} {self.pid}>'
@property
def returncode(self):
return self._transport.get_returncode()
@coroutine
def wait(self):
"""Wait until the process exit and return the process return code.
This method is a coroutine."""
return (yield from self._transport._wait())
async def wait(self):
"""Wait until the process exit and return the process return code."""
return await self._transport._wait()
def send_signal(self, signal):
self._transport.send_signal(signal)
@@ -124,17 +145,19 @@ class Process:
def kill(self):
self._transport.kill()
@coroutine
def _feed_stdin(self, input):
async def _feed_stdin(self, input):
debug = self._loop.get_debug()
self.stdin.write(input)
if debug:
logger.debug('%r communicate: feed stdin (%s bytes)',
self, len(input))
try:
yield from self.stdin.drain()
if input is not None:
self.stdin.write(input)
if debug:
logger.debug(
'%r communicate: feed stdin (%s bytes)', self, len(input))
await self.stdin.drain()
except (BrokenPipeError, ConnectionResetError) as exc:
# communicate() ignores BrokenPipeError and ConnectionResetError
# communicate() ignores BrokenPipeError and ConnectionResetError.
# write() and drain() can raise these exceptions.
if debug:
logger.debug('%r communicate: stdin got %r', self, exc)
@@ -142,12 +165,10 @@ class Process:
logger.debug('%r communicate: close stdin', self)
self.stdin.close()
@coroutine
def _noop(self):
async def _noop(self):
return None
@coroutine
def _read_stream(self, fd):
async def _read_stream(self, fd):
transport = self._transport.get_pipe_transport(fd)
if fd == 2:
stream = self.stderr
@@ -157,16 +178,15 @@ class Process:
if self._loop.get_debug():
name = 'stdout' if fd == 1 else 'stderr'
logger.debug('%r communicate: read %s', self, name)
output = yield from stream.read()
output = await stream.read()
if self._loop.get_debug():
name = 'stdout' if fd == 1 else 'stderr'
logger.debug('%r communicate: close %s', self, name)
transport.close()
return output
@coroutine
def communicate(self, input=None):
if input is not None:
async def communicate(self, input=None):
if self.stdin is not None:
stdin = self._feed_stdin(input)
else:
stdin = self._noop()
@@ -178,36 +198,32 @@ class Process:
stderr = self._read_stream(2)
else:
stderr = self._noop()
stdin, stdout, stderr = yield from tasks.gather(stdin, stdout, stderr,
loop=self._loop)
yield from self.wait()
stdin, stdout, stderr = await tasks.gather(stdin, stdout, stderr)
await self.wait()
return (stdout, stderr)
@coroutine
def create_subprocess_shell(cmd, stdin=None, stdout=None, stderr=None,
loop=None, limit=streams._DEFAULT_LIMIT, **kwds):
if loop is None:
loop = events.get_event_loop()
async def create_subprocess_shell(cmd, stdin=None, stdout=None, stderr=None,
limit=streams._DEFAULT_LIMIT, **kwds):
loop = events.get_running_loop()
protocol_factory = lambda: SubprocessStreamProtocol(limit=limit,
loop=loop)
transport, protocol = yield from loop.subprocess_shell(
protocol_factory,
cmd, stdin=stdin, stdout=stdout,
stderr=stderr, **kwds)
transport, protocol = await loop.subprocess_shell(
protocol_factory,
cmd, stdin=stdin, stdout=stdout,
stderr=stderr, **kwds)
return Process(transport, protocol, loop)
@coroutine
def create_subprocess_exec(program, *args, stdin=None, stdout=None,
stderr=None, loop=None,
limit=streams._DEFAULT_LIMIT, **kwds):
if loop is None:
loop = events.get_event_loop()
async def create_subprocess_exec(program, *args, stdin=None, stdout=None,
stderr=None, limit=streams._DEFAULT_LIMIT,
**kwds):
loop = events.get_running_loop()
protocol_factory = lambda: SubprocessStreamProtocol(limit=limit,
loop=loop)
transport, protocol = yield from loop.subprocess_exec(
protocol_factory,
program, *args,
stdin=stdin, stdout=stdout,
stderr=stderr, **kwds)
transport, protocol = await loop.subprocess_exec(
protocol_factory,
program, *args,
stdin=stdin, stdout=stdout,
stderr=stderr, **kwds)
return Process(transport, protocol, loop)

240
Lib/asyncio/taskgroups.py vendored Normal file
View File

@@ -0,0 +1,240 @@
# Adapted with permission from the EdgeDB project;
# license: PSFL.
__all__ = ("TaskGroup",)
from . import events
from . import exceptions
from . import tasks
class TaskGroup:
"""Asynchronous context manager for managing groups of tasks.
Example use:
async with asyncio.TaskGroup() as group:
task1 = group.create_task(some_coroutine(...))
task2 = group.create_task(other_coroutine(...))
print("Both tasks have completed now.")
All tasks are awaited when the context manager exits.
Any exceptions other than `asyncio.CancelledError` raised within
a task will cancel all remaining tasks and wait for them to exit.
The exceptions are then combined and raised as an `ExceptionGroup`.
"""
def __init__(self):
self._entered = False
self._exiting = False
self._aborting = False
self._loop = None
self._parent_task = None
self._parent_cancel_requested = False
self._tasks = set()
self._errors = []
self._base_error = None
self._on_completed_fut = None
def __repr__(self):
info = ['']
if self._tasks:
info.append(f'tasks={len(self._tasks)}')
if self._errors:
info.append(f'errors={len(self._errors)}')
if self._aborting:
info.append('cancelling')
elif self._entered:
info.append('entered')
info_str = ' '.join(info)
return f'<TaskGroup{info_str}>'
async def __aenter__(self):
if self._entered:
raise RuntimeError(
f"TaskGroup {self!r} has already been entered")
if self._loop is None:
self._loop = events.get_running_loop()
self._parent_task = tasks.current_task(self._loop)
if self._parent_task is None:
raise RuntimeError(
f'TaskGroup {self!r} cannot determine the parent task')
self._entered = True
return self
async def __aexit__(self, et, exc, tb):
self._exiting = True
if (exc is not None and
self._is_base_error(exc) and
self._base_error is None):
self._base_error = exc
propagate_cancellation_error = \
exc if et is exceptions.CancelledError else None
if self._parent_cancel_requested:
# If this flag is set we *must* call uncancel().
if self._parent_task.uncancel() == 0:
# If there are no pending cancellations left,
# don't propagate CancelledError.
propagate_cancellation_error = None
if et is not None:
if not self._aborting:
# Our parent task is being cancelled:
#
# async with TaskGroup() as g:
# g.create_task(...)
# await ... # <- CancelledError
#
# or there's an exception in "async with":
#
# async with TaskGroup() as g:
# g.create_task(...)
# 1 / 0
#
self._abort()
# We use while-loop here because "self._on_completed_fut"
# can be cancelled multiple times if our parent task
# is being cancelled repeatedly (or even once, when
# our own cancellation is already in progress)
while self._tasks:
if self._on_completed_fut is None:
self._on_completed_fut = self._loop.create_future()
try:
await self._on_completed_fut
except exceptions.CancelledError as ex:
if not self._aborting:
# Our parent task is being cancelled:
#
# async def wrapper():
# async with TaskGroup() as g:
# g.create_task(foo)
#
# "wrapper" is being cancelled while "foo" is
# still running.
propagate_cancellation_error = ex
self._abort()
self._on_completed_fut = None
assert not self._tasks
if self._base_error is not None:
raise self._base_error
# Propagate CancelledError if there is one, except if there
# are other errors -- those have priority.
if propagate_cancellation_error and not self._errors:
raise propagate_cancellation_error
if et is not None and et is not exceptions.CancelledError:
self._errors.append(exc)
if self._errors:
# Exceptions are heavy objects that can have object
# cycles (bad for GC); let's not keep a reference to
# a bunch of them.
try:
me = BaseExceptionGroup('unhandled errors in a TaskGroup', self._errors)
raise me from None
finally:
self._errors = None
def create_task(self, coro, *, name=None, context=None):
"""Create a new task in this group and return it.
Similar to `asyncio.create_task`.
"""
if not self._entered:
raise RuntimeError(f"TaskGroup {self!r} has not been entered")
if self._exiting and not self._tasks:
raise RuntimeError(f"TaskGroup {self!r} is finished")
if self._aborting:
raise RuntimeError(f"TaskGroup {self!r} is shutting down")
if context is None:
task = self._loop.create_task(coro)
else:
task = self._loop.create_task(coro, context=context)
tasks._set_task_name(task, name)
# optimization: Immediately call the done callback if the task is
# already done (e.g. if the coro was able to complete eagerly),
# and skip scheduling a done callback
if task.done():
self._on_task_done(task)
else:
self._tasks.add(task)
task.add_done_callback(self._on_task_done)
return task
# Since Python 3.8 Tasks propagate all exceptions correctly,
# except for KeyboardInterrupt and SystemExit which are
# still considered special.
def _is_base_error(self, exc: BaseException) -> bool:
assert isinstance(exc, BaseException)
return isinstance(exc, (SystemExit, KeyboardInterrupt))
def _abort(self):
self._aborting = True
for t in self._tasks:
if not t.done():
t.cancel()
def _on_task_done(self, task):
self._tasks.discard(task)
if self._on_completed_fut is not None and not self._tasks:
if not self._on_completed_fut.done():
self._on_completed_fut.set_result(True)
if task.cancelled():
return
exc = task.exception()
if exc is None:
return
self._errors.append(exc)
if self._is_base_error(exc) and self._base_error is None:
self._base_error = exc
if self._parent_task.done():
# Not sure if this case is possible, but we want to handle
# it anyways.
self._loop.call_exception_handler({
'message': f'Task {task!r} has errored out but its parent '
f'task {self._parent_task} is already completed',
'exception': exc,
'task': task,
})
return
if not self._aborting and not self._parent_cancel_requested:
# If parent task *is not* being cancelled, it means that we want
# to manually cancel it to abort whatever is being run right now
# in the TaskGroup. But we want to mark parent task as
# "not cancelled" later in __aexit__. Example situation that
# we need to handle:
#
# async def foo():
# try:
# async with TaskGroup() as g:
# g.create_task(crash_soon())
# await something # <- this needs to be canceled
# # by the TaskGroup, e.g.
# # foo() needs to be cancelled
# except Exception:
# # Ignore any exceptions raised in the TaskGroup
# pass
# await something_else # this line has to be called
# # after TaskGroup is finished.
self._abort()
self._parent_cancel_requested = True
self._parent_task.cancel()

853
Lib/asyncio/tasks.py vendored

File diff suppressed because it is too large Load Diff

View File

@@ -1,503 +0,0 @@
"""Utilities shared by tests."""
import collections
import contextlib
import io
import logging
import os
import re
import socket
import socketserver
import sys
import tempfile
import threading
import time
import unittest
import weakref
from unittest import mock
from http.server import HTTPServer
from wsgiref.simple_server import WSGIRequestHandler, WSGIServer
try:
import ssl
except ImportError: # pragma: no cover
ssl = None
from . import base_events
from . import compat
from . import events
from . import futures
from . import selectors
from . import tasks
from .coroutines import coroutine
from .log import logger
if sys.platform == 'win32': # pragma: no cover
from .windows_utils import socketpair
else:
from socket import socketpair # pragma: no cover
def dummy_ssl_context():
if ssl is None:
return None
else:
return ssl.SSLContext(ssl.PROTOCOL_SSLv23)
def run_briefly(loop):
@coroutine
def once():
pass
gen = once()
t = loop.create_task(gen)
# Don't log a warning if the task is not done after run_until_complete().
# It occurs if the loop is stopped or if a task raises a BaseException.
t._log_destroy_pending = False
try:
loop.run_until_complete(t)
finally:
gen.close()
def run_until(loop, pred, timeout=30):
deadline = time.time() + timeout
while not pred():
if timeout is not None:
timeout = deadline - time.time()
if timeout <= 0:
raise futures.TimeoutError()
loop.run_until_complete(tasks.sleep(0.001, loop=loop))
def run_once(loop):
"""Legacy API to run once through the event loop.
This is the recommended pattern for test code. It will poll the
selector once and run all callbacks scheduled in response to I/O
events.
"""
loop.call_soon(loop.stop)
loop.run_forever()
class SilentWSGIRequestHandler(WSGIRequestHandler):
def get_stderr(self):
return io.StringIO()
def log_message(self, format, *args):
pass
class SilentWSGIServer(WSGIServer):
request_timeout = 2
def get_request(self):
request, client_addr = super().get_request()
request.settimeout(self.request_timeout)
return request, client_addr
def handle_error(self, request, client_address):
pass
class SSLWSGIServerMixin:
def finish_request(self, request, client_address):
# The relative location of our test directory (which
# contains the ssl key and certificate files) differs
# between the stdlib and stand-alone asyncio.
# Prefer our own if we can find it.
here = os.path.join(os.path.dirname(__file__), '..', 'tests')
if not os.path.isdir(here):
here = os.path.join(os.path.dirname(os.__file__),
'test', 'test_asyncio')
keyfile = os.path.join(here, 'ssl_key.pem')
certfile = os.path.join(here, 'ssl_cert.pem')
context = ssl.SSLContext()
context.load_cert_chain(certfile, keyfile)
ssock = context.wrap_socket(request, server_side=True)
try:
self.RequestHandlerClass(ssock, client_address, self)
ssock.close()
except OSError:
# maybe socket has been closed by peer
pass
class SSLWSGIServer(SSLWSGIServerMixin, SilentWSGIServer):
pass
def _run_test_server(*, address, use_ssl=False, server_cls, server_ssl_cls):
def app(environ, start_response):
status = '200 OK'
headers = [('Content-type', 'text/plain')]
start_response(status, headers)
return [b'Test message']
# Run the test WSGI server in a separate thread in order not to
# interfere with event handling in the main thread
server_class = server_ssl_cls if use_ssl else server_cls
httpd = server_class(address, SilentWSGIRequestHandler)
httpd.set_app(app)
httpd.address = httpd.server_address
server_thread = threading.Thread(
target=lambda: httpd.serve_forever(poll_interval=0.05))
server_thread.start()
try:
yield httpd
finally:
httpd.shutdown()
httpd.server_close()
server_thread.join()
if hasattr(socket, 'AF_UNIX'):
class UnixHTTPServer(socketserver.UnixStreamServer, HTTPServer):
def server_bind(self):
socketserver.UnixStreamServer.server_bind(self)
self.server_name = '127.0.0.1'
self.server_port = 80
class UnixWSGIServer(UnixHTTPServer, WSGIServer):
request_timeout = 2
def server_bind(self):
UnixHTTPServer.server_bind(self)
self.setup_environ()
def get_request(self):
request, client_addr = super().get_request()
request.settimeout(self.request_timeout)
# Code in the stdlib expects that get_request
# will return a socket and a tuple (host, port).
# However, this isn't true for UNIX sockets,
# as the second return value will be a path;
# hence we return some fake data sufficient
# to get the tests going
return request, ('127.0.0.1', '')
class SilentUnixWSGIServer(UnixWSGIServer):
def handle_error(self, request, client_address):
pass
class UnixSSLWSGIServer(SSLWSGIServerMixin, SilentUnixWSGIServer):
pass
def gen_unix_socket_path():
with tempfile.NamedTemporaryFile() as file:
return file.name
@contextlib.contextmanager
def unix_socket_path():
path = gen_unix_socket_path()
try:
yield path
finally:
try:
os.unlink(path)
except OSError:
pass
@contextlib.contextmanager
def run_test_unix_server(*, use_ssl=False):
with unix_socket_path() as path:
yield from _run_test_server(address=path, use_ssl=use_ssl,
server_cls=SilentUnixWSGIServer,
server_ssl_cls=UnixSSLWSGIServer)
@contextlib.contextmanager
def run_test_server(*, host='127.0.0.1', port=0, use_ssl=False):
yield from _run_test_server(address=(host, port), use_ssl=use_ssl,
server_cls=SilentWSGIServer,
server_ssl_cls=SSLWSGIServer)
def make_test_protocol(base):
dct = {}
for name in dir(base):
if name.startswith('__') and name.endswith('__'):
# skip magic names
continue
dct[name] = MockCallback(return_value=None)
return type('TestProtocol', (base,) + base.__bases__, dct)()
class TestSelector(selectors.BaseSelector):
def __init__(self):
self.keys = {}
def register(self, fileobj, events, data=None):
key = selectors.SelectorKey(fileobj, 0, events, data)
self.keys[fileobj] = key
return key
def unregister(self, fileobj):
return self.keys.pop(fileobj)
def select(self, timeout):
return []
def get_map(self):
return self.keys
class TestLoop(base_events.BaseEventLoop):
"""Loop for unittests.
It manages self time directly.
If something scheduled to be executed later then
on next loop iteration after all ready handlers done
generator passed to __init__ is calling.
Generator should be like this:
def gen():
...
when = yield ...
... = yield time_advance
Value returned by yield is absolute time of next scheduled handler.
Value passed to yield is time advance to move loop's time forward.
"""
def __init__(self, gen=None):
super().__init__()
if gen is None:
def gen():
yield
self._check_on_close = False
else:
self._check_on_close = True
self._gen = gen()
next(self._gen)
self._time = 0
self._clock_resolution = 1e-9
self._timers = []
self._selector = TestSelector()
self.readers = {}
self.writers = {}
self.reset_counters()
self._transports = weakref.WeakValueDictionary()
def time(self):
return self._time
def advance_time(self, advance):
"""Move test time forward."""
if advance:
self._time += advance
def close(self):
super().close()
if self._check_on_close:
try:
self._gen.send(0)
except StopIteration:
pass
else: # pragma: no cover
raise AssertionError("Time generator is not finished")
def _add_reader(self, fd, callback, *args):
self.readers[fd] = events.Handle(callback, args, self)
def _remove_reader(self, fd):
self.remove_reader_count[fd] += 1
if fd in self.readers:
del self.readers[fd]
return True
else:
return False
def assert_reader(self, fd, callback, *args):
assert fd in self.readers, 'fd {} is not registered'.format(fd)
handle = self.readers[fd]
assert handle._callback == callback, '{!r} != {!r}'.format(
handle._callback, callback)
assert handle._args == args, '{!r} != {!r}'.format(
handle._args, args)
def _add_writer(self, fd, callback, *args):
self.writers[fd] = events.Handle(callback, args, self)
def _remove_writer(self, fd):
self.remove_writer_count[fd] += 1
if fd in self.writers:
del self.writers[fd]
return True
else:
return False
def assert_writer(self, fd, callback, *args):
assert fd in self.writers, 'fd {} is not registered'.format(fd)
handle = self.writers[fd]
assert handle._callback == callback, '{!r} != {!r}'.format(
handle._callback, callback)
assert handle._args == args, '{!r} != {!r}'.format(
handle._args, args)
def _ensure_fd_no_transport(self, fd):
try:
transport = self._transports[fd]
except KeyError:
pass
else:
raise RuntimeError(
'File descriptor {!r} is used by transport {!r}'.format(
fd, transport))
def add_reader(self, fd, callback, *args):
"""Add a reader callback."""
self._ensure_fd_no_transport(fd)
return self._add_reader(fd, callback, *args)
def remove_reader(self, fd):
"""Remove a reader callback."""
self._ensure_fd_no_transport(fd)
return self._remove_reader(fd)
def add_writer(self, fd, callback, *args):
"""Add a writer callback.."""
self._ensure_fd_no_transport(fd)
return self._add_writer(fd, callback, *args)
def remove_writer(self, fd):
"""Remove a writer callback."""
self._ensure_fd_no_transport(fd)
return self._remove_writer(fd)
def reset_counters(self):
self.remove_reader_count = collections.defaultdict(int)
self.remove_writer_count = collections.defaultdict(int)
def _run_once(self):
super()._run_once()
for when in self._timers:
advance = self._gen.send(when)
self.advance_time(advance)
self._timers = []
def call_at(self, when, callback, *args):
self._timers.append(when)
return super().call_at(when, callback, *args)
def _process_events(self, event_list):
return
def _write_to_self(self):
pass
def MockCallback(**kwargs):
return mock.Mock(spec=['__call__'], **kwargs)
class MockPattern(str):
"""A regex based str with a fuzzy __eq__.
Use this helper with 'mock.assert_called_with', or anywhere
where a regex comparison between strings is needed.
For instance:
mock_call.assert_called_with(MockPattern('spam.*ham'))
"""
def __eq__(self, other):
return bool(re.search(str(self), other, re.S))
def get_function_source(func):
source = events._get_function_source(func)
if source is None:
raise ValueError("unable to get the source of %r" % (func,))
return source
class TestCase(unittest.TestCase):
def set_event_loop(self, loop, *, cleanup=True):
assert loop is not None
# ensure that the event loop is passed explicitly in asyncio
events.set_event_loop(None)
if cleanup:
self.addCleanup(loop.close)
def new_test_loop(self, gen=None):
loop = TestLoop(gen)
self.set_event_loop(loop)
return loop
def setUp(self):
self._get_running_loop = events._get_running_loop
events._get_running_loop = lambda: None
def tearDown(self):
events._get_running_loop = self._get_running_loop
events.set_event_loop(None)
# Detect CPython bug #23353: ensure that yield/yield-from is not used
# in an except block of a generator
self.assertEqual(sys.exc_info(), (None, None, None))
if not compat.PY34:
# Python 3.3 compatibility
def subTest(self, *args, **kwargs):
class EmptyCM:
def __enter__(self):
pass
def __exit__(self, *exc):
pass
return EmptyCM()
@contextlib.contextmanager
def disable_logger():
"""Context manager to disable asyncio logger.
For example, it can be used to ignore warnings in debug mode.
"""
old_level = logger.level
try:
logger.setLevel(logging.CRITICAL+1)
yield
finally:
logger.setLevel(old_level)
def mock_nonblocking_socket(proto=socket.IPPROTO_TCP, type=socket.SOCK_STREAM,
family=socket.AF_INET):
"""Create a mock of a non-blocking socket."""
sock = mock.MagicMock(socket.socket)
sock.proto = proto
sock.type = type
sock.family = family
sock.gettimeout.return_value = 0.0
return sock
def force_legacy_ssl_support():
return mock.patch('asyncio.sslproto._is_sslproto_available',
return_value=False)

25
Lib/asyncio/threads.py vendored Normal file
View File

@@ -0,0 +1,25 @@
"""High-level support for working with threads in asyncio"""
import functools
import contextvars
from . import events
__all__ = "to_thread",
async def to_thread(func, /, *args, **kwargs):
"""Asynchronously run function *func* in a separate thread.
Any *args and **kwargs supplied for this function are directly passed
to *func*. Also, the current :class:`contextvars.Context` is propagated,
allowing context variables from the main thread to be accessed in the
separate thread.
Return a coroutine that can be awaited to get the eventual result of *func*.
"""
loop = events.get_running_loop()
ctx = contextvars.copy_context()
func_call = functools.partial(ctx.run, func, *args, **kwargs)
return await loop.run_in_executor(None, func_call)

168
Lib/asyncio/timeouts.py vendored Normal file
View File

@@ -0,0 +1,168 @@
import enum
from types import TracebackType
from typing import final, Optional, Type
from . import events
from . import exceptions
from . import tasks
__all__ = (
"Timeout",
"timeout",
"timeout_at",
)
class _State(enum.Enum):
CREATED = "created"
ENTERED = "active"
EXPIRING = "expiring"
EXPIRED = "expired"
EXITED = "finished"
@final
class Timeout:
"""Asynchronous context manager for cancelling overdue coroutines.
Use `timeout()` or `timeout_at()` rather than instantiating this class directly.
"""
def __init__(self, when: Optional[float]) -> None:
"""Schedule a timeout that will trigger at a given loop time.
- If `when` is `None`, the timeout will never trigger.
- If `when < loop.time()`, the timeout will trigger on the next
iteration of the event loop.
"""
self._state = _State.CREATED
self._timeout_handler: Optional[events.TimerHandle] = None
self._task: Optional[tasks.Task] = None
self._when = when
def when(self) -> Optional[float]:
"""Return the current deadline."""
return self._when
def reschedule(self, when: Optional[float]) -> None:
"""Reschedule the timeout."""
if self._state is not _State.ENTERED:
if self._state is _State.CREATED:
raise RuntimeError("Timeout has not been entered")
raise RuntimeError(
f"Cannot change state of {self._state.value} Timeout",
)
self._when = when
if self._timeout_handler is not None:
self._timeout_handler.cancel()
if when is None:
self._timeout_handler = None
else:
loop = events.get_running_loop()
if when <= loop.time():
self._timeout_handler = loop.call_soon(self._on_timeout)
else:
self._timeout_handler = loop.call_at(when, self._on_timeout)
def expired(self) -> bool:
"""Is timeout expired during execution?"""
return self._state in (_State.EXPIRING, _State.EXPIRED)
def __repr__(self) -> str:
info = ['']
if self._state is _State.ENTERED:
when = round(self._when, 3) if self._when is not None else None
info.append(f"when={when}")
info_str = ' '.join(info)
return f"<Timeout [{self._state.value}]{info_str}>"
async def __aenter__(self) -> "Timeout":
if self._state is not _State.CREATED:
raise RuntimeError("Timeout has already been entered")
task = tasks.current_task()
if task is None:
raise RuntimeError("Timeout should be used inside a task")
self._state = _State.ENTERED
self._task = task
self._cancelling = self._task.cancelling()
self.reschedule(self._when)
return self
async def __aexit__(
self,
exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> Optional[bool]:
assert self._state in (_State.ENTERED, _State.EXPIRING)
if self._timeout_handler is not None:
self._timeout_handler.cancel()
self._timeout_handler = None
if self._state is _State.EXPIRING:
self._state = _State.EXPIRED
if self._task.uncancel() <= self._cancelling and exc_type is exceptions.CancelledError:
# Since there are no new cancel requests, we're
# handling this.
raise TimeoutError from exc_val
elif self._state is _State.ENTERED:
self._state = _State.EXITED
return None
def _on_timeout(self) -> None:
assert self._state is _State.ENTERED
self._task.cancel()
self._state = _State.EXPIRING
# drop the reference early
self._timeout_handler = None
def timeout(delay: Optional[float]) -> Timeout:
"""Timeout async context manager.
Useful in cases when you want to apply timeout logic around block
of code or in cases when asyncio.wait_for is not suitable. For example:
>>> async with asyncio.timeout(10): # 10 seconds timeout
... await long_running_task()
delay - value in seconds or None to disable timeout logic
long_running_task() is interrupted by raising asyncio.CancelledError,
the top-most affected timeout() context manager converts CancelledError
into TimeoutError.
"""
loop = events.get_running_loop()
return Timeout(loop.time() + delay if delay is not None else None)
def timeout_at(when: Optional[float]) -> Timeout:
"""Schedule the timeout at absolute time.
Like timeout() but argument gives absolute time in the same clock system
as loop.time().
Please note: it is not POSIX time but a time with
undefined starting base, e.g. the time of the system power on.
>>> async with asyncio.timeout_at(loop.time() + 10):
... await long_running_task()
when - a deadline when timeout occurs or None to disable timeout logic
long_running_task() is interrupted by raising asyncio.CancelledError,
the top-most affected timeout() context manager converts CancelledError
into TimeoutError.
"""
return Timeout(when)

View File

@@ -1,15 +1,16 @@
"""Abstract Transport class."""
from asyncio import compat
__all__ = ['BaseTransport', 'ReadTransport', 'WriteTransport',
'Transport', 'DatagramTransport', 'SubprocessTransport',
]
__all__ = (
'BaseTransport', 'ReadTransport', 'WriteTransport',
'Transport', 'DatagramTransport', 'SubprocessTransport',
)
class BaseTransport:
"""Base class for transports."""
__slots__ = ('_extra',)
def __init__(self, extra=None):
if extra is None:
extra = {}
@@ -28,8 +29,8 @@ class BaseTransport:
Buffered data will be flushed asynchronously. No more data
will be received. After all buffered data is flushed, the
protocol's connection_lost() method will (eventually) called
with None as its argument.
protocol's connection_lost() method will (eventually) be
called with None as its argument.
"""
raise NotImplementedError
@@ -45,6 +46,12 @@ class BaseTransport:
class ReadTransport(BaseTransport):
"""Interface for read-only transports."""
__slots__ = ()
def is_reading(self):
"""Return True if the transport is receiving."""
raise NotImplementedError
def pause_reading(self):
"""Pause the receiving end.
@@ -65,6 +72,8 @@ class ReadTransport(BaseTransport):
class WriteTransport(BaseTransport):
"""Interface for write-only transports."""
__slots__ = ()
def set_write_buffer_limits(self, high=None, low=None):
"""Set the high- and low-water limits for write flow control.
@@ -90,6 +99,12 @@ class WriteTransport(BaseTransport):
"""Return the current size of the write buffer."""
raise NotImplementedError
def get_write_buffer_limits(self):
"""Get the high and low watermarks for write flow control.
Return a tuple (low, high) where low and high are
positive number of bytes."""
raise NotImplementedError
def write(self, data):
"""Write some data bytes to the transport.
@@ -104,7 +119,7 @@ class WriteTransport(BaseTransport):
The default implementation concatenates the arguments and
calls write() on the result.
"""
data = compat.flatten_list_bytes(list_of_data)
data = b''.join(list_of_data)
self.write(data)
def write_eof(self):
@@ -151,10 +166,14 @@ class Transport(ReadTransport, WriteTransport):
except writelines(), which calls write() in a loop.
"""
__slots__ = ()
class DatagramTransport(BaseTransport):
"""Interface for datagram (UDP) transports."""
__slots__ = ()
def sendto(self, data, addr=None):
"""Send data to the transport.
@@ -177,6 +196,8 @@ class DatagramTransport(BaseTransport):
class SubprocessTransport(BaseTransport):
__slots__ = ()
def get_pid(self):
"""Get subprocess id."""
raise NotImplementedError
@@ -244,6 +265,8 @@ class _FlowControlMixin(Transport):
resume_writing() may be called.
"""
__slots__ = ('_loop', '_protocol_paused', '_high_water', '_low_water')
def __init__(self, extra=None, loop=None):
super().__init__(extra)
assert loop is not None
@@ -259,7 +282,9 @@ class _FlowControlMixin(Transport):
self._protocol_paused = True
try:
self._protocol.pause_writing()
except Exception as exc:
except (SystemExit, KeyboardInterrupt):
raise
except BaseException as exc:
self._loop.call_exception_handler({
'message': 'protocol.pause_writing() failed',
'exception': exc,
@@ -269,11 +294,13 @@ class _FlowControlMixin(Transport):
def _maybe_resume_protocol(self):
if (self._protocol_paused and
self.get_write_buffer_size() <= self._low_water):
self.get_write_buffer_size() <= self._low_water):
self._protocol_paused = False
try:
self._protocol.resume_writing()
except Exception as exc:
except (SystemExit, KeyboardInterrupt):
raise
except BaseException as exc:
self._loop.call_exception_handler({
'message': 'protocol.resume_writing() failed',
'exception': exc,
@@ -287,14 +314,16 @@ class _FlowControlMixin(Transport):
def _set_write_buffer_limits(self, high=None, low=None):
if high is None:
if low is None:
high = 64*1024
high = 64 * 1024
else:
high = 4*low
high = 4 * low
if low is None:
low = high // 4
if not high >= low >= 0:
raise ValueError('high (%r) must be >= low (%r) must be >= 0' %
(high, low))
raise ValueError(
f'high ({high!r}) must be >= low ({low!r}) must be >= 0')
self._high_water = high
self._low_water = low

98
Lib/asyncio/trsock.py vendored Normal file
View File

@@ -0,0 +1,98 @@
import socket
class TransportSocket:
"""A socket-like wrapper for exposing real transport sockets.
These objects can be safely returned by APIs like
`transport.get_extra_info('socket')`. All potentially disruptive
operations (like "socket.close()") are banned.
"""
__slots__ = ('_sock',)
def __init__(self, sock: socket.socket):
self._sock = sock
@property
def family(self):
return self._sock.family
@property
def type(self):
return self._sock.type
@property
def proto(self):
return self._sock.proto
def __repr__(self):
s = (
f"<asyncio.TransportSocket fd={self.fileno()}, "
f"family={self.family!s}, type={self.type!s}, "
f"proto={self.proto}"
)
if self.fileno() != -1:
try:
laddr = self.getsockname()
if laddr:
s = f"{s}, laddr={laddr}"
except socket.error:
pass
try:
raddr = self.getpeername()
if raddr:
s = f"{s}, raddr={raddr}"
except socket.error:
pass
return f"{s}>"
def __getstate__(self):
raise TypeError("Cannot serialize asyncio.TransportSocket object")
def fileno(self):
return self._sock.fileno()
def dup(self):
return self._sock.dup()
def get_inheritable(self):
return self._sock.get_inheritable()
def shutdown(self, how):
# asyncio doesn't currently provide a high-level transport API
# to shutdown the connection.
self._sock.shutdown(how)
def getsockopt(self, *args, **kwargs):
return self._sock.getsockopt(*args, **kwargs)
def setsockopt(self, *args, **kwargs):
self._sock.setsockopt(*args, **kwargs)
def getpeername(self):
return self._sock.getpeername()
def getsockname(self):
return self._sock.getsockname()
def getsockbyname(self):
return self._sock.getsockbyname()
def settimeout(self, value):
if value == 0:
return
raise ValueError(
'settimeout(): only 0 timeout is allowed on transport sockets')
def gettimeout(self):
return 0
def setblocking(self, flag):
if not flag:
return
raise ValueError(
'setblocking(): transport sockets cannot be blocking')

File diff suppressed because it is too large Load Diff

View File

@@ -1,32 +1,41 @@
"""Selector and proactor event loops for Windows."""
import sys
if sys.platform != 'win32': # pragma: no cover
raise ImportError('win32 only')
import _overlapped
import _winapi
import errno
from functools import partial
import math
import msvcrt
import socket
import struct
import time
import weakref
from . import events
from . import base_subprocess
from . import futures
from . import exceptions
from . import proactor_events
from . import selector_events
from . import tasks
from . import windows_utils
# XXX RustPython TODO: _overlapped
# from . import _overlapped
from .coroutines import coroutine
from .log import logger
__all__ = ['SelectorEventLoop', 'ProactorEventLoop', 'IocpProactor',
'DefaultEventLoopPolicy',
]
__all__ = (
'SelectorEventLoop', 'ProactorEventLoop', 'IocpProactor',
'DefaultEventLoopPolicy', 'WindowsSelectorEventLoopPolicy',
'WindowsProactorEventLoopPolicy',
)
NULL = 0
INFINITE = 0xffffffff
NULL = _winapi.NULL
INFINITE = _winapi.INFINITE
ERROR_CONNECTION_REFUSED = 1225
ERROR_CONNECTION_ABORTED = 1236
@@ -53,7 +62,7 @@ class _OverlappedFuture(futures.Future):
info = super()._repr_info()
if self._ov is not None:
state = 'pending' if self._ov.pending else 'completed'
info.insert(1, 'overlapped=<%s, %#x>' % (state, self._ov.address))
info.insert(1, f'overlapped=<{state}, {self._ov.address:#x}>')
return info
def _cancel_overlapped(self):
@@ -72,9 +81,9 @@ class _OverlappedFuture(futures.Future):
self._loop.call_exception_handler(context)
self._ov = None
def cancel(self):
def cancel(self, msg=None):
self._cancel_overlapped()
return super().cancel()
return super().cancel(msg=msg)
def set_exception(self, exception):
super().set_exception(exception)
@@ -109,12 +118,12 @@ class _BaseWaitHandleFuture(futures.Future):
def _repr_info(self):
info = super()._repr_info()
info.append('handle=%#x' % self._handle)
info.append(f'handle={self._handle:#x}')
if self._handle is not None:
state = 'signaled' if self._poll() else 'waiting'
info.append(state)
if self._wait_handle is not None:
info.append('wait_handle=%#x' % self._wait_handle)
info.append(f'wait_handle={self._wait_handle:#x}')
return info
def _unregister_wait_cb(self, fut):
@@ -146,9 +155,9 @@ class _BaseWaitHandleFuture(futures.Future):
self._unregister_wait_cb(None)
def cancel(self):
def cancel(self, msg=None):
self._unregister_wait()
return super().cancel()
return super().cancel(msg=msg)
def set_exception(self, exception):
self._unregister_wait()
@@ -297,9 +306,6 @@ class PipeServer(object):
class _WindowsSelectorEventLoop(selector_events.BaseSelectorEventLoop):
"""Windows version of selector event loop."""
def _socketpair(self):
return windows_utils.socketpair()
class ProactorEventLoop(proactor_events.BaseProactorEventLoop):
"""Windows version of proactor event loop using IOCP."""
@@ -309,20 +315,34 @@ class ProactorEventLoop(proactor_events.BaseProactorEventLoop):
proactor = IocpProactor()
super().__init__(proactor)
def _socketpair(self):
return windows_utils.socketpair()
def run_forever(self):
try:
assert self._self_reading_future is None
self.call_soon(self._loop_self_reading)
super().run_forever()
finally:
if self._self_reading_future is not None:
ov = self._self_reading_future._ov
self._self_reading_future.cancel()
# self_reading_future always uses IOCP, so even though it's
# been cancelled, we need to make sure that the IOCP message
# is received so that the kernel is not holding on to the
# memory, possibly causing memory corruption later. Only
# unregister it if IO is complete in all respects. Otherwise
# we need another _poll() later to complete the IO.
if ov is not None and not ov.pending:
self._proactor._unregister(ov)
self._self_reading_future = None
@coroutine
def create_pipe_connection(self, protocol_factory, address):
async def create_pipe_connection(self, protocol_factory, address):
f = self._proactor.connect_pipe(address)
pipe = yield from f
pipe = await f
protocol = protocol_factory()
trans = self._make_duplex_pipe_transport(pipe, protocol,
extra={'addr': address})
return trans, protocol
@coroutine
def start_serving_pipe(self, protocol_factory, address):
async def start_serving_pipe(self, protocol_factory, address):
server = PipeServer(address)
def loop_accept_pipe(f=None):
@@ -347,6 +367,10 @@ class ProactorEventLoop(proactor_events.BaseProactorEventLoop):
return
f = self._proactor.accept_pipe(pipe)
except BrokenPipeError:
if pipe and pipe.fileno() != -1:
pipe.close()
self.call_soon(loop_accept_pipe)
except OSError as exc:
if pipe and pipe.fileno() != -1:
self.call_exception_handler({
@@ -358,7 +382,8 @@ class ProactorEventLoop(proactor_events.BaseProactorEventLoop):
elif self._debug:
logger.warning("Accept pipe failed on pipe %r",
pipe, exc_info=True)
except futures.CancelledError:
self.call_soon(loop_accept_pipe)
except exceptions.CancelledError:
if pipe:
pipe.close()
else:
@@ -368,28 +393,22 @@ class ProactorEventLoop(proactor_events.BaseProactorEventLoop):
self.call_soon(loop_accept_pipe)
return [server]
@coroutine
def _make_subprocess_transport(self, protocol, args, shell,
stdin, stdout, stderr, bufsize,
extra=None, **kwargs):
async def _make_subprocess_transport(self, protocol, args, shell,
stdin, stdout, stderr, bufsize,
extra=None, **kwargs):
waiter = self.create_future()
transp = _WindowsSubprocessTransport(self, protocol, args, shell,
stdin, stdout, stderr, bufsize,
waiter=waiter, extra=extra,
**kwargs)
try:
yield from waiter
except Exception as exc:
# Workaround CPython bug #23353: using yield/yield-from in an
# except block of a generator doesn't clear properly sys.exc_info()
err = exc
else:
err = None
if err is not None:
await waiter
except (SystemExit, KeyboardInterrupt):
raise
except BaseException:
transp.close()
yield from transp._wait()
raise err
await transp._wait()
raise
return transp
@@ -397,7 +416,7 @@ class ProactorEventLoop(proactor_events.BaseProactorEventLoop):
class IocpProactor:
"""Proactor implementation using IOCP."""
def __init__(self, concurrency=0xffffffff):
def __init__(self, concurrency=INFINITE):
self._loop = None
self._results = []
self._iocp = _overlapped.CreateIoCompletionPort(
@@ -407,10 +426,16 @@ class IocpProactor:
self._unregistered = []
self._stopped_serving = weakref.WeakSet()
def _check_closed(self):
if self._iocp is None:
raise RuntimeError('IocpProactor is closed')
def __repr__(self):
return ('<%s overlapped#=%s result#=%s>'
% (self.__class__.__name__, len(self._cache),
len(self._results)))
info = ['overlapped#=%s' % len(self._cache),
'result#=%s' % len(self._results)]
if self._iocp is None:
info.append('closed')
return '<%s %s>' % (self.__class__.__name__, " ".join(info))
def set_loop(self, loop):
self._loop = loop
@@ -420,13 +445,40 @@ class IocpProactor:
self._poll(timeout)
tmp = self._results
self._results = []
return tmp
try:
return tmp
finally:
# Needed to break cycles when an exception occurs.
tmp = None
def _result(self, value):
fut = self._loop.create_future()
fut.set_result(value)
return fut
@staticmethod
def finish_socket_func(trans, key, ov):
try:
return ov.getresult()
except OSError as exc:
if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED,
_overlapped.ERROR_OPERATION_ABORTED):
raise ConnectionResetError(*exc.args)
else:
raise
@classmethod
def _finish_recvfrom(cls, trans, key, ov, *, empty_result):
try:
return cls.finish_socket_func(trans, key, ov)
except OSError as exc:
# WSARecvFrom will report ERROR_PORT_UNREACHABLE when the same
# socket is used to send to an address that is not listening.
if exc.winerror == _overlapped.ERROR_PORT_UNREACHABLE:
return empty_result, None
else:
raise
def recv(self, conn, nbytes, flags=0):
self._register_with_iocp(conn)
ov = _overlapped.Overlapped(NULL)
@@ -438,16 +490,50 @@ class IocpProactor:
except BrokenPipeError:
return self._result(b'')
def finish_recv(trans, key, ov):
try:
return ov.getresult()
except OSError as exc:
if exc.winerror == _overlapped.ERROR_NETNAME_DELETED:
raise ConnectionResetError(*exc.args)
else:
raise
return self._register(ov, conn, self.finish_socket_func)
return self._register(ov, conn, finish_recv)
def recv_into(self, conn, buf, flags=0):
self._register_with_iocp(conn)
ov = _overlapped.Overlapped(NULL)
try:
if isinstance(conn, socket.socket):
ov.WSARecvInto(conn.fileno(), buf, flags)
else:
ov.ReadFileInto(conn.fileno(), buf)
except BrokenPipeError:
return self._result(0)
return self._register(ov, conn, self.finish_socket_func)
def recvfrom(self, conn, nbytes, flags=0):
self._register_with_iocp(conn)
ov = _overlapped.Overlapped(NULL)
try:
ov.WSARecvFrom(conn.fileno(), nbytes, flags)
except BrokenPipeError:
return self._result((b'', None))
return self._register(ov, conn, partial(self._finish_recvfrom,
empty_result=b''))
def recvfrom_into(self, conn, buf, flags=0):
self._register_with_iocp(conn)
ov = _overlapped.Overlapped(NULL)
try:
ov.WSARecvFromInto(conn.fileno(), buf, flags)
except BrokenPipeError:
return self._result((0, None))
return self._register(ov, conn, partial(self._finish_recvfrom,
empty_result=0))
def sendto(self, conn, buf, flags=0, addr=None):
self._register_with_iocp(conn)
ov = _overlapped.Overlapped(NULL)
ov.WSASendTo(conn.fileno(), buf, flags, addr)
return self._register(ov, conn, self.finish_socket_func)
def send(self, conn, buf, flags=0):
self._register_with_iocp(conn)
@@ -457,16 +543,7 @@ class IocpProactor:
else:
ov.WriteFile(conn.fileno(), buf)
def finish_send(trans, key, ov):
try:
return ov.getresult()
except OSError as exc:
if exc.winerror == _overlapped.ERROR_NETNAME_DELETED:
raise ConnectionResetError(*exc.args)
else:
raise
return self._register(ov, conn, finish_send)
return self._register(ov, conn, self.finish_socket_func)
def accept(self, listener):
self._register_with_iocp(listener)
@@ -483,12 +560,11 @@ class IocpProactor:
conn.settimeout(listener.gettimeout())
return conn, conn.getpeername()
@coroutine
def accept_coro(future, conn):
async def accept_coro(future, conn):
# Coroutine closing the accept socket if the future is cancelled
try:
yield from future
except futures.CancelledError:
await future
except exceptions.CancelledError:
conn.close()
raise
@@ -498,6 +574,14 @@ class IocpProactor:
return future
def connect(self, conn, address):
if conn.type == socket.SOCK_DGRAM:
# WSAConnect will complete immediately for UDP sockets so we don't
# need to register any IOCP operation
_overlapped.WSAConnect(conn.fileno(), address)
fut = self._loop.create_future()
fut.set_result(None)
return fut
self._register_with_iocp(conn)
# The socket needs to be locally bound before we call ConnectEx().
try:
@@ -520,6 +604,18 @@ class IocpProactor:
return self._register(ov, conn, finish_connect)
def sendfile(self, sock, file, offset, count):
self._register_with_iocp(sock)
ov = _overlapped.Overlapped(NULL)
offset_low = offset & 0xffff_ffff
offset_high = (offset >> 32) & 0xffff_ffff
ov.TransmitFile(sock.fileno(),
msvcrt.get_osfhandle(file.fileno()),
offset_low, offset_high,
count, 0, 0)
return self._register(ov, sock, self.finish_socket_func)
def accept_pipe(self, pipe):
self._register_with_iocp(pipe)
ov = _overlapped.Overlapped(NULL)
@@ -537,13 +633,12 @@ class IocpProactor:
return self._register(ov, pipe, finish_accept_pipe)
@coroutine
def connect_pipe(self, address):
async def connect_pipe(self, address):
delay = CONNECT_PIPE_INIT_DELAY
while True:
# Unfortunately there is no way to do an overlapped connect to a pipe.
# Call CreateFile() in a loop until it doesn't fail with
# ERROR_PIPE_BUSY
# Unfortunately there is no way to do an overlapped connect to
# a pipe. Call CreateFile() in a loop until it doesn't fail with
# ERROR_PIPE_BUSY.
try:
handle = _overlapped.ConnectPipe(address)
break
@@ -553,7 +648,7 @@ class IocpProactor:
# ConnectPipe() failed with ERROR_PIPE_BUSY: retry later
delay = min(delay * 2, CONNECT_PIPE_MAX_DELAY)
yield from tasks.sleep(delay, loop=self._loop)
await tasks.sleep(delay)
return windows_utils.PipeHandle(handle)
@@ -573,6 +668,8 @@ class IocpProactor:
return fut
def _wait_for_handle(self, handle, timeout, _is_cancel):
self._check_closed()
if timeout is None:
ms = _winapi.INFINITE
else:
@@ -615,6 +712,8 @@ class IocpProactor:
# that succeed immediately.
def _register(self, ov, obj, callback):
self._check_closed()
# Return a future which will be set with the result of the
# operation when it completes. The future's value is actually
# the value returned by callback().
@@ -651,6 +750,7 @@ class IocpProactor:
already be signalled (pending in the proactor event queue). It is also
safe if the event is never signalled (because it was cancelled).
"""
self._check_closed()
self._unregistered.append(ov)
def _get_accept_socket(self, family):
@@ -707,8 +807,10 @@ class IocpProactor:
else:
f.set_result(value)
self._results.append(f)
finally:
f = None
# Remove unregisted futures
# Remove unregistered futures
for ov in self._unregistered:
self._cache.pop(ov.address, None)
self._unregistered.clear()
@@ -720,8 +822,12 @@ class IocpProactor:
self._stopped_serving.add(obj)
def close(self):
if self._iocp is None:
# already closed
return
# Cancel remaining registered operations.
for address, (fut, ov, obj, callback) in list(self._cache.items()):
for fut, ov, obj, callback in list(self._cache.values()):
if fut.cancelled():
# Nothing to do with cancelled futures
pass
@@ -742,14 +848,25 @@ class IocpProactor:
context['source_traceback'] = fut._source_traceback
self._loop.call_exception_handler(context)
# Wait until all cancelled overlapped complete: don't exit with running
# overlapped to prevent a crash. Display progress every second if the
# loop is still running.
msg_update = 1.0
start_time = time.monotonic()
next_msg = start_time + msg_update
while self._cache:
if not self._poll(1):
logger.debug('taking long time to close proactor')
if next_msg <= time.monotonic():
logger.debug('%r is running after closing for %.1f seconds',
self, time.monotonic() - start_time)
next_msg = time.monotonic() + msg_update
# handle a few events, or timeout
self._poll(msg_update)
self._results = []
if self._iocp is not None:
_winapi.CloseHandle(self._iocp)
self._iocp = None
_winapi.CloseHandle(self._iocp)
self._iocp = None
def __del__(self):
self.close()
@@ -773,8 +890,12 @@ class _WindowsSubprocessTransport(base_subprocess.BaseSubprocessTransport):
SelectorEventLoop = _WindowsSelectorEventLoop
class _WindowsDefaultEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
class WindowsSelectorEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
_loop_factory = SelectorEventLoop
DefaultEventLoopPolicy = _WindowsDefaultEventLoopPolicy
class WindowsProactorEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
_loop_factory = ProactorEventLoop
DefaultEventLoopPolicy = WindowsProactorEventLoopPolicy

View File

@@ -1,6 +1,4 @@
"""
Various Windows specific bits and pieces
"""
"""Various Windows specific bits and pieces."""
import sys
@@ -11,13 +9,12 @@ import _winapi
import itertools
import msvcrt
import os
import socket
import subprocess
import tempfile
import warnings
__all__ = ['socketpair', 'pipe', 'Popen', 'PIPE', 'PipeHandle']
__all__ = 'pipe', 'Popen', 'PIPE', 'PipeHandle'
# Constants/globals
@@ -29,61 +26,14 @@ STDOUT = subprocess.STDOUT
_mmap_counter = itertools.count()
if hasattr(socket, 'socketpair'):
# Since Python 3.5, socket.socketpair() is now also available on Windows
socketpair = socket.socketpair
else:
# Replacement for socket.socketpair()
def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0):
"""A socket pair usable as a self-pipe, for Windows.
Origin: https://gist.github.com/4325783, by Geert Jansen.
Public domain.
"""
if family == socket.AF_INET:
host = '127.0.0.1'
elif family == socket.AF_INET6:
host = '::1'
else:
raise ValueError("Only AF_INET and AF_INET6 socket address "
"families are supported")
if type != socket.SOCK_STREAM:
raise ValueError("Only SOCK_STREAM socket type is supported")
if proto != 0:
raise ValueError("Only protocol zero is supported")
# We create a connected TCP socket. Note the trick with setblocking(0)
# that prevents us from having to create a thread.
lsock = socket.socket(family, type, proto)
try:
lsock.bind((host, 0))
lsock.listen(1)
# On IPv6, ignore flow_info and scope_id
addr, port = lsock.getsockname()[:2]
csock = socket.socket(family, type, proto)
try:
csock.setblocking(False)
try:
csock.connect((addr, port))
except (BlockingIOError, InterruptedError):
pass
csock.setblocking(True)
ssock, _ = lsock.accept()
except:
csock.close()
raise
finally:
lsock.close()
return (ssock, csock)
# Replacement for os.pipe() using handles instead of fds
def pipe(*, duplex=False, overlapped=(True, True), bufsize=BUFSIZE):
"""Like os.pipe() but with overlapped support and using handles not fds."""
address = tempfile.mktemp(prefix=r'\\.\pipe\python-pipe-%d-%d-' %
(os.getpid(), next(_mmap_counter)))
address = tempfile.mktemp(
prefix=r'\\.\pipe\python-pipe-{:d}-{:d}-'.format(
os.getpid(), next(_mmap_counter)))
if duplex:
openmode = _winapi.PIPE_ACCESS_DUPLEX
@@ -138,10 +88,10 @@ class PipeHandle:
def __repr__(self):
if self._handle is not None:
handle = 'handle=%r' % self._handle
handle = f'handle={self._handle!r}'
else:
handle = 'closed'
return '<%s %s>' % (self.__class__.__name__, handle)
return f'<{self.__class__.__name__} {handle}>'
@property
def handle(self):
@@ -149,7 +99,7 @@ class PipeHandle:
def fileno(self):
if self._handle is None:
raise ValueError("I/O operatioon on closed pipe")
raise ValueError("I/O operation on closed pipe")
return self._handle
def close(self, *, CloseHandle=_winapi.CloseHandle):
@@ -157,10 +107,9 @@ class PipeHandle:
CloseHandle(self._handle)
self._handle = None
def __del__(self):
def __del__(self, _warn=warnings.warn):
if self._handle is not None:
warnings.warn("unclosed %r" % self, ResourceWarning,
source=self)
_warn(f"unclosed {self!r}", ResourceWarning, source=self)
self.close()
def __enter__(self):

161
Lib/base64.py vendored
View File

@@ -1,4 +1,4 @@
#! /usr/bin/python3.6
#! /usr/bin/env python3
"""Base16, Base32, Base64 (RFC 3548), Base85 and Ascii85 data encodings"""
@@ -16,7 +16,7 @@ __all__ = [
'encode', 'decode', 'encodebytes', 'decodebytes',
# Generalized interface for other encodings
'b64encode', 'b64decode', 'b32encode', 'b32decode',
'b16encode', 'b16decode',
'b32hexencode', 'b32hexdecode', 'b16encode', 'b16decode',
# Base85 and Ascii85 encodings
'b85encode', 'b85decode', 'a85encode', 'a85decode',
# Standard Base64 encoding
@@ -76,15 +76,16 @@ def b64decode(s, altchars=None, validate=False):
normal base-64 alphabet nor the alternative alphabet are discarded prior
to the padding check. If validate is True, these non-alphabet characters
in the input result in a binascii.Error.
For more information about the strict base64 check, see:
https://docs.python.org/3.11/library/binascii.html#binascii.a2b_base64
"""
s = _bytes_from_decode_data(s)
if altchars is not None:
altchars = _bytes_from_decode_data(altchars)
assert len(altchars) == 2, repr(altchars)
s = s.translate(bytes.maketrans(altchars, b'+/'))
if validate and not re.match(b'^[A-Za-z0-9+/]*={0,2}$', s):
raise binascii.Error('Non-base64 digit found')
return binascii.a2b_base64(s)
return binascii.a2b_base64(s, strict_mode=validate)
def standard_b64encode(s):
@@ -135,19 +136,40 @@ def urlsafe_b64decode(s):
# Base32 encoding/decoding must be done in Python
_b32alphabet = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
_b32tab2 = None
_b32rev = None
_B32_ENCODE_DOCSTRING = '''
Encode the bytes-like objects using {encoding} and return a bytes object.
'''
_B32_DECODE_DOCSTRING = '''
Decode the {encoding} encoded bytes-like object or ASCII string s.
def b32encode(s):
"""Encode the bytes-like object s using Base32 and return a bytes object.
"""
Optional casefold is a flag specifying whether a lowercase alphabet is
acceptable as input. For security purposes, the default is False.
{extra_args}
The result is returned as a bytes object. A binascii.Error is raised if
the input is incorrectly padded or if there are non-alphabet
characters present in the input.
'''
_B32_DECODE_MAP01_DOCSTRING = '''
RFC 3548 allows for optional mapping of the digit 0 (zero) to the
letter O (oh), and for optional mapping of the digit 1 (one) to
either the letter I (eye) or letter L (el). The optional argument
map01 when not None, specifies which letter the digit 1 should be
mapped to (when map01 is not None, the digit 0 is always mapped to
the letter O). For security purposes the default is None, so that
0 and 1 are not allowed in the input.
'''
_b32alphabet = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
_b32hexalphabet = b'0123456789ABCDEFGHIJKLMNOPQRSTUV'
_b32tab2 = {}
_b32rev = {}
def _b32encode(alphabet, s):
global _b32tab2
# Delay the initialization of the table to not waste memory
# if the function is never called
if _b32tab2 is None:
b32tab = [bytes((i,)) for i in _b32alphabet]
_b32tab2 = [a + b for a in b32tab for b in b32tab]
if alphabet not in _b32tab2:
b32tab = [bytes((i,)) for i in alphabet]
_b32tab2[alphabet] = [a + b for a in b32tab for b in b32tab]
b32tab = None
if not isinstance(s, bytes_types):
@@ -158,9 +180,9 @@ def b32encode(s):
s = s + b'\0' * (5 - leftover) # Don't use += !
encoded = bytearray()
from_bytes = int.from_bytes
b32tab2 = _b32tab2
b32tab2 = _b32tab2[alphabet]
for i in range(0, len(s), 5):
c = from_bytes(s[i: i + 5], 'big')
c = from_bytes(s[i: i + 5]) # big endian
encoded += (b32tab2[c >> 30] + # bits 1 - 10
b32tab2[(c >> 20) & 0x3ff] + # bits 11 - 20
b32tab2[(c >> 10) & 0x3ff] + # bits 21 - 30
@@ -177,29 +199,12 @@ def b32encode(s):
encoded[-1:] = b'='
return bytes(encoded)
def b32decode(s, casefold=False, map01=None):
"""Decode the Base32 encoded bytes-like object or ASCII string s.
Optional casefold is a flag specifying whether a lowercase alphabet is
acceptable as input. For security purposes, the default is False.
RFC 3548 allows for optional mapping of the digit 0 (zero) to the
letter O (oh), and for optional mapping of the digit 1 (one) to
either the letter I (eye) or letter L (el). The optional argument
map01 when not None, specifies which letter the digit 1 should be
mapped to (when map01 is not None, the digit 0 is always mapped to
the letter O). For security purposes the default is None, so that
0 and 1 are not allowed in the input.
The result is returned as a bytes object. A binascii.Error is raised if
the input is incorrectly padded or if there are non-alphabet
characters present in the input.
"""
def _b32decode(alphabet, s, casefold=False, map01=None):
global _b32rev
# Delay the initialization of the table to not waste memory
# if the function is never called
if _b32rev is None:
_b32rev = {v: k for k, v in enumerate(_b32alphabet)}
if alphabet not in _b32rev:
_b32rev[alphabet] = {v: k for k, v in enumerate(alphabet)}
s = _bytes_from_decode_data(s)
if len(s) % 8:
raise binascii.Error('Incorrect padding')
@@ -220,7 +225,7 @@ def b32decode(s, casefold=False, map01=None):
padchars = l - len(s)
# Now decode the full quanta
decoded = bytearray()
b32rev = _b32rev
b32rev = _b32rev[alphabet]
for i in range(0, len(s), 8):
quanta = s[i: i + 8]
acc = 0
@@ -229,18 +234,38 @@ def b32decode(s, casefold=False, map01=None):
acc = (acc << 5) + b32rev[c]
except KeyError:
raise binascii.Error('Non-base32 digit found') from None
decoded += acc.to_bytes(5, 'big')
decoded += acc.to_bytes(5) # big endian
# Process the last, partial quanta
if l % 8 or padchars not in {0, 1, 3, 4, 6}:
raise binascii.Error('Incorrect padding')
if padchars and decoded:
acc <<= 5 * padchars
last = acc.to_bytes(5, 'big')
last = acc.to_bytes(5) # big endian
leftover = (43 - 5 * padchars) // 8 # 1: 4, 3: 3, 4: 2, 6: 1
decoded[-5:] = last[:leftover]
return bytes(decoded)
def b32encode(s):
return _b32encode(_b32alphabet, s)
b32encode.__doc__ = _B32_ENCODE_DOCSTRING.format(encoding='base32')
def b32decode(s, casefold=False, map01=None):
return _b32decode(_b32alphabet, s, casefold, map01)
b32decode.__doc__ = _B32_DECODE_DOCSTRING.format(encoding='base32',
extra_args=_B32_DECODE_MAP01_DOCSTRING)
def b32hexencode(s):
return _b32encode(_b32hexalphabet, s)
b32hexencode.__doc__ = _B32_ENCODE_DOCSTRING.format(encoding='base32hex')
def b32hexdecode(s, casefold=False):
# base32hex does not have the 01 mapping
return _b32decode(_b32hexalphabet, s, casefold)
b32hexdecode.__doc__ = _B32_DECODE_DOCSTRING.format(encoding='base32hex',
extra_args='')
# RFC 3548, Base 16 Alphabet specifies uppercase, but hexlify() returns
# lowercase. The RFC also recommends against accepting input case
# insensitively.
@@ -320,7 +345,7 @@ def a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False):
global _a85chars, _a85chars2
# Delay the initialization of tables to not waste memory
# if the function is never called
if _a85chars is None:
if _a85chars2 is None:
_a85chars = [bytes((i,)) for i in range(33, 118)]
_a85chars2 = [(a + b) for a in _a85chars for b in _a85chars]
@@ -428,7 +453,7 @@ def b85encode(b, pad=False):
global _b85chars, _b85chars2
# Delay the initialization of tables to not waste memory
# if the function is never called
if _b85chars is None:
if _b85chars2 is None:
_b85chars = [bytes((i,)) for i in _b85alphabet]
_b85chars2 = [(a + b) for a in _b85chars for b in _b85chars]
return _85encode(b, _b85chars, _b85chars2, pad)
@@ -483,14 +508,8 @@ MAXBINSIZE = (MAXLINESIZE//4)*3
def encode(input, output):
"""Encode a file; input and output are binary files."""
while True:
s = input.read(MAXBINSIZE)
if not s:
break
while len(s) < MAXBINSIZE:
ns = input.read(MAXBINSIZE-len(s))
if not ns:
break
while s := input.read(MAXBINSIZE):
while len(s) < MAXBINSIZE and (ns := input.read(MAXBINSIZE-len(s))):
s += ns
line = binascii.b2a_base64(s)
output.write(line)
@@ -498,10 +517,7 @@ def encode(input, output):
def decode(input, output):
"""Decode a file; input and output are binary files."""
while True:
line = input.readline()
if not line:
break
while line := input.readline():
s = binascii.a2b_base64(line)
output.write(s)
@@ -531,49 +547,34 @@ def encodebytes(s):
pieces.append(binascii.b2a_base64(chunk))
return b"".join(pieces)
def encodestring(s):
"""Legacy alias of encodebytes()."""
import warnings
warnings.warn("encodestring() is a deprecated alias since 3.1, "
"use encodebytes()",
DeprecationWarning, 2)
return encodebytes(s)
def decodebytes(s):
"""Decode a bytestring of base-64 data into a bytes object."""
_input_type_check(s)
return binascii.a2b_base64(s)
def decodestring(s):
"""Legacy alias of decodebytes()."""
import warnings
warnings.warn("decodestring() is a deprecated alias since Python 3.1, "
"use decodebytes()",
DeprecationWarning, 2)
return decodebytes(s)
# Usable as a script...
def main():
"""Small main program"""
import sys, getopt
usage = f"""usage: {sys.argv[0]} [-h|-d|-e|-u] [file|-]
-h: print this help message and exit
-d, -u: decode
-e: encode (default)"""
try:
opts, args = getopt.getopt(sys.argv[1:], 'deut')
opts, args = getopt.getopt(sys.argv[1:], 'hdeu')
except getopt.error as msg:
sys.stdout = sys.stderr
print(msg)
print("""usage: %s [-d|-e|-u|-t] [file|-]
-d, -u: decode
-e: encode (default)
-t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0])
print(usage)
sys.exit(2)
func = encode
for o, a in opts:
if o == '-e': func = encode
if o == '-d': func = decode
if o == '-u': func = decode
if o == '-t': test(); return
if o == '-h': print(usage); return
if args and args[0] != '-':
with open(args[0], 'rb') as f:
func(f, sys.stdout.buffer)
@@ -581,15 +582,5 @@ def main():
func(sys.stdin.buffer, sys.stdout.buffer)
def test():
s0 = b"Aladdin:open sesame"
print(repr(s0))
s1 = encodebytes(s0)
print(repr(s1))
s2 = decodebytes(s1)
print(repr(s2))
assert s0 == s2
if __name__ == '__main__':
main()

26
Lib/bdb.py vendored
View File

@@ -570,9 +570,12 @@ class Bdb:
rv = frame.f_locals['__return__']
s += '->'
s += reprlib.repr(rv)
line = linecache.getline(filename, lineno, frame.f_globals)
if line:
s += lprefix + line.strip()
if lineno is not None:
line = linecache.getline(filename, lineno, frame.f_globals)
if line:
s += lprefix + line.strip()
else:
s += f'{lprefix}Warning: lineno is None'
return s
# The following methods can be called by clients to use
@@ -805,15 +808,18 @@ def checkfuncname(b, frame):
return True
# Determines if there is an effective (active) breakpoint at this
# line of code. Returns breakpoint number or 0 if none
def effective(file, line, frame):
"""Determine which breakpoint for this file:line is to be acted upon.
"""Return (active breakpoint, delete temporary flag) or (None, None) as
breakpoint to act upon.
Called only if we know there is a breakpoint at this location. Return
the breakpoint that was triggered and a boolean that indicates if it is
ok to delete a temporary breakpoint. Return (None, None) if there is no
matching breakpoint.
The "active breakpoint" is the first entry in bplist[line, file] (which
must exist) that is enabled, for which checkfuncname is True, and that
has neither a False condition nor a positive ignore count. The flag,
meaning that a temporary breakpoint should be deleted, is False only
when the condiion cannot be evaluated (in which case, ignore count is
ignored).
If no such entry exists, then (None, None) is returned.
"""
possibles = Breakpoint.bplist[file, line]
for b in possibles:

502
Lib/binhex.py vendored
View File

@@ -1,502 +0,0 @@
"""Macintosh binhex compression/decompression.
easy interface:
binhex(inputfilename, outputfilename)
hexbin(inputfilename, outputfilename)
"""
#
# Jack Jansen, CWI, August 1995.
#
# The module is supposed to be as compatible as possible. Especially the
# easy interface should work "as expected" on any platform.
# XXXX Note: currently, textfiles appear in mac-form on all platforms.
# We seem to lack a simple character-translate in python.
# (we should probably use ISO-Latin-1 on all but the mac platform).
# XXXX The simple routines are too simple: they expect to hold the complete
# files in-core. Should be fixed.
# XXXX It would be nice to handle AppleDouble format on unix
# (for servers serving macs).
# XXXX I don't understand what happens when you get 0x90 times the same byte on
# input. The resulting code (xx 90 90) would appear to be interpreted as an
# escaped *value* of 0x90. All coders I've seen appear to ignore this nicety...
#
import binascii
import contextlib
import io
import os
import struct
import warnings
warnings.warn('the binhex module is deprecated', DeprecationWarning,
stacklevel=2)
__all__ = ["binhex","hexbin","Error"]
class Error(Exception):
pass
# States (what have we written)
_DID_HEADER = 0
_DID_DATA = 1
# Various constants
REASONABLY_LARGE = 32768 # Minimal amount we pass the rle-coder
LINELEN = 64
RUNCHAR = b"\x90"
#
# This code is no longer byte-order dependent
class FInfo:
def __init__(self):
self.Type = '????'
self.Creator = '????'
self.Flags = 0
def getfileinfo(name):
finfo = FInfo()
with io.open(name, 'rb') as fp:
# Quick check for textfile
data = fp.read(512)
if 0 not in data:
finfo.Type = 'TEXT'
fp.seek(0, 2)
dsize = fp.tell()
dir, file = os.path.split(name)
file = file.replace(':', '-', 1)
return file, finfo, dsize, 0
class openrsrc:
def __init__(self, *args):
pass
def read(self, *args):
return b''
def write(self, *args):
pass
def close(self):
pass
# DeprecationWarning is already emitted on "import binhex". There is no need
# to repeat the warning at each call to deprecated binascii functions.
@contextlib.contextmanager
def _ignore_deprecation_warning():
with warnings.catch_warnings():
warnings.filterwarnings('ignore', '', DeprecationWarning)
yield
class _Hqxcoderengine:
"""Write data to the coder in 3-byte chunks"""
def __init__(self, ofp):
self.ofp = ofp
self.data = b''
self.hqxdata = b''
self.linelen = LINELEN - 1
def write(self, data):
self.data = self.data + data
datalen = len(self.data)
todo = (datalen // 3) * 3
data = self.data[:todo]
self.data = self.data[todo:]
if not data:
return
with _ignore_deprecation_warning():
self.hqxdata = self.hqxdata + binascii.b2a_hqx(data)
self._flush(0)
def _flush(self, force):
first = 0
while first <= len(self.hqxdata) - self.linelen:
last = first + self.linelen
self.ofp.write(self.hqxdata[first:last] + b'\r')
self.linelen = LINELEN
first = last
self.hqxdata = self.hqxdata[first:]
if force:
self.ofp.write(self.hqxdata + b':\r')
def close(self):
if self.data:
with _ignore_deprecation_warning():
self.hqxdata = self.hqxdata + binascii.b2a_hqx(self.data)
self._flush(1)
self.ofp.close()
del self.ofp
class _Rlecoderengine:
"""Write data to the RLE-coder in suitably large chunks"""
def __init__(self, ofp):
self.ofp = ofp
self.data = b''
def write(self, data):
self.data = self.data + data
if len(self.data) < REASONABLY_LARGE:
return
with _ignore_deprecation_warning():
rledata = binascii.rlecode_hqx(self.data)
self.ofp.write(rledata)
self.data = b''
def close(self):
if self.data:
with _ignore_deprecation_warning():
rledata = binascii.rlecode_hqx(self.data)
self.ofp.write(rledata)
self.ofp.close()
del self.ofp
class BinHex:
def __init__(self, name_finfo_dlen_rlen, ofp):
name, finfo, dlen, rlen = name_finfo_dlen_rlen
close_on_error = False
if isinstance(ofp, str):
ofname = ofp
ofp = io.open(ofname, 'wb')
close_on_error = True
try:
ofp.write(b'(This file must be converted with BinHex 4.0)\r\r:')
hqxer = _Hqxcoderengine(ofp)
self.ofp = _Rlecoderengine(hqxer)
self.crc = 0
if finfo is None:
finfo = FInfo()
self.dlen = dlen
self.rlen = rlen
self._writeinfo(name, finfo)
self.state = _DID_HEADER
except:
if close_on_error:
ofp.close()
raise
def _writeinfo(self, name, finfo):
nl = len(name)
if nl > 63:
raise Error('Filename too long')
d = bytes([nl]) + name.encode("latin-1") + b'\0'
tp, cr = finfo.Type, finfo.Creator
if isinstance(tp, str):
tp = tp.encode("latin-1")
if isinstance(cr, str):
cr = cr.encode("latin-1")
d2 = tp + cr
# Force all structs to be packed with big-endian
d3 = struct.pack('>h', finfo.Flags)
d4 = struct.pack('>ii', self.dlen, self.rlen)
info = d + d2 + d3 + d4
self._write(info)
self._writecrc()
def _write(self, data):
self.crc = binascii.crc_hqx(data, self.crc)
self.ofp.write(data)
def _writecrc(self):
# XXXX Should this be here??
# self.crc = binascii.crc_hqx('\0\0', self.crc)
if self.crc < 0:
fmt = '>h'
else:
fmt = '>H'
self.ofp.write(struct.pack(fmt, self.crc))
self.crc = 0
def write(self, data):
if self.state != _DID_HEADER:
raise Error('Writing data at the wrong time')
self.dlen = self.dlen - len(data)
self._write(data)
def close_data(self):
if self.dlen != 0:
raise Error('Incorrect data size, diff=%r' % (self.rlen,))
self._writecrc()
self.state = _DID_DATA
def write_rsrc(self, data):
if self.state < _DID_DATA:
self.close_data()
if self.state != _DID_DATA:
raise Error('Writing resource data at the wrong time')
self.rlen = self.rlen - len(data)
self._write(data)
def close(self):
if self.state is None:
return
try:
if self.state < _DID_DATA:
self.close_data()
if self.state != _DID_DATA:
raise Error('Close at the wrong time')
if self.rlen != 0:
raise Error("Incorrect resource-datasize, diff=%r" % (self.rlen,))
self._writecrc()
finally:
self.state = None
ofp = self.ofp
del self.ofp
ofp.close()
def binhex(inp, out):
"""binhex(infilename, outfilename): create binhex-encoded copy of a file"""
finfo = getfileinfo(inp)
ofp = BinHex(finfo, out)
with io.open(inp, 'rb') as ifp:
# XXXX Do textfile translation on non-mac systems
while True:
d = ifp.read(128000)
if not d: break
ofp.write(d)
ofp.close_data()
ifp = openrsrc(inp, 'rb')
while True:
d = ifp.read(128000)
if not d: break
ofp.write_rsrc(d)
ofp.close()
ifp.close()
class _Hqxdecoderengine:
"""Read data via the decoder in 4-byte chunks"""
def __init__(self, ifp):
self.ifp = ifp
self.eof = 0
def read(self, totalwtd):
"""Read at least wtd bytes (or until EOF)"""
decdata = b''
wtd = totalwtd
#
# The loop here is convoluted, since we don't really now how
# much to decode: there may be newlines in the incoming data.
while wtd > 0:
if self.eof: return decdata
wtd = ((wtd + 2) // 3) * 4
data = self.ifp.read(wtd)
#
# Next problem: there may not be a complete number of
# bytes in what we pass to a2b. Solve by yet another
# loop.
#
while True:
try:
with _ignore_deprecation_warning():
decdatacur, self.eof = binascii.a2b_hqx(data)
break
except binascii.Incomplete:
pass
newdata = self.ifp.read(1)
if not newdata:
raise Error('Premature EOF on binhex file')
data = data + newdata
decdata = decdata + decdatacur
wtd = totalwtd - len(decdata)
if not decdata and not self.eof:
raise Error('Premature EOF on binhex file')
return decdata
def close(self):
self.ifp.close()
class _Rledecoderengine:
"""Read data via the RLE-coder"""
def __init__(self, ifp):
self.ifp = ifp
self.pre_buffer = b''
self.post_buffer = b''
self.eof = 0
def read(self, wtd):
if wtd > len(self.post_buffer):
self._fill(wtd - len(self.post_buffer))
rv = self.post_buffer[:wtd]
self.post_buffer = self.post_buffer[wtd:]
return rv
def _fill(self, wtd):
self.pre_buffer = self.pre_buffer + self.ifp.read(wtd + 4)
if self.ifp.eof:
with _ignore_deprecation_warning():
self.post_buffer = self.post_buffer + \
binascii.rledecode_hqx(self.pre_buffer)
self.pre_buffer = b''
return
#
# Obfuscated code ahead. We have to take care that we don't
# end up with an orphaned RUNCHAR later on. So, we keep a couple
# of bytes in the buffer, depending on what the end of
# the buffer looks like:
# '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0)
# '?\220' - Keep 2 bytes: repeated something-else
# '\220\0' - Escaped \220: Keep 2 bytes.
# '?\220?' - Complete repeat sequence: decode all
# otherwise: keep 1 byte.
#
mark = len(self.pre_buffer)
if self.pre_buffer[-3:] == RUNCHAR + b'\0' + RUNCHAR:
mark = mark - 3
elif self.pre_buffer[-1:] == RUNCHAR:
mark = mark - 2
elif self.pre_buffer[-2:] == RUNCHAR + b'\0':
mark = mark - 2
elif self.pre_buffer[-2:-1] == RUNCHAR:
pass # Decode all
else:
mark = mark - 1
with _ignore_deprecation_warning():
self.post_buffer = self.post_buffer + \
binascii.rledecode_hqx(self.pre_buffer[:mark])
self.pre_buffer = self.pre_buffer[mark:]
def close(self):
self.ifp.close()
class HexBin:
def __init__(self, ifp):
if isinstance(ifp, str):
ifp = io.open(ifp, 'rb')
#
# Find initial colon.
#
while True:
ch = ifp.read(1)
if not ch:
raise Error("No binhex data found")
# Cater for \r\n terminated lines (which show up as \n\r, hence
# all lines start with \r)
if ch == b'\r':
continue
if ch == b':':
break
hqxifp = _Hqxdecoderengine(ifp)
self.ifp = _Rledecoderengine(hqxifp)
self.crc = 0
self._readheader()
def _read(self, len):
data = self.ifp.read(len)
self.crc = binascii.crc_hqx(data, self.crc)
return data
def _checkcrc(self):
filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
#self.crc = binascii.crc_hqx('\0\0', self.crc)
# XXXX Is this needed??
self.crc = self.crc & 0xffff
if filecrc != self.crc:
raise Error('CRC error, computed %x, read %x'
% (self.crc, filecrc))
self.crc = 0
def _readheader(self):
len = self._read(1)
fname = self._read(ord(len))
rest = self._read(1 + 4 + 4 + 2 + 4 + 4)
self._checkcrc()
type = rest[1:5]
creator = rest[5:9]
flags = struct.unpack('>h', rest[9:11])[0]
self.dlen = struct.unpack('>l', rest[11:15])[0]
self.rlen = struct.unpack('>l', rest[15:19])[0]
self.FName = fname
self.FInfo = FInfo()
self.FInfo.Creator = creator
self.FInfo.Type = type
self.FInfo.Flags = flags
self.state = _DID_HEADER
def read(self, *n):
if self.state != _DID_HEADER:
raise Error('Read data at wrong time')
if n:
n = n[0]
n = min(n, self.dlen)
else:
n = self.dlen
rv = b''
while len(rv) < n:
rv = rv + self._read(n-len(rv))
self.dlen = self.dlen - n
return rv
def close_data(self):
if self.state != _DID_HEADER:
raise Error('close_data at wrong time')
if self.dlen:
dummy = self._read(self.dlen)
self._checkcrc()
self.state = _DID_DATA
def read_rsrc(self, *n):
if self.state == _DID_HEADER:
self.close_data()
if self.state != _DID_DATA:
raise Error('Read resource data at wrong time')
if n:
n = n[0]
n = min(n, self.rlen)
else:
n = self.rlen
self.rlen = self.rlen - n
return self._read(n)
def close(self):
if self.state is None:
return
try:
if self.rlen:
dummy = self.read_rsrc(self.rlen)
self._checkcrc()
finally:
self.state = None
self.ifp.close()
def hexbin(inp, out):
"""hexbin(infilename, outfilename) - Decode binhexed file"""
ifp = HexBin(inp)
finfo = ifp.FInfo
if not out:
out = ifp.FName
with io.open(out, 'wb') as ofp:
# XXXX Do translation on non-mac systems
while True:
d = ifp.read(128000)
if not d: break
ofp.write(d)
ifp.close_data()
d = ifp.read_rsrc(128000)
if d:
ofp = openrsrc(out, 'wb')
ofp.write(d)
while True:
d = ifp.read_rsrc(128000)
if not d: break
ofp.write(d)
ofp.close()
ifp.close()

10
Lib/bisect.py vendored
View File

@@ -8,6 +8,8 @@ def insort_right(a, x, lo=0, hi=None, *, key=None):
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
A custom key function can be supplied to customize the sort order.
"""
if key is None:
lo = bisect_right(a, x, lo, hi)
@@ -25,6 +27,8 @@ def bisect_right(a, x, lo=0, hi=None, *, key=None):
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
A custom key function can be supplied to customize the sort order.
"""
if lo < 0:
@@ -57,6 +61,8 @@ def insort_left(a, x, lo=0, hi=None, *, key=None):
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
A custom key function can be supplied to customize the sort order.
"""
if key is None:
@@ -74,6 +80,8 @@ def bisect_left(a, x, lo=0, hi=None, *, key=None):
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
A custom key function can be supplied to customize the sort order.
"""
if lo < 0:
@@ -107,4 +115,4 @@ except ImportError:
# Create aliases
bisect = bisect_right
insort = insort_right
insort = insort_right

128
Lib/calendar.py vendored
View File

@@ -7,15 +7,22 @@ set the first day of the week (0=Monday, 6=Sunday)."""
import sys
import datetime
from enum import IntEnum, global_enum
import locale as _locale
from itertools import repeat
import warnings
__all__ = ["IllegalMonthError", "IllegalWeekdayError", "setfirstweekday",
"firstweekday", "isleap", "leapdays", "weekday", "monthrange",
"monthcalendar", "prmonth", "month", "prcal", "calendar",
"timegm", "month_name", "month_abbr", "day_name", "day_abbr",
"Calendar", "TextCalendar", "HTMLCalendar", "LocaleTextCalendar",
"LocaleHTMLCalendar", "weekheader"]
"LocaleHTMLCalendar", "weekheader",
"Day", "Month", "JANUARY", "FEBRUARY", "MARCH",
"APRIL", "MAY", "JUNE", "JULY",
"AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER",
"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY",
"SATURDAY", "SUNDAY"]
# Exception raised for bad input (with string parameter for details)
error = ValueError
@@ -35,9 +42,46 @@ class IllegalWeekdayError(ValueError):
return "bad weekday number %r; must be 0 (Monday) to 6 (Sunday)" % self.weekday
# Constants for months referenced later
January = 1
February = 2
def __getattr__(name):
if name in ('January', 'February'):
warnings.warn(f"The '{name}' attribute is deprecated, use '{name.upper()}' instead",
DeprecationWarning, stacklevel=2)
if name == 'January':
return 1
else:
return 2
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
# Constants for months
@global_enum
class Month(IntEnum):
JANUARY = 1
FEBRUARY = 2
MARCH = 3
APRIL = 4
MAY = 5
JUNE = 6
JULY = 7
AUGUST = 8
SEPTEMBER = 9
OCTOBER = 10
NOVEMBER = 11
DECEMBER = 12
# Constants for days
@global_enum
class Day(IntEnum):
MONDAY = 0
TUESDAY = 1
WEDNESDAY = 2
THURSDAY = 3
FRIDAY = 4
SATURDAY = 5
SUNDAY = 6
# Number of days per month (except for February in leap years)
mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
@@ -93,9 +137,6 @@ day_abbr = _localized_day('%a')
month_name = _localized_month('%B')
month_abbr = _localized_month('%b')
# Constants for weekdays
(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)
def isleap(year):
"""Return True for leap years, False for non-leap years."""
@@ -114,7 +155,7 @@ def weekday(year, month, day):
"""Return weekday (0-6 ~ Mon-Sun) for year, month (1-12), day (1-31)."""
if not datetime.MINYEAR <= year <= datetime.MAXYEAR:
year = 2000 + year % 400
return datetime.date(year, month, day).weekday()
return Day(datetime.date(year, month, day).weekday())
def monthrange(year, month):
@@ -123,12 +164,12 @@ def monthrange(year, month):
if not 1 <= month <= 12:
raise IllegalMonthError(month)
day1 = weekday(year, month, 1)
ndays = mdays[month] + (month == February and isleap(year))
ndays = mdays[month] + (month == FEBRUARY and isleap(year))
return day1, ndays
def _monthlen(year, month):
return mdays[month] + (month == February and isleap(year))
return mdays[month] + (month == FEBRUARY and isleap(year))
def _prevmonth(year, month):
@@ -258,10 +299,7 @@ class Calendar(object):
Each month contains between 4 and 6 weeks and each week contains 1-7
days. Days are datetime.date objects.
"""
months = [
self.monthdatescalendar(year, i)
for i in range(January, January+12)
]
months = [self.monthdatescalendar(year, m) for m in Month]
return [months[i:i+width] for i in range(0, len(months), width) ]
def yeardays2calendar(self, year, width=3):
@@ -271,10 +309,7 @@ class Calendar(object):
(day number, weekday number) tuples. Day numbers outside this month are
zero.
"""
months = [
self.monthdays2calendar(year, i)
for i in range(January, January+12)
]
months = [self.monthdays2calendar(year, m) for m in Month]
return [months[i:i+width] for i in range(0, len(months), width) ]
def yeardayscalendar(self, year, width=3):
@@ -283,10 +318,7 @@ class Calendar(object):
yeardatescalendar()). Entries in the week lists are day numbers.
Day numbers outside this month are zero.
"""
months = [
self.monthdayscalendar(year, i)
for i in range(January, January+12)
]
months = [self.monthdayscalendar(year, m) for m in Month]
return [months[i:i+width] for i in range(0, len(months), width) ]
@@ -507,7 +539,7 @@ class HTMLCalendar(Calendar):
a('\n')
a('<tr><th colspan="%d" class="%s">%s</th></tr>' % (
width, self.cssclass_year_head, theyear))
for i in range(January, January+12, width):
for i in range(JANUARY, JANUARY+12, width):
# months in this row
months = range(i, min(i+width, 13))
a('<tr>')
@@ -546,71 +578,67 @@ class HTMLCalendar(Calendar):
class different_locale:
def __init__(self, locale):
self.locale = locale
self.oldlocale = None
def __enter__(self):
self.oldlocale = _locale.getlocale(_locale.LC_TIME)
self.oldlocale = _locale.setlocale(_locale.LC_TIME, None)
_locale.setlocale(_locale.LC_TIME, self.locale)
def __exit__(self, *args):
if self.oldlocale is None:
return
_locale.setlocale(_locale.LC_TIME, self.oldlocale)
def _get_default_locale():
locale = _locale.setlocale(_locale.LC_TIME, None)
if locale == "C":
with different_locale(""):
# The LC_TIME locale does not seem to be configured:
# get the user preferred locale.
locale = _locale.setlocale(_locale.LC_TIME, None)
return locale
class LocaleTextCalendar(TextCalendar):
"""
This class can be passed a locale name in the constructor and will return
month and weekday names in the specified locale. If this locale includes
an encoding all strings containing month and weekday names will be returned
as unicode.
month and weekday names in the specified locale.
"""
def __init__(self, firstweekday=0, locale=None):
TextCalendar.__init__(self, firstweekday)
if locale is None:
locale = _locale.getdefaultlocale()
locale = _get_default_locale()
self.locale = locale
def formatweekday(self, day, width):
with different_locale(self.locale):
if width >= 9:
names = day_name
else:
names = day_abbr
name = names[day]
return name[:width].center(width)
return super().formatweekday(day, width)
def formatmonthname(self, theyear, themonth, width, withyear=True):
with different_locale(self.locale):
s = month_name[themonth]
if withyear:
s = "%s %r" % (s, theyear)
return s.center(width)
return super().formatmonthname(theyear, themonth, width, withyear)
class LocaleHTMLCalendar(HTMLCalendar):
"""
This class can be passed a locale name in the constructor and will return
month and weekday names in the specified locale. If this locale includes
an encoding all strings containing month and weekday names will be returned
as unicode.
month and weekday names in the specified locale.
"""
def __init__(self, firstweekday=0, locale=None):
HTMLCalendar.__init__(self, firstweekday)
if locale is None:
locale = _locale.getdefaultlocale()
locale = _get_default_locale()
self.locale = locale
def formatweekday(self, day):
with different_locale(self.locale):
s = day_abbr[day]
return '<th class="%s">%s</th>' % (self.cssclasses[day], s)
return super().formatweekday(day)
def formatmonthname(self, theyear, themonth, withyear=True):
with different_locale(self.locale):
s = month_name[themonth]
if withyear:
s = '%s %s' % (s, theyear)
return '<tr><th colspan="7" class="month">%s</th></tr>' % s
return super().formatmonthname(theyear, themonth, withyear)
# Support for old module level interface
c = TextCalendar()
@@ -695,7 +723,7 @@ def main(args):
parser.add_argument(
"-L", "--locale",
default=None,
help="locale to be used from month and weekday names"
help="locale to use for month and weekday names"
)
parser.add_argument(
"-e", "--encoding",

46
Lib/cgi.py vendored
View File

@@ -13,6 +13,11 @@
This module defines a number of utilities for use by CGI scripts
written in Python.
The global variable maxlen can be set to an integer indicating the maximum size
of a POST request. POST requests larger than this size will result in a
ValueError being raised during parsing. The default value of this variable is 0,
meaning the request size is unlimited.
"""
# History
@@ -41,12 +46,16 @@ from email.message import Message
import html
import locale
import tempfile
import warnings
__all__ = ["MiniFieldStorage", "FieldStorage", "parse", "parse_multipart",
"parse_header", "test", "print_exception", "print_environ",
"print_form", "print_directory", "print_arguments",
"print_environ_usage"]
warnings._deprecated(__name__, remove=(3,13))
# Logging support
# ===============
@@ -77,9 +86,11 @@ def initlog(*allargs):
"""
global log, logfile, logfp
warnings.warn("cgi.log() is deprecated as of 3.10. Use logging instead",
DeprecationWarning, stacklevel=2)
if logfile and not logfp:
try:
logfp = open(logfile, "a")
logfp = open(logfile, "a", encoding="locale")
except OSError:
pass
if not logfp:
@@ -115,7 +126,8 @@ log = initlog # The current logging function
# 0 ==> unlimited input
maxlen = 0
def parse(fp=None, environ=os.environ, keep_blank_values=0, strict_parsing=0):
def parse(fp=None, environ=os.environ, keep_blank_values=0,
strict_parsing=0, separator='&'):
"""Parse a query in the environment or from a file (default stdin)
Arguments, all optional:
@@ -134,6 +146,9 @@ def parse(fp=None, environ=os.environ, keep_blank_values=0, strict_parsing=0):
strict_parsing: flag indicating what to do with parsing errors.
If false (the default), errors are silently ignored.
If true, errors raise a ValueError exception.
separator: str. The symbol to use for separating the query arguments.
Defaults to &.
"""
if fp is None:
fp = sys.stdin
@@ -154,7 +169,7 @@ def parse(fp=None, environ=os.environ, keep_blank_values=0, strict_parsing=0):
if environ['REQUEST_METHOD'] == 'POST':
ctype, pdict = parse_header(environ['CONTENT_TYPE'])
if ctype == 'multipart/form-data':
return parse_multipart(fp, pdict)
return parse_multipart(fp, pdict, separator=separator)
elif ctype == 'application/x-www-form-urlencoded':
clength = int(environ['CONTENT_LENGTH'])
if maxlen and clength > maxlen:
@@ -178,10 +193,10 @@ def parse(fp=None, environ=os.environ, keep_blank_values=0, strict_parsing=0):
qs = ""
environ['QUERY_STRING'] = qs # XXX Shouldn't, really
return urllib.parse.parse_qs(qs, keep_blank_values, strict_parsing,
encoding=encoding)
encoding=encoding, separator=separator)
def parse_multipart(fp, pdict, encoding="utf-8", errors="replace"):
def parse_multipart(fp, pdict, encoding="utf-8", errors="replace", separator='&'):
"""Parse multipart input.
Arguments:
@@ -194,15 +209,18 @@ def parse_multipart(fp, pdict, encoding="utf-8", errors="replace"):
value is a list of values for that field. For non-file fields, the value
is a list of strings.
"""
# RFC 2026, Section 5.1 : The "multipart" boundary delimiters are always
# RFC 2046, Section 5.1 : The "multipart" boundary delimiters are always
# represented as 7bit US-ASCII.
boundary = pdict['boundary'].decode('ascii')
ctype = "multipart/form-data; boundary={}".format(boundary)
headers = Message()
headers.set_type(ctype)
headers['Content-Length'] = pdict['CONTENT-LENGTH']
try:
headers['Content-Length'] = pdict['CONTENT-LENGTH']
except KeyError:
pass
fs = FieldStorage(fp, headers=headers, encoding=encoding, errors=errors,
environ={'REQUEST_METHOD': 'POST'})
environ={'REQUEST_METHOD': 'POST'}, separator=separator)
return {k: fs.getlist(k) for k in fs}
def _parseparam(s):
@@ -312,7 +330,7 @@ class FieldStorage:
def __init__(self, fp=None, headers=None, outerboundary=b'',
environ=os.environ, keep_blank_values=0, strict_parsing=0,
limit=None, encoding='utf-8', errors='replace',
max_num_fields=None):
max_num_fields=None, separator='&'):
"""Constructor. Read multipart/* until last part.
Arguments, all optional:
@@ -360,6 +378,7 @@ class FieldStorage:
self.keep_blank_values = keep_blank_values
self.strict_parsing = strict_parsing
self.max_num_fields = max_num_fields
self.separator = separator
if 'REQUEST_METHOD' in environ:
method = environ['REQUEST_METHOD'].upper()
self.qs_on_post = None
@@ -586,7 +605,7 @@ class FieldStorage:
query = urllib.parse.parse_qsl(
qs, self.keep_blank_values, self.strict_parsing,
encoding=self.encoding, errors=self.errors,
max_num_fields=self.max_num_fields)
max_num_fields=self.max_num_fields, separator=self.separator)
self.list = [MiniFieldStorage(key, value) for key, value in query]
self.skip_lines()
@@ -602,7 +621,7 @@ class FieldStorage:
query = urllib.parse.parse_qsl(
self.qs_on_post, self.keep_blank_values, self.strict_parsing,
encoding=self.encoding, errors=self.errors,
max_num_fields=self.max_num_fields)
max_num_fields=self.max_num_fields, separator=self.separator)
self.list.extend(MiniFieldStorage(key, value) for key, value in query)
klass = self.FieldStorageClass or self.__class__
@@ -646,7 +665,7 @@ class FieldStorage:
else self.limit - self.bytes_read
part = klass(self.fp, headers, ib, environ, keep_blank_values,
strict_parsing, limit,
self.encoding, self.errors, max_num_fields)
self.encoding, self.errors, max_num_fields, self.separator)
if max_num_fields is not None:
max_num_fields -= 1
@@ -736,7 +755,8 @@ class FieldStorage:
last_line_lfend = True
_read = 0
while 1:
if self.limit is not None and _read >= self.limit:
if self.limit is not None and 0 <= self.limit <= _read:
break
line = self.fp.readline(1<<16) # bytes
self.bytes_read += len(line)

25
Lib/cgitb.py vendored
View File

@@ -31,6 +31,11 @@ import tempfile
import time
import tokenize
import traceback
import warnings
from html import escape as html_escape
warnings._deprecated(__name__, remove=(3, 13))
def reset():
"""Return a string that resets the CGI and browser to a known state."""
@@ -69,7 +74,7 @@ def lookup(name, frame, locals):
return 'global', frame.f_globals[name]
if '__builtins__' in frame.f_globals:
builtins = frame.f_globals['__builtins__']
if type(builtins) is type({}):
if isinstance(builtins, dict):
if name in builtins:
return 'builtin', builtins[name]
else:
@@ -105,10 +110,16 @@ def html(einfo, context=5):
etype = etype.__name__
pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
date = time.ctime(time.time())
head = '<body bgcolor="#f0f0f8">' + pydoc.html.heading(
'<big><big>%s</big></big>' %
strong(pydoc.html.escape(str(etype))),
'#ffffff', '#6622aa', pyver + '<br>' + date) + '''
head = f'''
<body bgcolor="#f0f0f8">
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#6622aa">
<td valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial">&nbsp;<br>
<big><big><strong>{html_escape(str(etype))}</strong></big></big></font></td>
<td align=right valign=bottom>
<font color="#ffffff" face="helvetica, arial">{pyver}<br>{date}</font></td>
</tr></table>
<p>A problem occurred in a Python script. Here is the sequence of
function calls leading up to the error, in the order they occurred.</p>'''
@@ -181,8 +192,8 @@ function calls leading up to the error, in the order they occurred.</p>'''
<!-- The above is a description of an error in a Python program, formatted
for a Web browser because the 'cgitb' module was enabled. In case you
are not reading this in a Web browser, here is the original traceback:
for a web browser because the 'cgitb' module was enabled. In case you
are not reading this in a web browser, here is the original traceback:
%s
-->

6
Lib/chunk.py vendored
View File

@@ -48,6 +48,10 @@ specifies whether or not chunks are aligned on 2-byte boundaries. The
default is 1, i.e. aligned.
"""
import warnings
warnings._deprecated(__name__, remove=(3, 13))
class Chunk:
def __init__(self, file, align=True, bigendian=True, inclheader=False):
import struct
@@ -64,7 +68,7 @@ class Chunk:
try:
self.chunksize = struct.unpack_from(strflag+'L', file.read(4))[0]
except struct.error:
raise EOFError
raise EOFError from None
if inclheader:
self.chunksize = self.chunksize - 8 # subtract header
self.size_read = 0

10
Lib/cmd.py vendored
View File

@@ -310,10 +310,10 @@ class Cmd:
names = self.get_names()
cmds_doc = []
cmds_undoc = []
help = {}
topics = set()
for name in names:
if name[:5] == 'help_':
help[name[5:]]=1
topics.add(name[5:])
names.sort()
# There can be duplicates if routines overridden
prevname = ''
@@ -323,16 +323,16 @@ class Cmd:
continue
prevname = name
cmd=name[3:]
if cmd in help:
if cmd in topics:
cmds_doc.append(cmd)
del help[cmd]
topics.remove(cmd)
elif getattr(self, name).__doc__:
cmds_doc.append(cmd)
else:
cmds_undoc.append(cmd)
self.stdout.write("%s\n"%str(self.doc_leader))
self.print_topics(self.doc_header, cmds_doc, 15,80)
self.print_topics(self.misc_header, list(help.keys()),15,80)
self.print_topics(self.misc_header, sorted(topics),15,80)
self.print_topics(self.undoc_header, cmds_undoc, 15,80)
def print_topics(self, header, cmds, cmdlen, maxcol):

9
Lib/code.py vendored
View File

@@ -7,7 +7,6 @@
import sys
import traceback
import argparse
from codeop import CommandCompiler, compile_command
__all__ = ["InteractiveInterpreter", "InteractiveConsole", "interact",
@@ -41,7 +40,7 @@ class InteractiveInterpreter:
Arguments are as for compile_command().
One several things can happen:
One of several things can happen:
1) The input is incorrect; compile_command() raised an
exception (SyntaxError or OverflowError). A syntax traceback
@@ -107,6 +106,7 @@ class InteractiveInterpreter:
"""
type, value, tb = sys.exc_info()
sys.last_exc = value
sys.last_type = type
sys.last_value = value
sys.last_traceback = tb
@@ -120,7 +120,7 @@ class InteractiveInterpreter:
else:
# Stuff in the right filename
value = SyntaxError(msg, (filename, lineno, offset, line))
sys.last_value = value
sys.last_exc = sys.last_value = value
if sys.excepthook is sys.__excepthook__:
lines = traceback.format_exception_only(type, value)
self.write(''.join(lines))
@@ -139,6 +139,7 @@ class InteractiveInterpreter:
"""
sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
sys.last_traceback = last_tb
sys.last_exc = ei[1]
try:
lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next)
if sys.excepthook is sys.__excepthook__:
@@ -303,6 +304,8 @@ def interact(banner=None, readfunc=None, local=None, exitmsg=None):
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-q', action='store_true',
help="don't print version and copyright messages")

25
Lib/codecs.py vendored
View File

@@ -414,6 +414,9 @@ class StreamWriter(Codec):
def __exit__(self, type, value, tb):
self.stream.close()
def __reduce_ex__(self, proto):
raise TypeError("can't serialize %s" % self.__class__.__name__)
###
class StreamReader(Codec):
@@ -663,6 +666,9 @@ class StreamReader(Codec):
def __exit__(self, type, value, tb):
self.stream.close()
def __reduce_ex__(self, proto):
raise TypeError("can't serialize %s" % self.__class__.__name__)
###
class StreamReaderWriter:
@@ -750,6 +756,9 @@ class StreamReaderWriter:
def __exit__(self, type, value, tb):
self.stream.close()
def __reduce_ex__(self, proto):
raise TypeError("can't serialize %s" % self.__class__.__name__)
###
class StreamRecoder:
@@ -866,6 +875,9 @@ class StreamRecoder:
def __exit__(self, type, value, tb):
self.stream.close()
def __reduce_ex__(self, proto):
raise TypeError("can't serialize %s" % self.__class__.__name__)
### Shortcuts
def open(filename, mode='r', encoding=None, errors='strict', buffering=-1):
@@ -878,7 +890,8 @@ def open(filename, mode='r', encoding=None, errors='strict', buffering=-1):
codecs. Output is also codec dependent and will usually be
Unicode as well.
Underlying encoded files are always opened in binary mode.
If encoding is not None, then the
underlying encoded files are always opened in binary mode.
The default file mode is 'r', meaning to open the file in read mode.
encoding specifies the encoding which is to be used for the
@@ -1114,13 +1127,3 @@ except LookupError:
_false = 0
if _false:
import encodings
### Tests
if __name__ == '__main__':
# Make stdout translate Latin-1 output into UTF-8 output
sys.stdout = EncodedFile(sys.stdout, 'latin-1', 'utf-8')
# Have stdin translate Latin-1 input into UTF-8 input
sys.stdin = EncodedFile(sys.stdin, 'utf-8', 'latin-1')

103
Lib/codeop.py vendored
View File

@@ -10,30 +10,6 @@ and:
syntax error (OverflowError and ValueError can be produced by
malformed literals).
Approach:
First, check if the source consists entirely of blank lines and
comments; if so, replace it with 'pass', because the built-in
parser doesn't always do the right thing for these.
Compile three times: as is, with \n, and with \n\n appended. If it
compiles as is, it's complete. If it compiles with one \n appended,
we expect more. If it doesn't compile either way, we compare the
error we get when compiling with \n or \n\n appended. If the errors
are the same, the code is broken. But if the errors are different, we
expect more. Not intuitive; not even guaranteed to hold in future
releases; but this matches the compiler's behavior from Python 1.4
through 2.2, at least.
Caveat:
It is possible (but not likely) that the parser stops parsing with a
successful outcome before reaching the end of the source; in this
case, trailing symbols may be ignored instead of causing an error.
For example, a backslash followed by two newlines may be followed by
arbitrary garbage. This will be fixed once the API for the parser is
better.
The two interfaces are:
compile_command(source, filename, symbol):
@@ -64,53 +40,53 @@ _features = [getattr(__future__, fname)
__all__ = ["compile_command", "Compile", "CommandCompiler"]
PyCF_DONT_IMPLY_DEDENT = 0x200 # Matches pythonrun.h
# The following flags match the values from Include/cpython/compile.h
# Caveat emptor: These flags are undocumented on purpose and depending
# on their effect outside the standard library is **unsupported**.
PyCF_DONT_IMPLY_DEDENT = 0x200
PyCF_ALLOW_INCOMPLETE_INPUT = 0x4000
def _maybe_compile(compiler, source, filename, symbol):
# Check for source consisting of only blank lines and comments
# Check for source consisting of only blank lines and comments.
for line in source.split("\n"):
line = line.strip()
if line and line[0] != '#':
break # Leave it alone
break # Leave it alone.
else:
if symbol != "eval":
source = "pass" # Replace it with a 'pass' statement
err = err1 = err2 = None
code = code1 = code2 = None
try:
code = compiler(source, filename, symbol)
except SyntaxError:
pass
# Catch syntax warnings after the first compile
# to emit warnings (SyntaxWarning, DeprecationWarning) at most once.
# Disable compiler warnings when checking for incomplete input.
with warnings.catch_warnings():
warnings.simplefilter("error")
warnings.simplefilter("ignore", (SyntaxWarning, DeprecationWarning))
try:
code1 = compiler(source + "\n", filename, symbol)
except SyntaxError as e:
err1 = e
compiler(source, filename, symbol)
except SyntaxError: # Let other compile() errors propagate.
try:
compiler(source + "\n", filename, symbol)
return None
except SyntaxError as e:
if "incomplete input" in str(e):
return None
# fallthrough
try:
code2 = compiler(source + "\n\n", filename, symbol)
except SyntaxError as e:
err2 = e
return compiler(source, filename, symbol, incomplete_input=False)
try:
if code:
return code
if not code1 and repr(err1) == repr(err2):
raise err1
finally:
err1 = err2 = None
def _is_syntax_error(err1, err2):
rep1 = repr(err1)
rep2 = repr(err2)
if "was never closed" in rep1 and "was never closed" in rep2:
return False
if rep1 == rep2:
return True
return False
def _compile(source, filename, symbol):
return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT)
def _compile(source, filename, symbol, incomplete_input=True):
flags = 0
if incomplete_input:
flags |= PyCF_ALLOW_INCOMPLETE_INPUT
flags |= PyCF_DONT_IMPLY_DEDENT
return compile(source, filename, symbol, flags)
def compile_command(source, filename="<input>", symbol="single"):
@@ -134,24 +110,25 @@ def compile_command(source, filename="<input>", symbol="single"):
"""
return _maybe_compile(_compile, source, filename, symbol)
class Compile:
"""Instances of this class behave much like the built-in compile
function, but if one is used to compile text containing a future
statement, it "remembers" and compiles all subsequent program texts
with the statement in force."""
def __init__(self):
self.flags = PyCF_DONT_IMPLY_DEDENT
self.flags = PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT
def __call__(self, source, filename, symbol):
codeob = compile(source, filename, symbol, self.flags, True)
def __call__(self, source, filename, symbol, **kwargs):
flags = self.flags
if kwargs.get('incomplete_input', True) is False:
flags &= ~PyCF_DONT_IMPLY_DEDENT
flags &= ~PyCF_ALLOW_INCOMPLETE_INPUT
codeob = compile(source, filename, symbol, flags, True)
for feature in _features:
if codeob.co_flags & feature.compiler_flag:
self.flags |= feature.compiler_flag
return codeob
class CommandCompiler:
"""Instances of this class have __call__ methods identical in
signature to compile_command; the difference is that if the

View File

@@ -45,6 +45,11 @@ except ImportError:
else:
_collections_abc.MutableSequence.register(deque)
try:
from _collections import _deque_iterator
except ImportError:
pass
try:
from _collections import defaultdict
except ImportError:
@@ -94,17 +99,19 @@ class OrderedDict(dict):
# Individual links are kept alive by the hard reference in self.__map.
# Those hard references disappear when a key is deleted from an OrderedDict.
def __new__(cls, /, *args, **kwds):
"Create the ordered dict object and set up the underlying structures."
self = dict.__new__(cls)
self.__hardroot = _Link()
self.__root = root = _proxy(self.__hardroot)
root.prev = root.next = root
self.__map = {}
return self
def __init__(self, other=(), /, **kwds):
'''Initialize an ordered dictionary. The signature is the same as
regular dictionaries. Keyword argument order is preserved.
'''
try:
self.__root
except AttributeError:
self.__hardroot = _Link()
self.__root = root = _proxy(self.__hardroot)
root.prev = root.next = root
self.__map = {}
self.__update(other, **kwds)
def __setitem__(self, key, value,
@@ -240,11 +247,19 @@ class OrderedDict(dict):
is raised.
'''
if key in self:
result = self[key]
del self[key]
marker = self.__marker
result = dict.pop(self, key, marker)
if result is not marker:
# The same as in __delitem__().
link = self.__map.pop(key)
link_prev = link.prev
link_next = link.next
link_prev.next = link_next
link_next.prev = link_prev
link.prev = None
link.next = None
return result
if default is self.__marker:
if default is marker:
raise KeyError(key)
return default
@@ -263,14 +278,26 @@ class OrderedDict(dict):
'od.__repr__() <==> repr(od)'
if not self:
return '%s()' % (self.__class__.__name__,)
return '%s(%r)' % (self.__class__.__name__, list(self.items()))
return '%s(%r)' % (self.__class__.__name__, dict(self.items()))
def __reduce__(self):
'Return state information for pickling'
inst_dict = vars(self).copy()
for k in vars(OrderedDict()):
inst_dict.pop(k, None)
return self.__class__, (), inst_dict or None, None, iter(self.items())
state = self.__getstate__()
if state:
if isinstance(state, tuple):
state, slots = state
else:
slots = {}
state = state.copy()
slots = slots.copy()
for k in vars(OrderedDict()):
state.pop(k, None)
slots.pop(k, None)
if slots:
state = state, slots
else:
state = state or None
return self.__class__, (), state, None, iter(self.items())
def copy(self):
'od.copy() -> a shallow copy of od'
@@ -491,9 +518,12 @@ def namedtuple(typename, field_names, *, rename=False, defaults=None, module=Non
# specified a particular module.
if module is None:
try:
module = _sys._getframe(1).f_globals.get('__name__', '__main__')
except (AttributeError, ValueError):
pass
module = _sys._getframemodulename(1) or '__main__'
except AttributeError:
try:
module = _sys._getframe(1).f_globals.get('__name__', '__main__')
except (AttributeError, ValueError):
pass
if module is not None:
result.__module__ = module
@@ -613,11 +643,9 @@ class Counter(dict):
['A', 'A', 'B', 'B', 'C', 'C']
# Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1
>>> import math
>>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
>>> product = 1
>>> for factor in prime_factors.elements(): # loop over factors
... product *= factor # and multiply them
>>> product
>>> math.prod(prime_factors.elements())
1836
Note, if an element's count has been set to zero or is a negative
@@ -714,42 +742,6 @@ class Counter(dict):
if elem in self:
super().__delitem__(elem)
def __eq__(self, other):
'True if all counts agree. Missing counts are treated as zero.'
if not isinstance(other, Counter):
return NotImplemented
return all(self[e] == other[e] for c in (self, other) for e in c)
def __ne__(self, other):
'True if any counts disagree. Missing counts are treated as zero.'
if not isinstance(other, Counter):
return NotImplemented
return not self == other
def __le__(self, other):
'True if all counts in self are a subset of those in other.'
if not isinstance(other, Counter):
return NotImplemented
return all(self[e] <= other[e] for c in (self, other) for e in c)
def __lt__(self, other):
'True if all counts in self are a proper subset of those in other.'
if not isinstance(other, Counter):
return NotImplemented
return self <= other and self != other
def __ge__(self, other):
'True if all counts in self are a superset of those in other.'
if not isinstance(other, Counter):
return NotImplemented
return all(self[e] >= other[e] for c in (self, other) for e in c)
def __gt__(self, other):
'True if all counts in self are a proper superset of those in other.'
if not isinstance(other, Counter):
return NotImplemented
return self >= other and self != other
def __repr__(self):
if not self:
return f'{self.__class__.__name__}()'
@@ -795,6 +787,42 @@ class Counter(dict):
# (cp >= cq) == (sp >= sq)
# (cp > cq) == (sp > sq)
def __eq__(self, other):
'True if all counts agree. Missing counts are treated as zero.'
if not isinstance(other, Counter):
return NotImplemented
return all(self[e] == other[e] for c in (self, other) for e in c)
def __ne__(self, other):
'True if any counts disagree. Missing counts are treated as zero.'
if not isinstance(other, Counter):
return NotImplemented
return not self == other
def __le__(self, other):
'True if all counts in self are a subset of those in other.'
if not isinstance(other, Counter):
return NotImplemented
return all(self[e] <= other[e] for c in (self, other) for e in c)
def __lt__(self, other):
'True if all counts in self are a proper subset of those in other.'
if not isinstance(other, Counter):
return NotImplemented
return self <= other and self != other
def __ge__(self, other):
'True if all counts in self are a superset of those in other.'
if not isinstance(other, Counter):
return NotImplemented
return all(self[e] >= other[e] for c in (self, other) for e in c)
def __gt__(self, other):
'True if all counts in self are a proper superset of those in other.'
if not isinstance(other, Counter):
return NotImplemented
return self >= other and self != other
def __add__(self, other):
'''Add counts from two counters.
@@ -997,8 +1025,8 @@ class ChainMap(_collections_abc.MutableMapping):
def __iter__(self):
d = {}
for mapping in reversed(self.maps):
d.update(dict.fromkeys(mapping)) # reuses stored hash values if possible
for mapping in map(dict.fromkeys, reversed(self.maps)):
d |= mapping # reuses stored hash values if possible
return iter(d)
def __contains__(self, key):
@@ -1118,10 +1146,17 @@ class UserDict(_collections_abc.MutableMapping):
def __iter__(self):
return iter(self.data)
# Modify __contains__ to work correctly when __missing__ is present
# Modify __contains__ and get() to work like dict
# does when __missing__ is present.
def __contains__(self, key):
return key in self.data
def get(self, key, default=None):
if key in self:
return self[key]
return default
# Now, add the methods in dicts but not in MutableMapping
def __repr__(self):
return repr(self.data)

28
Lib/colorsys.py vendored
View File

@@ -1,10 +1,14 @@
"""Conversion functions between RGB and other color systems.
This modules provides two functions for each color system ABC:
rgb_to_abc(r, g, b) --> a, b, c
abc_to_rgb(a, b, c) --> r, g, b
All inputs and outputs are triples of floats in the range [0.0...1.0]
(with the exception of I and Q, which covers a slightly larger range).
Inputs outside the valid range may cause exceptions or invalid outputs.
Supported color systems:
RGB: Red, Green, Blue components
YIQ: Luminance, Chrominance (used by composite video signals)
@@ -71,17 +75,18 @@ def yiq_to_rgb(y, i, q):
def rgb_to_hls(r, g, b):
maxc = max(r, g, b)
minc = min(r, g, b)
# XXX Can optimize (maxc+minc) and (maxc-minc)
l = (minc+maxc)/2.0
sumc = (maxc+minc)
rangec = (maxc-minc)
l = sumc/2.0
if minc == maxc:
return 0.0, l, 0.0
if l <= 0.5:
s = (maxc-minc) / (maxc+minc)
s = rangec / sumc
else:
s = (maxc-minc) / (2.0-maxc-minc)
rc = (maxc-r) / (maxc-minc)
gc = (maxc-g) / (maxc-minc)
bc = (maxc-b) / (maxc-minc)
s = rangec / (2.0-maxc-minc) # Not always 2.0-sumc: gh-106498.
rc = (maxc-r) / rangec
gc = (maxc-g) / rangec
bc = (maxc-b) / rangec
if r == maxc:
h = bc-gc
elif g == maxc:
@@ -120,13 +125,14 @@ def _v(m1, m2, hue):
def rgb_to_hsv(r, g, b):
maxc = max(r, g, b)
minc = min(r, g, b)
rangec = (maxc-minc)
v = maxc
if minc == maxc:
return 0.0, 0.0, v
s = (maxc-minc) / maxc
rc = (maxc-r) / (maxc-minc)
gc = (maxc-g) / (maxc-minc)
bc = (maxc-b) / (maxc-minc)
s = rangec / maxc
rc = (maxc-r) / rangec
gc = (maxc-g) / rangec
bc = (maxc-b) / rangec
if r == maxc:
h = bc-gc
elif g == maxc:

286
Lib/compileall.py vendored
View File

@@ -4,7 +4,7 @@ When called as a script with arguments, this compiles the directories
given as arguments recursively; the -l option prevents it from
recursing into directories.
Without arguments, if compiles all modules on sys.path, without
Without arguments, it compiles all modules on sys.path, without
recursing into subdirectories. (Even though it should do so for
packages -- for now, you'll have to deal with packages separately.)
@@ -15,16 +15,14 @@ import sys
import importlib.util
import py_compile
import struct
import filecmp
try:
from concurrent.futures import ProcessPoolExecutor
except ImportError:
ProcessPoolExecutor = None
from functools import partial
from pathlib import Path
__all__ = ["compile_dir","compile_file","compile_path"]
def _walk_dir(dir, ddir=None, maxlevels=10, quiet=0):
def _walk_dir(dir, maxlevels, quiet=0):
if quiet < 2 and isinstance(dir, os.PathLike):
dir = os.fspath(dir)
if not quiet:
@@ -40,59 +38,94 @@ def _walk_dir(dir, ddir=None, maxlevels=10, quiet=0):
if name == '__pycache__':
continue
fullname = os.path.join(dir, name)
if ddir is not None:
dfile = os.path.join(ddir, name)
else:
dfile = None
if not os.path.isdir(fullname):
yield fullname
elif (maxlevels > 0 and name != os.curdir and name != os.pardir and
os.path.isdir(fullname) and not os.path.islink(fullname)):
yield from _walk_dir(fullname, ddir=dfile,
maxlevels=maxlevels - 1, quiet=quiet)
yield from _walk_dir(fullname, maxlevels=maxlevels - 1,
quiet=quiet)
def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
quiet=0, legacy=False, optimize=-1, workers=1):
def compile_dir(dir, maxlevels=None, ddir=None, force=False,
rx=None, quiet=0, legacy=False, optimize=-1, workers=1,
invalidation_mode=None, *, stripdir=None,
prependdir=None, limit_sl_dest=None, hardlink_dupes=False):
"""Byte-compile all modules in the given directory tree.
Arguments (only dir is required):
dir: the directory to byte-compile
maxlevels: maximum recursion level (default 10)
maxlevels: maximum recursion level (default `sys.getrecursionlimit()`)
ddir: the directory that will be prepended to the path to the
file as it is compiled into each byte-code file.
force: if True, force compilation, even if timestamps are up-to-date
quiet: full output with False or 0, errors only with 1,
no output with 2
legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
optimize: optimization level or -1 for level of the interpreter
optimize: int or list of optimization levels or -1 for level of
the interpreter. Multiple levels leads to multiple compiled
files each with one optimization level.
workers: maximum number of parallel workers
invalidation_mode: how the up-to-dateness of the pyc will be checked
stripdir: part of path to left-strip from source file path
prependdir: path to prepend to beginning of original file path, applied
after stripdir
limit_sl_dest: ignore symlinks if they are pointing outside of
the defined path
hardlink_dupes: hardlink duplicated pyc files
"""
if workers is not None and workers < 0:
ProcessPoolExecutor = None
if ddir is not None and (stripdir is not None or prependdir is not None):
raise ValueError(("Destination dir (ddir) cannot be used "
"in combination with stripdir or prependdir"))
if ddir is not None:
stripdir = dir
prependdir = ddir
ddir = None
if workers < 0:
raise ValueError('workers must be greater or equal to 0')
files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels,
ddir=ddir)
if workers != 1:
# Check if this is a system where ProcessPoolExecutor can function.
from concurrent.futures.process import _check_system_limits
try:
_check_system_limits()
except NotImplementedError:
workers = 1
else:
from concurrent.futures import ProcessPoolExecutor
if maxlevels is None:
maxlevels = sys.getrecursionlimit()
files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels)
success = True
if workers is not None and workers != 1 and ProcessPoolExecutor is not None:
if workers != 1 and ProcessPoolExecutor is not None:
# If workers == 0, let ProcessPoolExecutor choose
workers = workers or None
with ProcessPoolExecutor(max_workers=workers) as executor:
results = executor.map(partial(compile_file,
ddir=ddir, force=force,
rx=rx, quiet=quiet,
legacy=legacy,
optimize=optimize),
optimize=optimize,
invalidation_mode=invalidation_mode,
stripdir=stripdir,
prependdir=prependdir,
limit_sl_dest=limit_sl_dest,
hardlink_dupes=hardlink_dupes),
files)
success = min(results, default=True)
else:
for file in files:
if not compile_file(file, ddir, force, rx, quiet,
legacy, optimize):
legacy, optimize, invalidation_mode,
stripdir=stripdir, prependdir=prependdir,
limit_sl_dest=limit_sl_dest,
hardlink_dupes=hardlink_dupes):
success = False
return success
def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
legacy=False, optimize=-1):
legacy=False, optimize=-1,
invalidation_mode=None, *, stripdir=None, prependdir=None,
limit_sl_dest=None, hardlink_dupes=False):
"""Byte-compile one file.
Arguments (only fullname is required):
@@ -104,49 +137,114 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
quiet: full output with False or 0, errors only with 1,
no output with 2
legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
optimize: optimization level or -1 for level of the interpreter
optimize: int or list of optimization levels or -1 for level of
the interpreter. Multiple levels leads to multiple compiled
files each with one optimization level.
invalidation_mode: how the up-to-dateness of the pyc will be checked
stripdir: part of path to left-strip from source file path
prependdir: path to prepend to beginning of original file path, applied
after stripdir
limit_sl_dest: ignore symlinks if they are pointing outside of
the defined path.
hardlink_dupes: hardlink duplicated pyc files
"""
if ddir is not None and (stripdir is not None or prependdir is not None):
raise ValueError(("Destination dir (ddir) cannot be used "
"in combination with stripdir or prependdir"))
success = True
if quiet < 2 and isinstance(fullname, os.PathLike):
fullname = os.fspath(fullname)
fullname = os.fspath(fullname)
stripdir = os.fspath(stripdir) if stripdir is not None else None
name = os.path.basename(fullname)
dfile = None
if ddir is not None:
dfile = os.path.join(ddir, name)
else:
dfile = None
if stripdir is not None:
fullname_parts = fullname.split(os.path.sep)
stripdir_parts = stripdir.split(os.path.sep)
ddir_parts = list(fullname_parts)
for spart, opart in zip(stripdir_parts, fullname_parts):
if spart == opart:
ddir_parts.remove(spart)
dfile = os.path.join(*ddir_parts)
if prependdir is not None:
if dfile is None:
dfile = os.path.join(prependdir, fullname)
else:
dfile = os.path.join(prependdir, dfile)
if isinstance(optimize, int):
optimize = [optimize]
# Use set() to remove duplicates.
# Use sorted() to create pyc files in a deterministic order.
optimize = sorted(set(optimize))
if hardlink_dupes and len(optimize) < 2:
raise ValueError("Hardlinking of duplicated bytecode makes sense "
"only for more than one optimization level")
if rx is not None:
mo = rx.search(fullname)
if mo:
return success
if limit_sl_dest is not None and os.path.islink(fullname):
if Path(limit_sl_dest).resolve() not in Path(fullname).resolve().parents:
return success
opt_cfiles = {}
if os.path.isfile(fullname):
if legacy:
cfile = fullname + 'c'
else:
if optimize >= 0:
opt = optimize if optimize >= 1 else ''
cfile = importlib.util.cache_from_source(
fullname, optimization=opt)
for opt_level in optimize:
if legacy:
opt_cfiles[opt_level] = fullname + 'c'
else:
cfile = importlib.util.cache_from_source(fullname)
cache_dir = os.path.dirname(cfile)
if opt_level >= 0:
opt = opt_level if opt_level >= 1 else ''
cfile = (importlib.util.cache_from_source(
fullname, optimization=opt))
opt_cfiles[opt_level] = cfile
else:
cfile = importlib.util.cache_from_source(fullname)
opt_cfiles[opt_level] = cfile
head, tail = name[:-3], name[-3:]
if tail == '.py':
if not force:
try:
mtime = int(os.stat(fullname).st_mtime)
expect = struct.pack('<4sl', importlib.util.MAGIC_NUMBER,
mtime)
with open(cfile, 'rb') as chandle:
actual = chandle.read(8)
if expect == actual:
expect = struct.pack('<4sLL', importlib.util.MAGIC_NUMBER,
0, mtime & 0xFFFF_FFFF)
for cfile in opt_cfiles.values():
with open(cfile, 'rb') as chandle:
actual = chandle.read(12)
if expect != actual:
break
else:
return success
except OSError:
pass
if not quiet:
print('Compiling {!r}...'.format(fullname))
try:
ok = py_compile.compile(fullname, cfile, dfile, True,
optimize=optimize)
for index, opt_level in enumerate(optimize):
cfile = opt_cfiles[opt_level]
ok = py_compile.compile(fullname, cfile, dfile, True,
optimize=opt_level,
invalidation_mode=invalidation_mode)
if index > 0 and hardlink_dupes:
previous_cfile = opt_cfiles[optimize[index - 1]]
if filecmp.cmp(cfile, previous_cfile, shallow=False):
os.unlink(cfile)
os.link(previous_cfile, cfile)
except py_compile.PyCompileError as err:
success = False
if quiet >= 2:
@@ -156,9 +254,8 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
else:
print('*** ', end='')
# escape non-printable characters in msg
msg = err.msg.encode(sys.stdout.encoding,
errors='backslashreplace')
msg = msg.decode(sys.stdout.encoding)
encoding = sys.stdout.encoding or sys.getdefaultencoding()
msg = err.msg.encode(encoding, errors='backslashreplace').decode(encoding)
print(msg)
except (SyntaxError, UnicodeError, OSError) as e:
success = False
@@ -175,7 +272,8 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
return success
def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=0,
legacy=False, optimize=-1):
legacy=False, optimize=-1,
invalidation_mode=None):
"""Byte-compile all module on sys.path.
Arguments (all optional):
@@ -186,6 +284,7 @@ def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=0,
quiet: as for compile_dir() (default 0)
legacy: as for compile_dir() (default False)
optimize: as for compile_dir() (default -1)
invalidation_mode: as for compiler_dir()
"""
success = True
for dir in sys.path:
@@ -193,9 +292,16 @@ def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=0,
if quiet < 2:
print('Skipping current directory')
else:
success = success and compile_dir(dir, maxlevels, None,
force, quiet=quiet,
legacy=legacy, optimize=optimize)
success = success and compile_dir(
dir,
maxlevels,
None,
force,
quiet=quiet,
legacy=legacy,
optimize=optimize,
invalidation_mode=invalidation_mode,
)
return success
@@ -206,7 +312,7 @@ def main():
parser = argparse.ArgumentParser(
description='Utilities to support installing Python libraries.')
parser.add_argument('-l', action='store_const', const=0,
default=10, dest='maxlevels',
default=None, dest='maxlevels',
help="don't recurse into subdirectories")
parser.add_argument('-r', type=int, dest='recursion',
help=('control the maximum recursion level. '
@@ -224,6 +330,20 @@ def main():
'compile-time tracebacks and in runtime '
'tracebacks in cases where the source file is '
'unavailable'))
parser.add_argument('-s', metavar='STRIPDIR', dest='stripdir',
default=None,
help=('part of path to left-strip from path '
'to source file - for example buildroot. '
'`-d` and `-s` options cannot be '
'specified together.'))
parser.add_argument('-p', metavar='PREPENDDIR', dest='prependdir',
default=None,
help=('path to add as prefix to path '
'to source file - for example / to make '
'it absolute when some part is removed '
'by `-s` option. '
'`-d` and `-p` options cannot be '
'specified together.'))
parser.add_argument('-x', metavar='REGEXP', dest='rx', default=None,
help=('skip files matching the regular expression; '
'the regexp is searched for in the full path '
@@ -238,6 +358,23 @@ def main():
'to the equivalent of -l sys.path'))
parser.add_argument('-j', '--workers', default=1,
type=int, help='Run compileall concurrently')
invalidation_modes = [mode.name.lower().replace('_', '-')
for mode in py_compile.PycInvalidationMode]
parser.add_argument('--invalidation-mode',
choices=sorted(invalidation_modes),
help=('set .pyc invalidation mode; defaults to '
'"checked-hash" if the SOURCE_DATE_EPOCH '
'environment variable is set, and '
'"timestamp" otherwise.'))
parser.add_argument('-o', action='append', type=int, dest='opt_levels',
help=('Optimization levels to run compilation with. '
'Default is -1 which uses the optimization level '
'of the Python interpreter itself (see -O).'))
parser.add_argument('-e', metavar='DIR', dest='limit_sl_dest',
help='Ignore symlinks pointing outsite of the DIR')
parser.add_argument('--hardlink-dupes', action='store_true',
dest='hardlink_dupes',
help='Hardlink duplicated pyc files')
args = parser.parse_args()
compile_dests = args.compile_dest
@@ -246,16 +383,31 @@ def main():
import re
args.rx = re.compile(args.rx)
if args.limit_sl_dest == "":
args.limit_sl_dest = None
if args.recursion is not None:
maxlevels = args.recursion
else:
maxlevels = args.maxlevels
if args.opt_levels is None:
args.opt_levels = [-1]
if len(args.opt_levels) == 1 and args.hardlink_dupes:
parser.error(("Hardlinking of duplicated bytecode makes sense "
"only for more than one optimization level."))
if args.ddir is not None and (
args.stripdir is not None or args.prependdir is not None
):
parser.error("-d cannot be used in combination with -s or -p")
# if flist is provided then load it
if args.flist:
try:
with (sys.stdin if args.flist=='-' else open(args.flist)) as f:
with (sys.stdin if args.flist=='-' else
open(args.flist, encoding="utf-8")) as f:
for line in f:
compile_dests.append(line.strip())
except OSError:
@@ -263,8 +415,11 @@ def main():
print("Error reading file list {}".format(args.flist))
return False
if args.workers is not None:
args.workers = args.workers or None
if args.invalidation_mode:
ivl_mode = args.invalidation_mode.replace('-', '_').upper()
invalidation_mode = py_compile.PycInvalidationMode[ivl_mode]
else:
invalidation_mode = None
success = True
try:
@@ -272,17 +427,30 @@ def main():
for dest in compile_dests:
if os.path.isfile(dest):
if not compile_file(dest, args.ddir, args.force, args.rx,
args.quiet, args.legacy):
args.quiet, args.legacy,
invalidation_mode=invalidation_mode,
stripdir=args.stripdir,
prependdir=args.prependdir,
optimize=args.opt_levels,
limit_sl_dest=args.limit_sl_dest,
hardlink_dupes=args.hardlink_dupes):
success = False
else:
if not compile_dir(dest, maxlevels, args.ddir,
args.force, args.rx, args.quiet,
args.legacy, workers=args.workers):
args.legacy, workers=args.workers,
invalidation_mode=invalidation_mode,
stripdir=args.stripdir,
prependdir=args.prependdir,
optimize=args.opt_levels,
limit_sl_dest=args.limit_sl_dest,
hardlink_dupes=args.hardlink_dupes):
success = False
return success
else:
return compile_path(legacy=args.legacy, force=args.force,
quiet=args.quiet)
quiet=args.quiet,
invalidation_mode=invalidation_mode)
except KeyboardInterrupt:
if args.quiet < 2:
print("\n[interrupted]")

View File

@@ -37,7 +37,8 @@ def _python_exit():
threading._register_atexit(_python_exit)
# At fork, reinitialize the `_global_shutdown_lock` lock in the child process
if hasattr(os, 'register_at_fork'):
# TODO RUSTPYTHON - _at_fork_reinit is not implemented yet
if hasattr(os, 'register_at_fork') and hasattr(_global_shutdown_lock, '_at_fork_reinit'):
os.register_at_fork(before=_global_shutdown_lock.acquire,
after_in_child=_global_shutdown_lock._at_fork_reinit,
after_in_parent=_global_shutdown_lock.release)

204
Lib/configparser.py vendored
View File

@@ -19,36 +19,37 @@ ConfigParser -- responsible for parsing a list of
inline_comment_prefixes=None, strict=True,
empty_lines_in_values=True, default_section='DEFAULT',
interpolation=<unset>, converters=<unset>):
Create the parser. When `defaults' is given, it is initialized into the
Create the parser. When `defaults` is given, it is initialized into the
dictionary or intrinsic defaults. The keys must be strings, the values
must be appropriate for %()s string interpolation.
When `dict_type' is given, it will be used to create the dictionary
When `dict_type` is given, it will be used to create the dictionary
objects for the list of sections, for the options within a section, and
for the default values.
When `delimiters' is given, it will be used as the set of substrings
When `delimiters` is given, it will be used as the set of substrings
that divide keys from values.
When `comment_prefixes' is given, it will be used as the set of
When `comment_prefixes` is given, it will be used as the set of
substrings that prefix comments in empty lines. Comments can be
indented.
When `inline_comment_prefixes' is given, it will be used as the set of
When `inline_comment_prefixes` is given, it will be used as the set of
substrings that prefix comments in non-empty lines.
When `strict` is True, the parser won't allow for any section or option
duplicates while reading from a single source (file, string or
dictionary). Default is True.
When `empty_lines_in_values' is False (default: True), each empty line
When `empty_lines_in_values` is False (default: True), each empty line
marks the end of an option. Otherwise, internal empty lines of
a multiline option are kept as part of the value.
When `allow_no_value' is True (default: False), options without
When `allow_no_value` is True (default: False), options without
values are accepted; the value presented for these is None.
When `default_section' is given, the name of the special section is
When `default_section` is given, the name of the special section is
named accordingly. By default it is called ``"DEFAULT"`` but this can
be customized to point to any other valid section name. Its current
value can be retrieved using the ``parser_instance.default_section``
@@ -56,9 +57,9 @@ ConfigParser -- responsible for parsing a list of
When `interpolation` is given, it should be an Interpolation subclass
instance. It will be used as the handler for option value
pre-processing when using getters. RawConfigParser object s don't do
pre-processing when using getters. RawConfigParser objects don't do
any sort of interpolation, whereas ConfigParser uses an instance of
BasicInterpolation. The library also provides a ``zc.buildbot``
BasicInterpolation. The library also provides a ``zc.buildout``
inspired ExtendedInterpolation implementation.
When `converters` is given, it should be a dictionary where each key
@@ -80,14 +81,14 @@ ConfigParser -- responsible for parsing a list of
Return list of configuration options for the named section.
read(filenames, encoding=None)
Read and parse the list of named configuration files, given by
Read and parse the iterable of named configuration files, given by
name. A single filename is also allowed. Non-existing files
are ignored. Return list of successfully read files.
read_file(f, filename=None)
Read and parse one configuration file, given as a file object.
The filename defaults to f.name; it is only used in error
messages (if f has no `name' attribute, the string `<???>' is used).
messages (if f has no `name` attribute, the string `<???>` is used).
read_string(string)
Read configuration from a given string.
@@ -103,9 +104,9 @@ ConfigParser -- responsible for parsing a list of
Return a string value for the named option. All % interpolations are
expanded in the return values, based on the defaults passed into the
constructor and the DEFAULT section. Additional substitutions may be
provided using the `vars' argument, which must be a dictionary whose
contents override any pre-existing defaults. If `option' is a key in
`vars', the value from `vars' is used.
provided using the `vars` argument, which must be a dictionary whose
contents override any pre-existing defaults. If `option` is a key in
`vars`, the value from `vars` is used.
getint(section, options, raw=False, vars=None, fallback=_UNSET)
Like get(), but convert value to an integer.
@@ -134,28 +135,30 @@ ConfigParser -- responsible for parsing a list of
write(fp, space_around_delimiters=True)
Write the configuration state in .ini format. If
`space_around_delimiters' is True (the default), delimiters
`space_around_delimiters` is True (the default), delimiters
between keys and values are surrounded by spaces.
"""
from collections.abc import MutableMapping
from collections import OrderedDict as _default_dict, ChainMap as _ChainMap
from collections import ChainMap as _ChainMap
import functools
import io
import itertools
import os
import re
import sys
import warnings
__all__ = ["NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
__all__ = ("NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
"NoOptionError", "InterpolationError", "InterpolationDepthError",
"InterpolationMissingOptionError", "InterpolationSyntaxError",
"ParsingError", "MissingSectionHeaderError",
"ConfigParser", "SafeConfigParser", "RawConfigParser",
"ConfigParser", "RawConfigParser",
"Interpolation", "BasicInterpolation", "ExtendedInterpolation",
"LegacyInterpolation", "SectionProxy", "ConverterMapping",
"DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
"DEFAULTSECT", "MAX_INTERPOLATION_DEPTH")
_default_dict = dict
DEFAULTSECT = "DEFAULT"
MAX_INTERPOLATION_DEPTH = 10
@@ -295,41 +298,12 @@ class InterpolationDepthError(InterpolationError):
class ParsingError(Error):
"""Raised when a configuration file does not follow legal syntax."""
def __init__(self, source=None, filename=None):
# Exactly one of `source'/`filename' arguments has to be given.
# `filename' kept for compatibility.
if filename and source:
raise ValueError("Cannot specify both `filename' and `source'. "
"Use `source'.")
elif not filename and not source:
raise ValueError("Required argument `source' not given.")
elif filename:
source = filename
Error.__init__(self, 'Source contains parsing errors: %r' % source)
def __init__(self, source):
super().__init__(f'Source contains parsing errors: {source!r}')
self.source = source
self.errors = []
self.args = (source, )
@property
def filename(self):
"""Deprecated, use `source'."""
warnings.warn(
"The 'filename' attribute will be removed in future versions. "
"Use 'source' instead.",
DeprecationWarning, stacklevel=2
)
return self.source
@filename.setter
def filename(self, value):
"""Deprecated, user `source'."""
warnings.warn(
"The 'filename' attribute will be removed in future versions. "
"Use 'source' instead.",
DeprecationWarning, stacklevel=2
)
self.source = value
def append(self, lineno, line):
self.errors.append((lineno, line))
self.message += '\n\t[line %2d]: %s' % (lineno, line)
@@ -350,7 +324,7 @@ class MissingSectionHeaderError(ParsingError):
# Used in parser getters to indicate the default behaviour when a specific
# option is not found it to raise an exception. Created to enable `None' as
# option is not found it to raise an exception. Created to enable `None` as
# a valid fallback value.
_UNSET = object()
@@ -384,7 +358,7 @@ class BasicInterpolation(Interpolation):
would resolve the "%(dir)s" to the value of dir. All reference
expansions are done late, on demand. If a user needs to use a bare % in
a configuration file, she can escape it by writing %%. Other % usage
is considered a user error and raises `InterpolationSyntaxError'."""
is considered a user error and raises `InterpolationSyntaxError`."""
_KEYCRE = re.compile(r"%\(([^)]+)\)s")
@@ -445,7 +419,7 @@ class BasicInterpolation(Interpolation):
class ExtendedInterpolation(Interpolation):
"""Advanced variant of interpolation, supports the syntax used by
`zc.buildout'. Enables interpolation between sections."""
`zc.buildout`. Enables interpolation between sections."""
_KEYCRE = re.compile(r"\$\{([^}]+)\}")
@@ -523,6 +497,15 @@ class LegacyInterpolation(Interpolation):
_KEYCRE = re.compile(r"%\(([^)]*)\)s|.")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
warnings.warn(
"LegacyInterpolation has been deprecated since Python 3.2 "
"and will be removed from the configparser module in Python 3.13. "
"Use BasicInterpolation or ExtendedInterpolation instead.",
DeprecationWarning, stacklevel=2
)
def before_get(self, parser, section, option, value, vars):
rawval = value
depth = MAX_INTERPOLATION_DEPTH
@@ -561,7 +544,7 @@ class RawConfigParser(MutableMapping):
# Regular expressions for parsing section headers and options
_SECT_TMPL = r"""
\[ # [
(?P<header>[^]]+) # very permissive!
(?P<header>.+) # very permissive!
\] # ]
"""
_OPT_TMPL = r"""
@@ -609,9 +592,6 @@ class RawConfigParser(MutableMapping):
self._converters = ConverterMapping(self)
self._proxies = self._dict()
self._proxies[default_section] = SectionProxy(self, default_section)
if defaults:
for key, value in defaults.items():
self._defaults[self.optionxform(key)] = value
self._delimiters = tuple(delimiters)
if delimiters == ('=', ':'):
self._optcre = self.OPTCRE_NV if allow_no_value else self.OPTCRE
@@ -634,8 +614,15 @@ class RawConfigParser(MutableMapping):
self._interpolation = self._DEFAULT_INTERPOLATION
if self._interpolation is None:
self._interpolation = Interpolation()
if not isinstance(self._interpolation, Interpolation):
raise TypeError(
f"interpolation= must be None or an instance of Interpolation;"
f" got an object of type {type(self._interpolation)}"
)
if converters is not _UNSET:
self._converters.update(converters)
if defaults:
self._read_defaults(defaults)
def defaults(self):
return self._defaults
@@ -676,19 +663,20 @@ class RawConfigParser(MutableMapping):
return list(opts.keys())
def read(self, filenames, encoding=None):
"""Read and parse a filename or a list of filenames.
"""Read and parse a filename or an iterable of filenames.
Files that cannot be opened are silently ignored; this is
designed so that you can specify a list of potential
designed so that you can specify an iterable of potential
configuration file locations (e.g. current directory, user's
home directory, systemwide directory), and all existing
configuration files in the list will be read. A single
configuration files in the iterable will be read. A single
filename may also be given.
Return list of successfully read files.
"""
if isinstance(filenames, str):
if isinstance(filenames, (str, bytes, os.PathLike)):
filenames = [filenames]
encoding = io.text_encoding(encoding)
read_ok = []
for filename in filenames:
try:
@@ -696,16 +684,18 @@ class RawConfigParser(MutableMapping):
self._read(fp, filename)
except OSError:
continue
if isinstance(filename, os.PathLike):
filename = os.fspath(filename)
read_ok.append(filename)
return read_ok
def read_file(self, f, source=None):
"""Like read() but the argument must be a file-like object.
The `f' argument must be iterable, returning one line at a time.
Optional second argument is the `source' specifying the name of the
file being read. If not given, it is taken from f.name. If `f' has no
`name' attribute, `<???>' is used.
The `f` argument must be iterable, returning one line at a time.
Optional second argument is the `source` specifying the name of the
file being read. If not given, it is taken from f.name. If `f` has no
`name` attribute, `<???>` is used.
"""
if source is None:
try:
@@ -729,7 +719,7 @@ class RawConfigParser(MutableMapping):
All types held in the dictionary are converted to strings during
reading, including section names, option names and keys.
Optional second argument is the `source' specifying the name of the
Optional second argument is the `source` specifying the name of the
dictionary being read.
"""
elements_added = set()
@@ -750,27 +740,18 @@ class RawConfigParser(MutableMapping):
elements_added.add((section, key))
self.set(section, key, value)
def readfp(self, fp, filename=None):
"""Deprecated, use read_file instead."""
warnings.warn(
"This method will be removed in future versions. "
"Use 'parser.read_file()' instead.",
DeprecationWarning, stacklevel=2
)
self.read_file(fp, source=filename)
def get(self, section, option, *, raw=False, vars=None, fallback=_UNSET):
"""Get an option value for a given section.
If `vars' is provided, it must be a dictionary. The option is looked up
in `vars' (if provided), `section', and in `DEFAULTSECT' in that order.
If the key is not found and `fallback' is provided, it is used as
a fallback value. `None' can be provided as a `fallback' value.
If `vars` is provided, it must be a dictionary. The option is looked up
in `vars` (if provided), `section`, and in `DEFAULTSECT` in that order.
If the key is not found and `fallback` is provided, it is used as
a fallback value. `None` can be provided as a `fallback` value.
If interpolation is enabled and the optional argument `raw' is False,
If interpolation is enabled and the optional argument `raw` is False,
all interpolations are expanded in the return values.
Arguments `raw', `vars', and `fallback' are keyword only.
Arguments `raw`, `vars`, and `fallback` are keyword only.
The section DEFAULT is special.
"""
@@ -830,8 +811,8 @@ class RawConfigParser(MutableMapping):
All % interpolations are expanded in the return values, based on the
defaults passed into the constructor, unless the optional argument
`raw' is true. Additional substitutions may be provided using the
`vars' argument, which must be a dictionary whose contents overrides
`raw` is true. Additional substitutions may be provided using the
`vars` argument, which must be a dictionary whose contents overrides
any pre-existing defaults.
The section DEFAULT is special.
@@ -844,6 +825,7 @@ class RawConfigParser(MutableMapping):
except KeyError:
if section != self.default_section:
raise NoSectionError(section)
orig_keys = list(d.keys())
# Update with the entry specific variables
if vars:
for key, value in vars.items():
@@ -852,7 +834,7 @@ class RawConfigParser(MutableMapping):
section, option, d[option], d)
if raw:
value_getter = lambda option: d[option]
return [(option, value_getter(option)) for option in d.keys()]
return [(option, value_getter(option)) for option in orig_keys]
def popitem(self):
"""Remove a section from the parser and return it as
@@ -872,8 +854,8 @@ class RawConfigParser(MutableMapping):
def has_option(self, section, option):
"""Check for the existence of a given option in a given section.
If the specified `section' is None or an empty string, DEFAULT is
assumed. If the specified `section' does not exist, returns False."""
If the specified `section` is None or an empty string, DEFAULT is
assumed. If the specified `section` does not exist, returns False."""
if not section or section == self.default_section:
option = self.optionxform(option)
return option in self._defaults
@@ -901,8 +883,11 @@ class RawConfigParser(MutableMapping):
def write(self, fp, space_around_delimiters=True):
"""Write an .ini-format representation of the configuration state.
If `space_around_delimiters' is True (the default), delimiters
If `space_around_delimiters` is True (the default), delimiters
between keys and values are surrounded by spaces.
Please note that comments in the original configuration file are not
preserved when writing the configuration back.
"""
if space_around_delimiters:
d = " {} ".format(self._delimiters[0])
@@ -916,7 +901,7 @@ class RawConfigParser(MutableMapping):
self._sections[section].items(), d)
def _write_section(self, fp, section_name, section_items, delimiter):
"""Write a single section to the specified `fp'."""
"""Write a single section to the specified `fp`."""
fp.write("[{}]\n".format(section_name))
for key, value in section_items:
value = self._interpolation.before_write(self, section_name, key,
@@ -959,7 +944,8 @@ class RawConfigParser(MutableMapping):
def __setitem__(self, key, value):
# To conform with the mapping protocol, overwrites existing values in
# the section.
if key in self and self[key] is value:
return
# XXX this is not atomic if read_dict fails at any point. Then again,
# no update method in configparser is atomic in this implementation.
if key == self.default_section:
@@ -989,8 +975,8 @@ class RawConfigParser(MutableMapping):
"""Parse a sectioned configuration file.
Each section in a configuration file contains a header, indicated by
a name in square brackets (`[]'), plus key/value options, indicated by
`name' and `value' delimited with a specific substring (`=' or `:' by
a name in square brackets (`[]`), plus key/value options, indicated by
`name` and `value` delimited with a specific substring (`=` or `:` by
default).
Values can span multiple lines, as long as they are indented deeper
@@ -998,9 +984,9 @@ class RawConfigParser(MutableMapping):
lines may be treated as parts of multiline values or ignored.
Configuration files may include comments, prefixed by specific
characters (`#' and `;' by default). Comments may appear on their own
characters (`#` and `;` by default). Comments may appear on their own
in an otherwise empty line or may be entered in lines holding values or
section names.
section names. Please note that comments get stripped off when reading configuration files.
"""
elements_added = set()
cursect = None # None, or a dictionary
@@ -1119,6 +1105,12 @@ class RawConfigParser(MutableMapping):
section,
name, val)
def _read_defaults(self, defaults):
"""Read the defaults passed in the initializer.
Note: values can be non-string."""
for key, value in defaults.items():
self._defaults[self.optionxform(key)] = value
def _handle_error(self, exc, fpname, lineno, line):
if not exc:
exc = ParsingError(fpname)
@@ -1135,7 +1127,7 @@ class RawConfigParser(MutableMapping):
sectiondict = self._sections[section]
except KeyError:
if section != self.default_section:
raise NoSectionError(section)
raise NoSectionError(section) from None
# Update with the entry specific variables
vardict = {}
if vars:
@@ -1196,18 +1188,18 @@ class ConfigParser(RawConfigParser):
self._validate_value_types(section=section)
super().add_section(section)
def _read_defaults(self, defaults):
"""Reads the defaults passed in the initializer, implicitly converting
values to strings like the rest of the API.
class SafeConfigParser(ConfigParser):
"""ConfigParser alias for backwards compatibility purposes."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
warnings.warn(
"The SafeConfigParser class has been renamed to ConfigParser "
"in Python 3.2. This alias will be removed in future versions."
" Use ConfigParser directly instead.",
DeprecationWarning, stacklevel=2
)
Does not perform interpolation for backwards compatibility.
"""
try:
hold_interpolation = self._interpolation
self._interpolation = Interpolation()
self.read_dict({self.default_section: defaults})
finally:
self._interpolation = hold_interpolation
class SectionProxy(MutableMapping):

259
Lib/contextlib.py vendored
View File

@@ -1,20 +1,25 @@
"""Utilities for with-statement contexts. See PEP 343."""
import abc
import os
import sys
import _collections_abc
from collections import deque
from functools import wraps
from types import MethodType, GenericAlias
__all__ = ["asynccontextmanager", "contextmanager", "closing", "nullcontext",
"AbstractContextManager", "AbstractAsyncContextManager",
"AsyncExitStack", "ContextDecorator", "ExitStack",
"redirect_stdout", "redirect_stderr", "suppress"]
"redirect_stdout", "redirect_stderr", "suppress", "aclosing",
"chdir"]
class AbstractContextManager(abc.ABC):
"""An abstract base class for context managers."""
__class_getitem__ = classmethod(GenericAlias)
def __enter__(self):
"""Return `self` upon entering the runtime context."""
return self
@@ -35,6 +40,8 @@ class AbstractAsyncContextManager(abc.ABC):
"""An abstract base class for asynchronous context managers."""
__class_getitem__ = classmethod(GenericAlias)
async def __aenter__(self):
"""Return `self` upon entering the runtime context."""
return self
@@ -75,6 +82,22 @@ class ContextDecorator(object):
return inner
class AsyncContextDecorator(object):
"A base class or mixin that enables async context managers to work as decorators."
def _recreate_cm(self):
"""Return a recreated instance of self.
"""
return self
def __call__(self, func):
@wraps(func)
async def inner(*args, **kwds):
async with self._recreate_cm():
return await func(*args, **kwds)
return inner
class _GeneratorContextManagerBase:
"""Shared functionality for @contextmanager and @asynccontextmanager."""
@@ -92,18 +115,20 @@ class _GeneratorContextManagerBase:
# for the class instead.
# See http://bugs.python.org/issue19404 for more details.
class _GeneratorContextManager(_GeneratorContextManagerBase,
AbstractContextManager,
ContextDecorator):
"""Helper for @contextmanager decorator."""
def _recreate_cm(self):
# _GCM instances are one-shot context managers, so the
# _GCMB instances are one-shot context managers, so the
# CM must be recreated each time a decorated function is
# called
return self.__class__(self.func, self.args, self.kwds)
class _GeneratorContextManager(
_GeneratorContextManagerBase,
AbstractContextManager,
ContextDecorator,
):
"""Helper for @contextmanager decorator."""
def __enter__(self):
# do not keep args and kwds alive unnecessarily
# they are only needed for recreation, which is not possible anymore
@@ -113,21 +138,24 @@ class _GeneratorContextManager(_GeneratorContextManagerBase,
except StopIteration:
raise RuntimeError("generator didn't yield") from None
def __exit__(self, type, value, traceback):
if type is None:
def __exit__(self, typ, value, traceback):
if typ is None:
try:
next(self.gen)
except StopIteration:
return False
else:
raise RuntimeError("generator didn't stop")
try:
raise RuntimeError("generator didn't stop")
finally:
self.gen.close()
else:
if value is None:
# Need to force instantiation so we can reliably
# tell if we get the same exception back
value = type()
value = typ()
try:
self.gen.throw(type, value, traceback)
self.gen.throw(value)
except StopIteration as exc:
# Suppress StopIteration *unless* it's the same exception that
# was passed to throw(). This prevents a StopIteration
@@ -136,75 +164,109 @@ class _GeneratorContextManager(_GeneratorContextManagerBase,
except RuntimeError as exc:
# Don't re-raise the passed in exception. (issue27122)
if exc is value:
exc.__traceback__ = traceback
return False
# Likewise, avoid suppressing if a StopIteration exception
# Avoid suppressing if a StopIteration exception
# was passed to throw() and later wrapped into a RuntimeError
# (see PEP 479).
if type is StopIteration and exc.__cause__ is value:
# (see PEP 479 for sync generators; async generators also
# have this behavior). But do this only if the exception wrapped
# by the RuntimeError is actually Stop(Async)Iteration (see
# issue29692).
if (
isinstance(value, StopIteration)
and exc.__cause__ is value
):
value.__traceback__ = traceback
return False
raise
except:
except BaseException as exc:
# only re-raise if it's *not* the exception that was
# passed to throw(), because __exit__() must not raise
# an exception unless __exit__() itself failed. But throw()
# has to raise the exception to signal propagation, so this
# fixes the impedance mismatch between the throw() protocol
# and the __exit__() protocol.
#
# This cannot use 'except BaseException as exc' (as in the
# async implementation) to maintain compatibility with
# Python 2, where old-style class exceptions are not caught
# by 'except BaseException'.
if sys.exc_info()[1] is value:
return False
raise
raise RuntimeError("generator didn't stop after throw()")
if exc is not value:
raise
exc.__traceback__ = traceback
return False
try:
raise RuntimeError("generator didn't stop after throw()")
finally:
self.gen.close()
class _AsyncGeneratorContextManager(_GeneratorContextManagerBase,
AbstractAsyncContextManager):
"""Helper for @asynccontextmanager."""
class _AsyncGeneratorContextManager(
_GeneratorContextManagerBase,
AbstractAsyncContextManager,
AsyncContextDecorator,
):
"""Helper for @asynccontextmanager decorator."""
async def __aenter__(self):
# do not keep args and kwds alive unnecessarily
# they are only needed for recreation, which is not possible anymore
del self.args, self.kwds, self.func
try:
return await self.gen.__anext__()
return await anext(self.gen)
except StopAsyncIteration:
raise RuntimeError("generator didn't yield") from None
async def __aexit__(self, typ, value, traceback):
if typ is None:
try:
await self.gen.__anext__()
await anext(self.gen)
except StopAsyncIteration:
return
return False
else:
raise RuntimeError("generator didn't stop")
try:
raise RuntimeError("generator didn't stop")
finally:
await self.gen.aclose()
else:
if value is None:
# Need to force instantiation so we can reliably
# tell if we get the same exception back
value = typ()
# See _GeneratorContextManager.__exit__ for comments on subtleties
# in this implementation
try:
await self.gen.athrow(typ, value, traceback)
raise RuntimeError("generator didn't stop after throw()")
await self.gen.athrow(value)
except StopAsyncIteration as exc:
# Suppress StopIteration *unless* it's the same exception that
# was passed to throw(). This prevents a StopIteration
# raised inside the "with" statement from being suppressed.
return exc is not value
except RuntimeError as exc:
# Don't re-raise the passed in exception. (issue27122)
if exc is value:
exc.__traceback__ = traceback
return False
# Avoid suppressing if a StopIteration exception
# was passed to throw() and later wrapped into a RuntimeError
# Avoid suppressing if a Stop(Async)Iteration exception
# was passed to athrow() and later wrapped into a RuntimeError
# (see PEP 479 for sync generators; async generators also
# have this behavior). But do this only if the exception wrapped
# by the RuntimeError is actully Stop(Async)Iteration (see
# by the RuntimeError is actually Stop(Async)Iteration (see
# issue29692).
if isinstance(value, (StopIteration, StopAsyncIteration)):
if exc.__cause__ is value:
return False
if (
isinstance(value, (StopIteration, StopAsyncIteration))
and exc.__cause__ is value
):
value.__traceback__ = traceback
return False
raise
except BaseException as exc:
# only re-raise if it's *not* the exception that was
# passed to throw(), because __exit__() must not raise
# an exception unless __exit__() itself failed. But throw()
# has to raise the exception to signal propagation, so this
# fixes the impedance mismatch between the throw() protocol
# and the __exit__() protocol.
if exc is not value:
raise
exc.__traceback__ = traceback
return False
try:
raise RuntimeError("generator didn't stop after athrow()")
finally:
await self.gen.aclose()
def contextmanager(func):
@@ -298,6 +360,32 @@ class closing(AbstractContextManager):
self.thing.close()
class aclosing(AbstractAsyncContextManager):
"""Async context manager for safely finalizing an asynchronously cleaned-up
resource such as an async generator, calling its ``aclose()`` method.
Code like this:
async with aclosing(<module>.fetch(<arguments>)) as agen:
<block>
is equivalent to this:
agen = <module>.fetch(<arguments>)
try:
<block>
finally:
await agen.aclose()
"""
def __init__(self, thing):
self.thing = thing
async def __aenter__(self):
return self.thing
async def __aexit__(self, *exc_info):
await self.thing.aclose()
class _RedirectStream(AbstractContextManager):
_stream = None
@@ -365,7 +453,16 @@ class suppress(AbstractContextManager):
# exactly reproduce the limitations of the CPython interpreter.
#
# See http://bugs.python.org/issue12029 for more details
return exctype is not None and issubclass(exctype, self._exceptions)
if exctype is None:
return
if issubclass(exctype, self._exceptions):
return True
if issubclass(exctype, BaseExceptionGroup):
match, rest = excinst.split(self._exceptions)
if rest is None:
return True
raise rest
return False
class _BaseExitStack:
@@ -373,12 +470,10 @@ class _BaseExitStack:
@staticmethod
def _create_exit_wrapper(cm, cm_exit):
def _exit_wrapper(exc_type, exc, tb):
return cm_exit(cm, exc_type, exc, tb)
return _exit_wrapper
return MethodType(cm_exit, cm)
@staticmethod
def _create_cb_wrapper(callback, *args, **kwds):
def _create_cb_wrapper(callback, /, *args, **kwds):
def _exit_wrapper(exc_type, exc, tb):
callback(*args, **kwds)
return _exit_wrapper
@@ -421,13 +516,18 @@ class _BaseExitStack:
"""
# We look up the special methods on the type to match the with
# statement.
_cm_type = type(cm)
_exit = _cm_type.__exit__
result = _cm_type.__enter__(cm)
cls = type(cm)
try:
_enter = cls.__enter__
_exit = cls.__exit__
except AttributeError:
raise TypeError(f"'{cls.__module__}.{cls.__qualname__}' object does "
f"not support the context manager protocol") from None
result = _enter(cm)
self._push_cm_exit(cm, _exit)
return result
def callback(self, callback, *args, **kwds):
def callback(self, callback, /, *args, **kwds):
"""Registers an arbitrary callback and arguments.
Cannot suppress exceptions.
@@ -443,7 +543,6 @@ class _BaseExitStack:
def _push_cm_exit(self, cm, cm_exit):
"""Helper to correctly register callbacks to __exit__ methods."""
_exit_wrapper = self._create_exit_wrapper(cm, cm_exit)
_exit_wrapper.__self__ = cm
self._push_exit_callback(_exit_wrapper, True)
def _push_exit_callback(self, callback, is_sync=True):
@@ -475,10 +574,10 @@ class ExitStack(_BaseExitStack, AbstractContextManager):
# Context may not be correct, so find the end of the chain
while 1:
exc_context = new_exc.__context__
if exc_context is old_exc:
if exc_context is None or exc_context is old_exc:
# Context is already set correctly (see issue 20317)
return
if exc_context is None or exc_context is frame_exc:
if exc_context is frame_exc:
break
new_exc = exc_context
# Change the end of the chain to point to the exception
@@ -535,12 +634,10 @@ class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager):
@staticmethod
def _create_async_exit_wrapper(cm, cm_exit):
async def _exit_wrapper(exc_type, exc, tb):
return await cm_exit(cm, exc_type, exc, tb)
return _exit_wrapper
return MethodType(cm_exit, cm)
@staticmethod
def _create_async_cb_wrapper(callback, *args, **kwds):
def _create_async_cb_wrapper(callback, /, *args, **kwds):
async def _exit_wrapper(exc_type, exc, tb):
await callback(*args, **kwds)
return _exit_wrapper
@@ -551,9 +648,15 @@ class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager):
If successful, also pushes its __aexit__ method as a callback and
returns the result of the __aenter__ method.
"""
_cm_type = type(cm)
_exit = _cm_type.__aexit__
result = await _cm_type.__aenter__(cm)
cls = type(cm)
try:
_enter = cls.__aenter__
_exit = cls.__aexit__
except AttributeError:
raise TypeError(f"'{cls.__module__}.{cls.__qualname__}' object does "
f"not support the asynchronous context manager protocol"
) from None
result = await _enter(cm)
self._push_async_cm_exit(cm, _exit)
return result
@@ -575,7 +678,7 @@ class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager):
self._push_async_cm_exit(exit, exit_method)
return exit # Allow use as a decorator
def push_async_callback(self, callback, *args, **kwds):
def push_async_callback(self, callback, /, *args, **kwds):
"""Registers an arbitrary coroutine function and arguments.
Cannot suppress exceptions.
@@ -596,7 +699,6 @@ class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager):
"""Helper to correctly register coroutine function to __aexit__
method."""
_exit_wrapper = self._create_async_exit_wrapper(cm, cm_exit)
_exit_wrapper.__self__ = cm
self._push_exit_callback(_exit_wrapper, False)
async def __aenter__(self):
@@ -612,10 +714,10 @@ class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager):
# Context may not be correct, so find the end of the chain
while 1:
exc_context = new_exc.__context__
if exc_context is old_exc:
if exc_context is None or exc_context is old_exc:
# Context is already set correctly (see issue 20317)
return
if exc_context is None or exc_context is frame_exc:
if exc_context is frame_exc:
break
new_exc = exc_context
# Change the end of the chain to point to the exception
@@ -656,7 +758,7 @@ class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager):
return received_exc and suppressed_exc
class nullcontext(AbstractContextManager):
class nullcontext(AbstractContextManager, AbstractAsyncContextManager):
"""Context manager that does no additional processing.
Used as a stand-in for a normal context manager, when a particular
@@ -675,3 +777,24 @@ class nullcontext(AbstractContextManager):
def __exit__(self, *excinfo):
pass
async def __aenter__(self):
return self.enter_result
async def __aexit__(self, *excinfo):
pass
class chdir(AbstractContextManager):
"""Non thread-safe context manager to change the current working directory."""
def __init__(self, path):
self.path = path
self._old_cwd = []
def __enter__(self):
self._old_cwd.append(os.getcwd())
os.chdir(self.path)
def __exit__(self, *excinfo):
os.chdir(self._old_cwd.pop())

35
Lib/copy.py vendored
View File

@@ -39,8 +39,8 @@ Python's deep copy operation avoids these problems by:
set of components copied
This version does not copy types like module, class, function, method,
nor stack trace, stack frame, nor file, socket, window, nor array, nor
any similar types.
nor stack trace, stack frame, nor file, socket, window, nor any
similar types.
Classes can use the same interfaces to control copying that they use
to control pickling: they can define methods called __getinitargs__(),
@@ -56,11 +56,6 @@ class Error(Exception):
pass
error = Error # backward compatibility
try:
from org.python.core import PyStringMap
except ImportError:
PyStringMap = None
__all__ = ["Error", "copy", "deepcopy"]
def copy(x):
@@ -106,13 +101,11 @@ _copy_dispatch = d = {}
def _copy_immutable(x):
return x
for t in (type(None), int, float, bool, complex, str, tuple,
for t in (types.NoneType, int, float, bool, complex, str, tuple,
bytes, frozenset, type, range, slice, property,
types.BuiltinFunctionType, type(Ellipsis), type(NotImplemented),
types.FunctionType, weakref.ref):
d[t] = _copy_immutable
t = getattr(types, "CodeType", None)
if t is not None:
types.BuiltinFunctionType, types.EllipsisType,
types.NotImplementedType, types.FunctionType, types.CodeType,
weakref.ref):
d[t] = _copy_immutable
d[list] = list.copy
@@ -120,9 +113,6 @@ d[dict] = dict.copy
d[set] = set.copy
d[bytearray] = bytearray.copy
if PyStringMap is not None:
d[PyStringMap] = PyStringMap.copy
del d, t
def deepcopy(x, memo=None, _nil=[]):
@@ -181,9 +171,9 @@ _deepcopy_dispatch = d = {}
def _deepcopy_atomic(x, memo):
return x
d[type(None)] = _deepcopy_atomic
d[type(Ellipsis)] = _deepcopy_atomic
d[type(NotImplemented)] = _deepcopy_atomic
d[types.NoneType] = _deepcopy_atomic
d[types.EllipsisType] = _deepcopy_atomic
d[types.NotImplementedType] = _deepcopy_atomic
d[int] = _deepcopy_atomic
d[float] = _deepcopy_atomic
d[bool] = _deepcopy_atomic
@@ -192,6 +182,7 @@ d[bytes] = _deepcopy_atomic
d[str] = _deepcopy_atomic
d[types.CodeType] = _deepcopy_atomic
d[type] = _deepcopy_atomic
d[range] = _deepcopy_atomic
d[types.BuiltinFunctionType] = _deepcopy_atomic
d[types.FunctionType] = _deepcopy_atomic
d[weakref.ref] = _deepcopy_atomic
@@ -230,8 +221,6 @@ def _deepcopy_dict(x, memo, deepcopy=deepcopy):
y[deepcopy(key, memo)] = deepcopy(value, memo)
return y
d[dict] = _deepcopy_dict
if PyStringMap is not None:
d[PyStringMap] = _deepcopy_dict
def _deepcopy_method(x, memo): # Copy instance methods
return type(x)(x.__func__, deepcopy(x.__self__, memo))
@@ -257,7 +246,7 @@ def _keep_alive(x, memo):
def _reconstruct(x, memo, func, args,
state=None, listiter=None, dictiter=None,
deepcopy=deepcopy):
*, deepcopy=deepcopy):
deep = memo is not None
if deep and args:
args = (deepcopy(arg, memo) for arg in args)
@@ -300,4 +289,4 @@ def _reconstruct(x, memo, func, args,
y[key] = value
return y
del types, weakref, PyStringMap
del types, weakref

24
Lib/copyreg.py vendored
View File

@@ -25,16 +25,16 @@ def constructor(object):
# Example: provide pickling support for complex numbers.
try:
complex
except NameError:
pass
else:
def pickle_complex(c):
return complex, (c.real, c.imag)
def pickle_complex(c):
return complex, (c.real, c.imag)
pickle(complex, pickle_complex, complex)
pickle(complex, pickle_complex, complex)
def pickle_union(obj):
import functools, operator
return functools.reduce, (operator.or_, obj.__args__)
pickle(type(int | str), pickle_union)
# Support for pickling new-style objects
@@ -48,6 +48,7 @@ def _reconstructor(cls, base, state):
return obj
_HEAPTYPE = 1<<9
_new_type = type(int.__new__)
# Python code for object.__reduce_ex__ for protocols 0 and 1
@@ -57,6 +58,9 @@ def _reduce_ex(self, proto):
for base in cls.__mro__:
if hasattr(base, '__flags__') and not base.__flags__ & _HEAPTYPE:
break
new = base.__new__
if isinstance(new, _new_type) and new.__self__ is base:
break
else:
base = object # not really reachable
if base is object:
@@ -79,6 +83,10 @@ def _reduce_ex(self, proto):
except AttributeError:
dict = None
else:
if (type(self).__getstate__ is object.__getstate__ and
getattr(self, "__slots__", None)):
raise TypeError("a class that defines __slots__ without "
"defining __getstate__ cannot be pickled")
dict = getstate()
if dict:
return _reconstructor, args, dict

48
Lib/csv.py vendored
View File

@@ -4,17 +4,22 @@ csv.py - read/write/investigate CSV files
"""
import re
from _csv import Error, writer, reader, \
import types
from _csv import Error, __version__, writer, reader, register_dialect, \
unregister_dialect, get_dialect, list_dialects, \
field_size_limit, \
QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC, QUOTE_NONE, \
QUOTE_STRINGS, QUOTE_NOTNULL, \
__doc__
from _csv import Dialect as _Dialect
from collections import OrderedDict
from io import StringIO
__all__ = ["QUOTE_MINIMAL", "QUOTE_ALL", "QUOTE_NONNUMERIC", "QUOTE_NONE",
"QUOTE_STRINGS", "QUOTE_NOTNULL",
"Error", "Dialect", "__doc__", "excel", "excel_tab",
"field_size_limit", "reader", "writer",
"Sniffer",
"register_dialect", "get_dialect", "list_dialects", "Sniffer",
"unregister_dialect", "__version__", "DictReader", "DictWriter",
"unix_dialect"]
@@ -57,10 +62,12 @@ class excel(Dialect):
skipinitialspace = False
lineterminator = '\r\n'
quoting = QUOTE_MINIMAL
register_dialect("excel", excel)
class excel_tab(excel):
"""Describe the usual properties of Excel-generated TAB-delimited files."""
delimiter = '\t'
register_dialect("excel-tab", excel_tab)
class unix_dialect(Dialect):
"""Describe the usual properties of Unix-generated CSV files."""
@@ -70,11 +77,14 @@ class unix_dialect(Dialect):
skipinitialspace = False
lineterminator = '\n'
quoting = QUOTE_ALL
register_dialect("unix", unix_dialect)
class DictReader:
def __init__(self, f, fieldnames=None, restkey=None, restval=None,
dialect="excel", *args, **kwds):
if fieldnames is not None and iter(fieldnames) is fieldnames:
fieldnames = list(fieldnames)
self._fieldnames = fieldnames # list of keys for the dict
self.restkey = restkey # key to catch long rows
self.restval = restval # default value for short rows
@@ -111,7 +121,7 @@ class DictReader:
# values
while row == []:
row = next(self.reader)
d = OrderedDict(zip(self.fieldnames, row))
d = dict(zip(self.fieldnames, row))
lf = len(self.fieldnames)
lr = len(row)
if lf < lr:
@@ -121,13 +131,18 @@ class DictReader:
d[key] = self.restval
return d
__class_getitem__ = classmethod(types.GenericAlias)
class DictWriter:
def __init__(self, f, fieldnames, restval="", extrasaction="raise",
dialect="excel", *args, **kwds):
if fieldnames is not None and iter(fieldnames) is fieldnames:
fieldnames = list(fieldnames)
self.fieldnames = fieldnames # list of keys for the dict
self.restval = restval # for writing short dicts
if extrasaction.lower() not in ("raise", "ignore"):
extrasaction = extrasaction.lower()
if extrasaction not in ("raise", "ignore"):
raise ValueError("extrasaction (%s) must be 'raise' or 'ignore'"
% extrasaction)
self.extrasaction = extrasaction
@@ -135,7 +150,7 @@ class DictWriter:
def writeheader(self):
header = dict(zip(self.fieldnames, self.fieldnames))
self.writerow(header)
return self.writerow(header)
def _dict_to_list(self, rowdict):
if self.extrasaction == "raise":
@@ -151,11 +166,8 @@ class DictWriter:
def writerows(self, rowdicts):
return self.writer.writerows(map(self._dict_to_list, rowdicts))
# Guard Sniffer's type checking against builds that exclude complex()
try:
complex
except NameError:
complex = float
__class_getitem__ = classmethod(types.GenericAlias)
class Sniffer:
'''
@@ -404,14 +416,10 @@ class Sniffer:
continue # skip rows that have irregular number of columns
for col in list(columnTypes.keys()):
for thisType in [int, float, complex]:
try:
thisType(row[col])
break
except (ValueError, OverflowError):
pass
else:
thisType = complex
try:
thisType(row[col])
except (ValueError, OverflowError):
# fallback to length of string
thisType = len(row[col])
@@ -427,7 +435,7 @@ class Sniffer:
# on whether it's a header
hasHeader = 0
for col, colType in columnTypes.items():
if type(colType) == type(0): # it's a length
if isinstance(colType, int): # it's a length
if len(header[col]) != colType:
hasHeader += 1
else:

577
Lib/ctypes/__init__.py vendored Normal file
View File

@@ -0,0 +1,577 @@
"""create and manipulate C data types in Python"""
import os as _os, sys as _sys
import types as _types
__version__ = "1.1.0"
from _ctypes import Union, Structure, Array
from _ctypes import _Pointer
from _ctypes import CFuncPtr as _CFuncPtr
from _ctypes import __version__ as _ctypes_version
from _ctypes import RTLD_LOCAL, RTLD_GLOBAL
from _ctypes import ArgumentError
from _ctypes import SIZEOF_TIME_T
from struct import calcsize as _calcsize
if __version__ != _ctypes_version:
raise Exception("Version number mismatch", __version__, _ctypes_version)
if _os.name == "nt":
from _ctypes import FormatError
DEFAULT_MODE = RTLD_LOCAL
if _os.name == "posix" and _sys.platform == "darwin":
# On OS X 10.3, we use RTLD_GLOBAL as default mode
# because RTLD_LOCAL does not work at least on some
# libraries. OS X 10.3 is Darwin 7, so we check for
# that.
if int(_os.uname().release.split('.')[0]) < 8:
DEFAULT_MODE = RTLD_GLOBAL
from _ctypes import FUNCFLAG_CDECL as _FUNCFLAG_CDECL, \
FUNCFLAG_PYTHONAPI as _FUNCFLAG_PYTHONAPI, \
FUNCFLAG_USE_ERRNO as _FUNCFLAG_USE_ERRNO, \
FUNCFLAG_USE_LASTERROR as _FUNCFLAG_USE_LASTERROR
# WINOLEAPI -> HRESULT
# WINOLEAPI_(type)
#
# STDMETHODCALLTYPE
#
# STDMETHOD(name)
# STDMETHOD_(type, name)
#
# STDAPICALLTYPE
def create_string_buffer(init, size=None):
"""create_string_buffer(aBytes) -> character array
create_string_buffer(anInteger) -> character array
create_string_buffer(aBytes, anInteger) -> character array
"""
if isinstance(init, bytes):
if size is None:
size = len(init)+1
_sys.audit("ctypes.create_string_buffer", init, size)
buftype = c_char * size
buf = buftype()
buf.value = init
return buf
elif isinstance(init, int):
_sys.audit("ctypes.create_string_buffer", None, init)
buftype = c_char * init
buf = buftype()
return buf
raise TypeError(init)
# Alias to create_string_buffer() for backward compatibility
c_buffer = create_string_buffer
_c_functype_cache = {}
def CFUNCTYPE(restype, *argtypes, **kw):
"""CFUNCTYPE(restype, *argtypes,
use_errno=False, use_last_error=False) -> function prototype.
restype: the result type
argtypes: a sequence specifying the argument types
The function prototype can be called in different ways to create a
callable object:
prototype(integer address) -> foreign function
prototype(callable) -> create and return a C callable function from callable
prototype(integer index, method name[, paramflags]) -> foreign function calling a COM method
prototype((ordinal number, dll object)[, paramflags]) -> foreign function exported by ordinal
prototype((function name, dll object)[, paramflags]) -> foreign function exported by name
"""
flags = _FUNCFLAG_CDECL
if kw.pop("use_errno", False):
flags |= _FUNCFLAG_USE_ERRNO
if kw.pop("use_last_error", False):
flags |= _FUNCFLAG_USE_LASTERROR
if kw:
raise ValueError("unexpected keyword argument(s) %s" % kw.keys())
try:
return _c_functype_cache[(restype, argtypes, flags)]
except KeyError:
pass
class CFunctionType(_CFuncPtr):
_argtypes_ = argtypes
_restype_ = restype
_flags_ = flags
_c_functype_cache[(restype, argtypes, flags)] = CFunctionType
return CFunctionType
if _os.name == "nt":
from _ctypes import LoadLibrary as _dlopen
from _ctypes import FUNCFLAG_STDCALL as _FUNCFLAG_STDCALL
_win_functype_cache = {}
def WINFUNCTYPE(restype, *argtypes, **kw):
# docstring set later (very similar to CFUNCTYPE.__doc__)
flags = _FUNCFLAG_STDCALL
if kw.pop("use_errno", False):
flags |= _FUNCFLAG_USE_ERRNO
if kw.pop("use_last_error", False):
flags |= _FUNCFLAG_USE_LASTERROR
if kw:
raise ValueError("unexpected keyword argument(s) %s" % kw.keys())
try:
return _win_functype_cache[(restype, argtypes, flags)]
except KeyError:
pass
class WinFunctionType(_CFuncPtr):
_argtypes_ = argtypes
_restype_ = restype
_flags_ = flags
_win_functype_cache[(restype, argtypes, flags)] = WinFunctionType
return WinFunctionType
if WINFUNCTYPE.__doc__:
WINFUNCTYPE.__doc__ = CFUNCTYPE.__doc__.replace("CFUNCTYPE", "WINFUNCTYPE")
elif _os.name == "posix":
from _ctypes import dlopen as _dlopen
from _ctypes import sizeof, byref, addressof, alignment, resize
from _ctypes import get_errno, set_errno
from _ctypes import _SimpleCData
def _check_size(typ, typecode=None):
# Check if sizeof(ctypes_type) against struct.calcsize. This
# should protect somewhat against a misconfigured libffi.
from struct import calcsize
if typecode is None:
# Most _type_ codes are the same as used in struct
typecode = typ._type_
actual, required = sizeof(typ), calcsize(typecode)
if actual != required:
raise SystemError("sizeof(%s) wrong: %d instead of %d" % \
(typ, actual, required))
class py_object(_SimpleCData):
_type_ = "O"
def __repr__(self):
try:
return super().__repr__()
except ValueError:
return "%s(<NULL>)" % type(self).__name__
_check_size(py_object, "P")
class c_short(_SimpleCData):
_type_ = "h"
_check_size(c_short)
class c_ushort(_SimpleCData):
_type_ = "H"
_check_size(c_ushort)
class c_long(_SimpleCData):
_type_ = "l"
_check_size(c_long)
class c_ulong(_SimpleCData):
_type_ = "L"
_check_size(c_ulong)
if _calcsize("i") == _calcsize("l"):
# if int and long have the same size, make c_int an alias for c_long
c_int = c_long
c_uint = c_ulong
else:
class c_int(_SimpleCData):
_type_ = "i"
_check_size(c_int)
class c_uint(_SimpleCData):
_type_ = "I"
_check_size(c_uint)
class c_float(_SimpleCData):
_type_ = "f"
_check_size(c_float)
class c_double(_SimpleCData):
_type_ = "d"
_check_size(c_double)
class c_longdouble(_SimpleCData):
_type_ = "g"
if sizeof(c_longdouble) == sizeof(c_double):
c_longdouble = c_double
if _calcsize("l") == _calcsize("q"):
# if long and long long have the same size, make c_longlong an alias for c_long
c_longlong = c_long
c_ulonglong = c_ulong
else:
class c_longlong(_SimpleCData):
_type_ = "q"
_check_size(c_longlong)
class c_ulonglong(_SimpleCData):
_type_ = "Q"
## def from_param(cls, val):
## return ('d', float(val), val)
## from_param = classmethod(from_param)
_check_size(c_ulonglong)
class c_ubyte(_SimpleCData):
_type_ = "B"
c_ubyte.__ctype_le__ = c_ubyte.__ctype_be__ = c_ubyte
# backward compatibility:
##c_uchar = c_ubyte
_check_size(c_ubyte)
class c_byte(_SimpleCData):
_type_ = "b"
c_byte.__ctype_le__ = c_byte.__ctype_be__ = c_byte
_check_size(c_byte)
class c_char(_SimpleCData):
_type_ = "c"
c_char.__ctype_le__ = c_char.__ctype_be__ = c_char
_check_size(c_char)
class c_char_p(_SimpleCData):
_type_ = "z"
def __repr__(self):
return "%s(%s)" % (self.__class__.__name__, c_void_p.from_buffer(self).value)
_check_size(c_char_p, "P")
class c_void_p(_SimpleCData):
_type_ = "P"
c_voidp = c_void_p # backwards compatibility (to a bug)
_check_size(c_void_p)
class c_bool(_SimpleCData):
_type_ = "?"
from _ctypes import POINTER, pointer, _pointer_type_cache
class c_wchar_p(_SimpleCData):
_type_ = "Z"
def __repr__(self):
return "%s(%s)" % (self.__class__.__name__, c_void_p.from_buffer(self).value)
class c_wchar(_SimpleCData):
_type_ = "u"
def _reset_cache():
_pointer_type_cache.clear()
_c_functype_cache.clear()
if _os.name == "nt":
_win_functype_cache.clear()
# _SimpleCData.c_wchar_p_from_param
POINTER(c_wchar).from_param = c_wchar_p.from_param
# _SimpleCData.c_char_p_from_param
POINTER(c_char).from_param = c_char_p.from_param
_pointer_type_cache[None] = c_void_p
def create_unicode_buffer(init, size=None):
"""create_unicode_buffer(aString) -> character array
create_unicode_buffer(anInteger) -> character array
create_unicode_buffer(aString, anInteger) -> character array
"""
if isinstance(init, str):
if size is None:
if sizeof(c_wchar) == 2:
# UTF-16 requires a surrogate pair (2 wchar_t) for non-BMP
# characters (outside [U+0000; U+FFFF] range). +1 for trailing
# NUL character.
size = sum(2 if ord(c) > 0xFFFF else 1 for c in init) + 1
else:
# 32-bit wchar_t (1 wchar_t per Unicode character). +1 for
# trailing NUL character.
size = len(init) + 1
_sys.audit("ctypes.create_unicode_buffer", init, size)
buftype = c_wchar * size
buf = buftype()
buf.value = init
return buf
elif isinstance(init, int):
_sys.audit("ctypes.create_unicode_buffer", None, init)
buftype = c_wchar * init
buf = buftype()
return buf
raise TypeError(init)
# XXX Deprecated
def SetPointerType(pointer, cls):
if _pointer_type_cache.get(cls, None) is not None:
raise RuntimeError("This type already exists in the cache")
if id(pointer) not in _pointer_type_cache:
raise RuntimeError("What's this???")
pointer.set_type(cls)
_pointer_type_cache[cls] = pointer
del _pointer_type_cache[id(pointer)]
# XXX Deprecated
def ARRAY(typ, len):
return typ * len
################################################################
class CDLL(object):
"""An instance of this class represents a loaded dll/shared
library, exporting functions using the standard C calling
convention (named 'cdecl' on Windows).
The exported functions can be accessed as attributes, or by
indexing with the function name. Examples:
<obj>.qsort -> callable object
<obj>['qsort'] -> callable object
Calling the functions releases the Python GIL during the call and
reacquires it afterwards.
"""
_func_flags_ = _FUNCFLAG_CDECL
_func_restype_ = c_int
# default values for repr
_name = '<uninitialized>'
_handle = 0
_FuncPtr = None
def __init__(self, name, mode=DEFAULT_MODE, handle=None,
use_errno=False,
use_last_error=False,
winmode=None):
self._name = name
flags = self._func_flags_
if use_errno:
flags |= _FUNCFLAG_USE_ERRNO
if use_last_error:
flags |= _FUNCFLAG_USE_LASTERROR
if _sys.platform.startswith("aix"):
"""When the name contains ".a(" and ends with ")",
e.g., "libFOO.a(libFOO.so)" - this is taken to be an
archive(member) syntax for dlopen(), and the mode is adjusted.
Otherwise, name is presented to dlopen() as a file argument.
"""
if name and name.endswith(")") and ".a(" in name:
mode |= ( _os.RTLD_MEMBER | _os.RTLD_NOW )
if _os.name == "nt":
if winmode is not None:
mode = winmode
else:
import nt
mode = nt._LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
if '/' in name or '\\' in name:
self._name = nt._getfullpathname(self._name)
mode |= nt._LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
class _FuncPtr(_CFuncPtr):
_flags_ = flags
_restype_ = self._func_restype_
self._FuncPtr = _FuncPtr
if handle is None:
self._handle = _dlopen(self._name, mode)
else:
self._handle = handle
def __repr__(self):
return "<%s '%s', handle %x at %#x>" % \
(self.__class__.__name__, self._name,
(self._handle & (_sys.maxsize*2 + 1)),
id(self) & (_sys.maxsize*2 + 1))
def __getattr__(self, name):
if name.startswith('__') and name.endswith('__'):
raise AttributeError(name)
func = self.__getitem__(name)
setattr(self, name, func)
return func
def __getitem__(self, name_or_ordinal):
func = self._FuncPtr((name_or_ordinal, self))
if not isinstance(name_or_ordinal, int):
func.__name__ = name_or_ordinal
return func
class PyDLL(CDLL):
"""This class represents the Python library itself. It allows
accessing Python API functions. The GIL is not released, and
Python exceptions are handled correctly.
"""
_func_flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI
if _os.name == "nt":
class WinDLL(CDLL):
"""This class represents a dll exporting functions using the
Windows stdcall calling convention.
"""
_func_flags_ = _FUNCFLAG_STDCALL
# XXX Hm, what about HRESULT as normal parameter?
# Mustn't it derive from c_long then?
from _ctypes import _check_HRESULT, _SimpleCData
class HRESULT(_SimpleCData):
_type_ = "l"
# _check_retval_ is called with the function's result when it
# is used as restype. It checks for the FAILED bit, and
# raises an OSError if it is set.
#
# The _check_retval_ method is implemented in C, so that the
# method definition itself is not included in the traceback
# when it raises an error - that is what we want (and Python
# doesn't have a way to raise an exception in the caller's
# frame).
_check_retval_ = _check_HRESULT
class OleDLL(CDLL):
"""This class represents a dll exporting functions using the
Windows stdcall calling convention, and returning HRESULT.
HRESULT error values are automatically raised as OSError
exceptions.
"""
_func_flags_ = _FUNCFLAG_STDCALL
_func_restype_ = HRESULT
class LibraryLoader(object):
def __init__(self, dlltype):
self._dlltype = dlltype
def __getattr__(self, name):
if name[0] == '_':
raise AttributeError(name)
try:
dll = self._dlltype(name)
except OSError:
raise AttributeError(name)
setattr(self, name, dll)
return dll
def __getitem__(self, name):
return getattr(self, name)
def LoadLibrary(self, name):
return self._dlltype(name)
__class_getitem__ = classmethod(_types.GenericAlias)
cdll = LibraryLoader(CDLL)
pydll = LibraryLoader(PyDLL)
if _os.name == "nt":
pythonapi = PyDLL("python dll", None, _sys.dllhandle)
elif _sys.platform == "cygwin":
pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
else:
pythonapi = PyDLL(None)
if _os.name == "nt":
windll = LibraryLoader(WinDLL)
oledll = LibraryLoader(OleDLL)
GetLastError = windll.kernel32.GetLastError
from _ctypes import get_last_error, set_last_error
def WinError(code=None, descr=None):
if code is None:
code = GetLastError()
if descr is None:
descr = FormatError(code).strip()
return OSError(None, descr, None, code)
if sizeof(c_uint) == sizeof(c_void_p):
c_size_t = c_uint
c_ssize_t = c_int
elif sizeof(c_ulong) == sizeof(c_void_p):
c_size_t = c_ulong
c_ssize_t = c_long
elif sizeof(c_ulonglong) == sizeof(c_void_p):
c_size_t = c_ulonglong
c_ssize_t = c_longlong
# functions
from _ctypes import _memmove_addr, _memset_addr, _string_at_addr, _cast_addr
## void *memmove(void *, const void *, size_t);
memmove = CFUNCTYPE(c_void_p, c_void_p, c_void_p, c_size_t)(_memmove_addr)
## void *memset(void *, int, size_t)
memset = CFUNCTYPE(c_void_p, c_void_p, c_int, c_size_t)(_memset_addr)
def PYFUNCTYPE(restype, *argtypes):
class CFunctionType(_CFuncPtr):
_argtypes_ = argtypes
_restype_ = restype
_flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI
return CFunctionType
_cast = PYFUNCTYPE(py_object, c_void_p, py_object, py_object)(_cast_addr)
def cast(obj, typ):
return _cast(obj, obj, typ)
_string_at = PYFUNCTYPE(py_object, c_void_p, c_int)(_string_at_addr)
def string_at(ptr, size=-1):
"""string_at(addr[, size]) -> string
Return the string at addr."""
return _string_at(ptr, size)
try:
from _ctypes import _wstring_at_addr
except ImportError:
pass
else:
_wstring_at = PYFUNCTYPE(py_object, c_void_p, c_int)(_wstring_at_addr)
def wstring_at(ptr, size=-1):
"""wstring_at(addr[, size]) -> string
Return the string at addr."""
return _wstring_at(ptr, size)
if _os.name == "nt": # COM stuff
def DllGetClassObject(rclsid, riid, ppv):
try:
ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*'])
except ImportError:
return -2147221231 # CLASS_E_CLASSNOTAVAILABLE
else:
return ccom.DllGetClassObject(rclsid, riid, ppv)
def DllCanUnloadNow():
try:
ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*'])
except ImportError:
return 0 # S_OK
return ccom.DllCanUnloadNow()
from ctypes._endian import BigEndianStructure, LittleEndianStructure
from ctypes._endian import BigEndianUnion, LittleEndianUnion
# Fill in specifically-sized types
c_int8 = c_byte
c_uint8 = c_ubyte
for kind in [c_short, c_int, c_long, c_longlong]:
if sizeof(kind) == 2: c_int16 = kind
elif sizeof(kind) == 4: c_int32 = kind
elif sizeof(kind) == 8: c_int64 = kind
for kind in [c_ushort, c_uint, c_ulong, c_ulonglong]:
if sizeof(kind) == 2: c_uint16 = kind
elif sizeof(kind) == 4: c_uint32 = kind
elif sizeof(kind) == 8: c_uint64 = kind
del(kind)
if SIZEOF_TIME_T == 8:
c_time_t = c_int64
elif SIZEOF_TIME_T == 4:
c_time_t = c_int32
else:
raise SystemError(f"Unexpected sizeof(time_t): {SIZEOF_TIME_T=}")
_reset_cache()

327
Lib/ctypes/_aix.py vendored Normal file
View File

@@ -0,0 +1,327 @@
"""
Lib/ctypes.util.find_library() support for AIX
Similar approach as done for Darwin support by using separate files
but unlike Darwin - no extension such as ctypes.macholib.*
dlopen() is an interface to AIX initAndLoad() - primary documentation at:
https://www.ibm.com/support/knowledgecenter/en/ssw_aix_61/com.ibm.aix.basetrf1/dlopen.htm
https://www.ibm.com/support/knowledgecenter/en/ssw_aix_61/com.ibm.aix.basetrf1/load.htm
AIX supports two styles for dlopen(): svr4 (System V Release 4) which is common on posix
platforms, but also a BSD style - aka SVR3.
From AIX 5.3 Difference Addendum (December 2004)
2.9 SVR4 linking affinity
Nowadays, there are two major object file formats used by the operating systems:
XCOFF: The COFF enhanced by IBM and others. The original COFF (Common
Object File Format) was the base of SVR3 and BSD 4.2 systems.
ELF: Executable and Linking Format that was developed by AT&T and is a
base for SVR4 UNIX.
While the shared library content is identical on AIX - one is located as a filepath name
(svr4 style) and the other is located as a member of an archive (and the archive
is located as a filepath name).
The key difference arises when supporting multiple abi formats (i.e., 32 and 64 bit).
For svr4 either only one ABI is supported, or there are two directories, or there
are different file names. The most common solution for multiple ABI is multiple
directories.
For the XCOFF (aka AIX) style - one directory (one archive file) is sufficient
as multiple shared libraries can be in the archive - even sharing the same name.
In documentation the archive is also referred to as the "base" and the shared
library object is referred to as the "member".
For dlopen() on AIX (read initAndLoad()) the calls are similar.
Default activity occurs when no path information is provided. When path
information is provided dlopen() does not search any other directories.
For SVR4 - the shared library name is the name of the file expected: libFOO.so
For AIX - the shared library is expressed as base(member). The search is for the
base (e.g., libFOO.a) and once the base is found the shared library - identified by
member (e.g., libFOO.so, or shr.o) is located and loaded.
The mode bit RTLD_MEMBER tells initAndLoad() that it needs to use the AIX (SVR3)
naming style.
"""
__author__ = "Michael Felt <aixtools@felt.demon.nl>"
import re
from os import environ, path
from sys import executable
from ctypes import c_void_p, sizeof
from subprocess import Popen, PIPE, DEVNULL
# Executable bit size - 32 or 64
# Used to filter the search in an archive by size, e.g., -X64
AIX_ABI = sizeof(c_void_p) * 8
from sys import maxsize
def _last_version(libnames, sep):
def _num_version(libname):
# "libxyz.so.MAJOR.MINOR" => [MAJOR, MINOR]
parts = libname.split(sep)
nums = []
try:
while parts:
nums.insert(0, int(parts.pop()))
except ValueError:
pass
return nums or [maxsize]
return max(reversed(libnames), key=_num_version)
def get_ld_header(p):
# "nested-function, but placed at module level
ld_header = None
for line in p.stdout:
if line.startswith(('/', './', '../')):
ld_header = line
elif "INDEX" in line:
return ld_header.rstrip('\n')
return None
def get_ld_header_info(p):
# "nested-function, but placed at module level
# as an ld_header was found, return known paths, archives and members
# these lines start with a digit
info = []
for line in p.stdout:
if re.match("[0-9]", line):
info.append(line)
else:
# blank line (separator), consume line and end for loop
break
return info
def get_ld_headers(file):
"""
Parse the header of the loader section of executable and archives
This function calls /usr/bin/dump -H as a subprocess
and returns a list of (ld_header, ld_header_info) tuples.
"""
# get_ld_headers parsing:
# 1. Find a line that starts with /, ./, or ../ - set as ld_header
# 2. If "INDEX" in occurs in a following line - return ld_header
# 3. get info (lines starting with [0-9])
ldr_headers = []
p = Popen(["/usr/bin/dump", f"-X{AIX_ABI}", "-H", file],
universal_newlines=True, stdout=PIPE, stderr=DEVNULL)
# be sure to read to the end-of-file - getting all entries
while ld_header := get_ld_header(p):
ldr_headers.append((ld_header, get_ld_header_info(p)))
p.stdout.close()
p.wait()
return ldr_headers
def get_shared(ld_headers):
"""
extract the shareable objects from ld_headers
character "[" is used to strip off the path information.
Note: the "[" and "]" characters that are part of dump -H output
are not removed here.
"""
shared = []
for (line, _) in ld_headers:
# potential member lines contain "["
# otherwise, no processing needed
if "[" in line:
# Strip off trailing colon (:)
shared.append(line[line.index("["):-1])
return shared
def get_one_match(expr, lines):
"""
Must be only one match, otherwise result is None.
When there is a match, strip leading "[" and trailing "]"
"""
# member names in the ld_headers output are between square brackets
expr = rf'\[({expr})\]'
matches = list(filter(None, (re.search(expr, line) for line in lines)))
if len(matches) == 1:
return matches[0].group(1)
else:
return None
# additional processing to deal with AIX legacy names for 64-bit members
def get_legacy(members):
"""
This routine provides historical aka legacy naming schemes started
in AIX4 shared library support for library members names.
e.g., in /usr/lib/libc.a the member name shr.o for 32-bit binary and
shr_64.o for 64-bit binary.
"""
if AIX_ABI == 64:
# AIX 64-bit member is one of shr64.o, shr_64.o, or shr4_64.o
expr = r'shr4?_?64\.o'
member = get_one_match(expr, members)
if member:
return member
else:
# 32-bit legacy names - both shr.o and shr4.o exist.
# shr.o is the preferred name so we look for shr.o first
# i.e., shr4.o is returned only when shr.o does not exist
for name in ['shr.o', 'shr4.o']:
member = get_one_match(re.escape(name), members)
if member:
return member
return None
def get_version(name, members):
"""
Sort list of members and return highest numbered version - if it exists.
This function is called when an unversioned libFOO.a(libFOO.so) has
not been found.
Versioning for the member name is expected to follow
GNU LIBTOOL conventions: the highest version (x, then X.y, then X.Y.z)
* find [libFoo.so.X]
* find [libFoo.so.X.Y]
* find [libFoo.so.X.Y.Z]
Before the GNU convention became the standard scheme regardless of
binary size AIX packagers used GNU convention "as-is" for 32-bit
archive members but used an "distinguishing" name for 64-bit members.
This scheme inserted either 64 or _64 between libFOO and .so
- generally libFOO_64.so, but occasionally libFOO64.so
"""
# the expression ending for versions must start as
# '.so.[0-9]', i.e., *.so.[at least one digit]
# while multiple, more specific expressions could be specified
# to search for .so.X, .so.X.Y and .so.X.Y.Z
# after the first required 'dot' digit
# any combination of additional 'dot' digits pairs are accepted
# anything more than libFOO.so.digits.digits.digits
# should be seen as a member name outside normal expectations
exprs = [rf'lib{name}\.so\.[0-9]+[0-9.]*',
rf'lib{name}_?64\.so\.[0-9]+[0-9.]*']
for expr in exprs:
versions = []
for line in members:
m = re.search(expr, line)
if m:
versions.append(m.group(0))
if versions:
return _last_version(versions, '.')
return None
def get_member(name, members):
"""
Return an archive member matching the request in name.
Name is the library name without any prefix like lib, suffix like .so,
or version number.
Given a list of members find and return the most appropriate result
Priority is given to generic libXXX.so, then a versioned libXXX.so.a.b.c
and finally, legacy AIX naming scheme.
"""
# look first for a generic match - prepend lib and append .so
expr = rf'lib{name}\.so'
member = get_one_match(expr, members)
if member:
return member
elif AIX_ABI == 64:
expr = rf'lib{name}64\.so'
member = get_one_match(expr, members)
if member:
return member
# since an exact match with .so as suffix was not found
# look for a versioned name
# If a versioned name is not found, look for AIX legacy member name
member = get_version(name, members)
if member:
return member
else:
return get_legacy(members)
def get_libpaths():
"""
On AIX, the buildtime searchpath is stored in the executable.
as "loader header information".
The command /usr/bin/dump -H extracts this info.
Prefix searched libraries with LD_LIBRARY_PATH (preferred),
or LIBPATH if defined. These paths are appended to the paths
to libraries the python executable is linked with.
This mimics AIX dlopen() behavior.
"""
libpaths = environ.get("LD_LIBRARY_PATH")
if libpaths is None:
libpaths = environ.get("LIBPATH")
if libpaths is None:
libpaths = []
else:
libpaths = libpaths.split(":")
objects = get_ld_headers(executable)
for (_, lines) in objects:
for line in lines:
# the second (optional) argument is PATH if it includes a /
path = line.split()[1]
if "/" in path:
libpaths.extend(path.split(":"))
return libpaths
def find_shared(paths, name):
"""
paths is a list of directories to search for an archive.
name is the abbreviated name given to find_library().
Process: search "paths" for archive, and if an archive is found
return the result of get_member().
If an archive is not found then return None
"""
for dir in paths:
# /lib is a symbolic link to /usr/lib, skip it
if dir == "/lib":
continue
# "lib" is prefixed to emulate compiler name resolution,
# e.g., -lc to libc
base = f'lib{name}.a'
archive = path.join(dir, base)
if path.exists(archive):
members = get_shared(get_ld_headers(archive))
member = get_member(re.escape(name), members)
if member is not None:
return (base, member)
else:
return (None, None)
return (None, None)
def find_library(name):
"""AIX implementation of ctypes.util.find_library()
Find an archive member that will dlopen(). If not available,
also search for a file (or link) with a .so suffix.
AIX supports two types of schemes that can be used with dlopen().
The so-called SystemV Release4 (svr4) format is commonly suffixed
with .so while the (default) AIX scheme has the library (archive)
ending with the suffix .a
As an archive has multiple members (e.g., 32-bit and 64-bit) in one file
the argument passed to dlopen must include both the library and
the member names in a single string.
find_library() looks first for an archive (.a) with a suitable member.
If no archive+member pair is found, look for a .so file.
"""
libpaths = get_libpaths()
(base, member) = find_shared(libpaths, name)
if base is not None:
return f"{base}({member})"
# To get here, a member in an archive has not been found
# In other words, either:
# a) a .a file was not found
# b) a .a file did not have a suitable member
# So, look for a .so file
# Check libpaths for .so file
# Note, the installation must prepare a link from a .so
# to a versioned file
# This is common practice by GNU libtool on other platforms
soname = f"lib{name}.so"
for dir in libpaths:
# /lib is a symbolic link to /usr/lib, skip it
if dir == "/lib":
continue
shlib = path.join(dir, soname)
if path.exists(shlib):
return soname
# if we are here, we have not found anything plausible
return None

78
Lib/ctypes/_endian.py vendored Normal file
View File

@@ -0,0 +1,78 @@
import sys
from ctypes import *
_array_type = type(Array)
def _other_endian(typ):
"""Return the type with the 'other' byte order. Simple types like
c_int and so on already have __ctype_be__ and __ctype_le__
attributes which contain the types, for more complicated types
arrays and structures are supported.
"""
# check _OTHER_ENDIAN attribute (present if typ is primitive type)
if hasattr(typ, _OTHER_ENDIAN):
return getattr(typ, _OTHER_ENDIAN)
# if typ is array
if isinstance(typ, _array_type):
return _other_endian(typ._type_) * typ._length_
# if typ is structure
if issubclass(typ, Structure):
return typ
raise TypeError("This type does not support other endian: %s" % typ)
class _swapped_meta:
def __setattr__(self, attrname, value):
if attrname == "_fields_":
fields = []
for desc in value:
name = desc[0]
typ = desc[1]
rest = desc[2:]
fields.append((name, _other_endian(typ)) + rest)
value = fields
super().__setattr__(attrname, value)
class _swapped_struct_meta(_swapped_meta, type(Structure)): pass
class _swapped_union_meta(_swapped_meta, type(Union)): pass
################################################################
# Note: The Structure metaclass checks for the *presence* (not the
# value!) of a _swapped_bytes_ attribute to determine the bit order in
# structures containing bit fields.
if sys.byteorder == "little":
_OTHER_ENDIAN = "__ctype_be__"
LittleEndianStructure = Structure
class BigEndianStructure(Structure, metaclass=_swapped_struct_meta):
"""Structure with big endian byte order"""
__slots__ = ()
_swappedbytes_ = None
LittleEndianUnion = Union
class BigEndianUnion(Union, metaclass=_swapped_union_meta):
"""Union with big endian byte order"""
__slots__ = ()
_swappedbytes_ = None
elif sys.byteorder == "big":
_OTHER_ENDIAN = "__ctype_le__"
BigEndianStructure = Structure
class LittleEndianStructure(Structure, metaclass=_swapped_struct_meta):
"""Structure with little endian byte order"""
__slots__ = ()
_swappedbytes_ = None
BigEndianUnion = Union
class LittleEndianUnion(Union, metaclass=_swapped_union_meta):
"""Union with little endian byte order"""
__slots__ = ()
_swappedbytes_ = None
else:
raise RuntimeError("Invalid byteorder")

7
Lib/ctypes/macholib/README.ctypes vendored Normal file
View File

@@ -0,0 +1,7 @@
Files in this directory come from Bob Ippolito's py2app.
License: Any components of the py2app suite may be distributed under
the MIT or PSF open source licenses.
This is version 1.0, SVN revision 789, from 2006/01/25.
The main repository is http://svn.red-bean.com/bob/macholib/trunk/macholib/

9
Lib/ctypes/macholib/__init__.py vendored Normal file
View File

@@ -0,0 +1,9 @@
"""
Enough Mach-O to make your head spin.
See the relevant header files in /usr/include/mach-o
And also Apple's documentation.
"""
__version__ = '1.0'

165
Lib/ctypes/macholib/dyld.py vendored Normal file
View File

@@ -0,0 +1,165 @@
"""
dyld emulation
"""
import os
from ctypes.macholib.framework import framework_info
from ctypes.macholib.dylib import dylib_info
from itertools import *
try:
from _ctypes import _dyld_shared_cache_contains_path
except ImportError:
def _dyld_shared_cache_contains_path(*args):
raise NotImplementedError
__all__ = [
'dyld_find', 'framework_find',
'framework_info', 'dylib_info',
]
# These are the defaults as per man dyld(1)
#
DEFAULT_FRAMEWORK_FALLBACK = [
os.path.expanduser("~/Library/Frameworks"),
"/Library/Frameworks",
"/Network/Library/Frameworks",
"/System/Library/Frameworks",
]
DEFAULT_LIBRARY_FALLBACK = [
os.path.expanduser("~/lib"),
"/usr/local/lib",
"/lib",
"/usr/lib",
]
def dyld_env(env, var):
if env is None:
env = os.environ
rval = env.get(var)
if rval is None:
return []
return rval.split(':')
def dyld_image_suffix(env=None):
if env is None:
env = os.environ
return env.get('DYLD_IMAGE_SUFFIX')
def dyld_framework_path(env=None):
return dyld_env(env, 'DYLD_FRAMEWORK_PATH')
def dyld_library_path(env=None):
return dyld_env(env, 'DYLD_LIBRARY_PATH')
def dyld_fallback_framework_path(env=None):
return dyld_env(env, 'DYLD_FALLBACK_FRAMEWORK_PATH')
def dyld_fallback_library_path(env=None):
return dyld_env(env, 'DYLD_FALLBACK_LIBRARY_PATH')
def dyld_image_suffix_search(iterator, env=None):
"""For a potential path iterator, add DYLD_IMAGE_SUFFIX semantics"""
suffix = dyld_image_suffix(env)
if suffix is None:
return iterator
def _inject(iterator=iterator, suffix=suffix):
for path in iterator:
if path.endswith('.dylib'):
yield path[:-len('.dylib')] + suffix + '.dylib'
else:
yield path + suffix
yield path
return _inject()
def dyld_override_search(name, env=None):
# If DYLD_FRAMEWORK_PATH is set and this dylib_name is a
# framework name, use the first file that exists in the framework
# path if any. If there is none go on to search the DYLD_LIBRARY_PATH
# if any.
framework = framework_info(name)
if framework is not None:
for path in dyld_framework_path(env):
yield os.path.join(path, framework['name'])
# If DYLD_LIBRARY_PATH is set then use the first file that exists
# in the path. If none use the original name.
for path in dyld_library_path(env):
yield os.path.join(path, os.path.basename(name))
def dyld_executable_path_search(name, executable_path=None):
# If we haven't done any searching and found a library and the
# dylib_name starts with "@executable_path/" then construct the
# library name.
if name.startswith('@executable_path/') and executable_path is not None:
yield os.path.join(executable_path, name[len('@executable_path/'):])
def dyld_default_search(name, env=None):
yield name
framework = framework_info(name)
if framework is not None:
fallback_framework_path = dyld_fallback_framework_path(env)
for path in fallback_framework_path:
yield os.path.join(path, framework['name'])
fallback_library_path = dyld_fallback_library_path(env)
for path in fallback_library_path:
yield os.path.join(path, os.path.basename(name))
if framework is not None and not fallback_framework_path:
for path in DEFAULT_FRAMEWORK_FALLBACK:
yield os.path.join(path, framework['name'])
if not fallback_library_path:
for path in DEFAULT_LIBRARY_FALLBACK:
yield os.path.join(path, os.path.basename(name))
def dyld_find(name, executable_path=None, env=None):
"""
Find a library or framework using dyld semantics
"""
for path in dyld_image_suffix_search(chain(
dyld_override_search(name, env),
dyld_executable_path_search(name, executable_path),
dyld_default_search(name, env),
), env):
if os.path.isfile(path):
return path
try:
if _dyld_shared_cache_contains_path(path):
return path
except NotImplementedError:
pass
raise ValueError("dylib %s could not be found" % (name,))
def framework_find(fn, executable_path=None, env=None):
"""
Find a framework using dyld semantics in a very loose manner.
Will take input such as:
Python
Python.framework
Python.framework/Versions/Current
"""
error = None
try:
return dyld_find(fn, executable_path=executable_path, env=env)
except ValueError as e:
error = e
fmwk_index = fn.rfind('.framework')
if fmwk_index == -1:
fmwk_index = len(fn)
fn += '.framework'
fn = os.path.join(fn, os.path.basename(fn[:fmwk_index]))
try:
return dyld_find(fn, executable_path=executable_path, env=env)
except ValueError:
raise error
finally:
error = None

42
Lib/ctypes/macholib/dylib.py vendored Normal file
View File

@@ -0,0 +1,42 @@
"""
Generic dylib path manipulation
"""
import re
__all__ = ['dylib_info']
DYLIB_RE = re.compile(r"""(?x)
(?P<location>^.*)(?:^|/)
(?P<name>
(?P<shortname>\w+?)
(?:\.(?P<version>[^._]+))?
(?:_(?P<suffix>[^._]+))?
\.dylib$
)
""")
def dylib_info(filename):
"""
A dylib name can take one of the following four forms:
Location/Name.SomeVersion_Suffix.dylib
Location/Name.SomeVersion.dylib
Location/Name_Suffix.dylib
Location/Name.dylib
returns None if not found or a mapping equivalent to:
dict(
location='Location',
name='Name.SomeVersion_Suffix.dylib',
shortname='Name',
version='SomeVersion',
suffix='Suffix',
)
Note that SomeVersion and Suffix are optional and may be None
if not present.
"""
is_dylib = DYLIB_RE.match(filename)
if not is_dylib:
return None
return is_dylib.groupdict()

Some files were not shown because too many files have changed in this diff Show More