-
Notifications
You must be signed in to change notification settings - Fork 772
Build single Python.Runtime.dll for all platforms #1365
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
Merged
lostmsu
merged 21 commits into
pythonnet:master
from
losttech:features/VersionIndependent
Jan 28, 2021
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
909ed1f
dropped net40 target from modern projects
lostmsu 47e926e
use .NET Standard 2.0 platform detection features
lostmsu 21683b3
drop NativeCodePage alltogether
lostmsu 972c41d
WIP: use C# 9 function pointers for PInvoke
lostmsu 51e5184
allow setting PythonDLL
lostmsu 2498d47
always explicitly specify the way strings are marshaled
lostmsu 70fc803
CI: figure out DLL name from environment
lostmsu 28a5dab
use Roslyn preview in CI
lostmsu c75229a
fixed Linux and Mac DLL loaders breaking dll path
lostmsu a0a1dc1
correctly detect DLL on *nix when running from Python
lostmsu 1b88783
Windows library loader: add support for hModule == 0
lostmsu 2c1aaef
fix dll loading in tests
lostmsu 39e41d0
mentiond PythonDLL in changelog
lostmsu 17040fe
set PYDLL in AppVeyor
lostmsu b7410b6
revert automatically added 'm' suffix for *nix default dll name
lostmsu 275cae9
specify full DLL name instead of PYVER in GH Actions
lostmsu b4cb37e
use Microsoft.Net.Compilers.Toolset instead of Microsoft.Net.Compilers
lostmsu f68e581
in CI MacOS python DLL has 'm' suffix
lostmsu cda604a
only set PYTHONNET_PYDLL for test runs from .NET
lostmsu 3982892
workaround for pytest/clr module not preloading python C API library
lostmsu a6cbe20
addressed a few code comments
lostmsu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
always explicitly specify the way strings are marshaled
- Loading branch information
commit 2498d474a3ae8e84d505680de5a467be5b5c4e9f
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| using System; | ||
|
|
||
| namespace Python.Runtime.Native | ||
| { | ||
| [Flags] | ||
| enum PyCompilerFlags | ||
| { | ||
| SOURCE_IS_UTF8 = 0x0100, | ||
| DONT_IMPLY_DEDENT = 0x0200, | ||
| ONLY_AST = 0x0400, | ||
| IGNORE_COOKIE = 0x0800, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #nullable enable | ||
| using System; | ||
| using System.Runtime.InteropServices; | ||
| using System.Text; | ||
|
|
||
| namespace Python.Runtime.Native | ||
| { | ||
| [StructLayout(LayoutKind.Sequential)] | ||
| struct StrPtr : IDisposable | ||
| { | ||
| public IntPtr RawPointer { get; set; } | ||
| unsafe byte* Bytes => (byte*)this.RawPointer; | ||
|
|
||
| public unsafe StrPtr(string value, Encoding encoding) | ||
| { | ||
| if (value is null) throw new ArgumentNullException(nameof(value)); | ||
| if (encoding is null) throw new ArgumentNullException(nameof(encoding)); | ||
|
|
||
| var bytes = encoding.GetBytes(value); | ||
| this.RawPointer = Marshal.AllocHGlobal(checked(bytes.Length + 1)); | ||
| try | ||
| { | ||
| Marshal.Copy(bytes, 0, this.RawPointer, bytes.Length); | ||
| this.Bytes[bytes.Length] = 0; | ||
| } | ||
| catch | ||
| { | ||
| this.Dispose(); | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
| public unsafe string? ToString(Encoding encoding) | ||
| { | ||
| if (encoding is null) throw new ArgumentNullException(nameof(encoding)); | ||
| if (this.RawPointer == IntPtr.Zero) return null; | ||
|
|
||
| return encoding.GetString((byte*)this.RawPointer, byteCount: checked((int)this.ByteCount)); | ||
| } | ||
|
|
||
| public unsafe nuint ByteCount | ||
| { | ||
| get | ||
| { | ||
| if (this.RawPointer == IntPtr.Zero) throw new NullReferenceException(); | ||
|
|
||
| nuint zeroIndex = 0; | ||
| while (this.Bytes[zeroIndex] != 0) | ||
| { | ||
| zeroIndex++; | ||
| } | ||
| return zeroIndex; | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| if (this.RawPointer == IntPtr.Zero) | ||
| return; | ||
|
|
||
| Marshal.FreeHGlobal(this.RawPointer); | ||
| this.RawPointer = IntPtr.Zero; | ||
| } | ||
|
|
||
| internal static Encoding GetEncodingByPythonName(string pyEncodingName) | ||
| { | ||
| // https://stackoverflow.com/a/7798749/231238 | ||
| if (pyEncodingName == "mbcs") return Encoding.Default; | ||
|
|
||
| return Encoding.GetEncoding(pyEncodingName); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
NativeStringwould be more appropriate?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming things :-D
Not sure about
NativeString. It has connotations to native string type supported on the platform, but this is not it.It might make sense to have a
StrPtrtype per encoding for clarity. Then signatures could enforce it. But maybe that would be overengineering.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we use any encoding apart from UTF8? I thought all
FromStringfunctions nowadays used that.Indeed, I find exactly one usage with
Encoding.ASCII, everything else isUTF8. And the one case that uses ASCII isPyBuffer_SizeFromFormat, which doesn't specify ASCII encoding either, I highly doubt it will break if it's passed UTF8 instead as it will probably just compare bytes directly and bail out on anything that it doesn't understand.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main thing I find "problematic" is that this object is not a pure pointer, it has ownership over the buffer it points to.