Skip to content

add keyword argument to import-module to import submodules#30

Open
digikar99 wants to merge 1 commit intobendudson:masterfrom
digikar99:py4cl2-backport-import-improvements
Open

add keyword argument to import-module to import submodules#30
digikar99 wants to merge 1 commit intobendudson:masterfrom
digikar99:py4cl2-backport-import-improvements

Conversation

@digikar99
Copy link
Copy Markdown
Contributor

DO NOT MERGE YET!

In addition to this, there's one more change (not yet backported/committed) that adds import statements to the start of every function. So, if numpy were imported as np, and one were to call np:add after the process stops, one gets a numpy not defined error. To avoid this, the first statement of np:add becomes (python-exec "import numpy as np"). Should this be committed?

I don't think this breaks anyone's code; one may get a performance penalty though; but as it is, this project does not have much focus on speed.

DO NOT MERGE YET!

@bendudson
Copy link
Copy Markdown
Owner

Thanks for this! How does the code work out which imports to include? Is this just replaying any python-import calls the user has made, so they persist across python sessions? I agree that the performance penalty should not be a big issue: Python eliminates most of the cost of repeated imports, and py4cl calls in tight loops is likely to always be costly. I think my worry is more that other things in the python process (like variables, functions etc.) are not persistent across python process restarts, so this introduces a special handling for imports. It may also be that users want to restart a python session to get rid of an import.

@digikar99
Copy link
Copy Markdown
Contributor Author

How does the code work out which imports to include?

This simply during the calls to import-function and import-module. In py4cl2, I have kept this as an optional :safety keyword parameter to defpyfun and defpymodule. Hmm, keeping the :safety nil instead of the default t in py4cl2 should avoid breaking any possible user code; the statements are only added if safety is t.

So, the source form of math:sqrt is

(LAMBDA (&OPTIONAL (MATH::X 'NIL))
      "Return the square root of x."
      (BLOCK MATH:SQRT
        NIL
        (PYEXEC "import math") ; <================================ the addition of this line
        (RAW-PYEVAL "math.sqrt" "(" (PYTHONIZE MATH::X) "," ")")))

instead of

(LAMBDA (&REST PY4CL::ARGS)
      "Return the square root of x."
      (BLOCK MATH:SQRT (APPLY #'PY4CL:PYTHON-CALL "math.sqrt" PY4CL::ARGS)))

(Ignore the other changes)

@digikar99
Copy link
Copy Markdown
Contributor Author

Oh yes, should I proceed with the :safety?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants