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

Is it possible to combine Py.Import.add_module and Py.Run.eval ? #78

Closed
UnixJunkie opened this issue Jan 31, 2022 · 3 comments
Closed

Comments

@UnixJunkie
Copy link

i.e. I want to interpret some python lines (at run-time; e.g. using Py.Run.eval) and declare that this defines a new module
with a given name (like Py.Import.add_module does).

In other word, I would like pyml to be able to interpret some python code that is hard-coded into
my ocaml program, to define a new python module at run-time.
However, this Python module source file would not be installed on the system (because I don't want to have to,
so that the ocaml executable/library is easier to deploy).

I suspect pyml already provides this functionality, but I could not find it.

Thanks,
F.

@thierry-martinez
Copy link
Owner

thierry-martinez commented Jan 31, 2022

You may combine Py.compile and Py.Import.exec_code_module to import a module from a string. For instance, you may declare the following function:

let import_python_source ~module_name ~filename ~source =
  let bytecode = Py.compile ~filename ~source `Exec in
  Py.Import.exec_code_module module_name bytecode

and use it as follows:

let () =
  Py.initialize ~version:3 ();
  let m = import_python_source ~module_name:"toto" ~filename:"toto.py"
    ~source:{|
def f():
  print("Hello, world!")
|} in
  assert (Py.Module.get_function m "f" [| |] = Py.none)

@UnixJunkie
Copy link
Author

Ok, thanks a lot!
If I manage to make it work, I might send a PR to update the documentation.

@UnixJunkie
Copy link
Author

was related to #25

thierry-martinez added a commit to thierry-martinez/opam-repository that referenced this issue Mar 22, 2022
This pull-request publishes a new release for pyml (2022-03-22):

- New function `Py.Import.exec_code_module_from_string`
  (suggested by Francois Berenger, thierry-martinez/pyml#78)

- New function `Py.Module.compile` provides a better API than `Py.compile`.

- `Py.Object.t` can now be serialized (with Marshal or output_value), using Python
  pickle module

- Cross-compiling friendly architecture detection
  (suggested by @EduardoRFS, https://discuss.ocaml.org/t/a-zoo-of-values-for-system/8525/20)

- Detect macro `unix` instead of `__linux__`, to handle *BSD OSes
  (reported by Chris Pinnock, thierry-martinez/pyml#74)

- Fix bug in Windows

- Null checks for many functions raising OCaml exceptions, instead of segmentation fault
  (initial implementation by Laurent Mazare, thierry-martinez/pyml#72)

- Fix wide character conversion bugs leading to segmentation fault in Py_wfopen
  (fixed by Jerry James, thierry-martinez/pyml#75)

- `Gc.full_major ()` before unloading `libpython` in `Py.finalize`, to prevent
  segfaulting on finalizing dangling references to Python values after the library
  had been unloaded
  (reported by Denis Efremov on coccinelle mailing list)

- Fix segmentation fault when `~debug_build:true` was passed to `Py.initialize`
  (reported by Stéphane Glondu, thierry-martinez/pyml#79)
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

No branches or pull requests

2 participants