Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Morepatches #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CoreExposed.includes
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ org/python/core/PyBaseException.class
org/python/core/PyBoolean.class
org/python/core/PyBuiltinCallable.class
org/python/core/PyByteArray.class
org/python/core/PyBytes.class
org/python/core/PyCell.class
org/python/core/PyClass.class
org/python/core/PyClassMethod.class
Expand Down Expand Up @@ -38,6 +39,7 @@ org/python/core/PyModule.class
org/python/core/PyNone.class
org/python/core/PyObject.class
org/python/core/PyProperty.class
org/python/core/PyRange.class
org/python/core/PySet.class
org/python/core/PySlice.class
org/python/core/PySlot.class
Expand All @@ -49,7 +51,6 @@ org/python/core/PyTraceback.class
org/python/core/PyTuple.class
org/python/core/PyType.class
org/python/core/PyUnicode.class
org/python/core/PyRange.class
org/python/core/stringlib/MarkupIterator.class
org/python/core/stringlib/FieldNameIterator.class
org/python/modules/PyStruct.class
Expand Down
5 changes: 4 additions & 1 deletion src/org/python/compiler/CodeCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2604,10 +2604,13 @@ public Object visitName(Name node) throws Exception {
@Override
public Object visitStr(Str node) throws Exception {
PyString s = (PyString)node.getInternalS();
// TODO: in Python 3 there is no alteration here anymore -
// Bytes node ought to be used for the b'' strings
if (s instanceof PyUnicode) {
module.unicodeConstant(s.asString()).get(code);
} else {
module.stringConstant(s.asString()).get(code);
// TODO: this ought to be in Bytes visit
module.bytesConstant(s.asString()).get(code);
}
return null;
}
Expand Down
36 changes: 36 additions & 0 deletions src/org/python/compiler/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.python.core.CodeLoader;
import org.python.core.CompilerFlags;
import org.python.core.Py;
import org.python.core.PyBytes;
import org.python.core.PyCode;
import org.python.core.PyComplex;
import org.python.core.PyException;
Expand Down Expand Up @@ -145,7 +146,38 @@ public boolean equals(Object o) {
}
}

class PyBytesConstant extends Constant implements ClassConstants, Opcodes {
final String value;

PyBytesConstant(String value) {
this.value = value;
}

@Override
void get(Code c) throws IOException {
c.ldc(value);
c.invokestatic(p(PyBytes.class), "fromStringConstant", sig(PyBytes.class, String.class));
}

@Override
void put(Code c) throws IOException {}

@Override
public int hashCode() {
return value.hashCode();
}

@Override
public boolean equals(Object o) {
if (o instanceof PyBytesConstant) {
return ((PyBytesConstant)o).value.equals(value);
} else {
return false;
}
}
}

// TODO: remove
class PyStringConstant extends Constant implements ClassConstants, Opcodes {

final String value;
Expand Down Expand Up @@ -487,6 +519,10 @@ Constant stringConstant(String value) {
return findConstant(new PyStringConstant(value));
}

Constant bytesConstant(String value) {
return findConstant(new PyBytesConstant(value));
}

Constant unicodeConstant(String value) {
return findConstant(new PyUnicodeConstant(value));
}
Expand Down
8 changes: 6 additions & 2 deletions src/org/python/core/BaseBytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.function.Supplier;

import org.python.core.util.Allocator;

/**
* Base class for Jython <code>bytearray</code> (and <code>bytes</code> in due course) that provides
Expand Down Expand Up @@ -1908,7 +1911,8 @@ private static int hexDigit(char c) throws IllegalArgumentException {
* @param iter iterable of objects capable of being regarded as byte arrays
* @return the byte array that is their join
*/
final synchronized PyByteArray basebytes_join(Iterable<? extends PyObject> iter) {
final synchronized <T extends BaseBytes> T basebytes_join(
Iterable<? extends PyObject> iter, Allocator<T> allocator) {

List<PyBuffer> iterList = new LinkedList<PyBuffer>();
long mysize = this.size;
Expand Down Expand Up @@ -1941,7 +1945,7 @@ final synchronized PyByteArray basebytes_join(Iterable<? extends PyObject> iter)
}

// Load the Views from the iterator into a new PyByteArray
PyByteArray result = new PyByteArray((int)totalSize);
T result = allocator.allocate((int)totalSize);
int p = result.offset; // Copy-to pointer
first = true;

Expand Down
2 changes: 2 additions & 0 deletions src/org/python/core/Py.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ private Object readResolve() throws ObjectStreamException {
public final static PyBoolean True = new PyBoolean(true);
/** A zero-length Python byte string **/
public final static PyString EmptyString = new PyString("");
/** A zero-length Python bytes **/
public final static PyBytes EmptyBytes = new PyBytes();
/** A zero-length Python Unicode string **/
public final static PyUnicode EmptyUnicode = new PyUnicode("");
/** A Python string containing '\n' **/
Expand Down
8 changes: 7 additions & 1 deletion src/org/python/core/PyByteArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.python.core.buffer.BaseBuffer;
import org.python.core.buffer.SimpleWritableBuffer;
import org.python.core.util.Allocator;
import org.python.expose.ExposedClassMethod;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
Expand Down Expand Up @@ -1495,7 +1496,12 @@ public PyByteArray join(PyObject iterable) {

@ExposedMethod(doc = BuiltinDocs.bytearray_join_doc)
final PyByteArray bytearray_join(PyObject iterable) {
return basebytes_join(iterable.asIterable());
return basebytes_join(iterable.asIterable(), new Allocator<PyByteArray>() {
@Override
public PyByteArray allocate(int size) {
return new PyByteArray(size);
}
});
}

@ExposedMethod(doc = BuiltinDocs.bytearray___len___doc)
Expand Down
Loading