Skip to content

Conversation

@yungyuc
Copy link
Member

@yungyuc yungyuc commented Oct 29, 2022

Use Py_BytesMain for the "python" mode of viewer. Py_BytesMain is the stable API to enter the standard interpreter from an embedding executable: https://docs.python.org/3.10/c-api/veryhigh.html#c.Py_BytesMain

Create the new class ProcessInfo and the member datum CommandLineInfo. CommandLineInfo provides the flags processed from the command line arguments to provide information for the execution mode.

Py_BytesMain is the stable API to enter the standard interpreter from an embedding executable: https://docs.python.org/3.10/c-api/veryhigh.html#c.Py_BytesMain

Create the new class ProcessInfo and the member datum CommandLineInfo.  CommandLineInfo provides the flags processed from the command line arguments to provide information for the execution mode.
@yungyuc yungyuc added the enhancement New feature or request label Oct 29, 2022

if (clinfo.python_main())
{
ret = Py_BytesMain(clinfo.python_main_argc(), clinfo.python_main_argv_ptr());
Copy link
Member Author

Choose a reason for hiding this comment

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

Py_BytesMain() finalizes the interpreter when returning.

modmesh::python::Interpreter::instance().setup_process(argc, argv);
modmesh::python::Interpreter::instance().finalize();
return 0;
return ret;
Copy link
Member Author

Choose a reason for hiding this comment

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

Return the error code.

#include <stdexcept>
#include <array>
#include <vector>
#include <algorithm>
Copy link
Member Author

Choose a reason for hiding this comment

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

For std::copy_n.

Interpreter & setup_process(int argc, char ** argv);
Interpreter & setup_process();

int enter_main();
Copy link
Member Author

Choose a reason for hiding this comment

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

Add the "main" an inverse wrapper to call the Python entry function from C++.

for (int i = 0; i < argc; ++i)
{
std::string const & arg = m_populated_argv[i];
if ("--mode=python" == arg)
Copy link
Member Author

Choose a reason for hiding this comment

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

Remove --mode=python from the command-line argument for Py_BytesMain.

@yungyuc yungyuc self-assigned this Oct 29, 2022
@yungyuc
Copy link
Member Author

yungyuc commented Oct 29, 2022

Take command:

$ build/clion/debugtidy/cpp/binary/viewer/viewer.app/Contents/MacOS/viewer --mode=python -c 'print(mm)'
<module 'modmesh' from '/Users/yungyuc/work/modmesh/modmesh/__init__.py'>

Interactive mode:

$ build/clion/debugtidy/cpp/binary/viewer/viewer.app/Contents/MacOS/viewer --mode=python
Python 3.9.10+ (heads/3.9:76b072717a, Feb  8 2022, 19:44:52) 
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Run module:

$ build/clion/debugtidy/cpp/binary/viewer/viewer.app/Contents/MacOS/viewer --mode=python -m IPython
Python 3.9.10+ (heads/3.9:76b072717a, Feb  8 2022, 19:44:52) 
Type 'copyright', 'credits' or 'license' for more information
IPython 8.0.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: 

@yungyuc yungyuc merged commit ddf5279 into solvcon:master Oct 29, 2022
@yungyuc yungyuc deleted the feature/pymain branch October 29, 2022 09:07
@yungyuc
Copy link
Member Author

yungyuc commented Oct 29, 2022

This PR makes #103 more complete.

@yungyuc
Copy link
Member Author

yungyuc commented Oct 29, 2022

The new ProcessInfo and CommandLineInfo also help #84

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant