Merge pull request #2018 from RustPython/coolreader18/common-crate

Move a few non-python specific functions into a 'common' crate, make threading optional
This commit is contained in:
Jeong YunWon authored and GitHub committed 2020-07-26 22:03:59 +09:00
commit 75b91087cd
59 files changed
+981 -1752

No files matched your search

+5
View File
@@ -35,6 +35,11 @@ jobs:
with:
command: test
args: --verbose ${{ env.CARGO_ARGS }}
- name: check compilation without threading
uses: actions-rs/cargo@v1
with:
command: check
args: ${{ env.CARGO_ARGS }} --no-default-features
snippets_cpython:
name: Run snippets and cpython tests
Generated
+207 -189
View File
@@ -7,16 +7,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3"
[[package]]
name = "adler32"
version = "1.0.4"
name = "adler"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d2e7343e7fc9de883d1b0341e0b13970f764c14101234857d2ddafa1cb1cac2"
checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e"
[[package]]
name = "adler32"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567b077b825e468cc974f0020d4082ee6e03132512f207ef1a02fd5d00d1f32d"
[[package]]
name = "aho-corasick"
version = "0.7.10"
version = "0.7.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8716408b8bc624ed7f65d223ddb9ac2d044c0547b6fa4b0d554f3a9540496ada"
checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86"
dependencies = [
"memchr",
]
@@ -47,8 +53,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0609c78bd572f4edc74310dfb63a01f5609d53fa8b4dd7c4d98aef3b3e8d72d1"
dependencies = [
"proc-macro-hack",
"quote 1.0.3",
"syn 1.0.17",
"quote 1.0.7",
"syn 1.0.35",
]
[[package]]
@@ -112,9 +118,9 @@ checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7"
[[package]]
name = "bincode"
version = "1.2.1"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5753e2a71534719bf3f4e57006c3a4f0d2c672a4b676eec84161f763eca87dbf"
checksum = "f30d3a39baa26f9651f17b375061f3233dde33424a8b72b0dbe93a68a0bc896d"
dependencies = [
"byteorder 1.3.4",
"serde",
@@ -122,18 +128,18 @@ dependencies = [
[[package]]
name = "bit-set"
version = "0.5.1"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e84c238982c4b1e1ee668d136c510c67a13465279c0cb367ea6baf6310620a80"
checksum = "6e11e16035ea35e4e5997b393eacbf6f63983188f7a2ad25bfb13465f5ad59de"
dependencies = [
"bit-vec",
]
[[package]]
name = "bit-vec"
version = "0.5.1"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f59bbe95d4e52a6398ec21238d31577f2b28a9d86807f06ca59d191d8440d0bb"
checksum = "5f0dc55f2d8a1a85650ac47858bb001b4c0dd73d79e3c455a842925e68d29cd3"
[[package]]
name = "bitflags"
@@ -187,9 +193,9 @@ dependencies = [
[[package]]
name = "bstr"
version = "0.2.12"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2889e6d50f394968c8bf4240dc3f2a7eb4680844d27308f798229ac9d4725f41"
checksum = "31accafdb70df7871592c058eca3985b71104e15ac32f64706022c58867da931"
dependencies = [
"lazy_static 1.4.0",
"memchr",
@@ -205,9 +211,9 @@ checksum = "39092a32794787acd8525ee150305ff051b0aa6cc2abaf193924f5ab05425f39"
[[package]]
name = "bumpalo"
version = "3.2.1"
version = "3.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12ae9db68ad7fac5fe51304d20f016c911539251075a214f8e663babefa35187"
checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820"
[[package]]
name = "byte-tools"
@@ -239,9 +245,9 @@ dependencies = [
[[package]]
name = "cc"
version = "1.0.50"
version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95e28fa049fda1c330bcf9d723be7663a899c4679724b34c81e9f5a326aab8cd"
checksum = "f9a06fb2e53271d7c279ec1efea6ab691c35a2ae67ec0d91d7acec0caf13b518"
[[package]]
name = "cfg-if"
@@ -251,9 +257,9 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "chrono"
version = "0.4.11"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80094f509cf8b5ae86a4966a39b3ff66cd7e2a3e594accec3743ff3fabeab5b2"
checksum = "c74d84029116787153e02106bf53e66828452a4b325cc8652b788b5967c0a0b6"
dependencies = [
"js-sys",
"num-integer",
@@ -264,9 +270,9 @@ dependencies = [
[[package]]
name = "clap"
version = "2.33.0"
version = "2.33.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9"
checksum = "bdfa80d47f954d53a35a64987ca1422f495b8d6483c0fe9f7117b36c2a792129"
dependencies = [
"ansi_term",
"atty",
@@ -379,9 +385,9 @@ version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb582b60359da160a9477ee80f15c8d784c477e69c217ef2cdd4169c24ea380f"
dependencies = [
"proc-macro2 1.0.10",
"quote 1.0.3",
"syn 1.0.17",
"proc-macro2 1.0.19",
"quote 1.0.7",
"syn 1.0.35",
]
[[package]]
@@ -410,16 +416,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "dirs"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c4ef5a8b902d393339e2a2c7fe573af92ce7e0ee5a3ff827b4c9ad7e07e4fa1"
dependencies = [
"cfg-if",
"dirs-sys",
]
[[package]]
name = "dirs-next"
version = "1.0.1"
@@ -430,18 +426,6 @@ dependencies = [
"dirs-sys-next",
]
[[package]]
name = "dirs-sys"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afa0b23de8fd801745c471deffa6e12d248f962c9fd4b4c33787b055599bde7b"
dependencies = [
"cfg-if",
"libc",
"redox_users",
"winapi",
]
[[package]]
name = "dirs-sys-next"
version = "0.1.0"
@@ -455,9 +439,9 @@ dependencies = [
[[package]]
name = "dns-lookup"
version = "1.0.1"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13988670860b076248c74e1b54444efc4f1dec70c8bb25da4b7c0024396b72bf"
checksum = "4cbea23c15f7e685a7ad01c994407f3849fa7c88556aaafe4054215b6df93f78"
dependencies = [
"libc",
"socket2",
@@ -560,9 +544,9 @@ dependencies = [
[[package]]
name = "flate2"
version = "1.0.14"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2cfff41391129e0a856d6d822600b8d71179d46879e310417eb9c762eb178b42"
checksum = "68c90b0fc46cf89d227cc78b40e494ff81287a92dd07631e5af0d06fe3cf885e"
dependencies = [
"cfg-if",
"crc32fast",
@@ -573,9 +557,9 @@ dependencies = [
[[package]]
name = "fnv"
version = "1.0.6"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "foreign-types"
@@ -606,9 +590,9 @@ checksum = "1b980f2816d6ee8673b6517b52cb0e808a180efc92e5c19d02cdda79066703ef"
[[package]]
name = "generational-arena"
version = "0.2.3"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "921c3803adaeb9f9639de5149d9f0f9f4b79f00c423915b701db2e02ed80b9ce"
checksum = "8e1d3b771574f62d0548cee0ad9057857e9fc25d7a3335f140c84f6acd0bf601"
dependencies = [
"cfg-if",
]
@@ -645,10 +629,19 @@ dependencies = [
]
[[package]]
name = "hermit-abi"
version = "0.1.10"
name = "hashbrown"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "725cf19794cf90aa94e65050cb4191ff5d8fa87a498383774c47b332e3af952e"
checksum = "34f595585f103464d8d2f6e9864682d74c1601fed5e07d62b1c9058dba8246fb"
dependencies = [
"autocfg 1.0.0",
]
[[package]]
name = "hermit-abi"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3deed196b6e7f9e44a2ae8d94225d80302d81208b1bb673fd21fe634645c85a9"
dependencies = [
"libc",
]
@@ -676,18 +669,24 @@ dependencies = [
[[package]]
name = "indexmap"
version = "1.3.2"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "076f042c5b7b98f31d205f1249267e12a6518c1481e9dae9764af19b707d2292"
checksum = "5b88cd59ee5f71fea89a62248fc8f387d44400cefe05ef548466d61ced9029a7"
dependencies = [
"autocfg 1.0.0",
"hashbrown",
]
[[package]]
name = "instant"
version = "0.1.4"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7777a24a1ce5de49fcdde84ec46efa487c3af49d5b6e6e0a50367cc5c1096182"
checksum = "5b141fdc7836c525d4d594027d318c84161ca17aaf8113ab1f81ab93ae897485"
dependencies = [
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "is-macro"
@@ -697,9 +696,9 @@ checksum = "04807f3dc9e3ea39af3f8469a5297267faf94859637afb836b33f47d9b2650ee"
dependencies = [
"Inflector",
"pmutil",
"proc-macro2 1.0.10",
"quote 1.0.3",
"syn 1.0.17",
"proc-macro2 1.0.19",
"quote 1.0.7",
"syn 1.0.35",
]
[[package]]
@@ -713,15 +712,15 @@ dependencies = [
[[package]]
name = "itoa"
version = "0.4.5"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e"
checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
[[package]]
name = "js-sys"
version = "0.3.37"
version = "0.3.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a27d435371a2fa5b6d2b028a74bbdb1234f308da363226a2854ca3ff8ba7055"
checksum = "52732a3d3ad72c58ad2dc70624f9c17b46ecd0943b9a4f1ee37c4c18c5d983e2"
dependencies = [
"wasm-bindgen",
]
@@ -801,9 +800,9 @@ dependencies = [
[[package]]
name = "libc"
version = "0.2.71"
version = "0.2.73"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49"
checksum = "bd7d4bd64732af4bf3a67f367c27df8520ad7e230c5817b8ff485864d80242b9"
[[package]]
name = "libz-sys"
@@ -819,18 +818,18 @@ dependencies = [
[[package]]
name = "lock_api"
version = "0.4.0"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de302ce1fe7482db13738fbaf2e21cfb06a986b89c0bf38d88abf16681aada4e"
checksum = "28247cc5a5be2f05fbcd76dd0cf2c7d3b5400cb978a28042abcd4fa0b3f8261c"
dependencies = [
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.8"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b"
dependencies = [
"cfg-if",
]
@@ -876,11 +875,11 @@ checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
[[package]]
name = "miniz_oxide"
version = "0.3.6"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa679ff6578b1cddee93d7e82e263b94a575e0bfced07284eb0c037c1d2416a5"
checksum = "be0f75932c1f6cfae3c04000e40114adf955636e19040f9c0a2c380702aa1c7f"
dependencies = [
"adler32",
"adler",
]
[[package]]
@@ -985,9 +984,9 @@ dependencies = [
[[package]]
name = "num_cpus"
version = "1.12.0"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46203554f085ff89c235cd12f7075f3233af9b11ed7c9e16dfe2560d03313ce6"
checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"
dependencies = [
"hermit-abi",
"libc",
@@ -1010,16 +1009,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffa5a33ddddfee04c0283a7653987d634e880347e96b5b2ed64de07efb59db9d"
dependencies = [
"proc-macro-crate",
"proc-macro2 1.0.10",
"quote 1.0.3",
"syn 1.0.17",
"proc-macro2 1.0.19",
"quote 1.0.7",
"syn 1.0.35",
]
[[package]]
name = "once_cell"
version = "1.3.1"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1c601810575c99596d4afc46f78a678c80105117c379eb3650cf99b8a21ce5b"
checksum = "0b631f7e854af39a1739f401cf34a8a013dfe09eac4fa4dba91e9768bd28168d"
[[package]]
name = "opaque-debug"
@@ -1029,9 +1028,9 @@ checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c"
[[package]]
name = "openssl"
version = "0.10.29"
version = "0.10.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cee6d85f4cb4c4f59a6a85d5b68a233d280c82e29e822913b9c8b129fbf20bdd"
checksum = "8d575eff3665419f9b83678ff2815858ad9d11567e082f5ac1814baba4e2bcb4"
dependencies = [
"bitflags",
"cfg-if",
@@ -1049,18 +1048,18 @@ checksum = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de"
[[package]]
name = "openssl-src"
version = "111.8.1+1.1.1f"
version = "111.10.2+1.1.1g"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f04f0299a91de598dde58d2e99101895498dcf3d58896a3297798f28b27c8b72"
checksum = "a287fdb22e32b5b60624d4a5a7a02dbe82777f730ec0dbc42a0554326fef5a70"
dependencies = [
"cc",
]
[[package]]
name = "openssl-sys"
version = "0.9.55"
version = "0.9.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7717097d810a0f2e2323f9e5d11e71608355e24828410b55b9d4f18aa5f9a5d8"
checksum = "a842db4709b604f0fe5d1170ae3565899be2ad3d9cbc72dedc789ac0511f78de"
dependencies = [
"autocfg 1.0.0",
"cc",
@@ -1104,9 +1103,9 @@ dependencies = [
[[package]]
name = "paste"
version = "0.1.10"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab4fb1930692d1b6a9cfabdde3d06ea0a7d186518e2f4d67660d8970e2fa647a"
checksum = "45ca20c77d80be666aef2b45486da86238fabe33e38306bd3118fe4af33fa880"
dependencies = [
"paste-impl",
"proc-macro-hack",
@@ -1114,14 +1113,11 @@ dependencies = [
[[package]]
name = "paste-impl"
version = "0.1.10"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a62486e111e571b1e93b710b61e8f493c0013be39629b714cb166bdb06aa5a8a"
checksum = "d95a7db200b97ef370c8e6de0088252f7e0dfff7d047a28528e47456c0fc98b6"
dependencies = [
"proc-macro-hack",
"proc-macro2 1.0.10",
"quote 1.0.3",
"syn 1.0.17",
]
[[package]]
@@ -1155,9 +1151,9 @@ dependencies = [
[[package]]
name = "pkg-config"
version = "0.3.17"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677"
checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33"
[[package]]
name = "pmutil"
@@ -1165,16 +1161,16 @@ version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3894e5d549cccbe44afecf72922f277f603cd4bb0219c8342631ef18fffbe004"
dependencies = [
"proc-macro2 1.0.10",
"quote 1.0.3",
"syn 1.0.17",
"proc-macro2 1.0.19",
"quote 1.0.7",
"syn 1.0.35",
]
[[package]]
name = "ppv-lite86"
version = "0.2.6"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b"
checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea"
[[package]]
name = "precomputed-hash"
@@ -1184,18 +1180,18 @@ checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c"
[[package]]
name = "proc-macro-crate"
version = "0.1.4"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e10d4b51f154c8a7fb96fd6dad097cb74b863943ec010ac94b9fd1be8861fe1e"
checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785"
dependencies = [
"toml",
]
[[package]]
name = "proc-macro-hack"
version = "0.5.15"
version = "0.5.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d659fe7c6d27f25e9d80a1a094c223f5246f6a6596453e09d7229bf42750b63"
checksum = "7e0456befd48169b9f13ef0f0ad46d492cf9d2dbb918bcf38e01eed4ce3ec5e4"
[[package]]
name = "proc-macro2"
@@ -1208,11 +1204,11 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.10"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df246d292ff63439fea9bc8c0a270bed0e390d5ebd4db4ba15aba81111b5abe3"
checksum = "04f5f085b5d71e2188cb8271e5da0161ad52c3f227a661a3c135fdf28e258b12"
dependencies = [
"unicode-xid 0.2.0",
"unicode-xid 0.2.1",
]
[[package]]
@@ -1242,11 +1238,11 @@ dependencies = [
[[package]]
name = "quote"
version = "1.0.3"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bdc6c187c65bca4260c9011c9e3132efe4909da44726bad24cf7572ae338d7f"
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
dependencies = [
"proc-macro2 1.0.10",
"proc-macro2 1.0.19",
]
[[package]]
@@ -1407,9 +1403,9 @@ dependencies = [
[[package]]
name = "redox_syscall"
version = "0.1.56"
version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
[[package]]
name = "redox_users"
@@ -1424,9 +1420,9 @@ dependencies = [
[[package]]
name = "regex"
version = "1.3.6"
version = "1.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f6946991529684867e47d86474e3a6d0c0ab9b82d5821e314b1ede31fa3a4b3"
checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6"
dependencies = [
"aho-corasick",
"memchr",
@@ -1445,9 +1441,9 @@ dependencies = [
[[package]]
name = "regex-syntax"
version = "0.6.17"
version = "0.6.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fe5bd57d1d7414c6b5ed48563a2c855d995ff777729dcd91c369ec7fea395ae"
checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8"
[[package]]
name = "result-like"
@@ -1521,6 +1517,19 @@ dependencies = [
"serde",
]
[[package]]
name = "rustpython-common"
version = "0.0.0"
dependencies = [
"cfg-if",
"hexf-parse",
"lexical",
"num-bigint",
"num-traits",
"parking_lot",
"rand 0.7.3",
]
[[package]]
name = "rustpython-compiler"
version = "0.1.2"
@@ -1540,11 +1549,11 @@ version = "0.1.2"
dependencies = [
"maplit",
"once_cell",
"proc-macro2 1.0.10",
"quote 1.0.3",
"proc-macro2 1.0.19",
"quote 1.0.7",
"rustpython-bytecode",
"rustpython-compiler",
"syn 1.0.17",
"syn 1.0.35",
]
[[package]]
@@ -1631,6 +1640,7 @@ dependencies = [
"result-like",
"rustc_version_runtime",
"rustpython-bytecode",
"rustpython-common",
"rustpython-compiler",
"rustpython-derive",
"rustpython-parser",
@@ -1676,6 +1686,7 @@ dependencies = [
"futures",
"generational-arena",
"js-sys",
"parking_lot",
"rustpython-compiler",
"rustpython-parser",
"rustpython-vm",
@@ -1688,16 +1699,17 @@ dependencies = [
[[package]]
name = "rustyline"
version = "6.1.0"
version = "6.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6155a1169c281a8b75ee943a83eef0e2bb63aff057688c2111be319e51fc9f63"
checksum = "3358c21cbbc1a751892528db4e1de4b7a2b6a73f001e215aaba97d712cfa9777"
dependencies = [
"cfg-if",
"dirs 2.0.1",
"dirs-next",
"libc",
"log",
"memchr",
"nix",
"scopeguard",
"unicode-segmentation",
"unicode-width",
"utf8parse",
@@ -1706,15 +1718,15 @@ dependencies = [
[[package]]
name = "ryu"
version = "1.0.3"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "535622e6be132bccd223f4bb2b8ac8d53cda3c7a6394944d3b2b33fb974f9d76"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]]
name = "schannel"
version = "0.1.18"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "039c25b130bd8c1321ee2d7de7fde2659fa9c2744e4bb29711cfc852ea53cd19"
checksum = "8f05ba609c234e60bee0d547fe94a4c7e9da733d1c962cf6e59efa4cd9c8bc75"
dependencies = [
"lazy_static 1.4.0",
"winapi",
@@ -1743,9 +1755,9 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]]
name = "serde"
version = "1.0.106"
version = "1.0.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36df6ac6412072f67cf767ebbde4133a5b2e88e76dc6187fa7104cd16f783399"
checksum = "5317f7588f0a5078ee60ef675ef96735a1442132dc645eb1d12c018620ed8cd3"
dependencies = [
"serde_derive",
]
@@ -1764,20 +1776,20 @@ dependencies = [
[[package]]
name = "serde_derive"
version = "1.0.106"
version = "1.0.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e549e3abf4fb8621bd1609f11dfc9f5e50320802273b12f3811a67e6716ea6c"
checksum = "2a0be94b04690fbaed37cddffc5c134bf537c8e3329d53e982fe04c374978f8e"
dependencies = [
"proc-macro2 1.0.10",
"quote 1.0.3",
"syn 1.0.17",
"proc-macro2 1.0.19",
"quote 1.0.7",
"syn 1.0.35",
]
[[package]]
name = "serde_json"
version = "1.0.51"
version = "1.0.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da07b57ee2623368351e9a0488bb0b261322a15a6e0ae53e243cbdc0f4208da9"
checksum = "3433e879a558dde8b5e8feb2a04899cf34fdde1fafb894687e52105fc1162ac3"
dependencies = [
"itoa",
"ryu",
@@ -1798,9 +1810,9 @@ dependencies = [
[[package]]
name = "sha2"
version = "0.8.1"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27044adfd2e1f077f649f59deb9490d3941d674002f7d062870a60ebe9bd47a0"
checksum = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69"
dependencies = [
"block-buffer",
"digest",
@@ -1835,9 +1847,9 @@ checksum = "aa0eec6425219a55511a35afac416a2eecc62f5fa1bec14f9b97de0caa7daebe"
[[package]]
name = "smallvec"
version = "1.4.0"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7cb5678e1615754284ec264d9bb5b4c27d2018577fd90ac0ceb578591ed5ee4"
checksum = "3757cb9d89161a2f24e1cf78efa0c1fcff485d18e3f55e0aa3480824ddaa0f3f"
[[package]]
name = "socket2"
@@ -1889,8 +1901,8 @@ checksum = "f0f45ed1b65bf9a4bf2f7b7dc59212d1926e9eaf00fa998988e420fd124467c6"
dependencies = [
"phf_generator",
"phf_shared",
"proc-macro2 1.0.10",
"quote 1.0.3",
"proc-macro2 1.0.19",
"quote 1.0.7",
"string_cache_shared",
]
@@ -1931,13 +1943,13 @@ dependencies = [
[[package]]
name = "syn"
version = "1.0.17"
version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0df0eb663f387145cab623dea85b09c2c5b4b0aef44e945d928e682fce71bb03"
checksum = "fb7f4c519df8c117855e19dd8cc851e89eb746fe7a73f0157e0d95fdec5369b0"
dependencies = [
"proc-macro2 1.0.10",
"quote 1.0.3",
"unicode-xid 0.2.0",
"proc-macro2 1.0.19",
"quote 1.0.7",
"unicode-xid 0.2.1",
]
[[package]]
@@ -1947,7 +1959,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edd106a334b7657c10b7c540a0106114feadeb4dc314513e97df481d5d966f42"
dependencies = [
"byteorder 1.3.4",
"dirs 1.0.5",
"dirs",
"winapi",
]
@@ -1999,6 +2011,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "tinyvec"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53953d2d3a5ad81d9f844a32f14ebb121f50b650cd59d0ee2a07cf13c617efed"
[[package]]
name = "toml"
version = "0.5.6"
@@ -2010,9 +2028,9 @@ dependencies = [
[[package]]
name = "typenum"
version = "1.11.2"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9"
checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
[[package]]
name = "uname"
@@ -2147,11 +2165,11 @@ checksum = "623f59e6af2a98bdafeb93fa277ac8e1e40440973001ca15cf4ae1541cd16d56"
[[package]]
name = "unicode-normalization"
version = "0.1.12"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5479532badd04e128284890390c1e876ef7a993d0570b3597ae43dfa1d59afa4"
checksum = "6fb19cf769fa8c6a80a162df694621ebeb4dafb606470b2b2fce0be40a98a977"
dependencies = [
"smallvec",
"tinyvec",
]
[[package]]
@@ -2162,9 +2180,9 @@ checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0"
[[package]]
name = "unicode-width"
version = "0.1.7"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "unicode-xid"
@@ -2174,9 +2192,9 @@ checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
[[package]]
name = "unicode-xid"
version = "0.2.0"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "unicode_names2"
@@ -2202,15 +2220,15 @@ dependencies = [
[[package]]
name = "vcpkg"
version = "0.2.8"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fc439f2794e98976c88a2a2dafce96b930fe8010b0a256b3c2199a773933168"
checksum = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c"
[[package]]
name = "vec_map"
version = "0.8.1"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "void"
@@ -2232,9 +2250,9 @@ checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasm-bindgen"
version = "0.2.60"
version = "0.2.65"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2cc57ce05287f8376e998cbddfb4c8cb43b84a7ec55cf4551d7c00eef317a47f"
checksum = "f3edbcc9536ab7eababcc6d2374a0b7bfe13a2b6d562c5e07f370456b1a8f33d"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
@@ -2242,16 +2260,16 @@ dependencies = [
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.60"
version = "0.2.65"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d967d37bf6c16cca2973ca3af071d0a2523392e4a594548155d89a678f4237cd"
checksum = "89ed2fb8c84bfad20ea66b26a3743f3e7ba8735a69fe7d95118c33ec8fc1244d"
dependencies = [
"bumpalo",
"lazy_static 1.4.0",
"log",
"proc-macro2 1.0.10",
"quote 1.0.3",
"syn 1.0.17",
"proc-macro2 1.0.19",
"quote 1.0.7",
"syn 1.0.35",
"wasm-bindgen-shared",
]
@@ -2270,38 +2288,38 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.60"
version = "0.2.65"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8bd151b63e1ea881bb742cd20e1d6127cef28399558f3b5d415289bc41eee3a4"
checksum = "eb071268b031a64d92fc6cf691715ca5a40950694d8f683c5bb43db7c730929e"
dependencies = [
"quote 1.0.3",
"quote 1.0.7",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.60"
version = "0.2.65"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d68a5b36eef1be7868f668632863292e37739656a80fc4b9acec7b0bd35a4931"
checksum = "cf592c807080719d1ff2f245a687cbadb3ed28b2077ed7084b47aba8b691f2c6"
dependencies = [
"proc-macro2 1.0.10",
"quote 1.0.3",
"syn 1.0.17",
"proc-macro2 1.0.19",
"quote 1.0.7",
"syn 1.0.35",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.60"
version = "0.2.65"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "daf76fe7d25ac79748a37538b7daeed1c7a6867c92d3245c12c6222e4a20d639"
checksum = "72b6c0220ded549d63860c78c38f3bcc558d1ca3f4efa74942c536ddbbb55e87"
[[package]]
name = "web-sys"
version = "0.3.37"
version = "0.3.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d6f51648d8c56c366144378a33290049eafdd784071077f6fe37dae64c1c4cb"
checksum = "8be2398f326b7ba09815d0b403095f34dd708579220d099caae89be0b32137b2"
dependencies = [
"js-sys",
"wasm-bindgen",
@@ -2309,9 +2327,9 @@ dependencies = [
[[package]]
name = "winapi"
version = "0.3.8"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
@@ -2325,9 +2343,9 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.4"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa515c5163a99cc82bab70fd3bfdd36d827be85de63737b40fcef2ce084a436e"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
+6 -4
View File
@@ -8,15 +8,17 @@ repository = "https://github.com/RustPython/RustPython"
license = "MIT"
[workspace]
members = [".", "derive", "vm", "wasm/lib", "parser", "compiler", "bytecode", "examples/freeze", "vm/pylib-crate"]
members = [".", "derive", "vm", "wasm/lib", "parser", "compiler", "bytecode", "examples/freeze", "vm/pylib-crate", "common"]
[[bench]]
name = "bench"
path = "./benchmarks/bench.rs"
[features]
default = ["threading"]
flame-it = ["rustpython-vm/flame-it", "flame", "flamescope"]
freeze-stdlib = ["rustpython-vm/freeze-stdlib"]
threading = ["rustpython-vm/threading"]
ssl = ["rustpython-vm/ssl"]
@@ -24,9 +26,9 @@ ssl = ["rustpython-vm/ssl"]
log = "0.4"
env_logger = "0.7"
clap = "2.33"
rustpython-compiler = {path = "compiler", version = "0.1.1"}
rustpython-parser = {path = "parser", version = "0.1.1"}
rustpython-vm = {path = "vm", version = "0.1.1"}
rustpython-compiler = { path = "compiler", version = "0.1.1" }
rustpython-parser = { path = "parser", version = "0.1.1" }
rustpython-vm = { path = "vm", version = "0.1.1", default-features = false, features = ["compile-parse"] }
dirs = { package = "dirs-next", version = "1.0" }
num-traits = "0.2.8"
cfg-if = "0.1"
+4 -1
View File
@@ -1587,7 +1587,10 @@ def _setup(_bootstrap_module):
setattr(self_module, '_pathseps_with_colon', {f':{s}' for s in path_separators})
# Directly load the _thread module (needed during bootstrap).
thread_module = _bootstrap._builtin_from_name('_thread')
try:
thread_module = _bootstrap._builtin_from_name('_thread')
except ImportError:
thread_module = None
setattr(self_module, '_thread', thread_module)
# Directly load the _weakref module (needed during bootstrap).
+19
View File
@@ -0,0 +1,19 @@
[package]
name = "rustpython-common"
version = "0.0.0"
authors = ["RustPython Team"]
edition = "2018"
[features]
threading = ["parking_lot"]
[dependencies]
parking_lot = { version = "0.11.0", optional = true }
num-traits = "0.2"
num-bigint = "0.3"
lexical = "4"
hexf-parse = "0.1.0"
cfg-if = "0.1"
[dev-dependencies]
rand = "0.7.3"
+132
View File
@@ -0,0 +1,132 @@
#[cfg(feature = "threading")]
use parking_lot::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
#[cfg(not(feature = "threading"))]
use std::cell::{Ref, RefCell, RefMut};
use std::ops::{Deref, DerefMut};
cfg_if::cfg_if! {
if #[cfg(feature = "threading")] {
type MutexInner<T> = Mutex<T>;
type MutexGuardInner<'a, T> = MutexGuard<'a, T>;
const fn new_mutex<T>(value: T) -> MutexInner<T> {
parking_lot::const_mutex(value)
}
fn lock_mutex<T: ?Sized>(m: &MutexInner<T>) -> MutexGuardInner<T> {
m.lock()
}
} else {
type MutexInner<T> = RefCell<T>;
type MutexGuardInner<'a, T> = RefMut<'a, T>;
const fn new_mutex<T>(value: T) -> MutexInner<T> {
RefCell::new(value)
}
fn lock_mutex<T: ?Sized>(m: &MutexInner<T>) -> MutexGuardInner<T> {
m.borrow_mut()
}
}
}
#[derive(Debug, Default)]
#[repr(transparent)]
pub struct PyMutex<T: ?Sized>(MutexInner<T>);
impl<T> PyMutex<T> {
pub const fn new(value: T) -> Self {
Self(new_mutex(value))
}
}
impl<T: ?Sized> PyMutex<T> {
pub fn lock(&self) -> PyMutexGuard<T> {
PyMutexGuard(lock_mutex(&self.0))
}
}
#[derive(Debug)]
#[repr(transparent)]
pub struct PyMutexGuard<'a, T: ?Sized>(MutexGuardInner<'a, T>);
impl<T: ?Sized> Deref for PyMutexGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
self.0.deref()
}
}
impl<T: ?Sized> DerefMut for PyMutexGuard<'_, T> {
fn deref_mut(&mut self) -> &mut T {
self.0.deref_mut()
}
}
cfg_if::cfg_if! {
if #[cfg(feature = "threading")] {
type RwLockInner<T> = RwLock<T>;
type RwLockReadInner<'a, T> = RwLockReadGuard<'a, T>;
type RwLockWriteInner<'a, T> = RwLockWriteGuard<'a, T>;
const fn new_rwlock<T>(value: T) -> RwLockInner<T> {
parking_lot::const_rwlock(value)
}
fn read_rwlock<T: ?Sized>(m: &RwLockInner<T>) -> RwLockReadInner<T> {
m.read()
}
fn write_rwlock<T: ?Sized>(m: &RwLockInner<T>) -> RwLockWriteInner<T> {
m.write()
}
} else {
type RwLockInner<T> = RefCell<T>;
type RwLockReadInner<'a, T> = Ref<'a, T>;
type RwLockWriteInner<'a, T> = RefMut<'a, T>;
const fn new_rwlock<T>(value: T) -> RwLockInner<T> {
RefCell::new(value)
}
fn read_rwlock<T: ?Sized>(m: &RwLockInner<T>) -> RwLockReadInner<T> {
m.borrow()
}
fn write_rwlock<T: ?Sized>(m: &RwLockInner<T>) -> RwLockWriteInner<T> {
m.borrow_mut()
}
}
}
#[derive(Debug, Default)]
#[repr(transparent)]
pub struct PyRwLock<T: ?Sized>(RwLockInner<T>);
impl<T> PyRwLock<T> {
pub const fn new(value: T) -> Self {
Self(new_rwlock(value))
}
}
impl<T: ?Sized> PyRwLock<T> {
pub fn read(&self) -> PyRwLockReadGuard<T> {
PyRwLockReadGuard(read_rwlock(&self.0))
}
pub fn write(&self) -> PyRwLockWriteGuard<T> {
PyRwLockWriteGuard(write_rwlock(&self.0))
}
}
#[derive(Debug)]
#[repr(transparent)]
pub struct PyRwLockReadGuard<'a, T: ?Sized>(RwLockReadInner<'a, T>);
impl<T: ?Sized> Deref for PyRwLockReadGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
self.0.deref()
}
}
#[derive(Debug)]
#[repr(transparent)]
pub struct PyRwLockWriteGuard<'a, T: ?Sized>(RwLockWriteInner<'a, T>);
impl<T: ?Sized> Deref for PyRwLockWriteGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
self.0.deref()
}
}
impl<T: ?Sized> DerefMut for PyRwLockWriteGuard<'_, T> {
fn deref_mut(&mut self) -> &mut T {
self.0.deref_mut()
}
}
+194
View File
@@ -0,0 +1,194 @@
use num_bigint::{BigInt, ToBigInt};
use num_traits::{Float, Signed, ToPrimitive, Zero};
pub fn ufrexp(value: f64) -> (f64, i32) {
if 0.0 == value {
(0.0, 0i32)
} else {
let bits = value.to_bits();
let exponent: i32 = ((bits >> 52) & 0x7ff) as i32 - 1022;
let mantissa_bits = bits & (0x000f_ffff_ffff_ffff) | (1022 << 52);
(f64::from_bits(mantissa_bits), exponent)
}
}
pub fn eq_int(value: f64, other: &BigInt) -> bool {
if let (Some(self_int), Some(other_float)) = (value.to_bigint(), other.to_f64()) {
value == other_float && self_int == *other
} else {
false
}
}
pub fn lt_int(value: f64, other_int: &BigInt) -> bool {
match (value.to_bigint(), other_int.to_f64()) {
(Some(self_int), Some(other_float)) => value < other_float || self_int < *other_int,
// finite float, other_int too big for float,
// the result depends only on other_int’s sign
(Some(_), None) => other_int.is_positive(),
// infinite float must be bigger or lower than any int, depending on its sign
_ if value.is_infinite() => value.is_sign_negative(),
// NaN, always false
_ => false,
}
}
pub fn gt_int(value: f64, other_int: &BigInt) -> bool {
match (value.to_bigint(), other_int.to_f64()) {
(Some(self_int), Some(other_float)) => value > other_float || self_int > *other_int,
// finite float, other_int too big for float,
// the result depends only on other_int’s sign
(Some(_), None) => other_int.is_negative(),
// infinite float must be bigger or lower than any int, depending on its sign
_ if value.is_infinite() => value.is_sign_positive(),
// NaN, always false
_ => false,
}
}
pub fn parse_str(literal: &str) -> Option<f64> {
if literal.starts_with('_') || literal.ends_with('_') {
return None;
}
let mut buf = String::with_capacity(literal.len());
let mut last_tok: Option<char> = None;
for c in literal.chars() {
if !(c.is_ascii_alphanumeric() || c == '_' || c == '+' || c == '-' || c == '.') {
return None;
}
if !c.is_ascii_alphanumeric() {
if let Some(l) = last_tok {
if !l.is_ascii_alphanumeric() && !(c == '.' && (l == '-' || l == '+')) {
return None;
}
}
}
if c != '_' {
buf.push(c);
}
last_tok = Some(c);
}
if let Ok(f) = lexical::parse(buf.as_str()) {
Some(f)
} else {
None
}
}
pub fn is_integer(v: f64) -> bool {
(v - v.round()).abs() < std::f64::EPSILON
}
pub fn to_string(value: f64) -> String {
let lit = format!("{:e}", value);
if let Some(position) = lit.find('e') {
let significand = &lit[..position];
let exponent = &lit[position + 1..];
let exponent = exponent.parse::<i32>().unwrap();
if exponent < 16 && exponent > -5 {
if is_integer(value) {
format!("{:.1?}", value)
} else {
value.to_string()
}
} else {
format!("{}e{:+#03}", significand, exponent)
}
} else {
value.to_string()
}
}
pub fn from_hex(s: &str) -> Option<f64> {
if let Ok(f) = hexf_parse::parse_hexf64(s, false) {
return Some(f);
}
match s.to_ascii_lowercase().as_str() {
"nan" => Some(f64::NAN),
"+nan" => Some(f64::NAN),
"-nan" => Some(f64::NAN),
"inf" => Some(f64::INFINITY),
"infinity" => Some(f64::INFINITY),
"+inf" => Some(f64::INFINITY),
"+infinity" => Some(f64::INFINITY),
"-inf" => Some(f64::NEG_INFINITY),
"-infinity" => Some(f64::NEG_INFINITY),
value => {
let mut hex = String::with_capacity(value.len());
let has_0x = value.contains("0x");
let has_p = value.contains('p');
let has_dot = value.contains('.');
let mut start = 0;
if !has_0x && value.starts_with('-') {
hex.push_str("-0x");
start += 1;
} else if !has_0x {
hex.push_str("0x");
if value.starts_with('+') {
start += 1;
}
}
for (index, ch) in value.chars().enumerate() {
if ch == 'p' && has_dot {
hex.push_str("p");
} else if ch == 'p' && !has_dot {
hex.push_str(".p");
} else if index >= start {
hex.push(ch);
}
}
if !has_p && has_dot {
hex.push_str("p0");
} else if !has_p && !has_dot {
hex.push_str(".p0")
}
hexf_parse::parse_hexf64(hex.as_str(), false).ok()
}
}
}
pub fn to_hex(value: f64) -> String {
let (mantissa, exponent, sign) = value.integer_decode();
let sign_fmt = if sign < 0 { "-" } else { "" };
match value {
value if value.is_zero() => format!("{}0x0.0p+0", sign_fmt),
value if value.is_infinite() => format!("{}inf", sign_fmt),
value if value.is_nan() => "nan".to_owned(),
_ => {
const BITS: i16 = 52;
const FRACT_MASK: u64 = 0xf_ffff_ffff_ffff;
format!(
"{}0x{:x}.{:013x}p{:+}",
sign_fmt,
mantissa >> BITS,
mantissa & FRACT_MASK,
exponent + BITS
)
}
}
}
#[test]
fn test_to_hex() {
use rand::Rng;
for _ in 0..20000 {
let bytes = rand::thread_rng().gen::<[u64; 1]>();
let f = f64::from_bits(bytes[0]);
if !f.is_finite() {
continue;
}
let hex = to_hex(f);
// println!("{} -> {}", f, hex);
let roundtrip = hexf_parse::parse_hexf64(&hex, false).unwrap();
// println!(" -> {}", roundtrip);
assert!(f == roundtrip, "{} {} {}", f, hex, roundtrip);
}
}
+22 -46
View File
@@ -1,12 +1,8 @@
use num_bigint::BigInt;
use num_traits::ToPrimitive;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use crate::obj::objfloat;
use crate::pyobject::PyObjectRef;
use crate::pyobject::PyResult;
use crate::vm::VirtualMachine;
pub type PyHash = i64;
pub type PyUHash = u64;
@@ -18,6 +14,12 @@ pub const MODULUS: PyUHash = (1 << BITS) - 1;
pub const INF: PyHash = 314_159;
pub const NAN: PyHash = 0;
pub const IMAG: PyHash = MULTIPLIER;
pub const ALGO: &str = "siphasher13";
pub const HASH_BITS: usize = std::mem::size_of::<PyHash>() * 8;
// internally DefaultHasher uses 2 u64s as the seed, but
// that's not guaranteed to be consistent across Rust releases
// TODO: use something like the siphasher crate as our hash algorithm
pub const SEED_BITS: usize = std::mem::size_of::<PyHash>() * 2 * 8;
// pub const CUTOFF: usize = 7;
@@ -35,7 +37,7 @@ pub fn hash_float(value: f64) -> PyHash {
};
}
let frexp = objfloat::ufrexp(value);
let frexp = super::float_ops::ufrexp(value);
// process 28 bits at a time; this should work well both for binary
// and hexadecimal floating point.
@@ -67,30 +69,32 @@ pub fn hash_float(value: f64) -> PyHash {
}
pub fn hash_value<T: Hash>(data: &T) -> PyHash {
let mut hasher = std::collections::hash_map::DefaultHasher::new();
let mut hasher = DefaultHasher::new();
data.hash(&mut hasher);
hasher.finish() as PyHash
}
pub fn hash_iter<'a, I: std::iter::Iterator<Item = &'a PyObjectRef>>(
iter: I,
vm: &VirtualMachine,
) -> PyResult<PyHash> {
let mut hasher = std::collections::hash_map::DefaultHasher::new();
pub fn hash_iter<'a, T: 'a, I, F, E>(iter: I, hashf: F) -> Result<PyHash, E>
where
I: IntoIterator<Item = &'a T>,
F: Fn(&'a T) -> Result<PyHash, E>,
{
let mut hasher = DefaultHasher::new();
for element in iter {
let item_hash = vm._hash(&element)?;
let item_hash = hashf(element)?;
item_hash.hash(&mut hasher);
}
Ok(hasher.finish() as PyHash)
}
pub fn hash_iter_unordered<'a, I: std::iter::Iterator<Item = &'a PyObjectRef>>(
iter: I,
vm: &VirtualMachine,
) -> PyResult<PyHash> {
pub fn hash_iter_unordered<'a, T: 'a, I, F, E>(iter: I, hashf: F) -> Result<PyHash, E>
where
I: IntoIterator<Item = &'a T>,
F: Fn(&'a T) -> Result<PyHash, E>,
{
let mut hash: PyHash = 0;
for element in iter {
let item_hash = vm._hash(element)?;
let item_hash = hashf(element)?;
// xor is commutative and hash should be independent of order
hash ^= item_hash;
}
@@ -103,31 +107,3 @@ pub fn hash_bigint(value: &BigInt) -> PyHash {
None => (value % MODULUS).to_i64().unwrap(),
}
}
#[pystruct_sequence(module = "sys", name = "hash_info")]
#[derive(Debug)]
pub(crate) struct PyHashInfo {
width: usize,
modulus: PyUHash,
inf: PyHash,
nan: PyHash,
imag: PyHash,
algorithm: &'static str,
hash_bits: usize,
seed_bits: usize,
}
impl PyHashInfo {
pub const INFO: Self = PyHashInfo {
width: BITS,
modulus: MODULUS,
inf: INF,
nan: NAN,
imag: IMAG,
algorithm: "siphash13",
hash_bits: std::mem::size_of::<PyHash>() * 8,
// internally hash_map::DefaultHasher uses 2 u64s as the seed, but
// that's not guaranteed to be consistent across Rust releases
// TODO: use something like the siphasher crate as our hash algorithm
seed_bits: std::mem::size_of::<PyHash>() * 2 * 8,
};
}
+6
View File
@@ -0,0 +1,6 @@
//! A crate to hold types and functions common to all rustpython components.
pub mod cell;
pub mod float_ops;
pub mod hash;
pub mod rc;
+14
View File
@@ -0,0 +1,14 @@
#[cfg(not(feature = "threading"))]
use std::rc::{Rc, Weak};
#[cfg(feature = "threading")]
use std::sync::{Arc, Weak};
// type aliases instead of newtypes because you can't do `fn method(self: PyRc<Self>)` with a
// newtype; requires the arbitrary_self_types unstable feature
#[cfg(feature = "threading")]
pub type PyRc<T> = Arc<T>;
#[cfg(not(feature = "threading"))]
pub type PyRc<T> = Rc<T>;
pub type PyWeak<T> = Weak<T>;
+2 -2
View File
@@ -12,8 +12,8 @@ edition = "2018"
lalrpop="0.17"
[dependencies]
lalrpop-util="0.17"
log="0.4.1"
lalrpop-util = "0.17"
log = "0.4.1"
num-bigint = "0.3"
num-traits = "0.2"
unic-emoji-char = "0.9"
+4 -1
View File
@@ -9,10 +9,12 @@ edition = "2018"
include = ["src/**/*.rs", "Cargo.toml", "build.rs", "Lib/**/*.py"]
[features]
default = ["rustpython-parser", "rustpython-compiler"]
default = ["compile-parse", "threading"]
vm-tracing-logging = []
flame-it = ["flame", "flamer"]
freeze-stdlib = ["rustpython-pylib"]
threading = ["rustpython-common/threading"]
compile-parse = ["rustpython-parser", "rustpython-compiler"]
ssl = ["openssl", "openssl-sys", "openssl-probe"]
@@ -91,6 +93,7 @@ unic-ucd-ident = "0.9"
flame = { version = "0.2", optional = true }
flamer = { version = "0.3", optional = true }
rustpython-common = { path = "../common" }
[target.'cfg(unix)'.dependencies]
exitcode = "1.1.2"
+1 -1
View File
@@ -1,7 +1,7 @@
[package]
name = "rustpython-pylib"
version = "0.1.0"
authors = ["Noah <33094578+coolreader18@users.noreply.github.com>"]
authors = ["RustPython Team"]
edition = "2018"
[dependencies]
+2 -2
View File
@@ -27,7 +27,6 @@ mod decl {
use crate::obj::objsequence;
use crate::obj::objstr::{PyString, PyStringRef};
use crate::obj::objtype::{self, PyClassRef};
use crate::pyhash;
use crate::pyobject::{
Either, IdProtocol, ItemProtocol, PyCallable, PyIterable, PyObjectRef, PyResult, PyValue,
TryFromObject, TypeProtocol,
@@ -37,6 +36,7 @@ mod decl {
#[cfg(feature = "rustpython-parser")]
use crate::stdlib::ast;
use crate::vm::VirtualMachine;
use rustpython_common::hash::PyHash;
#[pyfunction]
fn abs(x: PyObjectRef, vm: &VirtualMachine) -> PyResult {
@@ -329,7 +329,7 @@ mod decl {
}
#[pyfunction]
fn hash(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<pyhash::PyHash> {
fn hash(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyHash> {
vm._hash(&obj)
}
+11 -11
View File
@@ -1,9 +1,9 @@
use crate::common::cell::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard};
use crate::obj::objstr::{PyString, PyStringRef};
use crate::pyhash;
use crate::pyobject::{IdProtocol, IntoPyObject, PyObjectRef, PyResult};
use crate::vm::VirtualMachine;
use num_bigint::ToBigInt;
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use rustpython_common::hash;
/// Ordered dictionary implementation.
/// Inspired by: https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html
/// And: https://www.youtube.com/watch?v=p33CVV29OG8
@@ -13,14 +13,14 @@ use std::hash::{Hash, Hasher};
use std::mem::size_of;
/// hash value of an object returned by __hash__
type HashValue = pyhash::PyHash;
type HashValue = hash::PyHash;
/// index calculated by resolving collision
type HashIndex = pyhash::PyHash;
type HashIndex = hash::PyHash;
/// entry index mapped in indices
type EntryIndex = usize;
pub struct Dict<T = PyObjectRef> {
inner: RwLock<InnerDict<T>>,
inner: PyRwLock<InnerDict<T>>,
}
struct InnerDict<T> {
@@ -42,7 +42,7 @@ impl<T: Clone> Clone for InnerDict<T> {
impl<T: Clone> Clone for Dict<T> {
fn clone(&self) -> Self {
Dict {
inner: RwLock::new(self.inner.read().clone()),
inner: PyRwLock::new(self.inner.read().clone()),
}
}
}
@@ -50,7 +50,7 @@ impl<T: Clone> Clone for Dict<T> {
impl<T> Default for Dict<T> {
fn default() -> Self {
Dict {
inner: RwLock::new(InnerDict {
inner: PyRwLock::new(InnerDict {
size: 0,
indices: HashMap::new(),
entries: Vec::new(),
@@ -82,11 +82,11 @@ pub struct DictSize {
}
impl<T: Clone> Dict<T> {
fn borrow_value(&self) -> RwLockReadGuard<'_, InnerDict<T>> {
fn borrow_value(&self) -> PyRwLockReadGuard<'_, InnerDict<T>> {
self.inner.read()
}
fn borrow_value_mut(&self) -> RwLockWriteGuard<'_, InnerDict<T>> {
fn borrow_value_mut(&self) -> PyRwLockWriteGuard<'_, InnerDict<T>> {
self.inner.write()
}
@@ -463,8 +463,8 @@ impl DictKey for &PyStringRef {
impl DictKey for &str {
fn do_hash(self, _vm: &VirtualMachine) -> PyResult<HashValue> {
// follow a similar route as the hashing of PyStringRef
let raw_hash = pyhash::hash_value(&self.to_owned()).to_bigint().unwrap();
let raw_hash = pyhash::hash_bigint(&raw_hash);
let raw_hash = hash::hash_value(&self.to_owned()).to_bigint().unwrap();
let raw_hash = hash::hash_bigint(&raw_hash);
let mut hasher = DefaultHasher::new();
raw_hash.hash(&mut hasher);
Ok(hasher.finish() as HashValue)
+9 -9
View File
@@ -1,3 +1,4 @@
use crate::common::cell::PyRwLock;
use crate::function::PyFuncArgs;
use crate::obj::objnone::PyNone;
use crate::obj::objstr::{PyString, PyStringRef};
@@ -14,7 +15,6 @@ use crate::types::create_type;
use crate::VirtualMachine;
use itertools::Itertools;
use parking_lot::RwLock;
use std::fmt;
use std::fs::File;
use std::io::{self, BufRead, BufReader, Write};
@@ -23,11 +23,11 @@ use crossbeam_utils::atomic::AtomicCell;
#[pyclass]
pub struct PyBaseException {
traceback: RwLock<Option<PyTracebackRef>>,
cause: RwLock<Option<PyBaseExceptionRef>>,
context: RwLock<Option<PyBaseExceptionRef>>,
traceback: PyRwLock<Option<PyTracebackRef>>,
cause: PyRwLock<Option<PyBaseExceptionRef>>,
context: PyRwLock<Option<PyBaseExceptionRef>>,
suppress_context: AtomicCell<bool>,
args: RwLock<PyTupleRef>,
args: PyRwLock<PyTupleRef>,
}
impl fmt::Debug for PyBaseException {
@@ -51,11 +51,11 @@ impl PyValue for PyBaseException {
impl PyBaseException {
pub(crate) fn new(args: Vec<PyObjectRef>, vm: &VirtualMachine) -> PyBaseException {
PyBaseException {
traceback: RwLock::new(None),
cause: RwLock::new(None),
context: RwLock::new(None),
traceback: PyRwLock::new(None),
cause: PyRwLock::new(None),
context: PyRwLock::new(None),
suppress_context: AtomicCell::new(false),
args: RwLock::new(PyTuple::from(args).into_ref(vm)),
args: PyRwLock::new(PyTuple::from(args).into_ref(vm)),
}
}
+3 -3
View File
@@ -3,10 +3,10 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use indexmap::IndexMap;
use itertools::Itertools;
use parking_lot::Mutex;
use crate::builtins::builtin_isinstance;
use crate::bytecode;
use crate::common::cell::PyMutex;
use crate::exceptions::{self, ExceptionCtor, PyBaseExceptionRef};
use crate::function::PyFuncArgs;
use crate::obj::objasyncgenerator::PyAsyncGenWrappedValue;
@@ -92,7 +92,7 @@ pub struct Frame {
pub scope: Scope,
/// index of last instruction ran
pub lasti: AtomicUsize,
state: Mutex<FrameState>,
state: PyMutex<FrameState>,
}
impl PyValue for Frame {
@@ -163,7 +163,7 @@ impl Frame {
code,
scope,
lasti: AtomicUsize::new(0),
state: Mutex::new(FrameState {
state: PyMutex::new(FrameState {
stack: Vec::new(),
blocks: Vec::new(),
}),
+1 -1
View File
@@ -68,7 +68,6 @@ pub mod function;
pub mod import;
pub mod obj;
pub mod py_serde;
mod pyhash;
pub mod pyobject;
pub mod readline;
pub mod scope;
@@ -84,6 +83,7 @@ mod vm;
// pub use self::pyobject::Executor;
pub use self::vm::{InitParameter, PySettings, VirtualMachine};
pub use rustpython_bytecode::*;
pub use rustpython_common as common;
#[doc(hidden)]
pub mod __exports {
+8 -8
View File
@@ -1,8 +1,8 @@
//! Implementation of the python bytearray object.
use crate::common::cell::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard};
use bstr::ByteSlice;
use crossbeam_utils::atomic::AtomicCell;
use num_traits::cast::ToPrimitive;
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::mem::size_of;
use super::objbyteinner::{
@@ -36,7 +36,7 @@ use crate::vm::VirtualMachine;
#[pyclass(name = "bytearray")]
#[derive(Debug)]
pub struct PyByteArray {
inner: RwLock<PyByteInner>,
inner: PyRwLock<PyByteInner>,
}
pub type PyByteArrayRef = PyRef<PyByteArray>;
@@ -44,21 +44,21 @@ pub type PyByteArrayRef = PyRef<PyByteArray>;
impl PyByteArray {
pub fn new(data: Vec<u8>) -> Self {
PyByteArray {
inner: RwLock::new(PyByteInner { elements: data }),
inner: PyRwLock::new(PyByteInner { elements: data }),
}
}
fn from_inner(inner: PyByteInner) -> Self {
PyByteArray {
inner: RwLock::new(inner),
inner: PyRwLock::new(inner),
}
}
pub fn borrow_value(&self) -> RwLockReadGuard<'_, PyByteInner> {
pub fn borrow_value(&self) -> PyRwLockReadGuard<'_, PyByteInner> {
self.inner.read()
}
pub fn borrow_value_mut(&self) -> RwLockWriteGuard<'_, PyByteInner> {
pub fn borrow_value_mut(&self) -> PyRwLockWriteGuard<'_, PyByteInner> {
self.inner.write()
}
}
@@ -98,8 +98,8 @@ impl PyByteArray {
}
#[pymethod(name = "__repr__")]
fn repr(&self) -> PyResult<String> {
Ok(format!("bytearray(b'{}')", self.borrow_value().repr()?))
fn repr(&self) -> String {
format!("bytearray(b'{}')", self.borrow_value().repr())
}
#[pymethod(name = "__len__")]
+5 -5
View File
@@ -14,11 +14,11 @@ use super::objslice::PySliceRef;
use super::objstr::{self, PyString, PyStringRef};
use super::pystr::{self, PyCommonString, PyCommonStringContainer, PyCommonStringWrapper};
use crate::function::{OptionalArg, OptionalOption};
use crate::pyhash;
use crate::pyobject::{
Either, PyComparisonValue, PyIterable, PyObjectRef, PyResult, TryFromObject, TypeProtocol,
};
use crate::vm::VirtualMachine;
use rustpython_common::hash;
#[derive(Debug, Default, Clone)]
pub struct PyByteInner {
@@ -253,7 +253,7 @@ impl ByteInnerSplitlinesOptions {
#[allow(clippy::len_without_is_empty)]
impl PyByteInner {
pub fn repr(&self) -> PyResult<String> {
pub fn repr(&self) -> String {
let mut res = String::with_capacity(self.elements.len());
for i in self.elements.iter() {
match i {
@@ -266,7 +266,7 @@ impl PyByteInner {
_ => res.push_str(&format!("\\x{:x}", i)),
}
}
Ok(res)
res
}
pub fn len(&self) -> usize {
@@ -303,8 +303,8 @@ impl PyByteInner {
self.cmp(other, |a, b| a < b, vm)
}
pub fn hash(&self) -> pyhash::PyHash {
pyhash::hash_value(&self.elements)
pub fn hash(&self) -> hash::PyHash {
hash::hash_value(&self.elements)
}
pub fn add(&self, other: PyByteInner) -> Vec<u8> {
+4 -4
View File
@@ -14,7 +14,6 @@ use super::objstr::{PyString, PyStringRef};
use super::objtype::PyClassRef;
use super::pystr::{self, PyCommonString};
use crate::function::{OptionalArg, OptionalOption};
use crate::pyhash;
use crate::pyobject::{
Either, IntoPyObject,
PyArithmaticValue::{self, *},
@@ -22,6 +21,7 @@ use crate::pyobject::{
TryFromObject, TypeProtocol,
};
use crate::vm::VirtualMachine;
use rustpython_common::hash::PyHash;
/// "bytes(iterable_of_ints) -> bytes\n\
/// bytes(string, encoding[, errors]) -> bytes\n\
@@ -102,8 +102,8 @@ impl PyBytes {
}
#[pymethod(name = "__repr__")]
fn repr(&self, vm: &VirtualMachine) -> PyResult {
Ok(vm.new_str(format!("b'{}'", self.inner.repr()?)))
pub(crate) fn repr(&self) -> String {
format!("b'{}'", self.inner.repr())
}
#[pymethod(name = "__len__")]
@@ -133,7 +133,7 @@ impl PyBytes {
}
#[pymethod(name = "__hash__")]
fn hash(&self) -> pyhash::PyHash {
fn hash(&self) -> PyHash {
self.inner.hash()
}
+5 -5
View File
@@ -8,11 +8,11 @@ use super::objint::{self, PyInt};
use super::objstr::PyString;
use super::objtype::PyClassRef;
use crate::function::OptionalArg;
use crate::pyhash;
use crate::pyobject::{
IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
};
use crate::vm::VirtualMachine;
use rustpython_common::hash;
/// Create a complex number from a real part and an optional imaginary part.
///
@@ -267,10 +267,10 @@ impl PyComplex {
}
#[pymethod(name = "__hash__")]
fn hash(&self) -> pyhash::PyHash {
let re_hash = pyhash::hash_float(self.value.re);
let im_hash = pyhash::hash_float(self.value.im);
let ret = Wrapping(re_hash) + Wrapping(im_hash) * Wrapping(pyhash::IMAG);
fn hash(&self) -> hash::PyHash {
let re_hash = hash::hash_float(self.value.re);
let im_hash = hash::hash_float(self.value.im);
let ret = Wrapping(re_hash) + Wrapping(im_hash) * Wrapping(hash::IMAG);
ret.0
}
+3 -3
View File
@@ -4,8 +4,8 @@ use crate::frame::{ExecutionResult, FrameRef};
use crate::pyobject::{PyObjectRef, PyResult};
use crate::vm::VirtualMachine;
use crate::common::cell::PyRwLock;
use crossbeam_utils::atomic::AtomicCell;
use parking_lot::RwLock;
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Variant {
@@ -37,7 +37,7 @@ pub struct Coro {
frame: FrameRef,
pub closed: AtomicCell<bool>,
running: AtomicCell<bool>,
exceptions: RwLock<Vec<PyBaseExceptionRef>>,
exceptions: PyRwLock<Vec<PyBaseExceptionRef>>,
started: AtomicCell<bool>,
variant: Variant,
}
@@ -48,7 +48,7 @@ impl Coro {
frame,
closed: AtomicCell::new(false),
running: AtomicCell::new(false),
exceptions: RwLock::new(vec![]),
exceptions: PyRwLock::new(vec![]),
started: AtomicCell::new(false),
variant,
}
+3 -3
View File
@@ -1,4 +1,4 @@
use parking_lot::RwLock;
use crate::common::cell::PyRwLock;
use std::ops::AddAssign;
use num_bigint::BigInt;
@@ -14,7 +14,7 @@ use crate::vm::VirtualMachine;
#[pyclass]
#[derive(Debug)]
pub struct PyEnumerate {
counter: RwLock<BigInt>,
counter: PyRwLock<BigInt>,
iterator: PyObjectRef,
}
type PyEnumerateRef = PyRef<PyEnumerate>;
@@ -41,7 +41,7 @@ impl PyEnumerate {
let iterator = objiter::get_iter(vm, &iterable)?;
PyEnumerate {
counter: RwLock::new(counter),
counter: PyRwLock::new(counter),
iterator,
}
.into_ref_with_type(vm, cls)
+23 -207
View File
@@ -1,20 +1,19 @@
use num_bigint::{BigInt, ToBigInt};
use num_rational::Ratio;
use num_traits::{float::Float, pow, sign::Signed, ToPrimitive, Zero};
use num_traits::{pow, ToPrimitive, Zero};
use super::objbytes::PyBytes;
use super::objint::{self, PyInt, PyIntRef};
use super::objstr::{PyString, PyStringRef};
use super::objtype::PyClassRef;
use crate::exceptions::PyBaseExceptionRef;
use crate::format::FormatSpec;
use crate::function::{OptionalArg, OptionalOption};
use crate::pyhash;
use crate::pyobject::{
IntoPyObject, PyArithmaticValue::*, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef,
PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
};
use crate::vm::VirtualMachine;
use rustpython_common::{float_ops, hash};
/// Convert a string or number to a floating point number, if possible.
#[pyclass(name = "float")]
@@ -135,32 +134,6 @@ fn inner_divmod(v1: f64, v2: f64, vm: &VirtualMachine) -> PyResult<(f64, f64)> {
}
}
fn inner_lt_int(value: f64, other_int: &BigInt) -> bool {
match (value.to_bigint(), other_int.to_f64()) {
(Some(self_int), Some(other_float)) => value < other_float || self_int < *other_int,
// finite float, other_int too big for float,
// the result depends only on other_int’s sign
(Some(_), None) => other_int.is_positive(),
// infinite float must be bigger or lower than any int, depending on its sign
_ if value.is_infinite() => value.is_sign_negative(),
// NaN, always false
_ => false,
}
}
fn inner_gt_int(value: f64, other_int: &BigInt) -> bool {
match (value.to_bigint(), other_int.to_f64()) {
(Some(self_int), Some(other_float)) => value > other_float || self_int > *other_int,
// finite float, other_int too big for float,
// the result depends only on other_int’s sign
(Some(_), None) => other_int.is_negative(),
// infinite float must be bigger or lower than any int, depending on its sign
_ if value.is_infinite() => value.is_sign_positive(),
// NaN, always false
_ => false,
}
}
pub fn float_pow(v1: f64, v2: f64, vm: &VirtualMachine) -> PyResult<f64> {
if v1.is_zero() {
let msg = format!("{} cannot be raised to a negative power", v1);
@@ -170,14 +143,6 @@ pub fn float_pow(v1: f64, v2: f64, vm: &VirtualMachine) -> PyResult<f64> {
}
}
fn int_eq(value: f64, other: &BigInt) -> bool {
if let (Some(self_int), Some(other_float)) = (value.to_bigint(), other.to_f64()) {
value == other_float && self_int == *other
} else {
false
}
}
#[pyimpl(flags(BASETYPE))]
#[allow(clippy::trivially_copy_pass_by_ref)]
impl PyFloat {
@@ -227,7 +192,7 @@ impl PyFloat {
#[pymethod(name = "__eq__")]
fn eq(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue {
self.cmp(other, |a, b| a == b, int_eq, vm)
self.cmp(other, |a, b| a == b, float_ops::eq_int, vm)
}
#[pymethod(name = "__ne__")]
@@ -237,7 +202,7 @@ impl PyFloat {
#[pymethod(name = "__lt__")]
fn lt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue {
self.cmp(other, |a, b| a < b, inner_lt_int, vm)
self.cmp(other, |a, b| a < b, float_ops::lt_int, vm)
}
#[pymethod(name = "__le__")]
@@ -249,7 +214,7 @@ impl PyFloat {
if let (Some(a_int), Some(b_float)) = (a.to_bigint(), b.to_f64()) {
a <= b_float && a_int <= *b
} else {
inner_lt_int(a, b)
float_ops::lt_int(a, b)
}
},
vm,
@@ -258,7 +223,7 @@ impl PyFloat {
#[pymethod(name = "__gt__")]
fn gt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue {
self.cmp(other, |a, b| a > b, inner_gt_int, vm)
self.cmp(other, |a, b| a > b, float_ops::gt_int, vm)
}
#[pymethod(name = "__ge__")]
@@ -270,7 +235,7 @@ impl PyFloat {
if let (Some(a_int), Some(b_float)) = (a.to_bigint(), b.to_f64()) {
a >= b_float && a_int >= *b
} else {
inner_gt_int(a, b)
float_ops::gt_int(a, b)
}
},
vm,
@@ -386,23 +351,7 @@ impl PyFloat {
#[pymethod(name = "__repr__")]
fn repr(&self) -> String {
let value = format!("{:e}", self.value);
if let Some(position) = value.find('e') {
let significand = &value[..position];
let exponent = &value[position + 1..];
let exponent = exponent.parse::<i32>().unwrap();
if exponent < 16 && exponent > -5 {
if self.is_integer() {
format!("{:.1?}", self.value)
} else {
self.value.to_string()
}
} else {
format!("{}e{:+#03}", significand, exponent)
}
} else {
self.value.to_string()
}
float_ops::to_string(self.value)
}
#[pymethod(name = "__truediv__")]
@@ -499,8 +448,8 @@ impl PyFloat {
}
#[pymethod(name = "__hash__")]
fn hash(&self) -> pyhash::PyHash {
pyhash::hash_float(self.value)
fn hash(&self) -> hash::PyHash {
hash::hash_float(self.value)
}
#[pyproperty]
@@ -520,8 +469,7 @@ impl PyFloat {
#[pymethod(name = "is_integer")]
fn is_integer(&self) -> bool {
let v = self.value;
(v - v.round()).abs() < std::f64::EPSILON
float_ops::is_integer(self.value)
}
#[pymethod(name = "as_integer_ratio")]
@@ -544,116 +492,33 @@ impl PyFloat {
#[pymethod]
fn fromhex(repr: PyStringRef, vm: &VirtualMachine) -> PyResult<f64> {
hexf_parse::parse_hexf64(repr.as_str().trim(), false).or_else(|_| {
match repr.as_str().to_lowercase().trim() {
"nan" => Ok(std::f64::NAN),
"+nan" => Ok(std::f64::NAN),
"-nan" => Ok(std::f64::NAN),
"inf" => Ok(std::f64::INFINITY),
"infinity" => Ok(std::f64::INFINITY),
"+inf" => Ok(std::f64::INFINITY),
"+infinity" => Ok(std::f64::INFINITY),
"-inf" => Ok(std::f64::NEG_INFINITY),
"-infinity" => Ok(std::f64::NEG_INFINITY),
value => {
let mut hex = String::new();
let has_0x = value.contains("0x");
let has_p = value.contains('p');
let has_dot = value.contains('.');
let mut start = 0;
if !has_0x && value.starts_with('-') {
hex.push_str("-0x");
start += 1;
} else if !has_0x {
hex.push_str("0x");
if value.starts_with('+') {
start += 1;
}
}
for (index, ch) in value.chars().enumerate() {
if ch == 'p' && has_dot {
hex.push_str("p");
} else if ch == 'p' && !has_dot {
hex.push_str(".p");
} else if index >= start {
hex.push(ch);
}
}
if !has_p && has_dot {
hex.push_str("p0");
} else if !has_p && !has_dot {
hex.push_str(".p0")
}
hexf_parse::parse_hexf64(hex.as_str(), false).map_err(|_| {
vm.new_value_error("invalid hexadecimal floating-point string".to_owned())
})
}
}
float_ops::from_hex(repr.as_str().trim()).ok_or_else(|| {
vm.new_value_error("invalid hexadecimal floating-point string".to_owned())
})
}
#[pymethod]
fn hex(&self) -> String {
to_hex(self.value)
float_ops::to_hex(self.value)
}
}
fn str_to_float(vm: &VirtualMachine, literal: &str) -> PyResult<f64> {
if literal.starts_with('_') || literal.ends_with('_') {
return Err(invalid_convert(vm, literal));
}
let mut buf = String::with_capacity(literal.len());
let mut last_tok: Option<char> = None;
for c in literal.chars() {
if !(c.is_ascii_alphanumeric() || c == '_' || c == '+' || c == '-' || c == '.') {
return Err(invalid_convert(vm, literal));
}
if !c.is_ascii_alphanumeric() {
if let Some(l) = last_tok {
if !l.is_ascii_alphanumeric() && !(c == '.' && (l == '-' || l == '+')) {
return Err(invalid_convert(vm, literal));
}
}
}
if c != '_' {
buf.push(c);
}
last_tok = Some(c);
}
if let Ok(f) = lexical::parse(buf.as_str()) {
Ok(f)
} else {
Err(invalid_convert(vm, literal))
}
}
fn invalid_convert(vm: &VirtualMachine, literal: &str) -> PyBaseExceptionRef {
vm.new_value_error(format!("could not convert string to float: '{}'", literal))
}
fn to_float(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<f64> {
let value = if let Some(float) = obj.payload_if_subclass::<PyFloat>(vm) {
float.value
} else if let Some(int) = obj.payload_if_subclass::<PyInt>(vm) {
objint::try_float(int.as_bigint(), vm)?
} else if let Some(s) = obj.payload_if_subclass::<PyString>(vm) {
str_to_float(vm, s.as_str().trim())?
float_ops::parse_str(s.as_str().trim()).ok_or_else(|| {
vm.new_value_error(format!("could not convert string to float: '{}'", s))
})?
} else if let Some(bytes) = obj.payload_if_subclass::<PyBytes>(vm) {
match lexical::parse(bytes.get_value()) {
Ok(f) => f,
Err(_) => {
let arg_repr = vm.to_pystr(obj)?;
return Err(invalid_convert(vm, arg_repr.as_str()));
}
}
lexical::parse(bytes.get_value()).map_err(|_| {
vm.new_value_error(format!(
"could not convert string to float: '{}'",
bytes.repr()
))
})?
} else {
let method = vm.get_method_or_type_error(obj.clone(), "__float__", || {
format!(
@@ -667,55 +532,6 @@ fn to_float(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<f64> {
Ok(value)
}
fn to_hex(value: f64) -> String {
let (mantissa, exponent, sign) = value.integer_decode();
let sign_fmt = if sign < 0 { "-" } else { "" };
match value {
value if value.is_zero() => format!("{}0x0.0p+0", sign_fmt),
value if value.is_infinite() => format!("{}inf", sign_fmt),
value if value.is_nan() => "nan".to_owned(),
_ => {
const BITS: i16 = 52;
const FRACT_MASK: u64 = 0xf_ffff_ffff_ffff;
format!(
"{}0x{:x}.{:013x}p{:+}",
sign_fmt,
mantissa >> BITS,
mantissa & FRACT_MASK,
exponent + BITS
)
}
}
}
#[test]
fn test_to_hex() {
use rand::Rng;
for _ in 0..20000 {
let bytes = rand::thread_rng().gen::<[u64; 1]>();
let f = f64::from_bits(bytes[0]);
if !f.is_finite() {
continue;
}
let hex = to_hex(f);
// println!("{} -> {}", f, hex);
let roundtrip = hexf_parse::parse_hexf64(&hex, false).unwrap();
// println!(" -> {}", roundtrip);
assert!(f == roundtrip, "{} {} {}", f, hex, roundtrip);
}
}
pub fn ufrexp(value: f64) -> (f64, i32) {
if 0.0 == value {
(0.0, 0i32)
} else {
let bits = value.to_bits();
let exponent: i32 = ((bits >> 52) & 0x7ff) as i32 - 1022;
let mantissa_bits = bits & (0x000f_ffff_ffff_ffff) | (1022 << 52);
(f64::from_bits(mantissa_bits), exponent)
}
}
pub type PyFloatRef = PyRef<PyFloat>;
// Retrieve inner float value:
+3 -3
View File
@@ -15,13 +15,13 @@ use super::objstr::{PyString, PyStringRef};
use super::objtype::{self, PyClassRef};
use crate::format::FormatSpec;
use crate::function::{OptionalArg, PyFuncArgs};
use crate::pyhash;
use crate::pyobject::{
IdProtocol, IntoPyObject, PyArithmaticValue, PyClassImpl, PyComparisonValue, PyContext,
PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
};
use crate::stdlib::array::PyArray;
use crate::vm::VirtualMachine;
use rustpython_common::hash;
/// int(x=0) -> integer
/// int(x, base=10) -> integer
@@ -419,8 +419,8 @@ impl PyInt {
}
#[pymethod(name = "__hash__")]
pub fn hash(&self) -> pyhash::PyHash {
pyhash::hash_bigint(&self.value)
pub fn hash(&self) -> hash::PyHash {
hash::hash_bigint(&self.value)
}
#[pymethod(name = "__abs__")]
+12 -12
View File
@@ -2,10 +2,10 @@ use std::fmt;
use std::mem::size_of;
use std::ops::{DerefMut, Range};
use crate::common::cell::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard};
use crossbeam_utils::atomic::AtomicCell;
use num_bigint::{BigInt, ToBigInt};
use num_traits::{One, Signed, ToPrimitive, Zero};
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use super::objbool;
use super::objbyteinner;
@@ -29,7 +29,7 @@ use crate::vm::{ReprGuard, VirtualMachine};
#[pyclass]
#[derive(Default)]
pub struct PyList {
elements: RwLock<Vec<PyObjectRef>>,
elements: PyRwLock<Vec<PyObjectRef>>,
}
impl fmt::Debug for PyList {
@@ -42,7 +42,7 @@ impl fmt::Debug for PyList {
impl From<Vec<PyObjectRef>> for PyList {
fn from(elements: Vec<PyObjectRef>) -> Self {
PyList {
elements: RwLock::new(elements),
elements: PyRwLock::new(elements),
}
}
}
@@ -54,11 +54,11 @@ impl PyValue for PyList {
}
impl PyList {
pub fn borrow_elements(&self) -> RwLockReadGuard<'_, Vec<PyObjectRef>> {
pub fn borrow_elements(&self) -> PyRwLockReadGuard<'_, Vec<PyObjectRef>> {
self.elements.read()
}
pub fn borrow_elements_mut(&self) -> RwLockWriteGuard<'_, Vec<PyObjectRef>> {
pub fn borrow_elements_mut(&self) -> PyRwLockWriteGuard<'_, Vec<PyObjectRef>> {
self.elements.write()
}
@@ -315,7 +315,7 @@ impl PyList {
}
fn _set_slice(
mut elements: RwLockWriteGuard<'_, Vec<PyObjectRef>>,
mut elements: PyRwLockWriteGuard<'_, Vec<PyObjectRef>>,
range: Range<usize>,
items: Vec<PyObjectRef>,
vm: &VirtualMachine,
@@ -327,7 +327,7 @@ impl PyList {
}
fn _set_stepped_slice(
elements: RwLockWriteGuard<'_, Vec<PyObjectRef>>,
elements: PyRwLockWriteGuard<'_, Vec<PyObjectRef>>,
range: Range<usize>,
step: usize,
items: Vec<PyObjectRef>,
@@ -365,7 +365,7 @@ impl PyList {
}
fn _set_stepped_slice_reverse(
elements: RwLockWriteGuard<'_, Vec<PyObjectRef>>,
elements: PyRwLockWriteGuard<'_, Vec<PyObjectRef>>,
range: Range<usize>,
step: usize,
items: Vec<PyObjectRef>,
@@ -403,7 +403,7 @@ impl PyList {
}
fn _replace_indexes<I>(
mut elements: RwLockWriteGuard<'_, Vec<PyObjectRef>>,
mut elements: PyRwLockWriteGuard<'_, Vec<PyObjectRef>>,
indexes: I,
items: &[PyObjectRef],
) where
@@ -673,12 +673,12 @@ impl PyList {
}
}
fn _del_slice(mut elements: RwLockWriteGuard<'_, Vec<PyObjectRef>>, range: Range<usize>) {
fn _del_slice(mut elements: PyRwLockWriteGuard<'_, Vec<PyObjectRef>>, range: Range<usize>) {
elements.drain(range);
}
fn _del_stepped_slice(
mut elements: RwLockWriteGuard<'_, Vec<PyObjectRef>>,
mut elements: PyRwLockWriteGuard<'_, Vec<PyObjectRef>>,
range: Range<usize>,
step: usize,
) {
@@ -702,7 +702,7 @@ impl PyList {
}
fn _del_stepped_slice_reverse(
mut elements: RwLockWriteGuard<'_, Vec<PyObjectRef>>,
mut elements: PyRwLockWriteGuard<'_, Vec<PyObjectRef>>,
range: Range<usize>,
step: usize,
) {
+2 -3
View File
@@ -5,7 +5,6 @@ use super::objstr::PyStringRef;
use super::objtype::PyClassRef;
use crate::function::{OptionalArg, PyFuncArgs};
use crate::obj::objtype::PyClass;
use crate::pyhash;
use crate::pyobject::{
IdProtocol, ItemProtocol, PyArithmaticValue::*, PyAttributes, PyClassImpl, PyComparisonValue,
PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, TypeProtocol,
@@ -85,8 +84,8 @@ impl PyBaseObject {
}
#[pymethod(magic)]
fn hash(zelf: PyObjectRef) -> pyhash::PyHash {
zelf.get_id() as pyhash::PyHash
fn hash(zelf: PyObjectRef) -> rustpython_common::hash::PyHash {
zelf.get_id() as _
}
#[pymethod(magic)]
+6 -6
View File
@@ -1,7 +1,7 @@
/*! Python `property` descriptor class.
*/
use parking_lot::RwLock;
use crate::common::cell::PyRwLock;
use super::objtype::PyClassRef;
use crate::function::OptionalArg;
@@ -49,7 +49,7 @@ pub struct PyProperty {
getter: Option<PyObjectRef>,
setter: Option<PyObjectRef>,
deleter: Option<PyObjectRef>,
doc: RwLock<Option<PyObjectRef>>,
doc: PyRwLock<Option<PyObjectRef>>,
}
impl PyValue for PyProperty {
@@ -98,7 +98,7 @@ impl PyProperty {
getter: args.fget,
setter: args.fset,
deleter: args.fdel,
doc: RwLock::new(args.doc),
doc: PyRwLock::new(args.doc),
}
.into_ref_with_type(vm, cls)
}
@@ -160,7 +160,7 @@ impl PyProperty {
getter: getter.or_else(|| zelf.getter.clone()),
setter: zelf.setter.clone(),
deleter: zelf.deleter.clone(),
doc: RwLock::new(None),
doc: PyRwLock::new(None),
}
.into_ref_with_type(vm, TypeProtocol::class(&zelf))
}
@@ -175,7 +175,7 @@ impl PyProperty {
getter: zelf.getter.clone(),
setter: setter.or_else(|| zelf.setter.clone()),
deleter: zelf.deleter.clone(),
doc: RwLock::new(None),
doc: PyRwLock::new(None),
}
.into_ref_with_type(vm, TypeProtocol::class(&zelf))
}
@@ -190,7 +190,7 @@ impl PyProperty {
getter: zelf.getter.clone(),
setter: zelf.setter.clone(),
deleter: deleter.or_else(|| zelf.deleter.clone()),
doc: RwLock::new(None),
doc: PyRwLock::new(None),
}
.into_ref_with_type(vm, TypeProtocol::class(&zelf))
}
+5 -4
View File
@@ -10,11 +10,12 @@ use super::objtuple::PyTuple;
use super::objtype::PyClassRef;
use crate::function::{OptionalArg, PyFuncArgs};
use crate::pyhash;
use crate::pyobject::{
PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
self, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
TypeProtocol,
};
use crate::vm::VirtualMachine;
use rustpython_common::hash::PyHash;
/// range(stop) -> range object
/// range(start, stop[, step]) -> range object
@@ -363,7 +364,7 @@ impl PyRange {
}
#[pymethod(name = "__hash__")]
fn hash(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<pyhash::PyHash> {
fn hash(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {
let length = zelf.length();
let elements = if length.is_zero() {
vec![vm.ctx.new_int(length), vm.get_none(), vm.get_none()]
@@ -380,7 +381,7 @@ impl PyRange {
zelf.step().into_object(),
]
};
pyhash::hash_iter(elements.iter(), vm)
pyobject::hash_iter(&elements, vm)
}
#[pyslot]
+5 -5
View File
@@ -7,12 +7,12 @@ use super::objlist::PyListIterator;
use super::objtype::{self, PyClassRef};
use crate::dictdatatype;
use crate::function::{Args, OptionalArg};
use crate::pyhash;
use crate::pyobject::{
PyClassImpl, PyContext, PyIterable, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
self, PyClassImpl, PyContext, PyIterable, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
TypeProtocol,
};
use crate::vm::{ReprGuard, VirtualMachine};
use rustpython_common::hash::PyHash;
pub type SetContentType = dictdatatype::Dict<()>;
@@ -329,8 +329,8 @@ impl PySetInner {
Ok(())
}
fn hash(&self, vm: &VirtualMachine) -> PyResult<pyhash::PyHash> {
pyhash::hash_iter_unordered(self.content.keys().iter(), vm)
fn hash(&self, vm: &VirtualMachine) -> PyResult<PyHash> {
pyobject::hash_iter_unordered(self.content.keys().iter(), vm)
}
}
@@ -787,7 +787,7 @@ impl PyFrozenSet {
}
#[pymethod(name = "__hash__")]
fn hash(&self, vm: &VirtualMachine) -> PyResult<pyhash::PyHash> {
fn hash(&self, vm: &VirtualMachine) -> PyResult<PyHash> {
self.inner.hash(vm)
}
}
+4 -4
View File
@@ -24,12 +24,12 @@ use super::pystr::{
};
use crate::format::{FormatParseError, FormatSpec, FormatString, FromTemplate};
use crate::function::{OptionalArg, OptionalOption, PyFuncArgs};
use crate::pyhash;
use crate::pyobject::{
IdProtocol, IntoPyObject, ItemProtocol, PyClassImpl, PyContext, PyIterable, PyObjectRef, PyRef,
PyResult, PyValue, TryFromObject, TryIntoRef, TypeProtocol,
};
use crate::vm::VirtualMachine;
use rustpython_common::hash;
/// str(object='') -> str
/// str(bytes_or_buffer[, encoding[, errors]]) -> str
@@ -45,7 +45,7 @@ use crate::vm::VirtualMachine;
#[derive(Debug)]
pub struct PyString {
value: String,
hash: AtomicCell<Option<pyhash::PyHash>>,
hash: AtomicCell<Option<hash::PyHash>>,
len: AtomicCell<Option<usize>>,
}
@@ -280,9 +280,9 @@ impl PyString {
}
#[pymethod(name = "__hash__")]
pub(crate) fn hash(&self) -> pyhash::PyHash {
pub(crate) fn hash(&self) -> hash::PyHash {
self.hash.load().unwrap_or_else(|| {
let hash = pyhash::hash_value(&self.value);
let hash = hash::hash_value(&self.value);
self.hash.store(Some(hash));
hash
})
+4 -4
View File
@@ -5,14 +5,14 @@ use super::objiter;
use super::objsequence::get_item;
use super::objtype::PyClassRef;
use crate::function::OptionalArg;
use crate::pyhash;
use crate::pyobject::{
IntoPyObject,
self, IntoPyObject,
PyArithmaticValue::{self, *},
PyClassImpl, PyComparisonValue, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
};
use crate::sequence::{self, SimpleSeq};
use crate::vm::{ReprGuard, VirtualMachine};
use rustpython_common::hash::PyHash;
/// tuple() -> empty tuple
/// tuple(iterable) -> tuple initialized from iterable's items
@@ -156,8 +156,8 @@ impl PyTuple {
}
#[pymethod(name = "__hash__")]
fn hash(&self, vm: &VirtualMachine) -> PyResult<pyhash::PyHash> {
pyhash::hash_iter(self.elements.iter(), vm)
fn hash(&self, vm: &VirtualMachine) -> PyResult<PyHash> {
pyobject::hash_iter(self.elements.iter(), vm)
}
#[pymethod(name = "__iter__")]
+8 -8
View File
@@ -1,4 +1,4 @@
use parking_lot::RwLock;
use crate::common::cell::PyRwLock;
use std::collections::{HashMap, HashSet};
use std::fmt;
@@ -29,9 +29,9 @@ pub struct PyClass {
pub name: String,
pub bases: Vec<PyClassRef>,
pub mro: Vec<PyClassRef>,
pub subclasses: RwLock<Vec<PyWeak>>,
pub attributes: RwLock<PyAttributes>,
pub slots: RwLock<PyClassSlots>,
pub subclasses: PyRwLock<Vec<PyWeak>>,
pub attributes: PyRwLock<PyAttributes>,
pub slots: PyRwLock<PyClassSlots>,
}
impl fmt::Display for PyClass {
@@ -595,12 +595,12 @@ pub fn new(
name: String::from(name),
bases,
mro,
subclasses: RwLock::default(),
attributes: RwLock::new(dict),
slots: RwLock::default(),
subclasses: PyRwLock::default(),
attributes: PyRwLock::new(dict),
slots: PyRwLock::default(),
},
dict: None,
typ: RwLock::new(typ.into_typed_pyobj()),
typ: PyRwLock::new(typ.into_typed_pyobj()),
}
.into_ref();
+3 -3
View File
@@ -6,10 +6,10 @@ use crate::pyobject::{
};
use crate::slots::SlotCall;
use crate::vm::VirtualMachine;
use rustpython_common::hash::PyHash;
use rustpython_common::rc::{PyRc, PyWeak as Weak};
use crate::pyhash::PyHash;
use crossbeam_utils::atomic::AtomicCell;
use std::sync::{Arc, Weak};
#[pyclass]
#[derive(Debug)]
@@ -21,7 +21,7 @@ pub struct PyWeak {
impl PyWeak {
pub fn downgrade(obj: &PyObjectRef) -> PyWeak {
PyWeak {
referent: Arc::downgrade(obj),
referent: PyRc::downgrade(obj),
hash: AtomicCell::new(None),
}
}
+46 -22
View File
@@ -3,7 +3,6 @@ use std::collections::HashMap;
use std::fmt;
use std::marker::PhantomData;
use std::ops::Deref;
use std::sync::Arc;
use indexmap::IndexMap;
use num_bigint::BigInt;
@@ -40,7 +39,8 @@ use crate::scope::Scope;
use crate::slots::PyTpFlags;
use crate::types::{create_type, initialize_types, TypeZoo};
use crate::vm::VirtualMachine;
use parking_lot::{RwLock, RwLockReadGuard};
use rustpython_common::cell::{PyRwLock, PyRwLockReadGuard};
use rustpython_common::rc::PyRc;
/* Python objects and references.
@@ -61,7 +61,7 @@ Basically reference counting, but then done by rust.
/// this reference counting is accounted for by this type. Use the `.clone()`
/// method to create a new reference and increment the amount of references
/// to the python object by 1.
pub type PyObjectRef = Arc<PyObject<dyn PyObjectPayload>>;
pub type PyObjectRef = PyRc<PyObject<dyn PyObjectPayload>>;
/// Use this type for functions which return a python object or an exception.
/// Both the python object and the python exception are `PyObjectRef` types
@@ -586,8 +586,8 @@ impl PyContext {
pub fn new_base_object(&self, class: PyClassRef, dict: Option<PyDictRef>) -> PyObjectRef {
PyObject {
typ: RwLock::new(class.into_typed_pyobj()),
dict: dict.map(|d| RwLock::new(d.into_typed_pyobj())),
typ: PyRwLock::new(class.into_typed_pyobj()),
dict: dict.map(|d| PyRwLock::new(d.into_typed_pyobj())),
payload: objobject::PyBaseObject,
}
.into_ref()
@@ -643,8 +643,8 @@ pub struct PyObject<T>
where
T: ?Sized + PyObjectPayload,
{
pub(crate) typ: RwLock<Arc<PyObject<PyClass>>>, // __class__ member
pub(crate) dict: Option<RwLock<Arc<PyObject<PyDict>>>>, // __dict__ member
pub(crate) typ: PyRwLock<PyRc<PyObject<PyClass>>>, // __class__ member
pub(crate) dict: Option<PyRwLock<PyRc<PyObject<PyDict>>>>, // __dict__ member
pub payload: T,
}
@@ -653,7 +653,7 @@ impl PyObject<dyn PyObjectPayload> {
///
/// If the downcast fails, the original ref is returned in as `Err` so
/// another downcast can be attempted without unnecessary cloning.
pub fn downcast<T: PyObjectPayload>(self: Arc<Self>) -> Result<PyRef<T>, PyObjectRef> {
pub fn downcast<T: PyObjectPayload>(self: PyRc<Self>) -> Result<PyRef<T>, PyObjectRef> {
if self.payload_is::<T>() {
Ok(PyRef {
obj: self,
@@ -664,15 +664,15 @@ impl PyObject<dyn PyObjectPayload> {
}
}
/// Downcast this PyObjectRef to an `Arc<PyObject<T>>`. The [`downcast`](#method.downcast) method
/// Downcast this PyObjectRef to an `PyRc<PyObject<T>>`. The [`downcast`](#method.downcast) method
/// is generally preferred, as the `PyRef<T>` it returns implements `Deref<Target=T>`, and
/// therefore can be used similarly to an `&T`.
pub fn downcast_generic<T: PyObjectPayload>(
self: Arc<Self>,
) -> Result<Arc<PyObject<T>>, PyObjectRef> {
self: PyRc<Self>,
) -> Result<PyRc<PyObject<T>>, PyObjectRef> {
if self.payload_is::<T>() {
let ptr = Arc::into_raw(self) as *const PyObject<T>;
let ret = unsafe { Arc::from_raw(ptr) };
let ptr = PyRc::into_raw(self) as *const PyObject<T>;
let ret = unsafe { PyRc::from_raw(ptr) };
Ok(ret)
} else {
Err(self)
@@ -738,7 +738,7 @@ impl<T: PyValue> PyRef<T> {
self.obj
}
pub fn into_typed_pyobj(self) -> Arc<PyObject<T>> {
pub fn into_typed_pyobj(self) -> PyRc<PyObject<T>> {
self.into_object().downcast_generic().unwrap()
}
}
@@ -850,7 +850,7 @@ impl<T: ?Sized + PyObjectPayload> IdProtocol for PyObject<T> {
}
}
impl<T: ?Sized + IdProtocol> IdProtocol for Arc<T> {
impl<T: ?Sized + IdProtocol> IdProtocol for PyRc<T> {
fn get_id(&self) -> usize {
(**self).get_id()
}
@@ -877,7 +877,7 @@ impl<T: IdProtocol> IdProtocol for &'_ T {
/// A borrow of a reference to a Python object. This avoids having clone the `PyRef<T>`/
/// `PyObjectRef`, which isn't that cheap as that increments the atomic reference counter.
pub struct PyLease<'a, T: PyObjectPayload> {
inner: RwLockReadGuard<'a, Arc<PyObject<T>>>,
inner: PyRwLockReadGuard<'a, PyRc<PyObject<T>>>,
}
impl<'a, T: PyObjectPayload + PyValue> PyLease<'a, T> {
@@ -1212,8 +1212,8 @@ where
#[allow(clippy::new_ret_no_self)]
pub fn new(payload: T, typ: PyClassRef, dict: Option<PyDictRef>) -> PyObjectRef {
PyObject {
typ: RwLock::new(typ.into_typed_pyobj()),
dict: dict.map(|d| RwLock::new(d.into_typed_pyobj())),
typ: PyRwLock::new(typ.into_typed_pyobj()),
dict: dict.map(|d| PyRwLock::new(d.into_typed_pyobj())),
payload,
}
.into_ref()
@@ -1221,10 +1221,10 @@ where
// Move this object into a reference object, transferring ownership.
pub fn into_ref(self) -> PyObjectRef {
Arc::new(self)
PyRc::new(self)
}
pub fn into_pyref(self: Arc<Self>) -> PyRef<T>
pub fn into_pyref(self: PyRc<Self>) -> PyRef<T>
where
T: PyValue,
{
@@ -1276,7 +1276,17 @@ impl PyObject<dyn PyObjectPayload> {
}
}
pub trait PyValue: fmt::Debug + Send + Sync + Sized + 'static {
cfg_if::cfg_if! {
if #[cfg(feature = "threading")] {
pub trait PyThreadingConstraint: Send + Sync {}
impl<T: Send + Sync> PyThreadingConstraint for T {}
} else {
pub trait PyThreadingConstraint {}
impl<T> PyThreadingConstraint for T {}
}
}
pub trait PyValue: fmt::Debug + PyThreadingConstraint + Sized + 'static {
const HAVE_DICT: bool = false;
fn class(vm: &VirtualMachine) -> PyClassRef;
@@ -1306,7 +1316,7 @@ pub trait PyValue: fmt::Debug + Send + Sync + Sized + 'static {
}
}
pub trait PyObjectPayload: Any + fmt::Debug + Send + Sync + 'static {
pub trait PyObjectPayload: Any + fmt::Debug + PyThreadingConstraint + 'static {
fn as_any(&self) -> &dyn Any;
}
@@ -1452,6 +1462,20 @@ impl<T: TryFromObject> TryFromObject for PySequence<T> {
}
}
pub fn hash_iter<'a, I: IntoIterator<Item = &'a PyObjectRef>>(
iter: I,
vm: &VirtualMachine,
) -> PyResult<rustpython_common::hash::PyHash> {
rustpython_common::hash::hash_iter(iter, |obj| vm._hash(obj))
}
pub fn hash_iter_unordered<'a, I: IntoIterator<Item = &'a PyObjectRef>>(
iter: I,
vm: &VirtualMachine,
) -> PyResult<rustpython_common::hash::PyHash> {
rustpython_common::hash::hash_iter_unordered(iter, |obj| vm._hash(obj))
}
#[cfg(test)]
mod tests {
use super::*;
+5 -5
View File
@@ -10,7 +10,7 @@ use crate::pyobject::{
};
use crate::VirtualMachine;
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use crate::common::cell::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard};
use std::fmt;
use crossbeam_utils::atomic::AtomicCell;
@@ -239,7 +239,7 @@ def_array_enum!(
#[pyclass(name = "array")]
#[derive(Debug)]
pub struct PyArray {
array: RwLock<ArrayContentType>,
array: PyRwLock<ArrayContentType>,
}
pub type PyArrayRef = PyRef<PyArray>;
@@ -252,11 +252,11 @@ impl PyValue for PyArray {
#[pyimpl(flags(BASETYPE))]
impl PyArray {
fn borrow_value(&self) -> RwLockReadGuard<'_, ArrayContentType> {
fn borrow_value(&self) -> PyRwLockReadGuard<'_, ArrayContentType> {
self.array.read()
}
fn borrow_value_mut(&self) -> RwLockWriteGuard<'_, ArrayContentType> {
fn borrow_value_mut(&self) -> PyRwLockWriteGuard<'_, ArrayContentType> {
self.array.write()
}
@@ -278,7 +278,7 @@ impl PyArray {
let array =
ArrayContentType::from_char(spec).map_err(|err| vm.new_value_error(err.to_string()))?;
let zelf = PyArray {
array: RwLock::new(array),
array: PyRwLock::new(array),
};
if let OptionalArg::Present(init) = init {
zelf.extend(init, vm)?;
+10 -10
View File
@@ -2,6 +2,7 @@ pub(crate) use _collections::make_module;
#[pymodule]
mod _collections {
use crate::common::cell::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard};
use crate::function::OptionalArg;
use crate::obj::{objiter, objtype::PyClassRef};
use crate::pyobject::{
@@ -12,7 +13,6 @@ mod _collections {
use crate::vm::ReprGuard;
use crate::VirtualMachine;
use itertools::Itertools;
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::collections::VecDeque;
use crossbeam_utils::atomic::AtomicCell;
@@ -20,7 +20,7 @@ mod _collections {
#[pyclass(name = "deque")]
#[derive(Debug)]
struct PyDeque {
deque: RwLock<VecDeque<PyObjectRef>>,
deque: PyRwLock<VecDeque<PyObjectRef>>,
maxlen: AtomicCell<Option<usize>>,
}
type PyDequeRef = PyRef<PyDeque>;
@@ -38,16 +38,16 @@ mod _collections {
}
impl PyDeque {
fn borrow_deque(&self) -> RwLockReadGuard<'_, VecDeque<PyObjectRef>> {
fn borrow_deque(&self) -> PyRwLockReadGuard<'_, VecDeque<PyObjectRef>> {
self.deque.read()
}
fn borrow_deque_mut(&self) -> RwLockWriteGuard<'_, VecDeque<PyObjectRef>> {
fn borrow_deque_mut(&self) -> PyRwLockWriteGuard<'_, VecDeque<PyObjectRef>> {
self.deque.write()
}
}
struct SimpleSeqDeque<'a>(RwLockReadGuard<'a, VecDeque<PyObjectRef>>);
struct SimpleSeqDeque<'a>(PyRwLockReadGuard<'a, VecDeque<PyObjectRef>>);
impl sequence::SimpleSeq for SimpleSeqDeque<'_> {
fn len(&self) -> usize {
@@ -59,8 +59,8 @@ mod _collections {
}
}
impl<'a> From<RwLockReadGuard<'a, VecDeque<PyObjectRef>>> for SimpleSeqDeque<'a> {
fn from(from: RwLockReadGuard<'a, VecDeque<PyObjectRef>>) -> Self {
impl<'a> From<PyRwLockReadGuard<'a, VecDeque<PyObjectRef>>> for SimpleSeqDeque<'a> {
fn from(from: PyRwLockReadGuard<'a, VecDeque<PyObjectRef>>) -> Self {
Self { 0: from }
}
}
@@ -75,7 +75,7 @@ mod _collections {
vm: &VirtualMachine,
) -> PyResult<PyRef<Self>> {
let py_deque = PyDeque {
deque: RwLock::default(),
deque: PyRwLock::default(),
maxlen: AtomicCell::new(maxlen),
};
if let OptionalArg::Present(iter) = iter {
@@ -110,7 +110,7 @@ mod _collections {
#[pymethod]
fn copy(&self) -> Self {
PyDeque {
deque: RwLock::new(self.borrow_deque().clone()),
deque: PyRwLock::new(self.borrow_deque().clone()),
maxlen: AtomicCell::new(self.maxlen.load()),
}
}
@@ -383,7 +383,7 @@ mod _collections {
};
let deque = mul.skip(skipped).cloned().collect();
PyDeque {
deque: RwLock::new(deque),
deque: PyRwLock::new(deque),
maxlen: AtomicCell::new(self.maxlen.load()),
}
}
+3 -4
View File
@@ -1,11 +1,10 @@
use parking_lot::RwLock;
use std::fmt::{self, Debug, Formatter};
use csv as rust_csv;
use itertools::join;
use crate::common::cell::PyRwLock;
use crate::function::PyFuncArgs;
use crate::obj::objiter;
use crate::obj::objstr::{self, PyString};
use crate::obj::objtype::PyClassRef;
@@ -126,7 +125,7 @@ impl ReadState {
#[pyclass(name = "Reader")]
struct Reader {
state: RwLock<ReadState>,
state: PyRwLock<ReadState>,
}
impl Debug for Reader {
@@ -143,7 +142,7 @@ impl PyValue for Reader {
impl Reader {
fn new(iter: PyIterable<PyObjectRef>, config: ReaderOption) -> Self {
let state = RwLock::new(ReadState::new(iter, config));
let state = PyRwLock::new(ReadState::new(iter, config));
Reader { state }
}
}
+5 -5
View File
@@ -1,10 +1,10 @@
use crate::common::cell::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard};
use crate::function::{OptionalArg, PyFuncArgs};
use crate::obj::objbytes::{PyBytes, PyBytesRef};
use crate::obj::objstr::PyStringRef;
use crate::obj::objtype::PyClassRef;
use crate::pyobject::{PyClassImpl, PyObjectRef, PyResult, PyValue};
use crate::vm::VirtualMachine;
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::fmt;
use blake2::{Blake2b, Blake2s};
@@ -17,7 +17,7 @@ use sha3::{Sha3_224, Sha3_256, Sha3_384, Sha3_512}; // TODO: , Shake128, Shake25
#[pyclass(name = "hasher")]
struct PyHasher {
name: String,
buffer: RwLock<HashWrapper>,
buffer: PyRwLock<HashWrapper>,
}
impl fmt::Debug for PyHasher {
@@ -37,15 +37,15 @@ impl PyHasher {
fn new(name: &str, d: HashWrapper) -> Self {
PyHasher {
name: name.to_owned(),
buffer: RwLock::new(d),
buffer: PyRwLock::new(d),
}
}
fn borrow_value(&self) -> RwLockReadGuard<'_, HashWrapper> {
fn borrow_value(&self) -> PyRwLockReadGuard<'_, HashWrapper> {
self.buffer.read()
}
fn borrow_value_mut(&self) -> RwLockWriteGuard<'_, HashWrapper> {
fn borrow_value_mut(&self) -> PyRwLockWriteGuard<'_, HashWrapper> {
self.buffer.write()
}
+7 -7
View File
@@ -1,13 +1,13 @@
/*
* I/O core tools.
*/
use parking_lot::{RwLock, RwLockWriteGuard};
use std::fs;
use std::io::{self, prelude::*, Cursor, SeekFrom};
use crossbeam_utils::atomic::AtomicCell;
use num_traits::ToPrimitive;
use crate::common::cell::{PyRwLock, PyRwLockWriteGuard};
use crate::exceptions::PyBaseExceptionRef;
use crate::function::{Args, KwArgs, OptionalArg, OptionalOption, PyFuncArgs};
use crate::obj::objbool;
@@ -121,7 +121,7 @@ impl BufferedIO {
#[derive(Debug)]
struct PyStringIO {
buffer: RwLock<BufferedIO>,
buffer: PyRwLock<BufferedIO>,
closed: AtomicCell<bool>,
}
@@ -134,7 +134,7 @@ impl PyValue for PyStringIO {
}
impl PyStringIORef {
fn buffer(&self, vm: &VirtualMachine) -> PyResult<RwLockWriteGuard<'_, BufferedIO>> {
fn buffer(&self, vm: &VirtualMachine) -> PyResult<PyRwLockWriteGuard<'_, BufferedIO>> {
if !self.closed.load() {
Ok(self.buffer.write())
} else {
@@ -236,7 +236,7 @@ fn string_io_new(
let input = flatten.map_or_else(Vec::new, |v| objstr::borrow_value(&v).as_bytes().to_vec());
PyStringIO {
buffer: RwLock::new(BufferedIO::new(Cursor::new(input))),
buffer: PyRwLock::new(BufferedIO::new(Cursor::new(input))),
closed: AtomicCell::new(false),
}
.into_ref_with_type(vm, cls)
@@ -244,7 +244,7 @@ fn string_io_new(
#[derive(Debug)]
struct PyBytesIO {
buffer: RwLock<BufferedIO>,
buffer: PyRwLock<BufferedIO>,
closed: AtomicCell<bool>,
}
@@ -257,7 +257,7 @@ impl PyValue for PyBytesIO {
}
impl PyBytesIORef {
fn buffer(&self, vm: &VirtualMachine) -> PyResult<RwLockWriteGuard<'_, BufferedIO>> {
fn buffer(&self, vm: &VirtualMachine) -> PyResult<PyRwLockWriteGuard<'_, BufferedIO>> {
if !self.closed.load() {
Ok(self.buffer.write())
} else {
@@ -341,7 +341,7 @@ fn bytes_io_new(
};
PyBytesIO {
buffer: RwLock::new(BufferedIO::new(Cursor::new(raw_bytes))),
buffer: PyRwLock::new(BufferedIO::new(Cursor::new(raw_bytes))),
closed: AtomicCell::new(false),
}
.into_ref_with_type(vm, cls)
+35 -35
View File
@@ -5,10 +5,9 @@ mod decl {
use crossbeam_utils::atomic::AtomicCell;
use num_bigint::BigInt;
use num_traits::{One, Signed, ToPrimitive, Zero};
use parking_lot::{RwLock, RwLockWriteGuard};
use std::iter;
use std::sync::Arc;
use crate::common::cell::{PyRwLock, PyRwLockWriteGuard};
use crate::function::{Args, OptionalArg, OptionalOption, PyFuncArgs};
use crate::obj::objbool;
use crate::obj::objint::{self, PyInt, PyIntRef};
@@ -19,13 +18,14 @@ mod decl {
IdProtocol, PyCallable, PyClassImpl, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
};
use crate::vm::VirtualMachine;
use rustpython_common::rc::PyRc;
#[pyclass(name = "chain")]
#[derive(Debug)]
struct PyItertoolsChain {
iterables: Vec<PyObjectRef>,
cur_idx: AtomicCell<usize>,
cached_iter: RwLock<Option<PyObjectRef>>,
cached_iter: PyRwLock<Option<PyObjectRef>>,
}
impl PyValue for PyItertoolsChain {
@@ -41,7 +41,7 @@ mod decl {
PyItertoolsChain {
iterables: args.args,
cur_idx: AtomicCell::new(0),
cached_iter: RwLock::new(None),
cached_iter: PyRwLock::new(None),
}
.into_ref_with_type(vm, cls)
}
@@ -99,7 +99,7 @@ mod decl {
PyItertoolsChain {
iterables,
cur_idx: AtomicCell::new(0),
cached_iter: RwLock::new(None),
cached_iter: PyRwLock::new(None),
}
.into_ref_with_type(vm, cls)
}
@@ -159,7 +159,7 @@ mod decl {
#[pyclass(name = "count")]
#[derive(Debug)]
struct PyItertoolsCount {
cur: RwLock<BigInt>,
cur: PyRwLock<BigInt>,
step: BigInt,
}
@@ -188,7 +188,7 @@ mod decl {
};
PyItertoolsCount {
cur: RwLock::new(start),
cur: PyRwLock::new(start),
step,
}
.into_ref_with_type(vm, cls)
@@ -212,7 +212,7 @@ mod decl {
#[derive(Debug)]
struct PyItertoolsCycle {
iter: PyObjectRef,
saved: RwLock<Vec<PyObjectRef>>,
saved: PyRwLock<Vec<PyObjectRef>>,
index: AtomicCell<usize>,
}
@@ -234,7 +234,7 @@ mod decl {
PyItertoolsCycle {
iter: iter.clone(),
saved: RwLock::new(Vec::new()),
saved: PyRwLock::new(Vec::new()),
index: AtomicCell::new(0),
}
.into_ref_with_type(vm, cls)
@@ -273,7 +273,7 @@ mod decl {
#[derive(Debug)]
struct PyItertoolsRepeat {
object: PyObjectRef,
times: Option<RwLock<BigInt>>,
times: Option<PyRwLock<BigInt>>,
}
impl PyValue for PyItertoolsRepeat {
@@ -292,7 +292,7 @@ mod decl {
vm: &VirtualMachine,
) -> PyResult<PyRef<Self>> {
let times = match times.into_option() {
Some(int) => Some(RwLock::new(int.as_bigint().clone())),
Some(int) => Some(PyRwLock::new(int.as_bigint().clone())),
None => None,
};
@@ -675,7 +675,7 @@ mod decl {
struct PyItertoolsAccumulate {
iterable: PyObjectRef,
binop: PyObjectRef,
acc_value: RwLock<Option<PyObjectRef>>,
acc_value: PyRwLock<Option<PyObjectRef>>,
}
impl PyValue for PyItertoolsAccumulate {
@@ -698,7 +698,7 @@ mod decl {
PyItertoolsAccumulate {
iterable: iter,
binop: binop.unwrap_or_else(|| vm.get_none()),
acc_value: RwLock::new(None),
acc_value: PyRwLock::new(None),
}
.into_ref_with_type(vm, cls)
}
@@ -734,14 +734,14 @@ mod decl {
#[derive(Debug)]
struct PyItertoolsTeeData {
iterable: PyObjectRef,
values: RwLock<Vec<PyObjectRef>>,
values: PyRwLock<Vec<PyObjectRef>>,
}
impl PyItertoolsTeeData {
fn new(iterable: PyObjectRef, vm: &VirtualMachine) -> PyResult<Arc<PyItertoolsTeeData>> {
Ok(Arc::new(PyItertoolsTeeData {
fn new(iterable: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyRc<PyItertoolsTeeData>> {
Ok(PyRc::new(PyItertoolsTeeData {
iterable: get_iter(vm, &iterable)?,
values: RwLock::new(vec![]),
values: PyRwLock::new(vec![]),
}))
}
@@ -757,7 +757,7 @@ mod decl {
#[pyclass(name = "tee")]
#[derive(Debug)]
struct PyItertoolsTee {
tee_data: Arc<PyItertoolsTeeData>,
tee_data: PyRc<PyItertoolsTeeData>,
index: AtomicCell<usize>,
}
@@ -812,7 +812,7 @@ mod decl {
#[pymethod(name = "__copy__")]
fn copy(&self, vm: &VirtualMachine) -> PyResult {
Ok(PyItertoolsTee {
tee_data: Arc::clone(&self.tee_data),
tee_data: PyRc::clone(&self.tee_data),
index: AtomicCell::new(self.index.load()),
}
.into_ref_with_type(vm, Self::class(vm))?
@@ -836,7 +836,7 @@ mod decl {
#[derive(Debug)]
struct PyItertoolsProduct {
pools: Vec<Vec<PyObjectRef>>,
idxs: RwLock<Vec<usize>>,
idxs: PyRwLock<Vec<usize>>,
cur: AtomicCell<usize>,
stop: AtomicCell<bool>,
}
@@ -883,7 +883,7 @@ mod decl {
PyItertoolsProduct {
pools,
idxs: RwLock::new(vec![0; l]),
idxs: PyRwLock::new(vec![0; l]),
cur: AtomicCell::new(l - 1),
stop: AtomicCell::new(false),
}
@@ -920,7 +920,7 @@ mod decl {
Ok(res.into_ref(vm).into_object())
}
fn update_idxs(&self, mut idxs: RwLockWriteGuard<'_, Vec<usize>>) {
fn update_idxs(&self, mut idxs: PyRwLockWriteGuard<'_, Vec<usize>>) {
let cur = self.cur.load();
let lst_idx = &self.pools[cur].len() - 1;
@@ -948,7 +948,7 @@ mod decl {
#[derive(Debug)]
struct PyItertoolsCombinations {
pool: Vec<PyObjectRef>,
indices: RwLock<Vec<usize>>,
indices: PyRwLock<Vec<usize>>,
r: AtomicCell<usize>,
exhausted: AtomicCell<bool>,
}
@@ -981,7 +981,7 @@ mod decl {
PyItertoolsCombinations {
pool,
indices: RwLock::new((0..r).collect()),
indices: PyRwLock::new((0..r).collect()),
r: AtomicCell::new(r),
exhausted: AtomicCell::new(r > n),
}
@@ -1047,7 +1047,7 @@ mod decl {
#[derive(Debug)]
struct PyItertoolsCombinationsWithReplacement {
pool: Vec<PyObjectRef>,
indices: RwLock<Vec<usize>>,
indices: PyRwLock<Vec<usize>>,
r: AtomicCell<usize>,
exhausted: AtomicCell<bool>,
}
@@ -1080,7 +1080,7 @@ mod decl {
PyItertoolsCombinationsWithReplacement {
pool,
indices: RwLock::new(vec![0; r]),
indices: PyRwLock::new(vec![0; r]),
r: AtomicCell::new(r),
exhausted: AtomicCell::new(n == 0 && r > 0),
}
@@ -1140,12 +1140,12 @@ mod decl {
#[pyclass(name = "permutations")]
#[derive(Debug)]
struct PyItertoolsPermutations {
pool: Vec<PyObjectRef>, // Collected input iterable
indices: RwLock<Vec<usize>>, // One index per element in pool
cycles: RwLock<Vec<usize>>, // One rollover counter per element in the result
result: RwLock<Option<Vec<usize>>>, // Indexes of the most recently returned result
r: AtomicCell<usize>, // Size of result tuple
exhausted: AtomicCell<bool>, // Set when the iterator is exhausted
pool: Vec<PyObjectRef>, // Collected input iterable
indices: PyRwLock<Vec<usize>>, // One index per element in pool
cycles: PyRwLock<Vec<usize>>, // One rollover counter per element in the result
result: PyRwLock<Option<Vec<usize>>>, // Indexes of the most recently returned result
r: AtomicCell<usize>, // Size of result tuple
exhausted: AtomicCell<bool>, // Set when the iterator is exhausted
}
impl PyValue for PyItertoolsPermutations {
@@ -1186,9 +1186,9 @@ mod decl {
PyItertoolsPermutations {
pool,
indices: RwLock::new((0..n).collect()),
cycles: RwLock::new((0..r).map(|i| n - i).collect()),
result: RwLock::new(None),
indices: PyRwLock::new((0..n).collect()),
cycles: PyRwLock::new((0..r).map(|i| n - i).collect()),
result: PyRwLock::new(None),
r: AtomicCell::new(r),
exhausted: AtomicCell::new(r > n),
}
+2 -1
View File
@@ -15,6 +15,7 @@ use crate::obj::objint::{self, PyInt, PyIntRef};
use crate::obj::objtype;
use crate::pyobject::{Either, PyObjectRef, PyResult, TypeProtocol};
use crate::vm::VirtualMachine;
use rustpython_common::float_ops;
#[cfg(not(target_arch = "wasm32"))]
use libc::c_double;
@@ -253,7 +254,7 @@ fn math_floor(value: PyObjectRef, vm: &VirtualMachine) -> PyResult {
fn math_frexp(value: IntoPyFloat) -> (f64, i32) {
let value = value.to_f64();
if value.is_finite() {
let (m, e) = objfloat::ufrexp(value);
let (m, e) = float_ops::ufrexp(value);
(m * value.signum(), e)
} else {
(value, 0)
+2
View File
@@ -32,6 +32,7 @@ mod string;
mod symtable;
mod sysconfigdata;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "threading")]
mod thread;
mod time_module;
#[cfg(feature = "rustpython-parser")]
@@ -131,6 +132,7 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
modules.insert("select".to_owned(), Box::new(select::make_module));
#[cfg(feature = "ssl")]
modules.insert("_ssl".to_owned(), Box::new(ssl::make_module));
#[cfg(feature = "threading")]
modules.insert("_thread".to_owned(), Box::new(thread::make_module));
#[cfg(not(target_os = "redox"))]
modules.insert("zlib".to_owned(), Box::new(zlib::make_module));
+3 -3
View File
@@ -1,4 +1,3 @@
use parking_lot::RwLock;
use std::ffi;
use std::fs::File;
use std::fs::OpenOptions;
@@ -26,6 +25,7 @@ use std::os::unix::io::RawFd;
use std::os::windows::io::RawHandle;
use super::errno::errors;
use crate::common::cell::PyRwLock;
use crate::exceptions::PyBaseExceptionRef;
use crate::function::{IntoPyNativeFunc, OptionalArg, PyFuncArgs};
use crate::obj::objbyteinner::PyBytesLike;
@@ -726,7 +726,7 @@ impl DirEntryRef {
#[pyclass]
#[derive(Debug)]
struct ScandirIterator {
entries: RwLock<fs::ReadDir>,
entries: PyRwLock<fs::ReadDir>,
exhausted: AtomicCell<bool>,
mode: OutputMode,
}
@@ -791,7 +791,7 @@ fn os_scandir(path: OptionalArg<PyPathLike>, vm: &VirtualMachine) -> PyResult {
match fs::read_dir(path.path) {
Ok(iter) => Ok(ScandirIterator {
entries: RwLock::new(iter),
entries: PyRwLock::new(iter),
exhausted: AtomicCell::new(false),
mode: path.mode,
}
+3 -4
View File
@@ -4,6 +4,7 @@ pub(crate) use _random::make_module;
#[pymodule]
mod _random {
use crate::common::cell::PyMutex;
use crate::function::OptionalOption;
use crate::obj::objint::PyIntRef;
use crate::obj::objtype::PyClassRef;
@@ -13,8 +14,6 @@ mod _random {
use num_traits::Signed;
use rand::{rngs::StdRng, RngCore, SeedableRng};
use parking_lot::Mutex;
#[derive(Debug)]
enum PyRng {
Std(Box<StdRng>),
@@ -57,7 +56,7 @@ mod _random {
#[pyclass(name = "Random")]
#[derive(Debug)]
struct PyRandom {
rng: Mutex<PyRng>,
rng: PyMutex<PyRng>,
}
impl PyValue for PyRandom {
@@ -71,7 +70,7 @@ mod _random {
#[pyslot(new)]
fn new(cls: PyClassRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
PyRandom {
rng: Mutex::default(),
rng: PyMutex::default(),
}
.into_ref_with_type(vm, cls)
}
+5 -5
View File
@@ -1,4 +1,4 @@
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use crate::common::cell::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard};
use std::io::{self, prelude::*};
use std::net::{Ipv4Addr, Shutdown, SocketAddr, ToSocketAddrs};
use std::time::Duration;
@@ -62,7 +62,7 @@ pub struct PySocket {
kind: AtomicCell<i32>,
family: AtomicCell<i32>,
proto: AtomicCell<i32>,
sock: RwLock<Socket>,
sock: PyRwLock<Socket>,
}
impl PyValue for PySocket {
@@ -75,11 +75,11 @@ pub type PySocketRef = PyRef<PySocket>;
#[pyimpl(flags(BASETYPE))]
impl PySocket {
fn sock(&self) -> RwLockReadGuard<'_, Socket> {
fn sock(&self) -> PyRwLockReadGuard<'_, Socket> {
self.sock.read()
}
fn sock_mut(&self) -> RwLockWriteGuard<'_, Socket> {
fn sock_mut(&self) -> PyRwLockWriteGuard<'_, Socket> {
self.sock.write()
}
@@ -89,7 +89,7 @@ impl PySocket {
kind: AtomicCell::default(),
family: AtomicCell::default(),
proto: AtomicCell::default(),
sock: RwLock::new(invalid_sock()),
sock: PyRwLock::new(invalid_sock()),
}
.into_ref_with_type(vm, cls)
}
+8 -8
View File
@@ -11,7 +11,7 @@ use crate::pyobject::{
use crate::types::create_type;
use crate::VirtualMachine;
use parking_lot::{RwLock, RwLockWriteGuard};
use crate::common::cell::{PyRwLock, PyRwLockWriteGuard};
use std::convert::TryFrom;
use std::ffi::{CStr, CString};
use std::fmt;
@@ -230,7 +230,7 @@ fn ssl_rand_pseudo_bytes(n: i32, vm: &VirtualMachine) -> PyResult<(Vec<u8>, bool
#[pyclass(name = "_SSLContext")]
struct PySslContext {
ctx: RwLock<SslContextBuilder>,
ctx: PyRwLock<SslContextBuilder>,
check_hostname: bool,
}
@@ -248,7 +248,7 @@ impl PyValue for PySslContext {
#[pyimpl(flags(BASETYPE))]
impl PySslContext {
fn builder(&self) -> RwLockWriteGuard<'_, SslContextBuilder> {
fn builder(&self) -> PyRwLockWriteGuard<'_, SslContextBuilder> {
self.ctx.write()
}
fn exec_ctx<F, R>(&self, func: F) -> R
@@ -308,7 +308,7 @@ impl PySslContext {
.map_err(|e| convert_openssl_error(vm, e))?;
PySslContext {
ctx: RwLock::new(builder),
ctx: PyRwLock::new(builder),
check_hostname,
}
.into_ref_with_type(vm, cls)
@@ -471,10 +471,10 @@ impl PySslContext {
Ok(PySslSocket {
ctx: zelf,
stream: RwLock::new(Some(stream)),
stream: PyRwLock::new(Some(stream)),
socket_type,
server_hostname: args.server_hostname,
owner: RwLock::new(args.owner.as_ref().map(PyWeak::downgrade)),
owner: PyRwLock::new(args.owner.as_ref().map(PyWeak::downgrade)),
})
}
}
@@ -507,10 +507,10 @@ struct LoadVerifyLocationsArgs {
#[pyclass(name = "_SSLSocket")]
struct PySslSocket {
ctx: PyRef<PySslContext>,
stream: RwLock<Option<ssl::SslStreamBuilder<PySocketRef>>>,
stream: PyRwLock<Option<ssl::SslStreamBuilder<PySocketRef>>>,
socket_type: SslServerOrClient,
server_hostname: Option<PyStringRef>,
owner: RwLock<Option<PyWeak>>,
owner: PyRwLock<Option<PyWeak>>,
}
impl fmt::Debug for PySslSocket {
+2 -2
View File
@@ -7,10 +7,10 @@
use crate::pyobject::PyObjectRef;
use crate::vm::VirtualMachine;
use std::sync::Arc;
use rustpython_common::rc::PyRc;
fn weakref_getweakrefcount(obj: PyObjectRef) -> usize {
Arc::weak_count(&obj)
PyRc::weak_count(&obj)
}
fn weakref_getweakrefs(_obj: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
+5 -5
View File
@@ -1,5 +1,5 @@
#![allow(non_snake_case)]
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use crate::common::cell::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard};
use std::convert::TryInto;
use std::io;
@@ -16,7 +16,7 @@ use winreg::{enums::RegType, RegKey, RegValue};
#[pyclass]
#[derive(Debug)]
struct PyHKEY {
key: RwLock<RegKey>,
key: PyRwLock<RegKey>,
}
type PyHKEYRef = PyRef<PyHKEY>;
@@ -33,15 +33,15 @@ impl PyValue for PyHKEY {
impl PyHKEY {
fn new(key: RegKey) -> Self {
Self {
key: RwLock::new(key),
key: PyRwLock::new(key),
}
}
fn key(&self) -> RwLockReadGuard<'_, RegKey> {
fn key(&self) -> PyRwLockReadGuard<'_, RegKey> {
self.key.read()
}
fn key_mut(&self) -> RwLockWriteGuard<'_, RegKey> {
fn key_mut(&self) -> PyRwLockWriteGuard<'_, RegKey> {
self.key.write()
}
+9 -9
View File
@@ -1,3 +1,4 @@
use crate::common::cell::PyMutex;
use crate::exceptions::PyBaseExceptionRef;
use crate::function::OptionalArg;
use crate::obj::objbyteinner::PyBytesLike;
@@ -15,7 +16,6 @@ use flate2::{
};
use libz_sys as libz;
use parking_lot::Mutex;
use std::io::Write;
// copied from zlibmodule.c (commit 530f506ac91338)
@@ -159,19 +159,19 @@ fn zlib_decompressobj(
dict.with_ref(|d| decompress.set_dictionary(d).unwrap());
}
PyDecompress {
decompress: Mutex::new(decompress),
decompress: PyMutex::new(decompress),
eof: AtomicCell::new(false),
unused_data: Mutex::new(PyBytes::new(vec![]).into_ref(vm)),
unconsumed_tail: Mutex::new(PyBytes::new(vec![]).into_ref(vm)),
unused_data: PyMutex::new(PyBytes::new(vec![]).into_ref(vm)),
unconsumed_tail: PyMutex::new(PyBytes::new(vec![]).into_ref(vm)),
}
}
#[pyclass(name = "Decompress")]
#[derive(Debug)]
struct PyDecompress {
decompress: Mutex<Decompress>,
decompress: PyMutex<Decompress>,
eof: AtomicCell<bool>,
unused_data: Mutex<PyBytesRef>,
unconsumed_tail: Mutex<PyBytesRef>,
unused_data: PyMutex<PyBytesRef>,
unconsumed_tail: PyMutex<PyBytesRef>,
}
impl PyValue for PyDecompress {
fn class(vm: &VirtualMachine) -> PyClassRef {
@@ -359,7 +359,7 @@ fn zlib_compressobj(
};
let compress = Compress::new_with_window_bits(Compression::new(level), header, wbits);
Ok(PyCompress {
inner: Mutex::new(CompressInner {
inner: PyMutex::new(CompressInner {
compress,
unconsumed: Vec::new(),
}),
@@ -375,7 +375,7 @@ struct CompressInner {
#[pyclass]
#[derive(Debug)]
struct PyCompress {
inner: Mutex<CompressInner>,
inner: PyMutex<CompressInner>,
}
impl PyValue for PyCompress {
+31 -3
View File
@@ -1,16 +1,16 @@
use std::sync::Arc;
use std::{env, mem};
use crate::builtins;
use crate::frame::FrameRef;
use crate::function::{Args, OptionalArg, PyFuncArgs};
use crate::obj::objstr::PyStringRef;
use crate::pyhash::PyHashInfo;
use crate::pyobject::{
IntoPyObject, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyResult, TypeProtocol,
};
use crate::version;
use crate::vm::{PySettings, VirtualMachine};
use rustpython_common::hash::{PyHash, PyUHash};
use rustpython_common::rc::PyRc;
/*
* The magic sys module.
@@ -110,7 +110,7 @@ impl SysFlags {
}
fn sys_getrefcount(obj: PyObjectRef) -> usize {
Arc::strong_count(&obj)
PyRc::strong_count(&obj)
}
fn sys_getsizeof(obj: PyObjectRef) -> usize {
@@ -257,6 +257,34 @@ pub fn sysconfigdata_name() -> String {
format!("_sysconfigdata_{}_{}_{}", ABIFLAGS, PLATFORM, MULTIARCH)
}
#[pystruct_sequence(module = "sys", name = "hash_info")]
#[derive(Debug)]
struct PyHashInfo {
width: usize,
modulus: PyUHash,
inf: PyHash,
nan: PyHash,
imag: PyHash,
algorithm: &'static str,
hash_bits: usize,
seed_bits: usize,
}
impl PyHashInfo {
pub const INFO: Self = {
use rustpython_common::hash::*;
PyHashInfo {
width: BITS,
modulus: MODULUS,
inf: INF,
nan: NAN,
imag: IMAG,
algorithm: ALGO,
hash_bits: HASH_BITS,
seed_bits: SEED_BITS,
}
};
}
pub fn make_module(vm: &VirtualMachine, module: PyObjectRef, builtins: PyObjectRef) {
let ctx = &vm.ctx;
+35 -31
View File
@@ -40,10 +40,9 @@ use crate::obj::objweakproxy;
use crate::obj::objweakref;
use crate::obj::objzip;
use crate::pyobject::{PyAttributes, PyContext, PyObject};
use parking_lot::RwLock;
use rustpython_common::{cell::PyRwLock, rc::PyRc};
use std::mem::MaybeUninit;
use std::ptr;
use std::sync::Arc;
/// Holder of references to builtin types.
#[derive(Debug)]
@@ -275,14 +274,16 @@ macro_rules! partially_init {
) => {{
// check all the fields are there but *don't* actually run it
if false {
#[allow(invalid_value)]
#[allow(invalid_value, dead_code, unreachable_code)]
let _ = {$ty {
$($init_field: $init_value,)*
$($uninit_field: ::std::mem::MaybeUninit::uninit().assume_init(),)*
$($uninit_field: unreachable!(),)*
}};
}
let mut m = ::std::mem::MaybeUninit::<$ty>::uninit();
$(::std::ptr::write(&mut (*m.as_mut_ptr()).$init_field, $init_value);)*
unsafe {
$(::std::ptr::write(&mut (*m.as_mut_ptr()).$init_field, $init_value);)*
}
m
}};
}
@@ -292,61 +293,64 @@ fn init_type_hierarchy() -> (PyClassRef, PyClassRef) {
// and both `type` and `object are instances of `type`.
// to produce this circular dependency, we need an unsafe block.
// (and yes, this will never get dropped. TODO?)
let (type_type, object_type) = unsafe {
let (type_type, object_type) = {
type PyClassObj = PyObject<PyClass>;
type UninitRef<T> = RwLock<Arc<MaybeUninit<T>>>;
type UninitRef<T> = PyRwLock<PyRc<MaybeUninit<T>>>;
let type_type: Arc<MaybeUninit<PyClassObj>> = Arc::new(partially_init!(
let type_type: PyRc<MaybeUninit<PyClassObj>> = PyRc::new(partially_init!(
PyObject::<PyClass> {
dict: None,
payload: PyClass {
name: String::from("type"),
bases: vec![],
mro: vec![],
subclasses: RwLock::default(),
attributes: RwLock::new(PyAttributes::new()),
slots: RwLock::default(),
subclasses: PyRwLock::default(),
attributes: PyRwLock::new(PyAttributes::new()),
slots: PyRwLock::default(),
},
},
Uninit { typ }
));
let object_type: Arc<MaybeUninit<PyClassObj>> = Arc::new(partially_init!(
let object_type: PyRc<MaybeUninit<PyClassObj>> = PyRc::new(partially_init!(
PyObject::<PyClass> {
dict: None,
payload: PyClass {
name: String::from("object"),
bases: vec![],
mro: vec![],
subclasses: RwLock::default(),
attributes: RwLock::new(PyAttributes::new()),
slots: RwLock::default(),
subclasses: PyRwLock::default(),
attributes: PyRwLock::new(PyAttributes::new()),
slots: PyRwLock::default(),
},
},
Uninit { typ },
));
let object_type_ptr =
Arc::into_raw(object_type) as *mut MaybeUninit<PyClassObj> as *mut PyClassObj;
PyRc::into_raw(object_type) as *mut MaybeUninit<PyClassObj> as *mut PyClassObj;
let type_type_ptr =
Arc::into_raw(type_type.clone()) as *mut MaybeUninit<PyClassObj> as *mut PyClassObj;
PyRc::into_raw(type_type.clone()) as *mut MaybeUninit<PyClassObj> as *mut PyClassObj;
ptr::write(
&mut (*object_type_ptr).typ as *mut RwLock<Arc<PyClassObj>>
as *mut UninitRef<PyClassObj>,
RwLock::new(type_type.clone()),
);
ptr::write(
&mut (*type_type_ptr).typ as *mut RwLock<Arc<PyClassObj>> as *mut UninitRef<PyClassObj>,
RwLock::new(type_type),
);
unsafe {
ptr::write(
&mut (*object_type_ptr).typ as *mut PyRwLock<PyRc<PyClassObj>>
as *mut UninitRef<PyClassObj>,
PyRwLock::new(type_type.clone()),
);
ptr::write(
&mut (*type_type_ptr).typ as *mut PyRwLock<PyRc<PyClassObj>>
as *mut UninitRef<PyClassObj>,
PyRwLock::new(type_type),
);
let type_type = PyClassRef::from_obj_unchecked(Arc::from_raw(type_type_ptr));
let object_type = PyClassRef::from_obj_unchecked(Arc::from_raw(object_type_ptr));
let type_type = PyClassRef::from_obj_unchecked(PyRc::from_raw(type_type_ptr));
let object_type = PyClassRef::from_obj_unchecked(PyRc::from_raw(object_type_ptr));
(*type_type_ptr).payload.mro = vec![object_type.clone()];
(*type_type_ptr).payload.bases = vec![object_type.clone()];
(*type_type_ptr).payload.mro = vec![object_type.clone()];
(*type_type_ptr).payload.bases = vec![object_type.clone()];
(type_type, object_type)
(type_type, object_type)
}
};
object_type
+8 -8
View File
@@ -7,7 +7,6 @@
use std::cell::{Cell, Ref, RefCell};
use std::collections::hash_map::HashMap;
use std::collections::hash_set::HashSet;
use std::sync::Arc;
use std::{env, fmt};
use arr_macro::arr;
@@ -40,7 +39,6 @@ use crate::obj::objobject;
use crate::obj::objstr::{PyString, PyStringRef};
use crate::obj::objtuple::PyTuple;
use crate::obj::objtype::{self, PyClassRef};
use crate::pyhash;
use crate::pyobject::{
IdProtocol, ItemProtocol, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject,
TryIntoRef, TypeProtocol,
@@ -48,6 +46,7 @@ use crate::pyobject::{
use crate::scope::Scope;
use crate::stdlib;
use crate::sysmodule;
use rustpython_common::rc::PyRc;
// use objects::objects;
@@ -58,7 +57,7 @@ use crate::sysmodule;
pub struct VirtualMachine {
pub builtins: PyObjectRef,
pub sys_module: PyObjectRef,
pub ctx: Arc<PyContext>,
pub ctx: PyRc<PyContext>,
pub frames: RefCell<Vec<FrameRef>>,
pub wasm_id: Option<String>,
pub exceptions: RefCell<Vec<PyBaseExceptionRef>>,
@@ -68,7 +67,7 @@ pub struct VirtualMachine {
pub use_tracing: Cell<bool>,
pub recursion_limit: Cell<usize>,
pub signal_handlers: Option<Box<RefCell<[PyObjectRef; NSIG]>>>,
pub state: Arc<PyGlobalState>,
pub state: PyRc<PyGlobalState>,
pub initialized: bool,
}
@@ -194,7 +193,7 @@ impl VirtualMachine {
let mut vm = VirtualMachine {
builtins: builtins.clone(),
sys_module: sysmod.clone(),
ctx: Arc::new(ctx),
ctx: PyRc::new(ctx),
frames: RefCell::new(vec![]),
wasm_id: None,
exceptions: RefCell::new(vec![]),
@@ -204,7 +203,7 @@ impl VirtualMachine {
use_tracing: Cell::new(false),
recursion_limit: Cell::new(if cfg!(debug_assertions) { 256 } else { 512 }),
signal_handlers: Some(Box::new(signal_handlers)),
state: Arc::new(PyGlobalState {
state: PyRc::new(PyGlobalState {
settings,
stdlib_inits,
frozen,
@@ -288,6 +287,7 @@ impl VirtualMachine {
}
}
#[cfg(feature = "threading")]
pub(crate) fn new_thread(&self) -> VirtualMachine {
VirtualMachine {
builtins: self.builtins.clone(),
@@ -753,7 +753,7 @@ impl VirtualMachine {
pub fn isinstance(&self, obj: &PyObjectRef, cls: &PyClassRef) -> PyResult<bool> {
// cpython first does an exact check on the type, although documentation doesn't state that
// https://github.com/python/cpython/blob/a24107b04c1277e3c1105f98aff5bfa3a98b33a0/Objects/abstract.c#L2408
if Arc::ptr_eq(&obj.class().into_object(), cls.as_object()) {
if PyRc::ptr_eq(&obj.class().into_object(), cls.as_object()) {
Ok(true)
} else {
let ret = self.call_method(cls.as_object(), "__instancecheck__", vec![obj.clone()])?;
@@ -1406,7 +1406,7 @@ impl VirtualMachine {
})
}
pub fn _hash(&self, obj: &PyObjectRef) -> PyResult<pyhash::PyHash> {
pub fn _hash(&self, obj: &PyObjectRef) -> PyResult<rustpython_common::hash::PyHash> {
let hash_obj = self.call_method(obj, "__hash__", vec![])?;
if let Some(hash_value) = hash_obj.payload_if_subclass::<PyInt>(self) {
Ok(hash_value.hash())
-1019
View File
File diff suppressed because it is too large. Load diff
+4 -1
View File
@@ -17,7 +17,8 @@ freeze-stdlib = ["rustpython-vm/freeze-stdlib"]
[dependencies]
rustpython-compiler = { path = "../../compiler" }
rustpython-parser = { path = "../../parser" }
rustpython-vm = { path = "../../vm" }
# no threading feature for rustpython-vm -- doesn't much matter anyway, but it might be more optimized
rustpython-vm = { path = "../../vm", default-features = false, features = ["compile-parse"] }
cfg-if = "0.1.2"
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.3"
@@ -26,6 +27,8 @@ serde = "1.0"
js-sys = "0.3"
futures = "0.1"
generational-arena = "0.2"
# make parking_lot use wasm-bingden for instant
parking_lot = { version = "0.11", features = ["wasm-bindgen"] }
[dependencies.web-sys]
version = "0.3"
+2 -2
View File
@@ -1,10 +1,10 @@
use futures::Future;
use js_sys::Promise;
use std::sync::Arc;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::{future_to_promise, JsFuture};
use rustpython_vm::common::rc::PyRc;
use rustpython_vm::function::{OptionalArg, PyFuncArgs};
use rustpython_vm::import::import_file;
use rustpython_vm::obj::{objdict::PyDictRef, objstr::PyStringRef, objtype::PyClassRef};
@@ -377,7 +377,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
}
pub fn setup_browser_module(vm: &mut VirtualMachine) {
let state = Arc::get_mut(&mut vm.state).unwrap();
let state = PyRc::get_mut(&mut vm.state).unwrap();
state
.stdlib_inits
.insert("_browser".to_owned(), Box::new(make_module));
+2 -2
View File
@@ -1,11 +1,11 @@
use js_sys::{Array, Object, Reflect};
use rustpython_vm::common::rc::PyRc;
use rustpython_vm::exceptions::PyBaseExceptionRef;
use rustpython_vm::function::Args;
use rustpython_vm::obj::{objfloat::PyFloatRef, objstr::PyStringRef, objtype::PyClassRef};
use rustpython_vm::pyobject::{PyClassImpl, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject};
use rustpython_vm::types::create_type;
use rustpython_vm::VirtualMachine;
use std::sync::Arc;
use wasm_bindgen::{prelude::*, JsCast};
#[wasm_bindgen(inline_js = "
@@ -259,7 +259,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
}
pub fn setup_js_module(vm: &mut VirtualMachine) {
let state = Arc::get_mut(&mut vm.state).unwrap();
let state = PyRc::get_mut(&mut vm.state).unwrap();
state
.stdlib_inits
.insert("_js".to_owned(), Box::new(make_module));
+4 -4
View File
@@ -1,12 +1,12 @@
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::{Rc, Weak};
use std::sync::Arc;
use js_sys::{Object, TypeError};
use wasm_bindgen::prelude::*;
use rustpython_compiler::compile;
use rustpython_vm::common::rc::{PyRc, PyWeak};
use rustpython_vm::pyobject::{ItemProtocol, PyObject, PyObjectPayload, PyObjectRef, PyValue};
use rustpython_vm::scope::{NameProtocol, Scope};
use rustpython_vm::{InitParameter, PySettings, VirtualMachine};
@@ -39,7 +39,7 @@ impl StoredVirtualMachine {
js_module::setup_js_module(&mut vm);
if inject_browser_module {
Arc::get_mut(&mut vm.state).unwrap().stdlib_inits.insert(
PyRc::get_mut(&mut vm.state).unwrap().stdlib_inits.insert(
"_window".to_owned(),
Box::new(|vm| {
py_module!(vm, "_window", {
@@ -177,9 +177,9 @@ impl WASMVirtualMachine {
pub(crate) fn push_held_rc(
&self,
obj: PyObjectRef,
) -> Result<std::sync::Weak<PyObject<dyn PyObjectPayload>>, JsValue> {
) -> Result<PyWeak<PyObject<dyn PyObjectPayload>>, JsValue> {
self.with(|stored_vm| {
let weak = std::sync::Arc::downgrade(&obj);
let weak = PyRc::downgrade(&obj);
stored_vm.held_objects.borrow_mut().push(obj);
weak
})