Skip to content

Commit

Permalink
Restyled by yapf
Browse files Browse the repository at this point in the history
  • Loading branch information
restyled-commits committed Jan 8, 2024
1 parent 9fe8b94 commit 75d985a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 32 deletions.
3 changes: 1 addition & 2 deletions pytox/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@ def check_len(name: str, data: T, expected_length: int) -> T:
if len(data) < expected_length:
raise LengthException(
f"parameter '{name}' received bytes of invalid"
f"length {len(data)}, expected at least {expected_length}"
)
f"length {len(data)}, expected at least {expected_length}")
return data
18 changes: 8 additions & 10 deletions test/auto_tests/self_connection_status_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class TestException(Exception):
class TestTox(core.Tox_Ptr):
connection_status_from_cb = core.TOX_CONNECTION_NONE

def handle_self_connection_status(
self, connection_status: core.Tox_Connection
) -> None:
def handle_self_connection_status(self,
connection_status: core.Tox_Connection
) -> None:
print(connection_status)
self.connection_status_from_cb = connection_status
raise TestException(connection_status)
Expand All @@ -27,16 +27,14 @@ def test_connection_status_cb(self) -> None:
with TestTox() as tox2:
# Test that exceptions can pass through C code.
with self.assertRaises(TestException) as ex:
while (
tox1.connection_status == core.TOX_CONNECTION_NONE
or tox2.connection_status == core.TOX_CONNECTION_NONE
):
while (tox1.connection_status == core.TOX_CONNECTION_NONE
or
tox2.connection_status == core.TOX_CONNECTION_NONE):
tox1.iterate()
tox2.iterate()
time.sleep(tox1.iteration_interval / 1000)
self.assertEqual(
tox1.connection_status, tox1.connection_status_from_cb
)
self.assertEqual(tox1.connection_status,
tox1.connection_status_from_cb)
self.assertEqual(ex.exception.status, tox1.connection_status)


Expand Down
13 changes: 6 additions & 7 deletions test/toxencryptsave_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@ class ToxencryptsaveTest(unittest.TestCase):
def test_encrypt_decrypt(self) -> None:
with c.Tox_Pass_Key_Ptr(b"hello") as pk:
self.assertNotEqual(pk.encrypt(b"hello world"), b"hello world")
self.assertEqual(pk.decrypt(pk.encrypt(b"hello world")), b"hello world")
self.assertEqual(pk.decrypt(pk.encrypt(b"hello world")),
b"hello world")

def test_encrypt_decrypt_with_correct_salt(self) -> None:
with c.Tox_Pass_Key_Ptr(b"hello", b"a" * 32) as pk1:
with c.Tox_Pass_Key_Ptr(b"hello", b"a" * 32) as pk2:
self.assertEqual(
pk2.decrypt(pk1.encrypt(b"hello world")), b"hello world"
)
self.assertEqual(pk2.decrypt(pk1.encrypt(b"hello world")),
b"hello world")

def test_encrypt_decrypt_with_wrong_salt(self) -> None:
with c.Tox_Pass_Key_Ptr(passphrase=b"hello", salt=b"a" * 32) as pk1:
with c.Tox_Pass_Key_Ptr(b"hello", b"b" * 32) as pk2:
with self.assertRaises(c.ApiException) as ex:
pk2.decrypt(pk1.encrypt(b"hello world"))
self.assertEqual(
ex.exception.error.name, c.TOX_ERR_DECRYPTION_FAILED.name
)
self.assertEqual(ex.exception.error.name,
c.TOX_ERR_DECRYPTION_FAILED.name)

def test_salt_too_small(self) -> None:
with self.assertRaises(common.LengthException):
Expand Down
25 changes: 12 additions & 13 deletions tools/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
]


def process_class(name: str, cls: list[str], imports: set[str], attr: object) -> None:
def process_class(name: str, cls: list[str], imports: set[str],
attr: object) -> None:
for mem in dir(attr):
mem_attr = getattr(attr, mem)
if mem == "__enter__":
Expand All @@ -26,10 +27,8 @@ def process_class(name: str, cls: list[str], imports: set[str], attr: object) ->
cls.append(
" def __exit__(self, exc_type: type[BaseException] | None, exc_value: BaseException | None, exc_traceback: TracebackType | None) -> None: ..."
)
elif (
mem == "__init__"
or "cython_function_or_method" in mem_attr.__class__.__name__
):
elif (mem == "__init__"
or "cython_function_or_method" in mem_attr.__class__.__name__):
doc = mem_attr.__doc__.split("\n")[0]
if " -> " not in doc:
doc += " -> None"
Expand Down Expand Up @@ -62,15 +61,14 @@ def main() -> None:
continue
if isinstance(attr, pytox.common.__class__):
continue
if (
hasattr(attr, "__module__")
and f"{prefix}.{attr.__module__}" != pkg.__name__
):
if (hasattr(attr, "__module__")
and f"{prefix}.{attr.__module__}" != pkg.__name__):
continue
attrs.append((sym, attr))

consts: list[str] = []
classes: collections.OrderedDict[str, list[str]] = collections.OrderedDict()
classes: collections.OrderedDict[
str, list[str]] = collections.OrderedDict()
imports: set[str] = set()
missing: set[str] = {
"Tox_Conference_Number",
Expand Down Expand Up @@ -106,7 +104,8 @@ def main() -> None:

for sym, attr in attrs:
if isinstance(attr, int):
enum = tuple(c for c in classes.keys() if sym.startswith(c.upper()))
enum = tuple(c for c in classes.keys()
if sym.startswith(c.upper()))
if enum:
consts.append(f"{sym}: {enum[0]}")
else:
Expand All @@ -119,8 +118,8 @@ def main() -> None:
raise Exception(f"nope {sym}: {type(attr)}")

with open(
os.path.join(out_dir, f"{pkg.__name__.replace('.', '/')}.pyi"), "w"
) as pyi:
os.path.join(out_dir, f"{pkg.__name__.replace('.', '/')}.pyi"),
"w") as pyi:
pyi.write(f"# {pkg.__name__}\n")
for s in sorted(imports):
pyi.write(s + "\n")
Expand Down

0 comments on commit 75d985a

Please sign in to comment.