Skip to content

Commit

Permalink
Update notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
saulshanabrook committed Apr 1, 2022
1 parent c8f50c0 commit d4e15c3
Showing 1 changed file with 248 additions and 3 deletions.
251 changes: 248 additions & 3 deletions examples/Analysis Scope.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"cell_type": "markdown",
"id": "0ec02e5d-37ee-4cfa-8626-0777d11094b7",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
Expand Down Expand Up @@ -95,7 +96,10 @@
{
"cell_type": "markdown",
"id": "8adb0f6e-5c4a-455d-a56c-10007f11fdc8",
"metadata": {},
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"## Function that mutate"
]
Expand Down Expand Up @@ -187,7 +191,10 @@
{
"cell_type": "markdown",
"id": "8324f147-0b24-4896-bfb3-c5e93b4d05ca",
"metadata": {},
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"## Tracking \"views\""
]
Expand Down Expand Up @@ -341,10 +348,248 @@
"We are able to track this by also annotating function if they add any \"views\" between their arguments or results. We consider views to be bidirectional, and if two objects are views of one another, a mutation of one will mutate the other."
]
},
{
"cell_type": "markdown",
"id": "b656eea9-6f49-4055-a687-d7d579ef36bc",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"## Control Flow"
]
},
{
"cell_type": "markdown",
"id": "ea1de464-8b77-4b13-a11b-0431b2579435",
"metadata": {},
"source": [
"We currently have special cases for all Python statements which are not simple expressions, such as `for` loops, `while` loops, `if` statements and `with` statements. We don't do trim any lines out from inside of these, but do analyze them to see what functions are called:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "bbee76cb-feb4-49cf-a0c4-168214da104e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"z = 5\n",
"if True:\n",
" x = 10 + z\n",
" y = 10\n",
"\n"
]
}
],
"source": [
"z = 5\n",
"\n",
"# We will include all of thise code, because we do not slice inside of control flow\n",
"if True:\n",
" x = 10 + z\n",
" y = 10\n",
"\n",
" \n",
"print(save(y, 'y').code)"
]
},
{
"cell_type": "markdown",
"id": "c0fd38d9-bb90-4881-80ba-a78e2fa9af81",
"metadata": {},
"source": [
"We also analyze all function calls that happen in these blocks, to know if they mutate their arguments:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "a296d20b-3746-4fa2-8d2d-979146d02074",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"z = []\n",
"if True:\n",
" z.append(10)\n",
"\n"
]
}
],
"source": [
"z = []\n",
"\n",
"# We know that this code calls append on `z` so we know it mutates z\n",
"if True:\n",
" z.append(10)\n",
"\n",
"# We know that this code does not mutate `z`\n",
"if True:\n",
" x = len(z)\n",
"\n",
"print(save(z, 'z').code)"
]
},
{
"cell_type": "markdown",
"id": "d836ee0e-d59a-41d6-bbdc-ca70e67c3251",
"metadata": {},
"source": [
"We only track the first call to each instruction while tracing control flow, so if it calls a different function the second time we won't pick it up:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "65d58e69-b7be-4ecf-80e9-1b6cde3976e3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"z = []\n",
"for method in (\"append\", \"__getitem__\"):\n",
" getattr(z, method)(0)\n",
"\n"
]
}
],
"source": [
"z = []\n",
"\n",
"# The first time in the loop, we call append, so we know about that call\n",
"for method in (\"append\", \"__getitem__\"):\n",
" getattr(z, method)(0)\n",
"\n",
"# In this loop, the first time we call __getitem__, so we only know about that, not append, so we don't know this mutates z\n",
"for method in (\"__getitem__\", \"append\"):\n",
" getattr(z, method)(0)\n",
"\n",
"print(save(z, 'z').code)"
]
},
{
"cell_type": "markdown",
"id": "5bea3916-306f-436c-8098-7f297da52c89",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"## User Defined Functions"
]
},
{
"cell_type": "markdown",
"id": "8926da09-726f-4697-b921-0c2312d7258c",
"metadata": {},
"source": [
"We also support some limited analysis of user defined functions.\n",
"\n",
"We don't track what functions are called inside of them, so we don't know if they mutate their args, so we assume any global they access was mutated:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "815ccd6e-285e-4fd6-b34a-b737b359661c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"def use_global():\n",
" return a + 10\n",
"a = 400\n",
"z = use_global()\n",
"\n"
]
}
],
"source": [
"# we know that calling this function will depend on the `a` global\n",
"def use_global():\n",
" return a + 10\n",
"\n",
"a = 400\n",
"\n",
"z = use_global()\n",
"\n",
"print(save(z, \"z\").code)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "3d0c8946-71ab-40e6-b15e-29543c91e609",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"def append_global():\n",
" return a.append(10)\n",
"a = []\n",
"append_global()\n",
"\n"
]
}
],
"source": [
"# we know this function uses `a`, but we don't know that it updates it. We assume it does\n",
"def append_global():\n",
" return a.append(10)\n",
"\n",
"a = []\n",
"\n",
"append_global()\n",
"\n",
"print(save(a, \"a\").code)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "b3e24b4e-07f4-4d2c-b9ec-3164b8673cfc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"def len_global():\n",
" return len(a)\n",
"a = []\n",
"len_global()\n",
"\n"
]
}
],
"source": [
"# we assume this mutates a, even if it does not!\n",
"def len_global():\n",
" return len(a)\n",
"\n",
"a = []\n",
"\n",
"len_global()\n",
"\n",
"print(save(a, \"a\").code)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f06bc614-df67-47f2-9a61-060f62712fd1",
"id": "47d5c325-0ad1-4df2-a585-cb126d23d604",
"metadata": {},
"outputs": [],
"source": []
Expand Down

0 comments on commit d4e15c3

Please sign in to comment.