Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: can not run in Python repl #139

Merged
merged 4 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion greenplumpython/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,24 @@ def create_in_db(self, db: Database) -> None:
for param in func_sig.parameters.values()
]
)
# make inspect.getsource can run in Python REPL(IPython do not have this issue)

# CPython issue https://github.com/python/cpython/issues/57129
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some thoughts after leaning this magical library:

Suggested change
# CPython issue https://github.com/python/cpython/issues/57129
# CPython issue https://github.com/python/cpython/issues/57129
# TODO: in the future, we might want to use `dill.dumps(func, recurse=True)`
# to send the function to the DBMS with dependencies like imports.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that will be wonderful we will use this library more not only the getsource things

# TODO: in the future, we might want to use `dill.dumps(func, recurse=True)`
# to send the function to the DBMS with dependencies like imports.
try:
source: str = inspect.getsource(self._wrapped_func)
# if run inspect.getsource(func) in REPL will raise IOError
# that func is not buildin
except IOError:
# use dill library to bypass that
from dill.source import getsource # type: ignore

source = getsource(self._wrapped_func)

# -- Loading Python code of Function
# FIXME: include things in func.__closure__
func_lines = textwrap.dedent(inspect.getsource(self._wrapped_func)).split("\n")
func_lines = textwrap.dedent(source).split("\n")
func_body = "\n".join([line for line in func_lines if re.match(r"^\s", line)])
return_type = to_pg_type(func_sig.return_annotation, db, for_return=True)
assert (
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ psycopg2==2.9.3
# Pin the setuptools version. The latest one seems to have problem with CFLAGS.
# See https://github.com/pypa/setuptools/commit/bd7613f7921e8d60fa089d3ab419d0f04db6db6f
setuptools==65.5.1
dill==0.3.6