{ "info": { "author": "Jelle Zijlstra", "author_email": "jelle.zijlstra@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: Apache Software License", "Programming Language :: Python :: 2.7", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "**********\nbytearound\n**********\n\nbytearound is a module for assembling and disassembling CPython 2.7.11 bytecode. It provides a\nrepresentation of bytecode that is easier to modify, create, and inspect than CPython's internal\nrepresentation and functionality for going back and forth between this representation and CPython\ncode objects.\n\nAn example of how to create code::\n\n from bytearound import ByteAround, Instruction, ops\n\n ba = ByteAround([\n ops.LOAD_CONST('Hello World!'),\n ops.PRINT_ITEM(),\n ops.PRINT_NEWLINE(),\n ops.LOAD_CONST(None),\n ops.RETURN_VALUE(),\n ])\n exec(ba.to_code())\n\nAnd a simple modification::\n\n from bytearound import ByteAround\n\n def f():\n print 'Hello World!'\n\n ba = ByteAround.from_code(f.func_code)\n for instr in ba:\n if instr.oparg == 'Hello World!':\n instr.oparg = 'Goodbye World!'\n f.func_code = ba.to_code()\n f()\n\nDesign and limitations\n----------------------\n\nI designed and wrote bytearound to ensure that ``co == ByteAround.from_code(co).to_code()`` always\nholds--that is, converting a Python code object to the bytearound representation and back should\ngive an identical code object. Ensuring that this invariant holds makes it easier to test the code for correctness. The function debug.check() exists to check this invariant.\n\nUnfortunately, there are a number of quirks in the way that CPython generates code objects that\nturn out to be hard to replicate. To replicate some of these, I added a ``pessimize=`` argument to\n``ByteAround.to_code`` that attempts to faithfully replicate CPython even when not doing so would\nbe a little more efficient, and I created a custom comparison function that ignores a few other\nknown issues. However, it may not turn out to be possible to remove all minor differences using\nthese approaches. Known issues include:\n\n* CPython computes some parts of the code object before it runs the peephole optimizer, which can\n cause co_stacksize to be too high (because the peephole optimizer can turn a series of opcodes\n building a tuple into a single LOAD_CONST opcode). The same issue can also affect the ordering\n of the co_consts field, apparently because the optimizer adds new constants to the end of the\n list. Similarly, mathematical operations on constants (e.g. 2 ** 32) may be optimized away by the\n peephole optimizer, possibly leaving behind unnecessary constants.\n* When singleton objects like None and True are used in a function, CPython adds their name to the\n co_names field (unnecessarily, because the objects are loaded directly with LOAD_CONST) and adds\n the constants to the end of the co_consts list. (Normally, co_consts includes constants in order\n of their first appearance in the function.) However, some other usages of None as a constant are\n placed in co_consts in the right position.\n* The code object for single-line generator expressions like (f(x) for x in y) has a nonempty\n co_lnotab field, but the co_lnotab for a function defined like \"def f(x): print(x)\" is empty. In\n some other circumstances CPython also generates an unnecessary 0 offset entry in co_lnotab.\n* Large opargs (using EXTENDED_ARG) and large line number offsets are not well-tested and have\n some known issues, noted in the code.\n\nbytearound has been tested only on Python 2.7.11. Previous releases in the 2.7 series should\nmostly work, but some changes have been made during the series that impact code objects (e.g.\n`issue 21523 `_).\n\nLinks\n-----\n\n* `compile.c source `_\n* `dis.py source `_\n* `lnotab explanation `_\n* `issue 26549 `_ (cause of some of the issues described above)\n\nSimilar modules\n---------------\n\n* `byteplay `_ (unmaintained, has bugs)\n* `bytecode `_ (Python 3 only, does not implement stack size computation)", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/JelleZijlstra/bytearound", "keywords": "", "license": "Apache 2.0 license", "maintainer": "", "maintainer_email": "", "name": "bytearound", "package_url": "https://pypi.org/project/bytearound/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/bytearound/", "project_urls": { "Homepage": "https://github.com/JelleZijlstra/bytearound" }, "release_url": "https://pypi.org/project/bytearound/0.2/", "requires_dist": null, "requires_python": "", "summary": "Python module to handle bytecode", "version": "0.2" }, "last_serial": 2121184, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "8c69eab148721c648ccb7be4125cb011", "sha256": "0c2846b295dc67c9846882ee8cac21ac23096874ced4f0ec7fbb5b46ff9fcdf0" }, "downloads": -1, "filename": "bytearound-0.1.tar.gz", "has_sig": false, "md5_digest": "8c69eab148721c648ccb7be4125cb011", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12200, "upload_time": "2016-05-01T02:16:35", "url": "https://files.pythonhosted.org/packages/a2/58/51f768b5844efa629fad1cc8bda40562e3a213a8a8fb2421446ffef13a0c/bytearound-0.1.tar.gz" } ], "0.2": [ { "comment_text": "", "digests": { "md5": "beee8cfe99d1bd9f51981b5384af0fca", "sha256": "e8b2ecb925a637e51dce1622f78dd1192e8623e4a8a0f961225121f5849dbb86" }, "downloads": -1, "filename": "bytearound-0.2.tar.gz", "has_sig": false, "md5_digest": "beee8cfe99d1bd9f51981b5384af0fca", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12459, "upload_time": "2016-05-18T06:11:17", "url": "https://files.pythonhosted.org/packages/2e/e9/688fd4921e541b56cb3526efcfb4ec506f7d40656f5ebe6f1f0eff57fba8/bytearound-0.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "beee8cfe99d1bd9f51981b5384af0fca", "sha256": "e8b2ecb925a637e51dce1622f78dd1192e8623e4a8a0f961225121f5849dbb86" }, "downloads": -1, "filename": "bytearound-0.2.tar.gz", "has_sig": false, "md5_digest": "beee8cfe99d1bd9f51981b5384af0fca", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12459, "upload_time": "2016-05-18T06:11:17", "url": "https://files.pythonhosted.org/packages/2e/e9/688fd4921e541b56cb3526efcfb4ec506f7d40656f5ebe6f1f0eff57fba8/bytearound-0.2.tar.gz" } ] }