Registry / testing / forbiddenfruit

forbiddenfruit

JSON →
library0.1.4pypypi✓ verified 24d ago

Forbiddenfruit is a Python library designed to extend or modify built-in types at runtime. It achieves this by patching built-in objects that are declared in C through Python's C API. While powerful, it is primarily intended for scenarios like advanced testing or profiling, rather than general production use due to its potential for unexpected side effects. The current version, 0.1.4, was released in January 2021, and the project has an infrequent release cadence.

pip install forbiddenfruit
INSTALL
IMPORT
SIG · FORBIDDENFRUIT
F
forbiddenfruit
testingpythonv0.1.4
Install
2.5s avg
Import
37ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.034s · 19.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.5s · import 0.040s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

curse
from forbiddenfruit import curse
Used to add or replace an attribute on a built-in type.
reverse
from forbiddenfruit import reverse
Used to remove an attribute added by `curse`.
cursed
from forbiddenfruit import cursed
A context manager/decorator (added in 0.1.4) that applies a curse and automatically reverses it upon exit.

This quickstart demonstrates how to add new methods to built-in types (`int`, `str`, `list`) using `curse()` and `cursed()`. It also shows how to remove modifications using `reverse()` or by leveraging the automatic cleanup of `cursed()` as a context manager or decorator.

from forbiddenfruit import curse, cursed, reverse def words_of_wisdom(self): return self * " blah " def temporary_method(self): return f"I am a temporary string: {self}" # --- Using curse() and reverse() --- # Add a new method to the int class curse(int, "words_of_wisdom", words_of_wisdom) assert (2).words_of_wisdom() == " blah blah " print(f"int(2).words_of_wisdom(): {(2).words_of_wisdom()}") # Remove the added method reverse(int, "words_of_wisdom") try: (2).words_of_wisdom() except AttributeError as e: print(f"Method removed: {e}") # --- Using cursed() as a context manager --- print("\n--- Using cursed() as a context manager ---") with cursed(str, "temp_str_method", temporary_method): s = "hello" assert s.temp_str_method() == "I am a temporary string: hello" print(f"Inside context: {s.temp_str_method()}") try: "test".temp_str_method() except AttributeError as e: print(f"Outside context, method removed: {e}") # --- Using cursed() as a decorator --- print("\n--- Using cursed() as a decorator ---") @cursed(list, "first_element", lambda self: self[0] if self else None) def demonstrate_list_patch(): my_list = [1, 2, 3] assert my_list.first_element() == 1 print(f"Inside decorated function: {my_list.first_element()}") demonstrate_list_patch() try: [4, 5].first_element() except AttributeError as e: print(f"Outside decorated function, method removed: {e}")
Debug
Known issues
breakingForbiddenfruit directly modifies Python's built-in types, which is generally discouraged for production code. This can lead to unpredictable behavior, global state corruption, and difficult-to-debug issues, especially when used in complex or shared environments. It is primarily intended for testing or highly isolated development scenarios.
fix
Restrict usage to test suites or isolated development environments. Avoid deploying code that uses `forbiddenfruit` to modify built-ins in shared libraries or critical production systems.
affects: All versions
gotchaThe `reverse()` function only removes attributes previously added by `curse()`. If `curse()` was used to *replace* an existing built-in attribute (e.g., `str.__add__`), `reverse()` will delete the replacement but *not* restore the original built-in attribute, potentially leaving the type without its original functionality.
fix
Carefully track which attributes are being replaced versus merely added. If replacing an existing attribute, you may need a manual mechanism to re-assign the original method/value if restoration is critical for subsequent operations.
affects: All versions
breakingForbiddenfruit is fundamentally dependent on the CPython C API and is therefore incompatible with other Python implementations such as Jython or PyPy. It is tested and designed to work exclusively with CPython.
fix
Only use `forbiddenfruit` with CPython. Do not attempt to use it in environments running alternative Python interpreters, as this will lead to errors or crashes.
affects: All versions
gotchaPatching certain dunder (magic) methods, particularly rich comparison methods (e.g., `__le__`) or `__hash__`, can lead to `KeyError` or outright interpreter crashes, especially on specific operating systems (like Windows) or with newer Python versions (e.g., Python 3.13 has reported crashes related to dunder method patching).
fix
Exercise extreme caution and thoroughly test when attempting to patch dunder methods. Prioritize patching non-dunder methods or consider alternative approaches if dunder method modification proves unstable.
affects: All versions, but issues may be more apparent in specific Python versions or environments.
gotchaDue to its low-level manipulation of Python's internals, improper or undocumented use of `forbiddenfruit` can easily lead to Python interpreter segfaults (crashes) or place the interpreter into an inconsistent state where expected operations break unexpectedly.
fix
Only modify built-in objects in ways explicitly demonstrated in `forbiddenfruit`'s official examples. Avoid attempting to alter internal type flags, C-API slots, or cache mechanisms unless you possess a deep understanding of CPython's internal structure and behavior.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'forbiddenfruit'
The 'forbiddenfruit' library is not installed in the Python environment where the code is being executed.
fix
Install the library using pip: `pip install forbiddenfruit`
TypeError: can't set attributes of built-in/extension type 'str'
Python's built-in types (like str, int, list, etc.) are implemented in C and do not natively allow the addition of new methods or attributes using standard Python assignment. Forbiddenfruit is specifically designed to overcome this limitation.
fix
Use the `curse` function from forbiddenfruit to add the desired attribute or method: `from forbiddenfruit import curse; curse(str, "my_method", my_function)`
forbiddenfruit not working on PyPy
Forbiddenfruit relies on specific internals of the CPython interpreter's C API to modify built-in types, making it incompatible with alternative Python implementations like PyPy or Jython.
fix
Ensure you are running your code with CPython, as forbiddenfruit is not designed to work with other Python implementations.
Upgrade
Version history
0.1.4latest on PyPI · released Jan 16, 2021
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
forbiddenfruit — pip install forbiddenfruit · libregistry