GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — PEP 305 (csv module) · robertpatrick/jython@0ab4708

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • CPythonLib.includes
  • CoreExposed.includes
  • src
    • org/python/modules
      • Setup.java
      • _csv
        • PyDialect.java
        • PyDialectDerived.java
        • PyReader.java
        • PyWriter.java
        • QuoteStyle.java
        • _csv.java
    • templates
      • dialect.derived
      • mappings
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎CPythonLib.includes‎

Copy file name to clipboardExpand all lines: CPythonLib.includes
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ cookielib.py
4343
copy.py
4444
copy_reg.py
4545
Cookie.py
46+
csv.py
4647
difflib.py
4748
dircache.py
4849
dircmp.py
Collapse file

‎CoreExposed.includes‎

Copy file name to clipboardExpand all lines: CoreExposed.includes
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ org/python/core/PyType.class
3636
org/python/core/PyUnicode.class
3737
org/python/core/PyXRange.class
3838
org/python/modules/_codecs$EncodingMap.class
39+
org/python/modules/_csv/PyDialect.class
40+
org/python/modules/_csv/PyReader.class
41+
org/python/modules/_csv/PyWriter.class
3942
org/python/modules/_functools/PyPartial.class
4043
org/python/modules/_weakref/CallableProxyType.class
4144
org/python/modules/_weakref/ReferenceType.class
Collapse file

‎src/org/python/modules/Setup.java‎

Copy file name to clipboardExpand all lines: src/org/python/modules/Setup.java
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class Setup
5353
"collections:org.python.modules.collections.Collections",
5454
"gc",
5555
"_hashlib",
56-
"_functools:org.python.modules._functools._functools"
56+
"_functools:org.python.modules._functools._functools",
57+
"_csv:org.python.modules._csv._csv"
5758
};
5859
}
Collapse file

‎src/org/python/modules/_csv/PyDialect.java‎

Copy file name to clipboard
+217Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
/* Copyright (c) Jython Developers */
2+
package org.python.modules._csv;
3+
4+
import org.python.core.ArgParser;
5+
import org.python.core.Py;
6+
import org.python.core.PyBaseString;
7+
import org.python.core.PyInteger;
8+
import org.python.core.PyNewWrapper;
9+
import org.python.core.PyObject;
10+
import org.python.core.PyString;
11+
import org.python.core.PyType;
12+
import org.python.expose.ExposedDelete;
13+
import org.python.expose.ExposedGet;
14+
import org.python.expose.ExposedNew;
15+
import org.python.expose.ExposedSet;
16+
import org.python.expose.ExposedType;
17+
18+
/**
19+
* The Python CSV Dialect type.
20+
*/
21+
@ExposedType(name = "_csv.Dialect")
22+
public class PyDialect extends PyObject {
23+
24+
public static final PyType TYPE = PyType.fromClass(PyDialect.class);
25+
26+
public PyString __doc__ = Py.newString(
27+
"CSV dialect\n" +
28+
"\n" +
29+
"The Dialect type records CSV parsing and generation options.\n");
30+
31+
/** Whether " is represented by "". */
32+
@ExposedGet
33+
public boolean doublequote;
34+
35+
/** Field separator. */
36+
@ExposedGet
37+
public char delimiter;
38+
39+
/** Quote character. */
40+
public char quotechar;
41+
42+
/** Escape character. */
43+
public char escapechar;
44+
45+
/** Ignore spaces following delimiter? */
46+
@ExposedGet
47+
public boolean skipinitialspace;
48+
49+
/** String to write between records. */
50+
@ExposedGet
51+
public String lineterminator;
52+
53+
/** Style of quoting to write. */
54+
public QuoteStyle quoting;
55+
56+
/** Whether an exception is raised on bad CSV. */
57+
@ExposedGet
58+
public boolean strict;
59+
60+
public PyDialect() {
61+
super(TYPE);
62+
}
63+
64+
public PyDialect(PyType subType) {
65+
super(subType);
66+
}
67+
68+
@ExposedNew
69+
final static PyObject Dialect___new__(PyNewWrapper new_, boolean init, PyType subtype,
70+
PyObject[] args, String[] keywords) {
71+
ArgParser ap = new ArgParser("__new__", args, keywords,
72+
new String[] {"dialect", "delimiter", "doublequote",
73+
"escapechar", "lineterminator", "quotechar",
74+
"quoting", "skipinitialspace", "strict"});
75+
PyObject dialect = ap.getPyObject(0, null);
76+
PyObject delimiter = ap.getPyObject(1, null);
77+
PyObject doublequote = ap.getPyObject(2, null);
78+
PyObject escapechar = ap.getPyObject(3, null);
79+
PyObject lineterminator = ap.getPyObject(4, null);
80+
PyObject quotechar = ap.getPyObject(5, null);
81+
PyObject quoting = ap.getPyObject(6, null);
82+
PyObject skipinitialspace = ap.getPyObject(7, null);
83+
PyObject strict = ap.getPyObject(8, null);
84+
85+
if (dialect instanceof PyString) {
86+
dialect = _csv.get_dialect_from_registry(dialect);
87+
}
88+
89+
// Can we reuse this instance?
90+
if (dialect instanceof PyDialect && delimiter == null && doublequote == null
91+
&& escapechar == null && lineterminator == null && quotechar == null && quoting == null
92+
&& skipinitialspace == null && strict == null) {
93+
return dialect;
94+
}
95+
96+
if (dialect != null) {
97+
delimiter = delimiter != null ? delimiter : dialect.__findattr__("delimiter");
98+
doublequote = doublequote != null
99+
? doublequote : dialect.__findattr__("doublequote");
100+
escapechar = escapechar != null ? escapechar : dialect.__findattr__("escapechar");
101+
lineterminator = lineterminator != null
102+
? lineterminator : dialect.__findattr__("lineterminator");
103+
quotechar = quotechar != null ? quotechar : dialect.__findattr__("quotechar");
104+
quoting = quoting != null ? quoting : dialect.__findattr__("quoting");
105+
skipinitialspace = skipinitialspace != null
106+
? skipinitialspace : dialect.__findattr__("skipinitialspace");
107+
strict = strict != null ? strict : dialect.__findattr__("strict");
108+
}
109+
110+
PyDialect self;
111+
if (new_.for_type == subtype) {
112+
self = new PyDialect();
113+
} else {
114+
self = new PyDialectDerived(subtype);
115+
}
116+
117+
// check types and convert to Java values
118+
int quotingOrdinal;
119+
self.delimiter = toChar("delimiter", delimiter, ',');
120+
self.doublequote = toBool("doublequote", doublequote, true);
121+
self.escapechar = toChar("escapechar", escapechar, '\0');
122+
self.lineterminator = toStr("lineterminator", lineterminator, "\r\n");
123+
self.quotechar = toChar("quotechar", quotechar, '"');
124+
quotingOrdinal = toInt("quoting", quoting, QuoteStyle.QUOTE_MINIMAL.ordinal());
125+
self.skipinitialspace = toBool("skipinitialspace", skipinitialspace, false);
126+
self.strict = toBool("strict", strict, false);
127+
128+
// validate options
129+
self.quoting = QuoteStyle.fromOrdinal(quotingOrdinal);
130+
if (self.quoting == null) {
131+
throw Py.TypeError("bad \"quoting\" value");
132+
}
133+
if (self.delimiter == '\0') {
134+
throw Py.TypeError("delimiter must be set");
135+
}
136+
if (quotechar == Py.None && quoting == null) {
137+
self.quoting = QuoteStyle.QUOTE_NONE;
138+
}
139+
if (self.quoting != QuoteStyle.QUOTE_NONE && self.quotechar == '\0') {
140+
throw Py.TypeError("quotechar must be set if quoting enabled");
141+
}
142+
if (self.lineterminator == null) {
143+
throw Py.TypeError("lineterminator must be set");
144+
}
145+
146+
return self;
147+
}
148+
149+
private static boolean toBool(String name, PyObject src, boolean dflt) {
150+
return src == null ? dflt : src.__nonzero__();
151+
}
152+
153+
private static char toChar(String name, PyObject src, char dflt) {
154+
if (src == null) {
155+
return dflt;
156+
}
157+
boolean isStr = Py.isInstance(src, PyString.TYPE);
158+
if (src == Py.None || isStr && src.__len__() == 0) {
159+
return '\0';
160+
} else if (!isStr || src.__len__() != 1) {
161+
throw Py.TypeError(String.format("\"%s\" must be an 1-character string", name));
162+
}
163+
return src.toString().charAt(0);
164+
}
165+
166+
private static int toInt(String name, PyObject src, int dflt) {
167+
if (src == null) {
168+
return dflt;
169+
}
170+
if (!(src instanceof PyInteger)) {
171+
throw Py.TypeError(String.format("\"%s\" must be an integer", name));
172+
}
173+
return src.asInt();
174+
}
175+
176+
private static String toStr(String name, PyObject src, String dflt) {
177+
if (src == null) {
178+
return dflt;
179+
}
180+
if (src == Py.None) {
181+
return null;
182+
}
183+
if (!(src instanceof PyBaseString)) {
184+
throw Py.TypeError(String.format("\"%s\" must be an string", name));
185+
}
186+
return src.toString();
187+
}
188+
189+
@ExposedGet(name = "escapechar")
190+
public PyObject getEscapechar() {
191+
return escapechar == '\0' ? Py.None : Py.newString(escapechar);
192+
}
193+
194+
@ExposedGet(name = "quotechar")
195+
public PyObject getQuotechar() {
196+
return quotechar == '\0' ? Py.None : Py.newString(quotechar);
197+
}
198+
199+
@ExposedGet(name = "quoting")
200+
public PyObject getQuoting() {
201+
return Py.newInteger(quoting.ordinal());
202+
}
203+
204+
// XXX: setQuoting and delQuoting are to make test_csv pass (currently we incorrectly
205+
// throw TypeErrors for get/set AttributeError failures)
206+
@ExposedSet(name = "quoting")
207+
public void setQuoting(PyObject obj) {
208+
throw Py.AttributeError(String.format("attribute '%s' of '%s' objects is not writable",
209+
"quoting", getType().fastGetName()));
210+
}
211+
212+
@ExposedDelete(name = "quoting")
213+
public void delQuoting() {
214+
throw Py.AttributeError(String.format("attribute '%s' of '%s' objects is not writable",
215+
"quoting", getType().fastGetName()));
216+
}
217+
}

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.