Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[WIP] Add ability to create module from C##1447

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
lostmsu merged 7 commits intopythonnet:masterfromslide:addmodule
Jul 29, 2021
Merged

[WIP] Add ability to create module from C##1447

lostmsu merged 7 commits intopythonnet:masterfromslide:addmodule
Jul 29, 2021

Conversation

@slide
Copy link
Contributor

What does this implement/fix? Explain your changes.

This allows doing something like this, which I haven't found any other way to do:

varmod=PythonEngine.AddModule("mymod");varmyobj=newobject();mod.SetAttr("myprop".ToPython(),myobj.ToPython());
importmymodprint(mymod.myprop)

This is very useful for providing objects to an embedded Python engine.

Does this close any currently open issues?

I could not find any specific issues that would be related.

Any other comments?

This is an initial PR to get feedback if this would be allowed, I have a build of the library locally with this change and use this in my code, but would like to have it upstream if possible. I will fill out the remaining details and add tests if folks think this would be ok.

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • If an enhancement PR, please create docs and at best an example
  • Add yourself toAUTHORS
  • Updated theCHANGELOG

@lostmsu
Copy link
Member

We now havehttps://github.com/pythonnet/pythonnet/blob/master/src/runtime/pymodule.cs
I think it would make sense to give it a static factory methodCreate instead of placing code intoPythonEngine.

Also, you should probably use

internalstaticNewReferencePyModule_New(stringname)

@slide
Copy link
ContributorAuthor

Cool, I didn't know about pymodule.cs, I'll definitely migrate there and use the PyModule_New


namespacePython.EmbeddingTest
{
classTestPyModule
Copy link
Member

Choose a reason for hiding this comment

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

I think test class must bepublic.

returnnewPyModule(refm);
}

publicstaticPyModuleCreate(stringname,stringfilename="none")
Copy link
Member

@lostmsulostmsuApr 27, 2021
edited
Loading

Choose a reason for hiding this comment

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

This method violates single responsibility principle.

First,Create should never return an existing module. Instead, the caller should be able to check if module already exists by name by calling a separate function.

Second, I am uncertain about setting up__builtins__ by default. It would be great to know why Python does not do it before discarding that potentially good rationale. Users can always manually donewModule.Dict.Set("__builtins__", GetBuiltIns()). If this is a common scenario in your product, just make a factory method combining the two.

Third, I don't think adding new module tosys.modules right away is a good idea. Especially, overwriting the existing value. This might cause hard to debug surprise issues.

Copy link
ContributorAuthor

@slideslideJul 27, 2021
edited
Loading

Choose a reason for hiding this comment

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

Second, I am uncertain about setting up__builtins__ by default. It would be great to know why Python does not do it before discarding that potentially good rationale. Users can always manually donewModule.Dict.Set("__builtins__", GetBuiltIns()). If this is a common scenario in your product, just make a factory method combining the two.

I didn't see a public method that would allow access to the __builtins__. There is an internal method, onRuntime, but not a public method that could be used outside.

PythonException.ThrowIfIsNull(__builtins__);
intrc=Runtime.PyDict_SetItemString(globals,"__builtins__",__builtins__);
PythonException.ThrowIfIsNotZero(rc);
rc=Runtime.PyDict_SetItemString(globals,"__file__",newBorrowedReference(filename.ToPython().Handle));
Copy link
Member

Choose a reason for hiding this comment

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

The default value forfilename should benull and when it is null__file__ should simply not be set.

@LimpingNinja
Copy link

This is a good add feature, is anyone working on it? I am migrating from C where I create a handful of accessible modules from C using Py_InitModule3() and passing in the methods (C function pointers) and vars. Is there an alternative method here or is this something that would pend this change being actioned?

@slide
Copy link
ContributorAuthor

I need to get back to it and make the changes that were requested. I just have a local compiled version with my changes that I have been using, so I haven't had a huge push to get me back to fixing this up. I'll try and work on it soon.

@slide
Copy link
ContributorAuthor

@lostmsu I think I addressed the feedback you provided, please let me know if there are more changes you would like me to make. If not, I will make this a normal PR and remove the WIP.

@slide
Copy link
ContributorAuthor

Feedback hopefully addressed. Thanks for being patient. I still don't know the codebase very well.

LimpingNinja reacted with thumbs up emojiLimpingNinja reacted with hooray emoji

@lostmsulostmsu merged commit9555248 intopythonnet:masterJul 29, 2021
@slideslide deleted the addmodule branchSeptember 29, 2022 15:26
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@lostmsulostmsulostmsu approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@slide@lostmsu@LimpingNinja

[8]ページ先頭

©2009-2026 Movatter.jp