Skip to content

Commit

Permalink
Merge pull request beeware#529 from katieamazing/bytes-capitalize
Browse files Browse the repository at this point in the history
Add bytes.capitalize and bytes.__iter__
  • Loading branch information
freakboy3742 authored May 3, 2017
2 parents dfdf402 + 9bf1684 commit 0b3ad20
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
27 changes: 23 additions & 4 deletions python/common/org/python/types/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,12 @@ public boolean __setattr_null(java.lang.String name, org.python.Object value) {
@org.python.Method(
__doc__ = ""
)
public org.python.Iterable __iter__(java.util.List<org.python.Object> args, java.util.Map<java.lang.String, org.python.Object> kwargs, java.util.List<org.python.Object> default_args, java.util.Map<java.lang.String, org.python.Object> default_kwargs) {
throw new org.python.exceptions.NotImplementedError("bytes.__iter__ has not been implemented.");
public org.python.Iterable __iter__() {
java.util.List<org.python.Object> listOfBytes = new java.util.ArrayList<org.python.Object>();
for (byte b: this.value) {
listOfBytes.add(new org.python.types.Int(b));
}
return new org.python.types.List(listOfBytes).__iter__();
}

@org.python.Method(
Expand Down Expand Up @@ -555,8 +559,23 @@ public org.python.Object __rmul__(java.util.List<org.python.Object> args, java.u
@org.python.Method(
__doc__ = ""
)
public org.python.Object capitalize(java.util.List<org.python.Object> args, java.util.Map<java.lang.String, org.python.Object> kwargs, java.util.List<org.python.Object> default_args, java.util.Map<java.lang.String, org.python.Object> default_kwargs) {
throw new org.python.exceptions.NotImplementedError("bytes.capitalize has not been implemented.");
public org.python.Object capitalize() {
byte[] value = new byte[this.value.length];
for (int i = 0; i < this.value.length; i++) {
byte b = this.value[i];
if (b < 127 && b > 32) {
char c = (char) b;
if (i == 0) {
c = Character.toUpperCase(c);
} else {
c = Character.toLowerCase(c);
}
value[i] = (byte) c;
} else {
value[i] = b;
}
}
return new Bytes(value);
}

@org.python.Method(
Expand Down
3 changes: 0 additions & 3 deletions tests/builtins/test_map.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from .. utils import TranspileTestCase, BuiltinTwoargFunctionTestCase

from unittest import expectedFailure


class MapTests(TranspileTestCase):
base_code = """
Expand All @@ -22,7 +20,6 @@ def testish(x):
def test_bool(self):
self.assertCodeExecution(self.base_code % ("[True, False, True]", "bool(x)"))

@expectedFailure
def test_bytearray(self):
self.assertCodeExecution(self.base_code % ("b'123'", "x"))

Expand Down
23 changes: 23 additions & 0 deletions tests/datatypes/test_bytes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from .. utils import TranspileTestCase, UnaryOperationTestCase, BinaryOperationTestCase, InplaceOperationTestCase

from unittest import expectedFailure


class BytesTests(TranspileTestCase):
def test_setattr(self):
Expand All @@ -20,6 +22,27 @@ def test_getattr(self):
print(err)
""")

def test_capitalize(self):
self.assertCodeExecution("""
print(b'hello, world'.capitalize())
print(b'helloWORLD'.capitalize())
print(b'HELLO WORLD'.capitalize())
print(b'2015638687'.capitalize())
""")

@expectedFailure
def test_capitalize_with_nonascii(self):
# Move this to test_capitalize upon resolution of #530
self.assertCodeExecution("""
print(b'\xc8'.capitalize())
""")

def test_iter(self):
self.assertCodeExecution("""
print([b for b in b''])
print([b for b in b'hello world'])
""")


class UnaryBytesOperationTests(UnaryOperationTestCase, TranspileTestCase):
data_type = 'bytes'
Expand Down

0 comments on commit 0b3ad20

Please sign in to comment.