def test_big_int_conversions3(selenium_module_scope, n, exp):
    @run_in_pyodide
    def main(selenium, s):
        import json

        from pyodide import run_js

        x_py = json.loads(s)
        run_js(f"""
            self.x_js = eval('{s}n'); // JSON.parse apparently doesn't work
            """)
        [x1, x2] = run_js("""
            (x_py) => [x_py.toString(), x_js.toString()]
            """)(x_py)
        assert x1 == x2
        from js import x_js

        check = run_js("""
            (x) => {
                const [a, b] = x.toJs();
                return a === b;
            }
            """)([str(x_js), str(x_py)])
        assert check

    with selenium_context_manager(selenium_module_scope) as selenium:
        val = 2**(32 * exp) - n
        import json

        s = json.dumps(val)
        main(selenium, s)
def test_bigint_conversions(selenium_module_scope, n):
    with selenium_context_manager(selenium_module_scope) as selenium:
        h = hex(n)
        selenium.run_js(f"self.h = {h!r};")
        selenium.run_js("""
            let negative = false;
            let h2 = h;
            if(h2.startsWith('-')){
                h2 = h2.slice(1);
                negative = true;
            }
            self.n = BigInt(h2);
            if(negative){
                self.n = -n;
            }
            pyodide.runPython(`
                from js import n, h
                n2 = int(h, 16)
                assert n == n2
            `);
            let n2 = pyodide.globals.get("n2");
            let n3 = Number(n2);
            if(Number.isSafeInteger(n3)){
                assert(() => typeof n2 === "number");
                assert(() => n2 === Number(n));
            } else {
                assert(() => typeof n2 === "bigint");
                assert(() => n2 === n);
            }
            """)
Esempio n. 3
0
def test_fernet(selenium_module_scope, data):
    sbytes = list(data)
    with selenium_context_manager(selenium_module_scope) as selenium:
        selenium.load_package("cryptography")
        selenium.run(f"""
            from cryptography.fernet import Fernet
            data = bytes({sbytes})
            f = Fernet(Fernet.generate_key())
            ct = f.encrypt(data)
            assert f.decrypt(ct) == data
            """)
Esempio n. 4
0
def test_logistic_regression(selenium_module_scope):
    with selenium_context_manager(selenium_module_scope) as selenium:
        selenium.load_package("scikit-learn")
        selenium.run("""
            from sklearn.datasets import load_iris
            from sklearn.linear_model import LogisticRegression
            X, y = load_iris(return_X_y=True)
            clf = LogisticRegression(random_state=0).fit(X, y)
            print(clf.predict(X[:2, :]))
            print(clf.predict_proba(X[:2, :]))
            print(clf.score(X, y))
            """)
Esempio n. 5
0
def test_ansix923(selenium_module_scope, block_size, data):
    sbytes = list(data)
    with selenium_context_manager(selenium_module_scope) as selenium:
        selenium.load_package("cryptography")
        selenium.run(f"""
            from cryptography.hazmat.primitives.padding import ANSIX923, PKCS7
            block_size = {block_size}
            data = bytes({sbytes})
            a = ANSIX923(block_size=block_size * 8)
            padder = a.padder()
            unpadder = a.unpadder()

            padded = padder.update(data) + padder.finalize()

            assert unpadder.update(padded) + unpadder.finalize() == data
            """)
Esempio n. 6
0
def test_scikit_learn(selenium_module_scope):
    with selenium_context_manager(selenium_module_scope) as selenium:
        selenium.load_package("scikit-learn")
        assert (selenium.run("""
                import numpy as np
                import sklearn
                from sklearn.linear_model import LogisticRegression

                rng = np.random.RandomState(42)
                X = rng.rand(100, 20)
                y = rng.randint(5, size=100)

                estimator = LogisticRegression(solver='liblinear')
                estimator.fit(X, y)
                print(estimator.predict(X))
                estimator.score(X, y)
                """) > 0)
Esempio n. 7
0
def test_pkcs7(selenium_module_scope, block_size, data):
    sbytes = list(data)
    with selenium_context_manager(selenium_module_scope) as selenium:
        selenium.load_package("cryptography")
        selenium.run(f"""
            from cryptography.hazmat.primitives.padding import ANSIX923, PKCS7
            block_size = {block_size}
            data = bytes({sbytes})
            # Generate in [1, 31] so we can easily get block_size in bits by
            # multiplying by 8.
            p = PKCS7(block_size=block_size * 8)
            padder = p.padder()
            unpadder = p.unpadder()

            padded = padder.update(data) + padder.finalize()

            assert unpadder.update(padded) + unpadder.finalize() == data
            """)
def test_number_conversions(selenium_module_scope, n):
    with selenium_context_manager(selenium_module_scope) as selenium:
        import json

        s = json.dumps(n)
        selenium.run_js(f"""
            self.x_js = eval({s!r}); // JSON.parse apparently doesn't work
            pyodide.runPython(`
                import json
                x_py = json.loads({s!r})
            `);
            """)
        assert selenium.run_js(
            """return pyodide.runPython('x_py') === x_js;""")
        assert selenium.run("""
            from js import x_js
            x_js == x_py
            """)
def test_string_conversion(selenium_module_scope, s):
    @run_in_pyodide
    def main(selenium, sbytes):
        from pyodide import run_js

        spy = bytes(sbytes).decode()
        sjs = run_js("""
            (sbytes) => {
                self.sjs = (new TextDecoder("utf8")).decode(new Uint8Array(sbytes));
                return sjs;
            }
            """)(sbytes)
        assert sjs == spy
        assert run_js("(spy) => spy === self.sjs")(spy)

    with selenium_context_manager(selenium_module_scope) as selenium:
        sbytes = list(s.encode())
        main(selenium, sbytes)
def test_big_int_conversions2(selenium_module_scope, n):
    @run_in_pyodide
    def main(selenium, s):
        import json

        from pyodide import run_js

        x_py = json.loads(s)
        x_js, check = run_js("""
            (s, x_py) => {
                let x_js = eval(s + 'n');

                return [x_js, x_py === x_js];
            }
            """)(s, x_py)
        assert check
        assert x_js == x_py

    with selenium_context_manager(selenium_module_scope) as selenium:
        import json

        s = json.dumps(n)
        main(selenium, s)