Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit955ef8d

Browse files
committed
Add intern string
1 parentde7c7c2 commit955ef8d

18 files changed

+238
-34
lines changed

‎src/embed_tests/TestDomainReload.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ def test_obj_call():
203203
// Create a new module
204204
IntPtrmodule=PyRuntime.PyModule_New(name);
205205
Assert.That(module!=IntPtr.Zero);
206-
IntPtrglobals=PyRuntime.PyObject_GetAttrString(module,"__dict__");
206+
IntPtrglobals=PyRuntime.PyObject_GetAttr(module,PyIdentifier.__dict__);
207207
Assert.That(globals!=IntPtr.Zero);
208208
try
209209
{
210210
// import builtins
211211
// module.__dict__[__builtins__] = builtins
212-
intres=PyRuntime.PyDict_SetItemString(globals,"__builtins__",
212+
intres=PyRuntime.PyDict_SetItem(globals,PyIdentifier.__builtins__,
213213
PyRuntime.PyEval_GetBuiltins());
214214
PythonException.ThrowIfIsNotZero(res);
215215

‎src/runtime/Python.Runtime.15.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,22 @@
143143
<PackageReferenceInclude="Microsoft.SourceLink.GitHub"Version="1.0.0"PrivateAssets="All" />
144144
</ItemGroup>
145145

146+
<ItemGroup>
147+
<NoneUpdate="intern_.tt">
148+
<Generator>TextTemplatingFileGenerator</Generator>
149+
<LastGenOutput>intern_.cs</LastGenOutput>
150+
</None>
151+
</ItemGroup>
152+
153+
154+
<ItemGroup>
155+
<CompileUpdate="intern_.cs">
156+
<DesignTime>True</DesignTime>
157+
<AutoGen>True</AutoGen>
158+
<DependentUpon>intern_.tt</DependentUpon>
159+
</Compile>
160+
</ItemGroup>
161+
146162
<ImportProject="Sdk.targets"Sdk="Microsoft.NET.Sdk" />
147163

148164
<PropertyGroup>

‎src/runtime/Python.Runtime.csproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,16 @@
8282
<CompileInclude="Codecs\TupleCodecs.cs" />
8383
<CompileInclude="converterextensions.cs" />
8484
<CompileInclude="finalizer.cs" />
85+
<CompileInclude="intern.cs" />
86+
<CompileInclude="intern_.cs" />
8587
<CompileInclude="Properties\AssemblyInfo.cs" />
8688
<CompileInclude="..\SharedAssemblyInfo.cs">
8789
<Link>Properties\SharedAssemblyInfo.cs</Link>
8890
</Compile>
8991
<CompileInclude="arrayobject.cs" />
9092
<CompileInclude="assemblymanager.cs" />
9193
<CompileInclude="BorrowedReference.cs" />
92-
<CompileInclude="bufferinterface.cs" />
94+
<CompileInclude="bufferinterface.cs" />
9395
<CompileInclude="classderived.cs" />
9496
<CompileInclude="classbase.cs" />
9597
<CompileInclude="classmanager.cs" />
@@ -131,7 +133,7 @@
131133
<CompileInclude="overload.cs" />
132134
<CompileInclude="propertyobject.cs" />
133135
<CompileInclude="pyansistring.cs" />
134-
<CompileInclude="pybuffer.cs" />
136+
<CompileInclude="pybuffer.cs" />
135137
<CompileInclude="pydict.cs" />
136138
<CompileInclude="PyExportAttribute.cs" />
137139
<CompileInclude="pyfloat.cs" />
@@ -185,4 +187,4 @@
185187
<CopySourceFiles="$(TargetAssembly)"DestinationFolder="$(PythonBuildDir)" />
186188
<!--Copy SourceFiles="$(TargetAssemblyPdb)" Condition="Exists('$(TargetAssemblyPdb)')" DestinationFolder="$(PythonBuildDir)" /-->
187189
</Target>
188-
</Project>
190+
</Project>

‎src/runtime/classbase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public static IntPtr tp_repr(IntPtr ob)
279279
IntPtrargs=Runtime.PyTuple_New(1);
280280
Runtime.XIncref(ob);
281281
Runtime.PyTuple_SetItem(args,0,ob);
282-
IntPtrreprFunc=Runtime.PyObject_GetAttrString(Runtime.PyBaseObjectType,"__repr__");
282+
IntPtrreprFunc=Runtime.PyObject_GetAttr(Runtime.PyBaseObjectType,PyIdentifier.__repr__);
283283
varoutput=Runtime.PyObject_Call(reprFunc,args,IntPtr.Zero);
284284
Runtime.XDecref(args);
285285
Runtime.XDecref(reprFunc);

‎src/runtime/classmanager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ private static void InitClassBase(Type type, ClassBase impl)
232232
varattr=(DocStringAttribute)attrs[0];
233233
stringdocStr=attr.DocString;
234234
doc=Runtime.PyString_FromString(docStr);
235-
Runtime.PyDict_SetItemString(dict,"__doc__",doc);
235+
Runtime.PyDict_SetItem(dict,PyIdentifier.__doc__,doc);
236236
Runtime.XDecref(doc);
237237
}
238238

@@ -249,16 +249,16 @@ private static void InitClassBase(Type type, ClassBase impl)
249249
varctors=newConstructorBinding(type,tp,co.binder);
250250
// ExtensionType types are untracked, so don't Incref() them.
251251
// TODO: deprecate __overloads__ soon...
252-
Runtime.PyDict_SetItemString(dict,"__overloads__",ctors.pyHandle);
253-
Runtime.PyDict_SetItemString(dict,"Overloads",ctors.pyHandle);
252+
Runtime.PyDict_SetItem(dict,PyIdentifier.__overloads__,ctors.pyHandle);
253+
Runtime.PyDict_SetItem(dict,PyIdentifier.Overloads,ctors.pyHandle);
254254
ctors.DecrRefCount();
255255
}
256256

257257
// don't generate the docstring if one was already set from a DocStringAttribute.
258258
if(!CLRModule._SuppressDocs&&doc==IntPtr.Zero)
259259
{
260260
doc=co.GetDocString();
261-
Runtime.PyDict_SetItemString(dict,"__doc__",doc);
261+
Runtime.PyDict_SetItem(dict,PyIdentifier.__doc__,doc);
262262
Runtime.XDecref(doc);
263263
}
264264
}

‎src/runtime/exceptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public static void SetError(Exception e)
284284
}
285285

286286
IntPtrop=CLRObject.GetInstHandle(e);
287-
IntPtretype=Runtime.PyObject_GetAttrString(op,"__class__");
287+
IntPtretype=Runtime.PyObject_GetAttr(op,PyIdentifier.__class__);
288288
Runtime.PyErr_SetObject(newBorrowedReference(etype),newBorrowedReference(op));
289289
Runtime.XDecref(etype);
290290
Runtime.XDecref(op);

‎src/runtime/importhook.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ static void InitImport()
4343
// look in CLR modules, then if we don't find any call the default
4444
// Python __import__.
4545
IntPtrbuiltins=Runtime.GetBuiltins();
46-
py_import=Runtime.PyObject_GetAttrString(builtins,"__import__");
46+
py_import=Runtime.PyObject_GetAttr(builtins,PyIdentifier.__import__);
4747
PythonException.ThrowIfIsNull(py_import);
4848

4949
hook=newMethodWrapper(typeof(ImportHook),"__import__","TernaryFunc");
50-
intres=Runtime.PyObject_SetAttrString(builtins,"__import__",hook.ptr);
50+
intres=Runtime.PyObject_SetAttr(builtins,PyIdentifier.__import__,hook.ptr);
5151
PythonException.ThrowIfIsNotZero(res);
5252

5353
Runtime.XDecref(builtins);
@@ -60,7 +60,7 @@ static void RestoreImport()
6060
{
6161
IntPtrbuiltins=Runtime.GetBuiltins();
6262

63-
intres=Runtime.PyObject_SetAttrString(builtins,"__import__",py_import);
63+
intres=Runtime.PyObject_SetAttr(builtins,PyIdentifier.__import__,py_import);
6464
PythonException.ThrowIfIsNotZero(res);
6565
Runtime.XDecref(py_import);
6666
py_import=IntPtr.Zero;

‎src/runtime/intern.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
usingSystem;
2+
usingSystem.Collections.Generic;
3+
usingSystem.Linq;
4+
5+
namespacePython.Runtime
6+
{
7+
staticpartialclassInternString
8+
{
9+
privatestaticDictionary<string,IntPtr>_string2interns;
10+
privatestaticDictionary<IntPtr,string>_intern2strings;
11+
12+
staticInternString()
13+
{
14+
varidentifierNames=typeof(PyIdentifier).GetFields().Select(fi=>fi.Name);
15+
varvalidNames=newHashSet<string>(identifierNames);
16+
if(validNames.Count!=_builtinNames.Length)
17+
{
18+
thrownewInvalidOperationException("Identifiers args not matching");
19+
}
20+
foreach(varnamein_builtinNames)
21+
{
22+
if(!validNames.Contains(name))
23+
{
24+
thrownewInvalidOperationException($"{name} is not declared");
25+
}
26+
}
27+
}
28+
29+
publicstaticvoidInitialize()
30+
{
31+
_string2interns=newDictionary<string,IntPtr>();
32+
_intern2strings=newDictionary<IntPtr,string>();
33+
34+
Typetype=typeof(PyIdentifier);
35+
foreach(stringnamein_builtinNames)
36+
{
37+
IntPtrop=Runtime.PyUnicode_InternFromString(name);
38+
SetIntern(name,op);
39+
type.GetField(name).SetValue(null,op);
40+
}
41+
}
42+
43+
publicstaticvoidShutdown()
44+
{
45+
foreach(varptrin_intern2strings.Keys)
46+
{
47+
Runtime.XDecref(ptr);
48+
}
49+
_string2interns=null;
50+
_intern2strings=null;
51+
}
52+
53+
publicstaticstringGetManagedString(IntPtrop)
54+
{
55+
strings;
56+
if(TryGetInterned(op,outs))
57+
{
58+
returns;
59+
}
60+
returnRuntime.GetManagedString(op);
61+
}
62+
63+
publicstaticboolTryGetInterned(IntPtrop,outstrings)
64+
{
65+
return_intern2strings.TryGetValue(op,outs);
66+
}
67+
68+
privatestaticvoidSetIntern(strings,IntPtrop)
69+
{
70+
_string2interns.Add(s,op);
71+
_intern2strings.Add(op,s);
72+
}
73+
}
74+
}

‎src/runtime/intern_.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
usingSystem;
2+
3+
namespacePython.Runtime
4+
{
5+
staticclassPyIdentifier
6+
{
7+
publicstaticIntPtr__name__;
8+
publicstaticIntPtr__dict__;
9+
publicstaticIntPtr__doc__;
10+
publicstaticIntPtr__class__;
11+
publicstaticIntPtr__module__;
12+
publicstaticIntPtr__file__;
13+
publicstaticIntPtr__slots__;
14+
publicstaticIntPtr__self__;
15+
publicstaticIntPtr__annotations__;
16+
publicstaticIntPtr__init__;
17+
publicstaticIntPtr__repr__;
18+
publicstaticIntPtr__import__;
19+
publicstaticIntPtr__builtins__;
20+
publicstaticIntPtrbuiltins;
21+
publicstaticIntPtr__overloads__;
22+
publicstaticIntPtrOverloads;
23+
}
24+
25+
26+
staticpartialclassInternString
27+
{
28+
privatestaticreadonlystring[]_builtinNames=newstring[]
29+
{
30+
"__name__",
31+
"__dict__",
32+
"__doc__",
33+
"__class__",
34+
"__module__",
35+
"__file__",
36+
"__slots__",
37+
"__self__",
38+
"__annotations__",
39+
"__init__",
40+
"__repr__",
41+
"__import__",
42+
"__builtins__",
43+
"builtins",
44+
"__overloads__",
45+
"Overloads",
46+
};
47+
}
48+
}

‎src/runtime/intern_.tt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<#@ template debug="true" hostSpecific="true" #>
2+
<#@ output extension=".cs" #>
3+
<#
4+
string[] internNames = new string[]
5+
{
6+
"__name__",
7+
"__dict__",
8+
"__doc__",
9+
"__class__",
10+
"__module__",
11+
"__file__",
12+
"__slots__",
13+
"__self__",
14+
"__annotations__",
15+
16+
"__init__",
17+
"__repr__",
18+
"__import__",
19+
"__builtins__",
20+
21+
"builtins",
22+
23+
"__overloads__",
24+
"Overloads",
25+
};
26+
#>
27+
using System;
28+
29+
namespace Python.Runtime
30+
{
31+
static class PyIdentifier
32+
{
33+
<#
34+
foreach (var name in internNames)
35+
{
36+
#>
37+
public static IntPtr <#= name #>;
38+
<#
39+
}
40+
#>
41+
}
42+
43+
44+
static partial class InternString
45+
{
46+
private static readonly string[] _builtinNames = new string[]
47+
{
48+
<#
49+
foreach (var name in internNames)
50+
{
51+
#>
52+
"<#= name #>",
53+
<#
54+
}
55+
#>
56+
};
57+
}
58+
}

‎src/runtime/metatype.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
109109
}
110110
}
111111

112-
IntPtrslots=Runtime.PyDict_GetItemString(dict,"__slots__");
112+
IntPtrslots=Runtime.PyDict_GetItem(dict,PyIdentifier.__slots__);
113113
if(slots!=IntPtr.Zero)
114114
{
115115
returnExceptions.RaiseTypeError("subclasses of managed classes do not support __slots__");
@@ -197,7 +197,7 @@ public static IntPtr tp_call(IntPtr tp, IntPtr args, IntPtr kw)
197197
returnIntPtr.Zero;
198198
}
199199

200-
varinit=Runtime.PyObject_GetAttrString(obj,"__init__");
200+
varinit=Runtime.PyObject_GetAttr(obj,PyIdentifier.__init__);
201201
Runtime.PyErr_Clear();
202202

203203
if(init!=IntPtr.Zero)

‎src/runtime/methodbinding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static IntPtr tp_getattro(IntPtr ob, IntPtr key)
8484
returnIntPtr.Zero;
8585
}
8686

87-
stringname=Runtime.GetManagedString(key);
87+
stringname=InternString.GetManagedString(key);
8888
switch(name)
8989
{
9090
case"__doc__":

‎src/runtime/methodobject.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ public static IntPtr tp_getattro(IntPtr ob, IntPtr key)
133133
returnExceptions.RaiseTypeError("string expected");
134134
}
135135

136-
stringname=Runtime.GetManagedString(key);
137-
if(name=="__doc__")
136+
if(Runtime.PyUnicode_Compare(key,PyIdentifier.__doc__)==0)
138137
{
139138
IntPtrdoc=self.GetDocString();
140139
Runtime.XIncref(doc);

‎src/runtime/moduleobject.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public ModuleObject(string name)
4848
IntPtrpyfilename=Runtime.PyString_FromString(filename);
4949
IntPtrpydocstring=Runtime.PyString_FromString(docstring);
5050
IntPtrpycls=TypeManager.GetTypeHandle(GetType());
51-
Runtime.PyDict_SetItemString(dict,"__name__",pyname);
52-
Runtime.PyDict_SetItemString(dict,"__file__",pyfilename);
53-
Runtime.PyDict_SetItemString(dict,"__doc__",pydocstring);
54-
Runtime.PyDict_SetItemString(dict,"__class__",pycls);
51+
Runtime.PyDict_SetItem(dict,PyIdentifier.__name__,pyname);
52+
Runtime.PyDict_SetItem(dict,PyIdentifier.__file__,pyfilename);
53+
Runtime.PyDict_SetItem(dict,PyIdentifier.__doc__,pydocstring);
54+
Runtime.PyDict_SetItem(dict,PyIdentifier.__class__,pycls);
5555
Runtime.XDecref(pyname);
5656
Runtime.XDecref(pyfilename);
5757
Runtime.XDecref(pydocstring);
@@ -282,7 +282,7 @@ public static IntPtr tp_getattro(IntPtr ob, IntPtr key)
282282
returnop;
283283
}
284284

285-
stringname=Runtime.GetManagedString(key);
285+
stringname=InternString.GetManagedString(key);
286286
if(name=="__dict__")
287287
{
288288
Runtime.XIncref(self.dict);

‎src/runtime/pyscope.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ internal PyScope(IntPtr ptr, PyScopeManager manager)
6868
variables=Runtime.PyModule_GetDict(obj);
6969
PythonException.ThrowIfIsNull(variables);
7070

71-
intres=Runtime.PyDict_SetItemString(
72-
variables,"__builtins__",
71+
intres=Runtime.PyDict_SetItem(
72+
variables,PyIdentifier.__builtins__,
7373
Runtime.PyEval_GetBuiltins()
7474
);
7575
PythonException.ThrowIfIsNotZero(res);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp