{"id":13595925,"url":"https://github.com/clarete/forbiddenfruit","last_synced_at":"2025-05-15T06:03:45.017Z","repository":{"id":7813840,"uuid":"9184612","full_name":"clarete/forbiddenfruit","owner":"clarete","description":"Patch built-in python objects","archived":false,"fork":false,"pushed_at":"2025-04-19T14:08:23.000Z","size":152,"stargazers_count":837,"open_issues_count":29,"forks_count":50,"subscribers_count":28,"default_branch":"master","last_synced_at":"2025-05-15T06:03:19.201Z","etag":null,"topics":["monkey-patching","python"],"latest_commit_sha":null,"homepage":"https://clarete.li/forbiddenfruit/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/clarete.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2013-04-03T01:55:07.000Z","updated_at":"2025-05-04T02:17:08.000Z","dependencies_parsed_at":"2024-01-16T22:18:46.254Z","dependency_job_id":"f8c7468a-999e-4273-9224-d66f3262a39f","html_url":"https://github.com/clarete/forbiddenfruit","commit_stats":{"total_commits":84,"total_committers":17,"mean_commits":"4.9411764705882355","dds":0.3571428571428571,"last_synced_commit":"0c6accc55c917f7140b30b8a7e614747ec1edfe2"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clarete%2Fforbiddenfruit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clarete%2Fforbiddenfruit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clarete%2Fforbiddenfruit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clarete%2Fforbiddenfruit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/clarete","download_url":"https://codeload.github.com/clarete/forbiddenfruit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254283336,"owners_count":22045140,"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":["monkey-patching","python"],"created_at":"2024-08-01T16:02:01.426Z","updated_at":"2025-05-15T06:03:45.010Z","avatar_url":"https://github.com/clarete.png","language":"Python","readme":"[![Build Status](https://travis-ci.org/clarete/forbiddenfruit.png?branch=master)](https://travis-ci.org/clarete/forbiddenfruit)\n\n# Forbidden Fruit\n\n![Forbidden Fruit](logo.png)\n\nThis project allows Python code to extend built-in types.\n\nIf that's a good idea or not, you tell me. The first need this project\nattended was allowing a [Python assertion\nlibrary](https://github.com/gabrielfalcao/sure) to implement a similar\nAPI to [RSpec\nExpectations](https://github.com/rspec/rspec-expectations) and\n[should.js](https://shouldjs.github.io/). But people got creative and\nused it to among other things [spy on\nthings](https://github.com/ikamensh/flynt/blob/43a64ac1a030be79741402d8920a6da253a96670/src/flynt/file_spy.py)\nor to [integrate\nprofiling](https://github.com/localstack/localstack/blob/e38eae0d1fe442924f4256d4bc87710a4cb6f142/localstack/utils/analytics/profiler.py).\n\n## Tiny Example\n\nIt basically allows you to patch built-in objects, declared in C through\npython. Just like this:\n\n1. Add a new method to the `int` class:\n\n```python\nfrom forbiddenfruit import curse\n\n\ndef words_of_wisdom(self):\n    return self * \"blah \"\n\n\ncurse(int, \"words_of_wisdom\", words_of_wisdom)\n\nassert (2).words_of_wisdom() == \"blah blah \"\n```\n\n2. Add a `classmethod` to the `str` class:\n\n```python\nfrom forbiddenfruit import curse\n\n\ndef hello(self):\n    return \"blah\"\n\n\ncurse(str, \"hello\", classmethod(hello))\n\nassert str.hello() == \"blah\"\n```\n\n### Reversing a curse\n\nIf you want to free your object from a curse, you can use the `reverse()`\nfunction. Just like this:\n\n```python\nfrom forbiddenfruit import curse, reverse\n\ncurse(str, \"test\", \"blah\")\nassert 'test' in dir(str)\n\n# Time to reverse the curse\nreverse(str, \"test\")\nassert 'test' not in dir(str)\n```\n\n**Beware:** `reverse()` only deletes attributes. If you `curse()`'d to replace\na pre-existing attribute, `reverse()` won't re-install the existing attribute.\n\n### Context Manager / Decorator\n\n`cursed()` acts as a context manager to make a `curse()`, and then `reverse()`\nit on exit. It uses\n[`contextlib.contextmanager()`](https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager),\nso on Python 3.2+ it can also be used as a function decorator. Like so:\n\n```python\nfrom forbiddenfruit import cursed\n\nwith cursed(str, \"test\", \"blah\"):\n    assert str.test == \"blah\"\n\nassert \"test\" not in dir(str)\n\n\n@cursed(str, \"test\", \"blah\")\ndef function():\n    assert str.test == \"blah\"\n\n\nfunction()\n\nassert \"test\" not in dir(str)\n```\n\n## Compatibility\n\nForbbiden Fruit is tested on CPython 3.7-3.13.\n\nSince Forbidden Fruit is fundamentally dependent on the C API,\nthis library won't work on other python implementations, such\nas Jython, pypy, etc.\n\n## License\n\nCopyright (C) 2013,2024 Lincoln Clarete \u003clincoln@clarete.li\u003e\n\nThis software is available under two different licenses at your\nchoice:\n\n### GPLv3\n\nThis program is free software: you can redistribute it and/or modify\nit under the terms of the GNU General Public License as published by\nthe Free Software Foundation, either version 3 of the License, or\n(at your option) any later version.\n\nThis program is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\nGNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License\nalong with this program.  If not, see \u003chttp://www.gnu.org/licenses/\u003e.\n\n### MIT\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\nBE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\nACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n### Logo by\n\nKimberly Chandler, from The Noun Project\n\n### Changelog\n\n#### 0.1.4\n\n  * Add cursed() context manager/decorator\n  * Conditionally build test C extension\n  * Allow cursing dunder methods with non functions\n  * Fix dual licensing issues. Distribute both GPLv3 \u0026 MIT license\n    files.\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclarete%2Fforbiddenfruit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fclarete%2Fforbiddenfruit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclarete%2Fforbiddenfruit/lists"}