Permalink
Browse files

Remove uses of PythonBuffer that are no longer necessary, and improve…

… some of the uses that just convert it to a byte[] to use the internal cache instead.

There shouldn't be any public API changes as a result.
  • Loading branch information...
jdhardy committed Sep 16, 2011
1 parent 4913ead commit 3aae5bef3de0f5be933179e3782072d673e34dd7
@@ -499,8 +499,6 @@ public BytesIO(CodeContext/*!*/ context, [DefaultParameterValue(null)]object ini
return DoWrite(((ArrayModule.array)bytes).ToByteArray()); // as byte[]
} else if (bytes is ICollection<byte>) {
return DoWrite((ICollection<byte>)bytes);
- } else if (bytes is PythonBuffer) {
- return DoWrite(((PythonBuffer)bytes).ToString()); // as string
} else if (bytes is string) {
// TODO Remove this when we move to 3.x
return DoWrite((string)bytes); // as string
@@ -477,11 +477,6 @@ public FileIO(CodeContext/*!*/ context, string name, [DefaultParameterValue("r")
return write(bBytes);
}
- PythonBuffer bBuffer = b as PythonBuffer;
- if (bBuffer != null) {
- return write(bBuffer.ToString());
- }
-
ArrayModule.array bPythonArray = b as ArrayModule.array;
if (bPythonArray != null) {
return write(bPythonArray.ToByteArray());
@@ -3214,9 +3214,7 @@ private class _BlockingIOErrorException : IOException {
string str = buf as string;
if (str == null) {
- if (buf is PythonBuffer) {
- str = ((PythonBuffer)buf).ToString();
- } else if (buf is Extensible<string>) {
+ if (buf is Extensible<string>) {
str = ((Extensible<string>)buf).Value;
}
}
@@ -104,11 +104,6 @@ public class MD5Type : ICloneable {
update(initialBytes);
}
- internal MD5Type(PythonBuffer initialBuffer) {
- _bytes = new byte[0];
- update(initialBuffer);
- }
-
[Documentation("update(string) -> None (update digest with string data)")]
public void update(object newData) {
update(Converter.ConvertToString(newData).MakeByteArray());
@@ -126,7 +121,7 @@ public class MD5Type : ICloneable {
[Documentation("update(bytes) -> None (update digest with string data)")]
public void update(PythonBuffer newData) {
- update(newData.ToString().MakeByteArray());
+ update((IList<byte>)newData);
}
private void update(IList<byte> newBytes) {
@@ -108,11 +108,6 @@ public class sha
update(initialBytes);
}
- internal sha(PythonBuffer initialBuffer) {
- _bytes = new byte[0];
- update(initialBuffer);
- }
-
[Documentation("update(string) -> None (update digest with string data)")]
public void update(object newData) {
update(Converter.ConvertToString(newData).MakeByteArray());
@@ -123,7 +118,7 @@ public class sha
}
public void update(PythonBuffer newBytes) {
- update(newBytes.ToString().MakeByteArray());
+ update((IList<byte>)newBytes);
}
public void update(ByteArray newBytes) {
@@ -54,7 +54,7 @@ public static class PythonSha256 {
}
public static Sha256Object sha256(PythonBuffer data) {
- return new Sha256Object(data);
+ return new Sha256Object((IList<byte>)data);
}
public static Sha256Object sha256(ByteArray data) {
@@ -91,11 +91,6 @@ public sealed class Sha256Object : HashBase
update(initialBytes);
}
- internal Sha256Object(PythonBuffer initialBuffer) {
- _bytes = new byte[0];
- update(initialBuffer);
- }
-
internal override HashAlgorithm Hasher {
get {
return GetHasher();
@@ -155,7 +150,7 @@ public class HashBase {
}
public void update(PythonBuffer buffer) {
- update(buffer.ToString().MakeByteArray());
+ update((IList<byte>)buffer);
}
[Documentation("digest() -> int (current digest value)")]
@@ -61,9 +61,9 @@ public static class PythonSha512 {
public static Sha512Object sha512(Bytes data) {
return new Sha512Object((IList<byte>)data);
}
-
+
public static Sha512Object sha512(PythonBuffer data) {
- return new Sha512Object(data);
+ return new Sha512Object((IList<byte>)data);
}
public static Sha512Object sha512(ByteArray data) {
@@ -83,14 +83,14 @@ public static class PythonSha512 {
}
public static Sha384Object sha384(PythonBuffer data) {
- return new Sha384Object(data);
+ return new Sha384Object((IList<byte>)data);
}
public static Sha384Object sha384(ByteArray data) {
return new Sha384Object((IList<byte>)data);
}
-
+
public static Sha384Object sha384() {
return new Sha384Object();
}
@@ -109,11 +109,6 @@ public sealed class Sha384Object : HashBase, ICloneable {
update(initialBytes);
}
- internal Sha384Object(PythonBuffer initialBuffer) {
- _bytes = new byte[0];
- update(initialBuffer);
- }
-
internal override HashAlgorithm Hasher {
get {
return GetHasher384();
@@ -149,11 +144,6 @@ public sealed class Sha512Object : HashBase, ICloneable {
update(initialBytes);
}
- internal Sha512Object(PythonBuffer initialBuffer) {
- _bytes = new byte[0];
- update(initialBuffer);
- }
-
internal override HashAlgorithm Hasher {
get {
return GetHasher512();
@@ -272,7 +272,9 @@ public class array : IPythonArray, IEnumerable, IWeakReferenceable, ICollection,
}
public void fromstring([NotNull]PythonBuffer buf) {
- fromstring(buf.ToString());
+ if ((buf.Size % itemsize) != 0) throw PythonOps.ValueError("string length not a multiple of itemsize");
+
+ FromStream(new MemoryStream(buf.byteCache, false));
}
public void fromunicode(CodeContext/*!*/ context, string s) {
@@ -637,7 +637,7 @@ public class socket : IWeakReferenceable {
+ "had room to buffer your data for a network send"
)]
public int send(PythonBuffer data, [DefaultParameterValue(0)] int flags) {
- byte[] buffer = data.ToString().MakeByteArray();
+ byte[] buffer = data.byteCache;
try {
return _socket.Send(buffer, (SocketFlags)flags);
} catch (Exception e) {
@@ -699,7 +699,7 @@ public class socket : IWeakReferenceable {
+ "had room to buffer your data for a network send"
)]
public void sendall(PythonBuffer data, [DefaultParameterValue(0)] int flags) {
- byte[] buffer = data.ToString().MakeByteArray();
+ byte[] buffer = data.byteCache;
try {
int bytesTotal = buffer.Length;
int bytesRemaining = bytesTotal;
@@ -291,7 +291,7 @@ public BufferMeta(Expression expr, BindingRestrictions restrictions, object valu
#region IList[System.Byte] implementation
byte[] _objectByteCache = null;
- byte[] byteCache {
+ internal byte[] byteCache {
get {
return _objectByteCache ?? (_objectByteCache = PythonOps.ConvertBufferToByteArray(this));
}
@@ -1614,35 +1614,7 @@ private enum PythonFileMode {
}
public void write([NotNull]PythonBuffer buf) {
- WriteWorker(buf, true);
- }
-
- private void WriteWorker(PythonBuffer/*!*/ buf, bool locking) {
- Debug.Assert(buf != null);
-
- string str = buf._object as string;
- IPythonArray pyArr;
- if (str != null || buf._object is IList<byte>) {
- if (locking) {
- write(buf.ToString());
- } else {
- WriteNoLock(buf.ToString());
- }
- } else if (buf._object is Array) {
- throw new NotImplementedException("writing buffer of .NET array to file");
- } else if ((pyArr = buf._object as IPythonArray) != null) {
- if (_fileMode != PythonFileMode.Binary) {
- throw PythonOps.TypeError("char buffer type not available");
- }
-
- if (locking) {
- write(pyArr.tostring());
- } else {
- WriteNoLock(pyArr.tostring());
- }
- } else {
- Debug.Assert(false, "unsupported buffer object");
- }
+ write((IList<byte>)buf);
}
public void write([NotNull]object arr) {
@@ -1685,7 +1657,7 @@ private enum PythonFileMode {
PythonBuffer buf = e.Current as PythonBuffer;
if (buf != null) {
- WriteWorker(buf, false);
+ WriteNoLock(buf);
continue;
}

0 comments on commit 3aae5be

Please sign in to comment.