|
From: <pj...@us...> - 2008-11-06 05:20:07
|
Revision: 5548
http://jython.svn.sourceforge.net/jython/?rev=5548&view=rev
Author: pjenvey
Date: 2008-11-06 05:20:01 +0000 (Thu, 06 Nov 2008)
Log Message:
-----------
__import__ requires __builtins__ to be initialized, use imp.load
Modified Paths:
--------------
trunk/jython/src/org/python/core/codecs.java
Modified: trunk/jython/src/org/python/core/codecs.java
===================================================================
--- trunk/jython/src/org/python/core/codecs.java 2008-11-05 17:51:34 UTC (rev 5547)
+++ trunk/jython/src/org/python/core/codecs.java 2008-11-06 05:20:01 UTC (rev 5548)
@@ -112,7 +112,7 @@
if (!import_encodings_called) {
import_encodings_called = true;
try {
- __builtin__.__import__("encodings");
+ imp.load("encodings");
} catch (PyException exc) {
if (exc.type != Py.ImportError) {
throw exc;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <nr...@us...> - 2009-07-31 19:20:03
|
Revision: 6616
http://jython.svn.sourceforge.net/jython/?rev=6616&view=rev
Author: nriley
Date: 2009-07-31 19:19:56 +0000 (Fri, 31 Jul 2009)
Log Message:
-----------
Make clear which encoding can't be found.
Modified Paths:
--------------
trunk/jython/src/org/python/core/codecs.java
Modified: trunk/jython/src/org/python/core/codecs.java
===================================================================
--- trunk/jython/src/org/python/core/codecs.java 2009-07-31 19:19:46 UTC (rev 6615)
+++ trunk/jython/src/org/python/core/codecs.java 2009-07-31 19:19:56 UTC (rev 6616)
@@ -79,7 +79,7 @@
if (searchPath.__len__() == 0) {
throw new PyException(Py.LookupError,
"no codec search functions registered: " +
- "can't find encoding");
+ "can't find encoding '" + encoding + "'");
}
PyObject iter = searchPath.__iter__();
@@ -96,8 +96,8 @@
break;
}
if (func == null) {
- throw new PyException(Py.LookupError, "unknown encoding " +
- encoding);
+ throw new PyException(Py.LookupError, "unknown encoding '" +
+ encoding + "'");
}
searchCache.__setitem__(v, result);
return (PyTuple) result;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cg...@us...> - 2009-09-11 05:22:08
|
Revision: 6781
http://jython.svn.sourceforge.net/jython/?rev=6781&view=rev
Author: cgroves
Date: 2009-09-11 05:22:00 +0000 (Fri, 11 Sep 2009)
Log Message:
-----------
Widening, standards
Modified Paths:
--------------
trunk/jython/src/org/python/core/codecs.java
Modified: trunk/jython/src/org/python/core/codecs.java
===================================================================
--- trunk/jython/src/org/python/core/codecs.java 2009-09-11 03:35:35 UTC (rev 6780)
+++ trunk/jython/src/org/python/core/codecs.java 2009-09-11 05:22:00 UTC (rev 6781)
@@ -71,42 +71,34 @@
public static PyTuple lookup(String encoding) {
registry_init();
PyString v = new PyString(normalizestring(encoding));
- PyObject result = searchCache.__finditem__(v);
- if (result != null) {
- return (PyTuple) result;
+ PyObject cached = searchCache.__finditem__(v);
+ if (cached != null) {
+ return (PyTuple)cached;
}
if (searchPath.__len__() == 0) {
throw new PyException(Py.LookupError,
- "no codec search functions registered: " +
- "can't find encoding '" + encoding + "'");
+ "no codec search functions registered: can't find encoding '" + encoding + "'");
}
- PyObject iter = searchPath.__iter__();
- PyObject func = null;
- while ((func = iter.__iternext__()) != null) {
- result = func.__call__(v);
- if (result == Py.None) {
+ for (PyObject func : searchPath.asIterable()) {
+ PyObject created = func.__call__(v);
+ if (created == Py.None) {
continue;
}
- if (!(result instanceof PyTuple) || result.__len__() != 4) {
- throw Py.TypeError("codec search functions must " +
- "return 4-tuples");
+ if (!(created instanceof PyTuple) || created.__len__() != 4) {
+ throw Py.TypeError("codec search functions must return 4-tuples");
}
- break;
+ searchCache.__setitem__(v, created);
+ return (PyTuple)created;
}
- if (func == null) {
- throw new PyException(Py.LookupError, "unknown encoding '" +
- encoding + "'");
- }
- searchCache.__setitem__(v, result);
- return (PyTuple) result;
+ throw new PyException(Py.LookupError, "unknown encoding '" + encoding + "'");
}
private static String normalizestring(String string) {
return string.toLowerCase().replace(' ', '-');
}
- private static boolean import_encodings_called = false;
+ private static boolean import_encodings_called;
private static void import_encodings() {
if (!import_encodings_called) {
@@ -135,14 +127,12 @@
/* Shortcut for ascii encoding */
if (encoding.equals("ascii")) {
- return new PyUnicode(
- PyUnicode_DecodeASCII(v.toString(), v.__len__(), errors),
- true);
+ return new PyUnicode(PyUnicode_DecodeASCII(v.toString(), v.__len__(), errors), true);
}
/* Decode via the codec registry */
- PyObject decoder = getDecoder(encoding);
- PyObject result = null;
+ PyObject decoder = lookup(encoding).__getitem__(1);
+ PyObject result;
if (errors != null) {
result = decoder.__call__(v, new PyString(errors));
} else {
@@ -150,17 +140,11 @@
}
if (!(result instanceof PyTuple) || result.__len__() != 2) {
- throw Py.TypeError("decoder must return a tuple " +
- "(object,integer)");
+ throw Py.TypeError("decoder must return a tuple (object,integer)");
}
return result.__getitem__(0);
}
- private static PyObject getDecoder(String encoding) {
- PyObject codecs = lookup(encoding);
- return codecs.__getitem__(1);
- }
-
public static String encode(PyString v, String encoding,
String errors) {
if (encoding == null) {
@@ -174,18 +158,16 @@
}
/* Shortcuts for common default encodings. latin-1 must not use the
- * lookup registry for the encodigs module to work correctly */
+ * lookup registry for the encodings module to work correctly */
if (encoding.equals("latin-1")) {
return PyUnicode_EncodeLatin1(v.toString(), v.__len__(), errors);
-
} else if (encoding.equals("ascii")) {
- return PyUnicode_EncodeASCII(v.toString(),
- v.__len__(), errors);
+ return PyUnicode_EncodeASCII(v.toString(), v.__len__(), errors);
}
/* Decode via the codec registry */
- PyObject encoder = getEncoder(encoding);
- PyObject result = null;
+ PyObject encoder = lookup(encoding).__getitem__(0);
+ PyObject result;
if (errors != null) {
result = encoder.__call__(v, new PyString(errors));
} else {
@@ -193,23 +175,17 @@
}
if (!(result instanceof PyTuple) || result.__len__() != 2) {
- throw Py.TypeError("encoder must return a tuple " +
- "(object,integer)");
+ throw Py.TypeError("encoder must return a tuple (object,integer)");
}
PyObject encoded = result.__getitem__(0);
if (encoded instanceof PyString) {
return encoded.toString();
} else {
- throw Py.TypeError("decoder did not return a string/unicode object (type=" +
- encoded.getType().fastGetName() + ")");
+ throw Py.TypeError("decoder did not return a string/unicode object (type="
+ + encoded.getType().fastGetName() + ")");
}
}
- private static PyObject getEncoder(String encoding) {
- PyObject codecs = lookup(encoding);
- return codecs.__getitem__(0);
- }
-
public static PyObject strict_errors(PyObject[] args, String[] kws) {
ArgParser ap = new ArgParser("strict_errors", args, kws, "exc");
PyObject exc = ap.getPyObject(0);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <pj...@us...> - 2009-10-27 23:49:55
|
Revision: 6919
http://jython.svn.sourceforge.net/jython/?rev=6919&view=rev
Author: pjenvey
Date: 2009-10-27 23:49:37 +0000 (Tue, 27 Oct 2009)
Log Message:
-----------
simplify
Modified Paths:
--------------
trunk/jython/src/org/python/core/codecs.java
Modified: trunk/jython/src/org/python/core/codecs.java
===================================================================
--- trunk/jython/src/org/python/core/codecs.java 2009-10-27 22:21:57 UTC (rev 6918)
+++ trunk/jython/src/org/python/core/codecs.java 2009-10-27 23:49:37 UTC (rev 6919)
@@ -7,12 +7,13 @@
*/
package org.python.core;
-import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
+import org.python.core.util.StringUtil;
+
/**
* Contains the implementation of the builtin codecs.
* @since Jython 2.0
@@ -805,17 +806,7 @@
}
public static String PyUnicode_EncodeUTF8(String str, String errors) {
- final Charset utf8 = Charset.forName("UTF-8");
- final ByteBuffer bbuf = utf8.encode(str);
- final StringBuilder v = new StringBuilder(bbuf.limit());
- while (bbuf.remaining() > 0) {
- int val = bbuf.get();
- if (val < 0) {
- val = 256 + val;
- }
- v.appendCodePoint(val);
- }
- return v.toString();
+ return StringUtil.fromBytes(Charset.forName("UTF-8").encode(str));
}
public static String PyUnicode_DecodeASCII(String str, int size, String errors) {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <pj...@us...> - 2011-03-20 19:43:09
|
Revision: 7252
http://jython.svn.sourceforge.net/jython/?rev=7252&view=rev
Author: pjenvey
Date: 2011-03-20 19:43:03 +0000 (Sun, 20 Mar 2011)
Log Message:
-----------
reuse exceptions.getEnd
Modified Paths:
--------------
trunk/jython/src/org/python/core/codecs.java
Modified: trunk/jython/src/org/python/core/codecs.java
===================================================================
--- trunk/jython/src/org/python/core/codecs.java 2011-03-20 19:41:31 UTC (rev 7251)
+++ trunk/jython/src/org/python/core/codecs.java 2011-03-20 19:43:03 UTC (rev 7252)
@@ -252,23 +252,17 @@
public static PyObject replace_errors(PyObject[] args, String[] kws) {
ArgParser ap = new ArgParser("replace_errors", args, kws, "exc");
PyObject exc = ap.getPyObject(0);
- if (Py.isInstance(exc, Py.UnicodeDecodeError)) {
- PyObject object = exc.__getattr__("object");
- if (!Py.isInstance(object, PyString.TYPE) || Py.isInstance(object, PyUnicode.TYPE)) {
- throw Py.TypeError("object attribute must be str");
- }
- PyObject end = exc.__getattr__("end");
- return new PyTuple(new PyUnicode(Py_UNICODE_REPLACEMENT_CHARACTER), end);
- } else if (Py.isInstance(exc, Py.UnicodeEncodeError)) {
- PyObject object = exc.__getattr__("object");
- if (!Py.isInstance(object, PyUnicode.TYPE)) {
- throw Py.TypeError("object attribute must be unicode");
- }
- PyObject end = exc.__getattr__("end");
- return new PyTuple(Py.java2py("?"), end);
+ if (Py.isInstance(exc, Py.UnicodeEncodeError)) {
+ int end = exceptions.getEnd(exc, true);
+ return new PyTuple(new PyUnicode("?"), Py.newInteger(end));
+ } else if (Py.isInstance(exc, Py.UnicodeDecodeError)) {
+ int end = exceptions.getEnd(exc, false);
+ return new PyTuple(new PyUnicode(Py_UNICODE_REPLACEMENT_CHARACTER),
+ Py.newInteger(end));
} else if (Py.isInstance(exc, Py.UnicodeTranslateError)) {
- PyObject end = exc.__getattr__("end");
- return new PyTuple(new PyUnicode(Py_UNICODE_REPLACEMENT_CHARACTER), end);
+ int end = exceptions.getEnd(exc, true);
+ return new PyTuple(new PyUnicode(Py_UNICODE_REPLACEMENT_CHARACTER),
+ Py.newInteger(end));
}
throw wrong_exception_type(exc);
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|