question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using String.Format crashes inside pythonnet

See original GitHub issue

Environment

  • Pythonnet version: 3.x
  • Python version: 3.8.10
  • Operating System: Windows 11 64bit
  • .NET Runtime: net6.0

Details

How to reproduce(with project Console.vsproj): @filmor

PythonEngine.Initialize();
PythonEngine.Exec("from System import String\nString.Format('{0},{1}', 1, 2)"); // crashes here
int i = Runtime.Py_Main(cmd.Length, cmd);
PythonEngine.Shutdown();

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:15 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
sekkitcommented, Dec 23, 2021

System.AccessViolationException: ‘Attempted to read or write protected memory. This is often an indication that other memory is corrupt.’ [External Code]

Python.Runtime.dll!Python.Runtime.Runtime.PyErr_Occurred() Line 1853 C# Python.Runtime.dll!Python.Runtime.Exceptions.ErrorOccurred() Line 315 C# Python.Runtime.dll!Python.Runtime.Runtime.AssertNoErorSet() Line 1130 C# Python.Runtime.dll!Python.Runtime.Runtime.PyObject_Str(Python.Runtime.BorrowedReference pointer) Line 1122 C# Python.Runtime.dll!Python.Runtime.PyObject.ToString() Line 1054 C# [External Code] Python.Runtime.dll!Python.Runtime.MethodBinder.Invoke(Python.Runtime.BorrowedReference inst, Python.Runtime.BorrowedReference args, Python.Runtime.BorrowedReference kw, System.Reflection.MethodBase info, System.Reflection.MethodInfo[] methodinfo) Line 919 C# Python.Runtime.dll!Python.Runtime.MethodObject.Invoke(Python.Runtime.BorrowedReference target, Python.Runtime.BorrowedReference args, Python.Runtime.BorrowedReference kw, System.Reflection.MethodBase info) Line 68 C# Python.Runtime.dll!Python.Runtime.MethodBinding.tp_call(Python.Runtime.BorrowedReference ob, Python.Runtime.BorrowedReference args, Python.Runtime.BorrowedReference kw) Line 238 C# [External Code] Python.Runtime.dll!Python.Runtime.Runtime.PyRun_String(string code, Python.Runtime.RunFlagType st, Python.Runtime.BorrowedReference globals, Python.Runtime.BorrowedReference locals) Line 941 C# Python.Runtime.dll!Python.Runtime.PythonEngine.RunString(string code, Python.Runtime.BorrowedReference globals, Python.Runtime.BorrowedReference locals, Python.Runtime.RunFlagType flag) Line 670 C# Python.Runtime.dll!Python.Runtime.PythonEngine.Exec(string code, Python.Runtime.PyDict globals, Python.Runtime.PyObject locals) Line 578 C# nPython.exe!Python.Runtime.PythonConsole.Main(string[] args) Line 36 C#

0reactions
lostmsucommented, Mar 3, 2022

@sekkit @filmor FYI, the workaround we are planning is going to be somewhat misleading. In your example if you added integer format specifiers, and still used number literals in Python, you would get your format specifiers ignored.

UPD. NVM, I will just add IFormattable implementation to PyInt.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pythonnet crashes .Net application on exit. · Issue #245
Crash occurs when application exits. This is sample of code that produces crash. Also it contains 'probably workaround".
Read more >
pythonnet
I'm trying to define a base class in C# and derive from that class in python / pythonnet. The C# class is very...
Read more >
Script crashing when trying to test if a string is an integer
I'm able to convert that to an int with int(), but if there's nothing or if there's something there other than numbers it...
Read more >
CHANGELOG.md - pythonnet/pythonnet
Fixed a refcount problem that caused a crash when CLR was imported in an existing installed Python interpreter. Added an automatic conversion from...
Read more >
[Python.NET] Embedded Python .Net example - running scripts
The crash I am >> getting happens the second time I run a script that imports ... My environment: >> >> Windows 10...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found