Skip to content

Added ToPythonAs() extension method for explicit conversion using an arbitrary type #2419

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Update authors and changelog
@microsoft-github-policy-service agree
  • Loading branch information
joaompneves committed Jul 18, 2024
commit 1073b03ef600925040721ea5bca0160eff6ecdb5
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- Jan Krivanek ([@jakrivan](https://github.com/jakrivan))
- Jeff Reback ([@jreback](https://github.com/jreback))
- Jeff Robbins ([@jeff17robbins](https://github.com/jeff17robbins))
- João Neves ([@joaompneves](https://github.com/joaompneves))
- Joe Frayne ([@jfrayne](https://github.com/jfrayne))
- Joe Lidbetter ([@jmlidbetter](https://github.com/jmlidbetter))
- Joe Savage ([@s4v4g3](https://github.com/s4v4g3))
Expand Down
87 changes: 40 additions & 47 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].

### Added

- Added `ToPythonAs()` extension method to allow for explicit conversion using a specific type not known at build time. ([#2419][i2419])

- Added `ToPythonAs<T>()` extension method to allow for explicit conversion using a specific type. ([#2311][i2311])

- Added `IComparable` and `IEquatable` implementations to `PyInt`, `PyFloat`, and `PyString`
to compare with primitive .NET types like `long`.
to compare with primitive .NET types like `long`.

### Changed

Expand Down Expand Up @@ -56,7 +58,6 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Fixed `PyInt` conversion to `BigInteger` and `System.String` produced incorrect result for values between 128 and 255.
- Fixed implementing a generic interface with a Python class


## [3.0.0](https://github.com/pythonnet/pythonnet/releases/tag/v3.0.0) - 2022-09-29

### Added
Expand All @@ -67,61 +68,63 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Ability to implement delegates with `ref` and `out` parameters in Python, by returning the modified parameter values in a tuple. ([#1355][i1355])
- Ability to override .NET methods that have `out` or `ref` in Python by returning the modified parameter values in a tuple. ([#1481][i1481])
- `PyType` - a wrapper for Python type objects, that also permits creating new heap types from `TypeSpec`
- Improved exception handling:
* exceptions can now be converted with codecs
* `InnerException` and `__cause__` are propagated properly
- Improved exception handling:

* exceptions can now be converted with codecs
* `InnerException` and `__cause__` are propagated properly

- `__name__` and `__signature__` to reflected .NET methods
- .NET collection types now implement standard Python collection interfaces from `collections.abc`.
See [Mixins/collections.py](src/runtime/Mixins/collections.py).
See [Mixins/collections.py](src/runtime/Mixins/collections.py).
- you can cast objects to generic .NET interfaces without specifying generic arguments as long as there is no ambiguity.
- .NET arrays implement Python buffer protocol
- Python integer interoperability with `System.Numerics.BigInteger`
- Python.NET will correctly resolve .NET methods, that accept `PyList`, `PyInt`,
and other `PyObject` derived types when called from Python.
and other `PyObject` derived types when called from Python.
- .NET classes, that have `__call__` method are callable from Python
- `PyIterable` type, that wraps any iterable object in Python
- `PythonEngine` properties for supported Python versions: `MinSupportedVersion`, `MaxSupportedVersion`, and `IsSupportedVersion`
- The runtime that is loaded on `import clr` can now be configured via environment variables


### Changed

- Drop support for Python 2, 3.4, 3.5, and 3.6
- `wchar_t` size aka `Runtime.UCS` is now determined at runtime
- `clr.AddReference` may now throw errors besides `FileNotFoundException`, that provide more
details about the cause of the failure
details about the cause of the failure
- `clr.AddReference` no longer adds ".dll" implicitly
- `PyIter(PyObject)` constructor replaced with static `PyIter.GetIter(PyObject)` method
- Python runtime can no longer be shut down if the Python error indicator is set, as it would have unpredictable behavior
- BREAKING: Return values from .NET methods that return an interface are now automatically
wrapped in that interface. This is a breaking change for users that rely on being
able to access members that are part of the implementation class, but not the
interface. Use the new `__implementation__` or `__raw_implementation__` properties to
if you need to "downcast" to the implementation class.
wrapped in that interface. This is a breaking change for users that rely on being
able to access members that are part of the implementation class, but not the
interface. Use the new `__implementation__` or `__raw_implementation__` properties to
if you need to "downcast" to the implementation class.
- BREAKING: `==` and `!=` operators on `PyObject` instances now use Python comparison
(previously was equivalent to `object.ReferenceEquals(,)`)
(previously was equivalent to `object.ReferenceEquals(,)`)
- BREAKING: Parameters marked with `ParameterAttributes.Out` are no longer returned in addition
to the regular method return value (unless they are passed with `ref` or `out` keyword).
- BREAKING: Drop support for the long-deprecated CLR.* prefix.
to the regular method return value (unless they are passed with `ref` or `out` keyword).
- BREAKING: Drop support for the long-deprecated CLR.\* prefix.
- `PyObject` now implements `IEnumerable<PyObject>` in addition to `IEnumerable`
- floating point values passed from Python are no longer silently truncated
when .NET expects an integer [#1342][i1342]
when .NET expects an integer [#1342][i1342]
- More specific error messages for method argument mismatch
- members of `PyObject` inherited from `System.Object and `DynamicObject` now autoacquire GIL
- BREAKING: when inheriting from .NET types in Python if you override `__init__` you
must explicitly call base constructor using `super().__init__(.....)`. Not doing so will lead
to undefined behavior.
must explicitly call base constructor using `super().__init__(.....)`. Not doing so will lead
to undefined behavior.
- BREAKING: most `PyScope` methods will never return `null`. Instead, `PyObject` `None` will be returned.
- BREAKING: `PyScope` was renamed to `PyModule`
- BREAKING: Methods with `ref` or `out` parameters and void return type return a tuple of only the `ref` and `out` parameters.
- BREAKING: to call Python from .NET `Runtime.PythonDLL` property must be set to Python DLL name
or the DLL must be loaded in advance. This must be done before calling any other Python.NET functions.
or the DLL must be loaded in advance. This must be done before calling any other Python.NET functions.
- BREAKING: `PyObject.Length()` now raises a `PythonException` when object does not support a concept of length.
- BREAKING: disabled implicit conversion from C# enums to Python `int` and back.
One must now either use enum members (e.g. `MyEnum.Option`), or use enum constructor
(e.g. `MyEnum(42)` or `MyEnum(42, True)` when `MyEnum` does not have a member with value 42).
One must now either use enum members (e.g. `MyEnum.Option`), or use enum constructor
(e.g. `MyEnum(42)` or `MyEnum(42, True)` when `MyEnum` does not have a member with value 42).
- BREAKING: disabled implicit conversion from Python objects implementing sequence protocol to
.NET arrays when the target .NET type is `System.Object`. The conversion is still attempted when the
target type is a `System.Array`.
.NET arrays when the target .NET type is `System.Object`. The conversion is still attempted when the
target type is a `System.Array`.
- Sign Runtime DLL with a strong name
- Implement loading through `clr_loader` instead of the included `ClrModule`, enables
support for .NET Core
Expand All @@ -131,23 +134,23 @@ target type is a `System.Array`.
- Replaced the old `__import__` hook hack with a PEP302-style Meta Path Loader
- BREAKING: Names of .NET types (e.g. `str(__class__)`) changed to better support generic types
- BREAKING: overload resolution will no longer prefer basic types. Instead, first matching overload will
be chosen.
be chosen.
- BREAKING: acquiring GIL using `Py.GIL` no longer forces `PythonEngine` to initialize
- BREAKING: `Exec` and `Eval` from `PythonEngine` no longer accept raw pointers.
- BREAKING: .NET collections and arrays are no longer automatically converted to
Python collections. Instead, they implement standard Python
collection interfaces from `collections.abc`.
See [Mixins/collections.py](src/runtime/Mixins/collections.py).
Python collections. Instead, they implement standard Python
collection interfaces from `collections.abc`.
See [Mixins/collections.py](src/runtime/Mixins/collections.py).
- BREAKING: When trying to convert Python `int` to `System.Object`, result will
be of type `PyInt` instead of `System.Int32` due to possible loss of information.
Python `float` will continue to be converted to `System.Double`.
be of type `PyInt` instead of `System.Int32` due to possible loss of information.
Python `float` will continue to be converted to `System.Double`.
- BREAKING: Python.NET will no longer implicitly convert types like `numpy.float64`, that implement `__float__` to
`System.Single` and `System.Double`. An explicit conversion is required on Python or .NET side.
`System.Single` and `System.Double`. An explicit conversion is required on Python or .NET side.
- BREAKING: `PyObject.GetHashCode` can fail.
- BREAKING: Python.NET will no longer implicitly convert any Python object to `System.Boolean`.
- BREAKING: `PyObject.GetAttr(name, default)` now only ignores `AttributeError` (previously ignored all exceptions).
- BREAKING: `PyObject` no longer implements `IEnumerable<PyObject>`.
Instead, `PyIterable` does that.
Instead, `PyIterable` does that.
- BREAKING: `IPyObjectDecoder.CanDecode` `objectType` parameter type changed from `PyObject` to `PyType`

### Fixed
Expand Down Expand Up @@ -179,18 +182,19 @@ Instead, `PyIterable` does that.
### Removed

- `ShutdownMode` has been removed. The only shutdown mode supported now is an equivalent of `ShutdownMode.Reload`.
There is no need to specify it.
There is no need to specify it.
- implicit assembly loading (you have to explicitly `clr.AddReference` before doing import)
- messages in `PythonException` no longer start with exception type
- `PyScopeManager`, `PyScopeException`, `PyScope` (use `PyModule` instead)
- support for .NET Framework 4.0-4.6; Mono before 5.4. Python.NET now requires .NET Standard 2.0
(see [the matrix](https://docs.microsoft.com/en-us/dotnet/standard/net-standard#net-implementation-support))
(see [the matrix](https://docs.microsoft.com/en-us/dotnet/standard/net-standard#net-implementation-support))

## [2.5.2](https://github.com/pythonnet/pythonnet/releases/tag/v2.5.2) - 2021-02-05

Bugfix release.

### Fixed

- Fix `object[]` parameters taking precedence when should not in overload resolution
- Empty parameter names (as can be generated from F#) do not cause crashes

Expand All @@ -200,8 +204,8 @@ Bugfix release.

### Fixed

- Fix incorrect dereference of wrapper object in `tp_repr`, which may result in a program crash
- Fix incorrect dereference in params array handling
- Fix incorrect dereference of wrapper object in `tp_repr`, which may result in a program crash
- Fix incorrect dereference in params array handling

## [2.5.0](https://github.com/pythonnet/pythonnet/releases/tag/v2.5.0) - 2020-06-14

Expand Down Expand Up @@ -308,7 +312,6 @@ This version improves performance on benchmarks significantly compared to 2.3.
- Fix spurious assembly loading exceptions from private types ([#703][i703])
- Fix inheritance of non-abstract base methods ([#755][i755])


## [2.3.0][] - 2017-03-11

### Added
Expand Down Expand Up @@ -845,25 +848,15 @@ This version improves performance on benchmarks significantly compared to 2.3.
- Initial (mostly) working experimental release.

[keep a changelog]: http://keepachangelog.com/

[semantic versioning]: http://semver.org/

[unreleased]: ../../compare/v3.0.1...HEAD

[2.3.0]: ../../compare/v2.2.2...v2.3.0

[2.2.2]: ../../compare/v2.2.1...v2.2.2

[2.2.1]: ../../compare/v2.2.0-dev1...v2.2.1

[2.2.0-dev1]: ../../compare/v2.1.0...v2.2.0-dev1

[2.1.0]: ../../compare/v2.0.0...v2.1.0

[2.0.0]: ../../compare/1.0...v2.0.0

[1.0.0]: https://github.com/pythonnet/pythonnet/releases/tag/1.0

[i714]: https://github.com/pythonnet/pythonnet/issues/714
[i608]: https://github.com/pythonnet/pythonnet/issues/608
[i443]: https://github.com/pythonnet/pythonnet/issues/443
Expand Down