Skip to content

Commit 05dcabb

Browse files
committed
fixes #795
1 parent adc4937 commit 05dcabb

5 files changed

Lines changed: 37 additions & 33 deletions

File tree

fastcore/docments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,6 @@ def _repr_markdown_(self):
439439

440440
def __repr__(self):
441441
doc = str(self.dm)
442-
if self.docs: doc += f'"""\n{self.docs}\n"""'
442+
if self.docs: doc += f'"""{self.docs}"""'
443443
return doc
444444
__str__=__repr__

fastcore/imports.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ def any_is_instance(t, *args): return any(isinstance(a,t) for a in args)
4444

4545
def isinstance_str(x, cls_name):
4646
"Like `isinstance`, except takes a type name instead of a type"
47-
return cls_name in [t.__name__ for t in type(x).__mro__]
47+
if isinstance(cls_name, str): cls_name = (cls_name,)
48+
names = [t.__name__ for t in type(x).__mro__]
49+
return any(c in names for c in cls_name)
4850

4951
def array_equal(a,b):
5052
if hasattr(a, '__array__'): a = a.__array__()

fastcore/tools.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,16 @@ def run_cmd(
6363
except: return explain_exc(f'running cmd')
6464
res = outp.stdout
6565
if res and outp.stderr: res += '\n'
66-
return res + outp.stderr
66+
res += outp.stderr
67+
if not res.strip() and re.search(r'\\[|*?]', argstr):
68+
return r'Warning: no output and argstr contains \-escaped chars. Shell escaping is not needed: try without \-escaping.'
69+
return res
70+
6771

6872
# %% ../nbs/12_tools.ipynb #eb253a39
6973
@llmtool
7074
def rg(
71-
argstr:str, # All args to the command, will be split with shlex
75+
argstr:str, # All args to the command, will be split with shlex. No shell escaping needed for regex chars like `|`
7276
disallow_re:str=None, # optional regex which, if matched on argstr, will disallow the command
7377
allow_re:str=None # optional regex which, if not matched on argstr, will disallow the command
7478
):
@@ -78,7 +82,7 @@ def rg(
7882
# %% ../nbs/12_tools.ipynb #09de7b32
7983
@llmtool
8084
def sed(
81-
argstr:str, # All args to the command, will be split with shlex
85+
argstr:str, # All args to the command, will be split with shlex. No shell escaping needed for regex chars like `|`
8286
disallow_re:str=None, # optional regex which, if matched on argstr, will disallow the command
8387
allow_re:str=None # optional regex which, if not matched on argstr, will disallow the command
8488
):

nbs/04_docments.ipynb

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,7 +2494,7 @@
24942494
"\n",
24952495
" def __repr__(self):\n",
24962496
" doc = str(self.dm)\n",
2497-
" if self.docs: doc += f'\"\"\"\\n{self.docs}\\n\"\"\"'\n",
2497+
" if self.docs: doc += f'\"\"\"{self.docs}\"\"\"'\n",
24982498
" return doc\n",
24992499
" __str__=__repr__"
25002500
]
@@ -2525,9 +2525,7 @@
25252525
" a, b:callable=print, # param b\n",
25262526
" c:str='foo', # param c\n",
25272527
")->str: # Result of doing it\n",
2528-
"\"\"\"\n",
2529-
"Do a thing\n",
2530-
"\"\"\""
2528+
"\"\"\"Do a thing\"\"\""
25312529
]
25322530
},
25332531
"execution_count": null,
@@ -2560,9 +2558,7 @@
25602558
" a, b:callable=print, # param b\n",
25612559
" c:str='foo', # param c\n",
25622560
")->str: # Result of doing it\n",
2563-
"\"\"\"\n",
2564-
"Do a thing\n",
2565-
"\"\"\"\n"
2561+
"\"\"\"Do a thing\"\"\"\n"
25662562
]
25672563
}
25682564
],
@@ -2613,9 +2609,7 @@
26132609
" b:int | str, # bb\n",
26142610
" a:int=0, # aa\n",
26152611
"):\n",
2616-
"\"\"\"\n",
2617-
"Call self as a function.\n",
2618-
"\"\"\""
2612+
"\"\"\"Call self as a function.\"\"\""
26192613
]
26202614
},
26212615
"execution_count": null,
@@ -2655,9 +2649,7 @@
26552649
" b:int, # This is the second of the operands to the *addition* operator.\n",
26562650
"Note that passing a negative value here is the equivalent of the *subtraction* operator.\n",
26572651
")->int: # The result is calculated using Python's builtin `+` operator.\n",
2658-
"\"\"\"\n",
2659-
"Add `a` to `b`\n",
2660-
"\"\"\""
2652+
"\"\"\"Add `a` to `b`\"\"\""
26612653
]
26622654
},
26632655
"execution_count": null,
@@ -2692,12 +2684,10 @@
26922684
],
26932685
"text/plain": [
26942686
"def next(iterator, default=..., /)\n",
2695-
"\"\"\"\n",
2696-
"Return the next item from the iterator.\n",
2687+
"\"\"\"Return the next item from the iterator.\n",
26972688
"\n",
26982689
"If default is given and the iterator is exhausted,\n",
2699-
"it is returned instead of raising StopIteration.\n",
2700-
"\"\"\""
2690+
"it is returned instead of raising StopIteration.\"\"\""
27012691
]
27022692
},
27032693
"execution_count": null,
@@ -2804,9 +2794,7 @@
28042794
"def Foo.__call__(\n",
28052795
" x\n",
28062796
"):\n",
2807-
"\"\"\"\n",
2808-
"Call foo\n",
2809-
"\"\"\""
2797+
"\"\"\"Call foo\"\"\""
28102798
]
28112799
},
28122800
"execution_count": null,

nbs/12_tools.ipynb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@
8484
]
8585
},
8686
"execution_count": null,
87-
"metadata": {},
87+
"metadata": {
88+
"__type": "str"
89+
},
8890
"output_type": "execute_result"
8991
}
9092
],
@@ -122,7 +124,9 @@
122124
]
123125
},
124126
"execution_count": null,
125-
"metadata": {},
127+
"metadata": {
128+
"__type": "str"
129+
},
126130
"output_type": "execute_result"
127131
}
128132
],
@@ -260,7 +264,10 @@
260264
" except: return explain_exc(f'running cmd')\n",
261265
" res = outp.stdout\n",
262266
" if res and outp.stderr: res += '\\n'\n",
263-
" return res + outp.stderr"
267+
" res += outp.stderr\n",
268+
" if not res.strip() and re.search(r'\\\\[|*?]', argstr):\n",
269+
" return r'Warning: no output and argstr contains \\-escaped chars. Shell escaping is not needed: try without \\-escaping.'\n",
270+
" return res\n"
264271
]
265272
},
266273
{
@@ -283,12 +290,13 @@
283290
"text": [
284291
"\u001b[34maai-ws\u001b[m\u001b[m\n",
285292
"\u001b[34mApplications\u001b[m\u001b[m\n",
286-
"autoexec.ipynb\n",
287293
"\u001b[34mbooks\u001b[m\u001b[m\n",
288294
"cachy.jsonl\n",
289295
"\u001b[34mchats\u001b[m\u001b[m\n",
290296
"CRAFT.ipynb\n",
291-
"\u001b[34mDesktop\u001b\n"
297+
"CRAFT.py\n",
298+
"\u001b[34mCRAFTs\u001b[m\u001b[m\n",
299+
"\u001b\n"
292300
]
293301
}
294302
],
@@ -341,7 +349,7 @@
341349
"#| export\n",
342350
"@llmtool\n",
343351
"def rg(\n",
344-
" argstr:str, # All args to the command, will be split with shlex\n",
352+
" argstr:str, # All args to the command, will be split with shlex. No shell escaping needed for regex chars like `|`\n",
345353
" disallow_re:str=None, # optional regex which, if matched on argstr, will disallow the command\n",
346354
" allow_re:str=None # optional regex which, if not matched on argstr, will disallow the command\n",
347355
"):\n",
@@ -362,7 +370,9 @@
362370
]
363371
},
364372
"execution_count": null,
365-
"metadata": {},
373+
"metadata": {
374+
"__type": "str"
375+
},
366376
"output_type": "execute_result"
367377
}
368378
],
@@ -498,7 +508,7 @@
498508
"#| export\n",
499509
"@llmtool\n",
500510
"def sed(\n",
501-
" argstr:str, # All args to the command, will be split with shlex\n",
511+
" argstr:str, # All args to the command, will be split with shlex. No shell escaping needed for regex chars like `|`\n",
502512
" disallow_re:str=None, # optional regex which, if matched on argstr, will disallow the command\n",
503513
" allow_re:str=None # optional regex which, if not matched on argstr, will disallow the command\n",
504514
"):\n",

0 commit comments

Comments
 (0)