Skip to content

Commit

Permalink
Changes from ipy2.7 (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
slozier authored and slide committed Mar 23, 2017
1 parent bcc4ae9 commit 4981ad2
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 48 deletions.
5 changes: 5 additions & 0 deletions Src/IronPython.Modules/ModuleOps.cs
Expand Up @@ -338,6 +338,11 @@ public static partial class ModuleOps {
return arr.UnsafeAddress;
}

CTypes._CFuncPtr func = value as CTypes._CFuncPtr;
if (func != null) {
return func.UnsafeAddress;
}

CTypes.Pointer pointer = value as CTypes.Pointer;
if (pointer != null) {
return pointer.UnsafeAddress;
Expand Down
56 changes: 26 additions & 30 deletions Src/IronPython.Modules/_bisect.cs
Expand Up @@ -41,7 +41,7 @@ common approach.
}
IComparer comparer = PythonContext.GetContext(context).GetComparer(null, GetComparisonType(list));
while (lo < hi) {
mid = (lo + hi) / 2;
mid = (int)(((long)lo + hi) / 2);
litem = list[mid];
if (comparer.Compare(litem, item) < 0)
lo = mid + 1;
Expand All @@ -64,7 +64,7 @@ common approach.
}
IComparer comparer = PythonContext.GetContext(context).GetComparer(null, GetComparisonType(context, list));
while (lo < hi) {
mid = (lo + hi) / 2;
mid = (int)(((long)lo + hi) / 2);
litem = PythonOps.GetIndex(context, list, mid);
if (comparer.Compare(litem, item) < 0)
lo = mid + 1;
Expand All @@ -87,7 +87,7 @@ common approach.
}
IComparer comparer = PythonContext.GetContext(context).GetComparer(null, GetComparisonType(list));
while (lo < hi) {
mid = (lo + hi) / 2;
mid = (int)(((long)lo + hi) / 2);
litem = list[mid];
if (comparer.Compare(item, litem) < 0)
hi = mid;
Expand All @@ -111,7 +111,7 @@ common approach.

IComparer comparer = PythonContext.GetContext(context).GetComparer(null, GetComparisonType(context, list));
while (lo < hi) {
mid = (lo + hi) / 2;
mid = (int)(((long)lo + hi) / 2);
litem = PythonOps.GetIndex(context, list, mid);
if (comparer.Compare(item, litem) < 0)
hi = mid;
Expand Down Expand Up @@ -154,11 +154,12 @@ common approach.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
")]
public static object bisect_right(CodeContext/*!*/ context, List a, object x, int lo=0, int hi=-1) {
return InternalBisectRight(context, a, x, lo, hi);
}

public static object bisect_right(CodeContext/*!*/ context, object a, object x, int lo=0, int hi=-1) {
List l = a as List;
if (l != null && l.GetType() == typeof(List)) {
return InternalBisectRight(context, l, x, lo, hi);
}

return InternalBisectRight(context, a, x, lo, hi);
}

Expand All @@ -171,11 +172,13 @@ common approach.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
")]
public static void insort_right(CodeContext/*!*/ context, List a, object x, int lo=0, int hi=-1) {
a.Insert(InternalBisectRight(context, a, x, lo, hi), x);
}

public static void insort_right(CodeContext/*!*/ context, object a, object x, int lo=0, int hi=-1) {
List l = a as List;
if (l != null && l.GetType() == typeof(List)) {
l.Insert(InternalBisectRight(context, l, x, lo, hi), x);
return;
}

PythonOps.Invoke(context, a, "insert", InternalBisectRight(context, a, x, lo, hi), x);
}

Expand All @@ -190,11 +193,12 @@ common approach.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
")]
public static object bisect_left(CodeContext/*!*/ context, List a, object x, int lo=0, int hi=-1) {
return InternalBisectLeft(context, a, x, lo, hi);
}

public static object bisect_left(CodeContext/*!*/ context, object a, object x, int lo=0, int hi=-1) {
List l = a as List;
if (l != null && l.GetType() == typeof(List)) {
return InternalBisectLeft(context, l, x, lo, hi);
}

return InternalBisectLeft(context, a, x, lo, hi);
}

Expand All @@ -207,29 +211,21 @@ common approach.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
")]
public static void insort_left(CodeContext/*!*/ context, List a, object x, int lo=0, int hi=-1) {
a.Insert(InternalBisectLeft(context, a, x, lo, hi), x);
}
public static void insort_left(CodeContext/*!*/ context, object a, object x, int lo=0, int hi=-1) {
List l = a as List;
if (l != null && l.GetType() == typeof(List)) {
l.Insert(InternalBisectLeft(context, l, x, lo, hi), x);
return;
}

public static void insort_left(CodeContext/*!*/ context, object a, object x, int lo=0, int hi=-1) {
PythonOps.Invoke(context, a, "insert", InternalBisectLeft(context, a, x, lo, hi), x);
}

[Documentation("Alias for bisect_right().")]
public static object bisect(CodeContext/*!*/ context, List a, object x, int lo=0, int hi=-1) {
return bisect_right(context, a, x, lo, hi);
}

[Documentation("Alias for bisect_right().")]
public static object bisect(CodeContext/*!*/ context, object a, object x, int lo=0, int hi=-1) {
return bisect_right(context, a, x, lo, hi);
}

[Documentation("Alias for insort_right().")]
public static void insort(CodeContext/*!*/ context, List a, object x, int lo=0, int hi=-1) {
insort_right(context, a, x, lo, hi);
}

[Documentation("Alias for insort_right().")]
public static void insort(CodeContext/*!*/ context, object a, object x, int lo=0, int hi=-1) {
insort_right(context, a, x, lo, hi);
Expand Down
36 changes: 32 additions & 4 deletions Src/IronPython.Modules/_codecs.cs
Expand Up @@ -116,7 +116,7 @@ public class EncodingMap {
return PythonTuple.MakeTuple(res.ToString(), res.Length);
}

public static object charbuffer_encode() {
public static object charbuffer_encode(string input) {
throw PythonOps.NotImplementedError("charbuffer_encode");
}

Expand Down Expand Up @@ -364,7 +364,7 @@ public class EncodingMap {
);
}

public static object readbuffer_encode() {
public static object readbuffer_encode(string input) {
throw PythonOps.NotImplementedError("readbuffer_encode");
}

Expand All @@ -378,11 +378,11 @@ public class EncodingMap {

#region Unicode Escape Encoding

public static PythonTuple unicode_escape_decode() {
public static PythonTuple unicode_escape_decode(string input) {
throw PythonOps.NotImplementedError("unicode_escape_decode");
}

public static PythonTuple unicode_escape_encode() {
public static PythonTuple unicode_escape_encode(string input) {
throw PythonOps.NotImplementedError("unicode_escape_encode");
}

Expand Down Expand Up @@ -608,6 +608,34 @@ public class EncodingMap {
}

#endregion

#region Utf-32 Be Functions

private static Encoding utf32BeEncoding = null;
private static Encoding UTF32BE {
get {
if (utf32BeEncoding == null) utf32BeEncoding = new UTF32Encoding(true, true);
return utf32BeEncoding;
}
}

public static PythonTuple utf_32_be_decode(object input) {
return utf_32_be_decode(input, "strict", false);
}

public static PythonTuple utf_32_be_decode(object input, string errors, [Optional]bool ignored) {
return DoDecode(UTF32BE, input, errors);
}

public static PythonTuple utf_32_be_encode(object input) {
return utf_32_be_encode(input, "strict");
}

public static PythonTuple utf_32_be_encode(object input, string errors) {
return DoEncode(UTF32BE, input, errors);
}

#endregion
#endif


Expand Down
23 changes: 15 additions & 8 deletions Src/IronPython.Modules/_csv.cs
Expand Up @@ -438,7 +438,7 @@ static string SetChar(string name, object src, bool found, string @default)
else if (source.Length != 1)
{
throw PythonOps.TypeError(
"\"{0}\" must be a 1-character string",
"\"{0}\" must be an 1-character string",
name);
}
else
Expand All @@ -447,7 +447,7 @@ static string SetChar(string name, object src, bool found, string @default)
else
{
throw PythonOps.TypeError(
"\"{0}\" must be a 1-character string", name);
"\"{0}\" must be string, not {1}", name, PythonOps.GetPythonTypeName(src));
}
}
return result;
Expand All @@ -463,7 +463,7 @@ static string SetString(string name, object src, bool found, string @default)
else if (!(src is string))
{
throw PythonOps.TypeError(
"\"{0}\" must be an string", name);
"\"{0}\" must be a string", name);
}
else
{
Expand Down Expand Up @@ -560,7 +560,7 @@ static string SetString(string name, object src, bool found, string @default)
if (_quoting < QUOTE_MINIMAL || _quoting > QUOTE_NONE)
throw PythonOps.TypeError("bad \"quoting\" value");
if (string.IsNullOrEmpty(_delimiter))
throw PythonOps.TypeError("delimiter must be set");
throw PythonOps.TypeError("\"delimiter\" must be an 1-character string");

if ((foundParams["quotechar"] && quotechar == null) && quoting == null)
_quoting = QUOTE_NONE;
Expand Down Expand Up @@ -693,8 +693,15 @@ public bool MoveNext()
object lineobj = null;
if (!_iterator.MoveNext())
{
if (_field.Length != 0)
throw MakeError("newline inside string");
// End of input OR exception
if(_field.Length > 0 || _state == State.InQuotedField) {
if(_reader._dialect.strict) {
throw MakeError("unexpected end of data");
} else {
ParseSaveField();
return true;
}
}
return false;
}
else
Expand Down Expand Up @@ -1067,12 +1074,12 @@ public void writerow(CodeContext/*!*/ context, object sequence)
JoinAppend((string)field, quoted, rowlen == 1);
else if (field is double)
{
JoinAppend(DoubleOps.__str__(context, (double)field),
JoinAppend(DoubleOps.__repr__(context, (double)field),
quoted, rowlen == 1);
}
else if (field is float)
{
JoinAppend(SingleOps.__str__(context, (float)field),
JoinAppend(SingleOps.__repr__(context, (float)field),
quoted, rowlen == 1);
}
else if (field == null)
Expand Down
22 changes: 19 additions & 3 deletions Src/IronPython.Modules/_ctypes/NativeFunctions.cs
Expand Up @@ -53,10 +53,10 @@ static class NativeFunctions {
public static extern void CopyMemory(IntPtr destination, IntPtr source, IntPtr Length);

// unix entry points, VM needs to map the filenames.
[DllImport("libdl.so")]
[DllImport("libdl")]
private static extern IntPtr dlopen(string filename, int flags);

[DllImport("libdl.so")]
[DllImport("libdl")]
private static extern IntPtr dlsym(IntPtr handle, string symbol);

public static IntPtr LoadDLL(string filename, int flags) {
Expand Down Expand Up @@ -92,6 +92,11 @@ static class NativeFunctions {
/// </summary>
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public static IntPtr Calloc(IntPtr size) {
if (Environment.OSVersion.Platform == PlatformID.Unix ||
Environment.OSVersion.Platform == PlatformID.MacOSX) {
return calloc((IntPtr)1, size);
}

return LocalAlloc(LMEM_ZEROINIT, size);
}

Expand All @@ -106,11 +111,17 @@ static class NativeFunctions {
[DllImport("kernel32.dll"), ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
private static extern IntPtr LocalAlloc(uint flags, IntPtr size);

[DllImport("libc")]
private static extern IntPtr calloc(IntPtr num, IntPtr size);

private const int LMEM_ZEROINIT = 0x0040;

[DllImport("kernel32.dll")]
private static extern void RtlMoveMemory(IntPtr Destination, IntPtr src, IntPtr length);

[DllImport("libc")]
private static extern IntPtr memmove(IntPtr dst, IntPtr src, IntPtr length);

/// <summary>
/// Helper function for implementing memset. Could be more efficient if we
/// could P/Invoke or call some otherwise native code to do this.
Expand All @@ -124,7 +135,12 @@ static class NativeFunctions {
}

private static IntPtr MoveMemory(IntPtr dest, IntPtr src, IntPtr length) {
RtlMoveMemory(dest, src, length);
if (Environment.OSVersion.Platform == PlatformID.Unix ||
Environment.OSVersion.Platform == PlatformID.MacOSX) {
memmove(dest, src, length);
} else {
RtlMoveMemory(dest, src, length);
}
return dest;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Src/IronPython.Modules/_pickle.cs
Expand Up @@ -2140,7 +2140,7 @@ internal UnpicklerObject(CodeContext context, FileInput input)
}

PythonType cls = PopStack() as PythonType;
if (args == null) {
if (cls == null) {
throw PythonOps.TypeError("expected new-style type as first argument to NEWOBJ, got {0}", DynamicHelpers.GetPythonType(args));
}

Expand Down
2 changes: 1 addition & 1 deletion Src/IronPython/Runtime/ConversionWrappers.cs
Expand Up @@ -232,7 +232,7 @@ public T Current
}
catch (System.InvalidCastException iex)
{
throw new System.InvalidCastException(string.Format("Error in IEnumeratorOfTWrapper.Current. Could not cast: {0} in {0}", typeof(T).ToString(), enumerable.Current.GetType().ToString()), iex);
throw new System.InvalidCastException(string.Format("Error in IEnumeratorOfTWrapper.Current. Could not cast: {0} in {1}", typeof(T).ToString(), enumerable.Current.GetType().ToString()), iex);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Src/IronPython/Runtime/Operations/StringOps.cs
Expand Up @@ -1892,7 +1892,7 @@ static class CodecsInfo {
d["cp" + encs[i].CodePage.ToString()] = d[normalizedName] = d["us"] = d["ascii"] = d["646"] = d["us_ascii"] = new AsciiEncodingInfoWrapper();
continue;
case "iso_8859_1":
d["8859"] = d["latin_1"] = d["latin1"] = d["iso 8859_1"] = d["iso8859_1"] = d["cp819"] = d["819"] = d["latin"] = d["latin1"] = d["l1"] = encs[i];
d["8859"] = d["latin_1"] = d["latin1"] = d["iso 8859_1"] = d["iso8859_1"] = d["cp819"] = d["819"] = d["latin"] = d["l1"] = encs[i];
break;
case "utf_7":
d["u7"] = d["unicode-1-1-utf-7"] = encs[i];
Expand Down

0 comments on commit 4981ad2

Please sign in to comment.