Skip to content

Commit

Permalink
fix bytecode writing and bytes representation (closes beeware#530)
Browse files Browse the repository at this point in the history
This fixes the bytecode writing issue that was being triggered by
unsigned bytes and also fixes printing bytes representation.
  • Loading branch information
eliasdorneles committed May 24, 2017
1 parent 26c2fe0 commit 15e24b8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
12 changes: 9 additions & 3 deletions python/common/org/python/types/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,17 @@ public org.python.types.Str __repr__() {
public org.python.types.Str __str__() {
StringBuilder sb = new StringBuilder();
sb.append("b'");
for (int c : this.value) {
if (c >= 32 && c < 128) {
for (byte c : this.value) {
if (c == '\n') {
sb.append("\\n");
} else if (c == '\t') {
sb.append("\\t");
} else if (c == '\r') {
sb.append("\\r");
} else if (c >= 32 && c < 127) {
sb.append((char) c);
} else {
sb.append(String.format("\\x%02d", c));
sb.append(String.format("\\x%02x", c));
}
}
sb.append("'");
Expand Down
14 changes: 8 additions & 6 deletions tests/datatypes/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ def test_getattr(self):
""")

def test_capitalize(self):
self.assertCodeExecution("""
self.assertCodeExecution(r"""
print(b'hello, world'.capitalize())
print(b'helloWORLD'.capitalize())
print(b'HELLO WORLD'.capitalize())
print(b'2015638687'.capitalize())
print(b'\xc8'.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_repr(self):
self.assertCodeExecution(r"""
print(repr(b'\xc8'))
print(repr(b'abcdef \xc8 abcdef'))
print(repr(b'abcdef \xc8 abcdef\n\r\t'))
print(b'abcdef \xc8 abcdef\n\r\t')
""")

def test_iter(self):
Expand Down
2 changes: 1 addition & 1 deletion voc/java/opcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def read_extra(cls, reader, dump=None):
return cls(const)

def write_extra(self, writer):
writer.write_s1(self.const)
writer.write_u1(self.const)

@property
def produce_count(self):
Expand Down

0 comments on commit 15e24b8

Please sign in to comment.