{"id":18456298,"url":"https://github.com/martmists-gh/pyasm","last_synced_at":"2025-04-15T15:38:04.815Z","repository":{"id":106860075,"uuid":"409879861","full_name":"Martmists-GH/pyasm","owner":"Martmists-GH","description":"Python bytecode modification library","archived":false,"fork":false,"pushed_at":"2023-11-16T09:04:06.000Z","size":47,"stargazers_count":4,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T21:51:12.443Z","etag":null,"topics":["bytecode","bytecode-manipulation","framework","python","python3"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Martmists-GH.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-09-24T07:52:35.000Z","updated_at":"2024-09-09T14:36:35.000Z","dependencies_parsed_at":null,"dependency_job_id":"0e54c48a-cbb1-4316-8b60-11b0709788f7","html_url":"https://github.com/Martmists-GH/pyasm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Martmists-GH%2Fpyasm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Martmists-GH%2Fpyasm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Martmists-GH%2Fpyasm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Martmists-GH%2Fpyasm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Martmists-GH","download_url":"https://codeload.github.com/Martmists-GH/pyasm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249099558,"owners_count":21212690,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bytecode","bytecode-manipulation","framework","python","python3"],"created_at":"2024-11-06T08:11:06.574Z","updated_at":"2025-04-15T15:38:04.795Z","avatar_url":"https://github.com/Martmists-GH.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PyASM\n\nPyASM is a python library designed to help modify bytecode in a somewhat easy manner.\nThe API design is loosely based on objectweb:asm for Java.\n\n## Features\n\n### Deconstruct code objects\n\nYou can use the `asm.Deserializer` class to convert a code object into a list of opcodes\n\n```python\n\u003e\u003e\u003e from asm import *\n\u003e\u003e\u003e \n\u003e\u003e\u003e def my_function(x: int = 1) -\u003e int:\n...     if x \u003e 20:\n...         return x / 20\n...     return x\n... \n\u003e\u003e\u003e deserializer = Deserializer(my_function.__code__)\n\u003e\u003e\u003e deserializer.deserialize()  # Below is formatted for readability\n[\n    LOAD_FAST(id=124, arg='x'), \n    LOAD_CONST(id=100, arg=20), \n    COMPARE_OP(id=107, arg=4), \n    POP_JUMP_IF_FALSE(id=114, arg=Label(0x7fd2fbeb0d60)), \n    LOAD_FAST(id=124, arg='x'), \n    LOAD_CONST(id=100, arg=20), \n    BINARY_TRUE_DIVIDE(id=27, arg=0), \n    RETURN_VALUE(id=83, arg=0), \n    Label(0x7fd2fbeb0d60), \n    LOAD_FAST(id=124, arg='x'), \n    RETURN_VALUE(id=83, arg=0)\n]\n```\n\n### Construct code objects\n\nConversely, you can also turn a list of opcodes into a code object using `asm.Serializer`\n\n```python\n\u003e\u003e\u003e from asm import *\n\u003e\u003e\u003e \n\u003e\u003e\u003e dummy = lambda x: None\n\u003e\u003e\u003e serializer = Serializer([   # Bytecode for `return x * 20`\n...     LOAD_FAST(\"x\"),\n...     LOAD_CONST(20),\n...     BINARY_MULTIPLY(),\n...     RETURN_VALUE()\n... ], dummy.__code__)\n\u003e\u003e\u003e dummy.__code__ = serializer.serialize()\n\u003e\u003e\u003e dummy(10)\n200\n```\n\n### Jumps\n\nA big issue when modifying bytecode is jumps. We solve this by using `asm.Label`:\n\n```python\n\u003e\u003e\u003e from asm import *\n\u003e\u003e\u003e lbl = Label()\n\u003e\u003e\u003e ops = [\n...     LOAD_CONST(10),\n...     LOAD_CONST(20),\n...     COMPARE_OP(\"\u003c\"),\n...     POP_JUMP_IF_FALSE(lbl),     # jump to where lbl is located\n...     LOAD_CONST(True),\n...     RETURN_VALUE(),\n...     lbl,                        # jump to here\n...     LOAD_CONST(False),\n...     RETURN_VALUE()\n... ]\n```\n\nThis way, no matter how many instructions you place between the jump and the label, it will always resolve correctly.\n\n## Installing\n\nTo install PyASM, you can just use pip:\n\n```shell\npip install pyasm\n```\n\n## License\n\nPyASM is licensed under MIT.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartmists-gh%2Fpyasm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmartmists-gh%2Fpyasm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartmists-gh%2Fpyasm/lists"}