Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d649d6c

Browse files
committed
fixed circular dependency in Runtime PyMembers and InternString initialization
1 parent 6335d97 commit d649d6c

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/runtime/intern.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Python.Runtime
88
{
99
static partial class InternString
1010
{
11-
private static readonly Dictionary<string, PyObject> _string2interns = new();
11+
private static readonly Dictionary<string, PyString> _string2interns = new();
1212
private static readonly Dictionary<IntPtr, string> _intern2strings = new();
1313
const BindingFlags PyIdentifierFieldFlags = BindingFlags.Static | BindingFlags.NonPublic;
1414

@@ -37,7 +37,9 @@ public static void Initialize()
3737
Type type = typeof(PyIdentifier);
3838
foreach (string name in _builtinNames)
3939
{
40-
var op = Runtime.PyUnicode_InternFromString(name).MoveToPyObject();
40+
NewReference pyStr = Runtime.PyUnicode_InternFromString(name);
41+
var op = new PyString(pyStr.StealOrThrow());
42+
Debug.Assert(name == op.ToString());
4143
SetIntern(name, op);
4244
var field = type.GetField("f" + name, PyIdentifierFieldFlags)!;
4345
field.SetValue(null, op.rawPtr);
@@ -48,8 +50,8 @@ public static void Shutdown()
4850
{
4951
foreach (var entry in _string2interns)
5052
{
51-
entry.Value.Dispose();
5253
var field = typeof(PyIdentifier).GetField("f" + entry.Value, PyIdentifierFieldFlags)!;
54+
entry.Value.Dispose();
5355
field.SetValue(null, IntPtr.Zero);
5456
}
5557

@@ -72,7 +74,7 @@ public static bool TryGetInterned(BorrowedReference op, out string s)
7274
return _intern2strings.TryGetValue(op.DangerousGetAddress(), out s);
7375
}
7476

75-
private static void SetIntern(string s, PyObject op)
77+
private static void SetIntern(string s, PyString op)
7678
{
7779
_string2interns.Add(s, op);
7880
_intern2strings.Add(op.rawPtr, s);

src/runtime/runtime.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,13 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
134134
MainManagedThreadId = Thread.CurrentThread.ManagedThreadId;
135135

136136
IsFinalizing = false;
137-
InternString.Initialize();
138137

139138
InitPyMembers();
140139

141140
ABI.Initialize(PyVersion);
142141

142+
InternString.Initialize();
143+
143144
GenericUtil.Reset();
144145
ClassManager.Reset();
145146
ClassDerivedObject.Reset();
@@ -176,7 +177,7 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
176177

177178
private static void InitPyMembers()
178179
{
179-
using (var builtinsOwned = PyImport_Import(PyIdentifier.builtins))
180+
using (var builtinsOwned = PyImport_ImportModule("builtins"))
180181
{
181182
var builtins = builtinsOwned.Borrow();
182183
SetPyMember(out PyNotImplemented, PyObject_GetAttrString(builtins, "NotImplemented").StealNullable());
@@ -197,7 +198,7 @@ private static void InitPyMembers()
197198
// a wrapper_descriptor, even though dict.__setitem__ is.
198199
//
199200
// object.__init__ seems safe, though.
200-
SetPyMemberTypeOf(out PyWrapperDescriptorType, PyObject_GetAttr(PyBaseObjectType, PyIdentifier.__init__).StealNullable());
201+
SetPyMemberTypeOf(out PyWrapperDescriptorType, PyObject_GetAttrString(PyBaseObjectType, "__init__").StealNullable());
201202

202203
SetPyMember(out PySuper_Type, PyObject_GetAttrString(builtins, "super").StealNullable());
203204
}

0 commit comments

Comments
 (0)