Skip to content

Instantly share code, notes, and snippets.

@shtaxxx
Last active February 14, 2016 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shtaxxx/0b31b67e3ac2d02a4487 to your computer and use it in GitHub Desktop.
Save shtaxxx/0b31b67e3ac2d02a4487 to your computer and use it in GitHub Desktop.
Test using inspect.currentframe()
from __future__ import absolute_import
from __future__ import print_function
import os
import sys
import inspect
import ast
import types
import textwrap
def foo():
a = 100
b = 200
return a + b
def bar():
return 0
def hoge():
c = 300
return c + foo() + bar()
def thread(method):
frame = inspect.currentframe()
_locals = frame.f_back.f_locals
_globals = frame.f_back.f_globals
for key, value in _locals.items():
if isinstance(value, types.FunctionType):
print("FunctionType", key)
for key, value in _globals.items():
if isinstance(value, types.FunctionType):
print("FunctionType", key)
source = inspect.getsource(method)
source = textwrap.dedent(source)
print(source)
_ast = ast.parse(source)
print(_ast)
thread(hoge)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment