|
1391 | 1391 | "outputs": [], |
1392 | 1392 | "source": [ |
1393 | 1393 | "#export\n", |
1394 | | - "def argnames(f):\n", |
1395 | | - " \"Names of arguments to function `f`\"\n", |
1396 | | - " code = f.__code__\n", |
1397 | | - " return code.co_varnames[:code.co_argcount]" |
| 1394 | + "def argnames(f, frame=False):\n", |
| 1395 | + " \"Names of arguments to function or frame `f`\"\n", |
| 1396 | + " code = getattr(f, 'f_code' if frame else '__code__')\n", |
| 1397 | + " return code.co_varnames[:code.co_argcount+code.co_kwonlyargcount]" |
| 1398 | + ] |
| 1399 | + }, |
| 1400 | + { |
| 1401 | + "cell_type": "code", |
| 1402 | + "execution_count": null, |
| 1403 | + "metadata": {}, |
| 1404 | + "outputs": [], |
| 1405 | + "source": [ |
| 1406 | + "test_eq(argnames(f), ['x'])" |
1398 | 1407 | ] |
1399 | 1408 | }, |
1400 | 1409 | { |
|
1459 | 1468 | "def store_attr(names=None, self=None, but='', cast=False, **attrs):\n", |
1460 | 1469 | " \"Store params named in comma-separated `names` from calling context into attrs in `self`\"\n", |
1461 | 1470 | " fr = sys._getframe(1)\n", |
1462 | | - " args = fr.f_code.co_varnames[:fr.f_code.co_argcount+fr.f_code.co_kwonlyargcount]\n", |
| 1471 | + " args = argnames(fr, True)\n", |
1463 | 1472 | " if self: args = ('self', *args)\n", |
1464 | 1473 | " else: self = fr.f_locals[args[0]]\n", |
1465 | 1474 | " if not hasattr(self, '__stored_args__'): self.__stored_args__ = {}\n", |
|
0 commit comments