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

Compiler conversions #171

Merged
merged 22 commits into from
Aug 28, 2024
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1676815
Create an overload for generic collections to let the compiler do all…
tonybaloney Aug 27, 2024
1bb34a6
Generic dictionaries
tonybaloney Aug 27, 2024
a3ca0e8
Undo program changes for profiling
tonybaloney Aug 27, 2024
79ed334
Add tests
tonybaloney Aug 27, 2024
c5ab40b
Add missing GIL lock
tonybaloney Aug 27, 2024
fce0fcc
Reducing down the number of public methods for to/from PyObject
aaronpowell Aug 27, 2024
3719404
Merge branch 'compiler_conversions' of https://github.com/tonybaloney…
aaronpowell Aug 27, 2024
9c9ca30
Make sure all references to the python exception factory throw
tonybaloney Aug 27, 2024
f502984
Don't hard code python paths Anthony
aaronpowell Aug 27, 2024
fa174a2
Do error checks inside Python interop classes
tonybaloney Aug 27, 2024
97b34c0
Merge branch 'compiler_conversions' of https://github.com/tonybaloney…
tonybaloney Aug 27, 2024
768da2b
Fixing tests by going back via the PyObject.As method
aaronpowell Aug 27, 2024
91c763a
Merge branch 'compiler_conversions' of https://github.com/tonybaloney…
aaronpowell Aug 27, 2024
7591b66
Add missing throw
tonybaloney Aug 27, 2024
7057490
Add more missing throws
tonybaloney Aug 27, 2024
a65481c
Merge branch 'compiler_conversions' of https://github.com/tonybaloney…
tonybaloney Aug 27, 2024
9031345
Fixed some tuple tests
aaronpowell Aug 27, 2024
53134a6
Check if something is a tuple or generator before converting
tonybaloney Aug 27, 2024
074a727
Merge branch 'compiler_conversions' of https://github.com/tonybaloney…
tonybaloney Aug 27, 2024
05ad7ca
Fixing first-level tuple nesting
aaronpowell Aug 27, 2024
7e9e299
Move the PyTuple_Size call back to after the type check
tonybaloney Aug 27, 2024
2fc6027
Put GIL dispose requests in a queue instead of running them in the fi…
tonybaloney Aug 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Do error checks inside Python interop classes
  • Loading branch information
tonybaloney committed Aug 27, 2024
commit fa174a2adc228226c272998945f8ca6bcce6b156
14 changes: 12 additions & 2 deletions src/CSnakes.Runtime/CPython/Float.cs
Original file line number Diff line number Diff line change
@@ -15,13 +15,23 @@ internal unsafe partial class CPythonAPI
[LibraryImport(PythonLibraryName)]
internal static partial nint PyFloat_FromDouble(double value);

internal static double PyFloat_AsDouble(PyObject p)
{
double result = PyFloat_AsDouble_(p);
if (result == -1 && PyErr_Occurred())
{
throw PyObject.ThrowPythonExceptionAsClrException("Error converting Python object to double, check that the object was a Python float. See InnerException for details.");
}
return result;
}

/// <summary>
/// Convery a PyFloat to a C double
/// </summary>
/// <param name="p"></param>
/// <returns>The double value</returns>
[LibraryImport(PythonLibraryName)]
internal static partial double PyFloat_AsDouble(PyObject obj);
[LibraryImport(PythonLibraryName, EntryPoint = "PyFloat_AsDouble")]
private static partial double PyFloat_AsDouble_(PyObject obj);

internal static bool IsPyFloat(PyObject p)
{
38 changes: 34 additions & 4 deletions src/CSnakes.Runtime/CPython/Long.cs
Original file line number Diff line number Diff line change
@@ -13,11 +13,41 @@ internal unsafe partial class CPythonAPI
[LibraryImport(PythonLibraryName)]
internal static partial nint PyLong_FromLongLong(long v);

[LibraryImport(PythonLibraryName)]
internal static partial long PyLong_AsLongLong(PyObject p);
/// <summary>
/// Calls PyLong_AsLongLong and throws a Python Exception if an error occurs.
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
internal static long PyLong_AsLongLong(PyObject p)
{
long result = PyLong_AsLongLong_(p);
if (result == -1 && PyErr_Occurred())
{
throw PyObject.ThrowPythonExceptionAsClrException("Error converting Python object to int, check that the object was a Python int or that the value wasn't too large. See InnerException for details.");
}
return result;
}

[LibraryImport(PythonLibraryName)]
internal static partial int PyLong_AsLong(PyObject p);
[LibraryImport(PythonLibraryName, EntryPoint = "PyLong_AsLongLong")]
private static partial long PyLong_AsLongLong_(PyObject p);

/// <summary>
/// Calls PyLong_AsLong and throws a Python Exception if an error occurs.
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
internal static long PyLong_AsLong(PyObject p)
{
long result = PyLong_AsLong_(p);
if (result == -1 && PyErr_Occurred())
{
throw PyObject.ThrowPythonExceptionAsClrException("Error converting Python object to int, check that the object was a Python int or that the value wasn't too large. See InnerException for details.");
}
return result;
}

[LibraryImport(PythonLibraryName, EntryPoint = "PyLong_AsLong")]
private static partial int PyLong_AsLong_(PyObject p);

internal static bool IsPyLong(PyObject p)
{
5 changes: 5 additions & 0 deletions src/CSnakes.Runtime/CPython/Unicode.cs
Original file line number Diff line number Diff line change
@@ -17,6 +17,11 @@ internal static nint AsPyUnicodeObject(string s)
[LibraryImport(PythonLibraryName)]
internal static partial nint PyUnicode_DecodeUTF16(char* str, nint size, IntPtr errors, IntPtr byteorder);

/// <summary>
/// Calls PyUnicode_AsUTF8 and throws a Python Exception if an error occurs.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
internal static string PyUnicode_AsUTF8(PyObject s)
{
var result = PyUnicode_AsUTF8_(s);