test_reentrant.py
Overview
The [test_reentrant.py](/projects/287/67683) file is a minimalistic test module designed to verify the behavior of the `orjson` JSON parsing library under reentrant and recursive conditions. Specifically, it tests whether `orjson.loads()` can handle being called from within a destructor (`__del__` method) of an object and during the parsing of deeply nested JSON arrays.
This file contains a single class `C` with a destructor that triggers `orjson.loads()` on a large string, and a test function `test_reentrant()` which creates an instance of `C` with a self-reference to induce recursive references, deletes it to invoke the destructor, and subsequently runs another `orjson.loads()` on a large nested JSON array. The test is aimed at uncovering issues such as deadlocks, infinite recursion, or crashes caused by reentrant calls to the JSON parser.
Classes and Functions
Class C
class C:
c: "C"
def __del__(self):
orjson.loads('"' + "a" * 10000 + '"')
Description
Cis a simple class containing one attributecwhich is intended to hold a reference to anotherCinstance (or itself).Its destructor,
__del__, is overridden to callorjson.loads()on a large string when an instance ofCis garbage collected.
Attributes
c: "C": A type-annotated attribute expected to hold an instance ofC. In the test case, it is assigned to self-reference to create recursive references.
Methods
__del__(self):Triggered when the instance is about to be destroyed by the Python garbage collector.
Executes
orjson.loads()on a large string ('"' + "a" * 10000 + '"'), which is essentially a JSON string containing 10,000 'a' characters.This method tests if calling the JSON parser during object destruction causes any issues.
Usage Example
c = C()
c.c = c # Creates a cyclic reference
del c # Triggers __del__, invoking orjson.loads inside the destructor
Function test_reentrant
def test_reentrant():
c = C()
c.c = c
del c
orjson.loads("[" + "[]," * 1000 + "[]]")
Description
This function encapsulates the test scenario.
It instantiates class
C, assignsc.c = cto create a cyclic reference, and immediately deletes the instance to trigger the__del__method.After the destructor runs (and the reentrant
orjson.loadsis executed), it callsorjson.loads()again on a large JSON array string consisting of 1001 empty arrays nested within a top-level array.
Parameters
None
Returns
None
Usage Example
Simply call the function to run the test:
test_reentrant()
Implementation Details and Algorithms
Reentrancy Test: The critical aspect of this code is to test reentrancy in the
orjsonlibrary. Reentrancy means that the JSON parser (orjson.loads) is invoked while it might still be processing or doing internal operations, here triggered by object destruction.Destructor Invocation: The
__del__method is a special method in Python that is called when an object is garbage collected. The test creates a cyclic reference (c.c = c) so that the object is part of a reference cycle, which delays immediate destruction and can complicate garbage collection.Large Input Strings: The strings used for parsing are very large:
A JSON string of 10,000 characters
"aaaa...a"to test handling of large string literals.A JSON array with 1001 empty arrays
[[],[],...,[]]to test handling of large nested arrays.
Garbage Collection and Timing: Assigning
c.c = ccreates a cycle, and deletingctriggers garbage collection that breaks this cycle, eventually calling__del__. This tests for stability of orjson when called during the delicate GC phase.
Interaction with Other Parts of the System
Dependency on
orjson: This file directly imports and tests theorjsonlibrary’sloadsfunction. It relies onorjsonas the JSON parsing backend.Test or Utility Role: This file appears to be a test utility rather than part of the main application logic. It is likely run as part of a test suite to ensure the robustness of the
orjsonintegration in the overall system.Garbage Collector: By manipulating object lifetimes and references, this module indirectly interacts with Python's garbage collector, testing orjson's behavior in that context.
No External Side Effects: There are no external I/O operations or interactions with databases or other system components.
Visual Diagram
classDiagram
class C {
+c: C
+__del__()
}
class test_reentrant {
+test_reentrant()
}
test_reentrant --> C : creates instance
test_reentrant --> orjson : calls loads()
C --> orjson : calls loads() in __del__
Summary
The [test_reentrant.py](/projects/287/67683) file is a focused test module designed to verify the stability and correctness of the `orjson` JSON parsing library under reentrant conditions, specifically when invoked inside an object's destructor and on large nested JSON inputs. It uses Python's garbage collection intricacies to trigger reentrant calls, ensuring that the JSON parser can safely handle such scenarios without deadlocks or crashes.
This file is intended for use in testing environments to validate the robustness of `orjson` integration and does not form part of the main application logic. It provides a valuable edge-case test to prevent subtle bugs related to reentrancy and recursive data structures.