@@ -33,13 +33,14 @@ class Command(BaseCommand):
33
33
* _("hello") # <-- this will become just "hello"
34
34
* "hello"
35
35
36
- Furthermore, objects which are not easily JSON-ifiable will stringified using their `repr(...)`, e.g.:
37
- * "Path('my/path')" # a Path object
38
- * "<lms.myapp.MyClass object at 0x704599fa2fd0>" # some random class instance
39
- * "<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>" # sys.stderr
36
+ Furthermore, functions and classes are printed as JSON objects like:
37
+ {
38
+ "module": "path.to.module",
39
+ "qualname": "MyClass.MyInnerClass.my_method", // Or, "<lambda>"
40
+ "source_hint": "MY_SETTING = lambda: x + y", // For <lambda>s only
41
+ }
40
42
41
- and lambdas are printed by *roughly* printing out their source lines (it's impossible in Python to get the *exact*
42
- source code, as it's been compiled into bytecode).
43
+ And everything else will be stringified as its `repr(...)`.
43
44
"""
44
45
45
46
def handle (self , * args , ** kwargs ):
@@ -82,12 +83,18 @@ def _to_json_friendly_repr(value: object) -> object:
82
83
# Print gettext_lazy as simply the wrapped string
83
84
return proxy_args [0 ]
84
85
try :
86
+ module = value .__module__
85
87
qualname = value .__qualname__
86
88
except AttributeError :
87
89
pass
88
90
else :
89
- if qualname == "<lambda>" :
90
- # Handle lambdas by printing the source lines
91
- return "lambda defined with line(s): " + inspect .getsource (value ).strip ()
91
+ # Handle functions and classes by printing their location (plus approximate source, for lambdas)
92
+ return {
93
+ "module" : module ,
94
+ "qualname" : qualname ,
95
+ ** ({
96
+ "source_hint" : inspect .getsource (value ).strip (),
97
+ } if qualname == "<lambda>" else {}),
98
+ }
92
99
# For all other objects, print the repr
93
100
return repr (value )
0 commit comments