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

Skip to content

Commit 3488140

Browse files
author
Barton Cline
committed
* Added pyansistring.cs file.
* Added pyansistring.cs to project file.
1 parent c48b8f7 commit 3488140

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

pythonnet/src/runtime/Python.Runtime.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
<Compile Include="nativecall.cs" />
126126
<Compile Include="overload.cs" />
127127
<Compile Include="propertyobject.cs" />
128+
<Compile Include="pyansistring.cs" />
128129
<Compile Include="pydict.cs" />
129130
<Compile Include="pyfloat.cs" />
130131
<Compile Include="pyint.cs" />

pythonnet/src/runtime/pyansistring.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
public class PyAnsiString : PySequence {
2+
/// <summary>
3+
/// PyAnsiString Constructor
4+
/// </summary>
5+
///
6+
/// <remarks>
7+
/// Creates a new PyAnsiString from an existing object reference. Note
8+
/// that the instance assumes ownership of the object reference.
9+
/// The object reference is not checked for type-correctness.
10+
/// </remarks>
11+
12+
public PyAnsiString(IntPtr ptr) : base(ptr) { }
13+
14+
15+
/// <summary>
16+
/// PyString Constructor
17+
/// </summary>
18+
///
19+
/// <remarks>
20+
/// Copy constructor - obtain a PyAnsiString from a generic PyObject.
21+
/// An ArgumentException will be thrown if the given object is not
22+
/// a Python string object.
23+
/// </remarks>
24+
25+
public PyAnsiString(PyObject o)
26+
: base() {
27+
if (!IsStringType(o)) {
28+
throw new ArgumentException("object is not a string");
29+
}
30+
Runtime.Incref(o.obj);
31+
obj = o.obj;
32+
}
33+
34+
35+
/// <summary>
36+
/// PyAnsiString Constructor
37+
/// </summary>
38+
///
39+
/// <remarks>
40+
/// Creates a Python string from a managed string.
41+
/// </remarks>
42+
43+
public PyAnsiString(string s)
44+
: base() {
45+
obj = Runtime.PyString_FromStringAndSize(s, s.Length);
46+
if (obj == IntPtr.Zero) {
47+
throw new PythonException();
48+
}
49+
}
50+
51+
52+
/// <summary>
53+
/// IsStringType Method
54+
/// </summary>
55+
///
56+
/// <remarks>
57+
/// Returns true if the given object is a Python string.
58+
/// </remarks>
59+
60+
public static bool IsStringType(PyObject value) {
61+
return Runtime.PyString_Check(value.obj);
62+
}
63+
}

0 commit comments

Comments
 (0)