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

Skip to content

Commit 0b066e8

Browse files
author
Barton Cline
committed
* Why does pystring.cs claim to be ansi, then call PyUnicode_FromUnicode() in the string contructor?
* pyansistring.cs added it to the Python.Runtime namespace
1 parent 3488140 commit 0b066e8

File tree

2 files changed

+82
-62
lines changed

2 files changed

+82
-62
lines changed

pythonnet/src/runtime/pyansistring.cs

Lines changed: 81 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,82 @@
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-
}
1+
// ==========================================================================
2+
// This is a user contribution to the pythondotnet project.
3+
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
4+
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
5+
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
6+
// FOR A PARTICULAR PURPOSE.
7+
// ==========================================================================
8+
9+
using System;
10+
11+
namespace Python.Runtime {
12+
13+
public class PyAnsiString : PySequence
14+
{
15+
/// <summary>
16+
/// PyAnsiString Constructor
17+
/// </summary>
18+
///
19+
/// <remarks>
20+
/// Creates a new PyAnsiString from an existing object reference. Note
21+
/// that the instance assumes ownership of the object reference.
22+
/// The object reference is not checked for type-correctness.
23+
/// </remarks>
24+
25+
public PyAnsiString(IntPtr ptr) : base(ptr) { }
26+
27+
28+
/// <summary>
29+
/// PyString Constructor
30+
/// </summary>
31+
///
32+
/// <remarks>
33+
/// Copy constructor - obtain a PyAnsiString from a generic PyObject.
34+
/// An ArgumentException will be thrown if the given object is not
35+
/// a Python string object.
36+
/// </remarks>
37+
38+
public PyAnsiString(PyObject o)
39+
: base()
40+
{
41+
if (!IsStringType(o))
42+
{
43+
throw new ArgumentException("object is not a string");
44+
}
45+
Runtime.Incref(o.obj);
46+
obj = o.obj;
47+
}
48+
49+
50+
/// <summary>
51+
/// PyAnsiString Constructor
52+
/// </summary>
53+
///
54+
/// <remarks>
55+
/// Creates a Python string from a managed string.
56+
/// </remarks>
57+
58+
public PyAnsiString(string s)
59+
: base()
60+
{
61+
obj = Runtime.PyString_FromStringAndSize(s, s.Length);
62+
if (obj == IntPtr.Zero)
63+
{
64+
throw new PythonException();
65+
}
66+
}
67+
68+
69+
/// <summary>
70+
/// IsStringType Method
71+
/// </summary>
72+
///
73+
/// <remarks>
74+
/// Returns true if the given object is a Python string.
75+
/// </remarks>
76+
77+
public static bool IsStringType(PyObject value)
78+
{
79+
return Runtime.PyString_Check(value.obj);
80+
}
81+
}
6382
}

pythonnet/src/runtime/pystring.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace Python.Runtime {
1414
/// <summary>
1515
/// Represents a Python (ansi) string object. See the documentation at
1616
/// http://www.python.org/doc/current/api/stringObjects.html for details.
17+
/// 2011-01-29: ...Then why does the string constructor call PyUnicode_FromUnicode()???
1718
/// </summary>
1819

1920
public class PyString : PySequence {

0 commit comments

Comments
 (0)