Skip to content
Merged
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
2 changes: 2 additions & 0 deletions Doc/library/json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ Basic Usage
into JSON and then back into a dictionary, the dictionary may not equal
the original one. That is, ``loads(dumps(x)) != x`` if x has non-string
keys.
*sort_keys* sorts the keys before they are coerced to strings,
so numeric keys are sorted by value, not by their string representation.

.. function:: load(fp, *, cls=None, object_hook=None, parse_float=None, \
parse_int=None, parse_constant=None, \
Expand Down
2 changes: 1 addition & 1 deletion Doc/library/tk.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ alternative `GUI frameworks and tools <https://wiki.python.org/moin/GuiProgrammi
tkinter.colorchooser.rst
tkinter.font.rst
tkinter.fontchooser.rst
dialog.rst
tkinter.dialogs.rst
tkinter.messagebox.rst
tkinter.scrolledtext.rst
tkinter.systray.rst
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions Doc/tools/removed-ids.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,6 @@ reference/expressions.html: generator.__next__
reference/expressions.html: generator.close
reference/expressions.html: generator.send
reference/expressions.html: generator.throw

# Renamed to library/tkinter.dialogs.html
library/dialog.html: (page missing)
23 changes: 21 additions & 2 deletions Lib/test/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ def test_write_quoting(self):
quoting = csv.QUOTE_STRINGS)
self._write_test(['a','',None,1], '"a","",,"1"',
quoting = csv.QUOTE_NOTNULL)
# FULLWIDTH QUOTATION MARK
self._write_test(['a', 1, 'p,q', 'r"s', 'x!y'],
'a,1,"p,q","r""s",x!y',
quotechar='"')

def test_write_delimiter(self):
self._write_test(['a', 1, 'p,q', 'x;y'], 'a,1,"p,q",x;y')
self._write_test(['a', 1, 'p;q', 'x,y'], 'a;1;"p;q";x,y', delimiter=';')
self._write_test(['a', 1, 'p\0q', 'x,y'], 'a\x001\0"p\0q"\0x,y',
delimiter='\0')
self._write_test(['a', 1, 'p🍌q', 'x🍍y'], 'a🍌1🍌"p🍌q"🍌x🍍y',
delimiter='🍌')

def test_write_escape(self):
self._write_test(['a',1,'p,q'], 'a,1,"p,q"',
Expand Down Expand Up @@ -258,19 +270,26 @@ def test_write_escape(self):
escapechar='\\', quoting=csv.QUOTE_MINIMAL)
self._write_test(['C\\', '6', '7', 'X"'], 'C\\\\,6,7,"X"""',
escapechar='\\', quoting=csv.QUOTE_MINIMAL)
# SYMBOL FOR ESCAPE
self._write_test(['a', 1, 'p,q', 'r\u241bs', 'x\u241ay'],
'a,1,p\u241b,q,r\u241b\u241bs,x\u241ay',
escapechar='\u241b', quoting=csv.QUOTE_NONE)

def test_write_lineterminator(self):
for lineterminator in '\r\n', '\n', '\r', '!@#', '\0':
for lineterminator in ('\r\n', '\n', '\r', '!@#', '\0',
'\x85', '\u2028', '\U0001f600'):
with self.subTest(lineterminator=lineterminator):
with StringIO() as sio:
writer = csv.writer(sio, lineterminator=lineterminator)
writer.writerow(['a', 'b'])
writer.writerow([1, 2])
writer.writerow(['\r', '\n'])
writer.writerow([f'a{lineterminator[-1]}b', 'c'])
self.assertEqual(sio.getvalue(),
f'a,b{lineterminator}'
f'1,2{lineterminator}'
f'"\r","\n"{lineterminator}')
f'"\r","\n"{lineterminator}'
f'"a{lineterminator[-1]}b",c{lineterminator}')

def test_write_iterable(self):
self._write_test(iter(['a', 1, 'p,q']), 'a,1,"p,q"')
Expand Down
40 changes: 40 additions & 0 deletions Lib/test/test_tkinter/test_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ def actual_size(self, desc):
# The requested size is not always available (e.g. bitmap fonts).
return self.root.tk.call('font', 'actual', desc, '-size')

def tcl_font_object(self, desc):
# Return a font name or description as a Tcl object representing a
# font, as Tk returns for example from ttk.Style().lookup().
tk = self.root.tk
tk.call('set', '_font', desc)
tk.eval('font measure $_font x') # convert the Tcl object to a font
obj = tk.call('set', '_font')
tk.call('unset', '_font')
return obj

def test_configure(self):
self.assertEqual(self.font.config, self.font.configure)
options = self.font.configure()
Expand Down Expand Up @@ -150,6 +160,36 @@ def test_existing(self):
# A name or a description is required.
self.assertRaises(TypeError, font.Font, root=self.root, exists=True)

def test_tcl_object(self):
# Tk can return a font as a Tcl object (gh-156961).
if not self.wantobjects:
self.skipTest('Tcl objects are converted to strings')
obj = self.tcl_font_object(fontname)
self.assertEqual(obj.typename, 'font')

# It can be used as the name of an existing named font.
for f in (font.Font(root=self.root, name=obj, exists=True),
font.nametofont(obj, root=self.root)):
# The Tcl object is kept as is, so that it is passed back to Tk.
self.assertIs(f.name, obj)
self.assertEqual(str(f), fontname)
self.assertEqual(f.actual(), self.font.actual())
self.assertEqual(f, self.font)
self.assertEqual(self.font, f)
# Referring to a non-existent named font still fails.
self.assertRaisesRegex(tkinter.TclError, 'named font nosuchfont',
font.Font, root=self.root, exists=True,
name=self.tcl_font_object('nosuchfont'))

# It can also be wrapped as a font description.
obj = self.tcl_font_object(('Times', 20, 'bold'))
f = font.Font(root=self.root, font=obj, exists=True)
self.assertIs(f.name, obj)
self.assertEqual(str(f), 'Times 20 bold')
self.assertNotIn(f.name, font.names(self.root))
self.assertEqual(f.actual('weight'), 'bold')
self.assertEqual(f.actual('size'), self.actual_size(('Times', 20, 'bold')))

def test_copy(self):
# size=-20 (pixels): copy() copies the configured options, so the
# size is preserved rather than resolved (gh-143990).
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -4211,6 +4211,21 @@ def close(self):
('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'))

for doctype, expected in [
('<!DOCTYPE html>', ('html', None, None)),
('<!DOCTYPE html [<!ENTITY e "v">]>', ('html', None, None)),
('<!DOCTYPE html SYSTEM "a.dtd">', ('html', None, 'a.dtd')),
('<!DOCTYPE html SYSTEM "a.dtd" [<!ENTITY e "v">]>',
('html', None, 'a.dtd')),
('<!DOCTYPE html PUBLIC "-//P" "a.dtd">', ('html', '-//P', 'a.dtd')),
("<!DOCTYPE\nhtml\nPUBLIC\n'-//P'\n'a.dtd'\n>",
('html', '-//P', 'a.dtd')),
]:
with self.subTest(doctype=doctype):
parser = ET.XMLParser(target=DoctypeParser())
parser.feed(doctype + '<html/>')
self.assertEqual(parser.close(), expected)

def test_builder_lookup_errors(self):
class RaisingBuilder:
def __init__(self, raise_in=None, what=ValueError):
Expand Down
21 changes: 14 additions & 7 deletions Lib/tkinter/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ def __init__(self, root=None, font=None, name=None, exists=False,
if exists:
self.name = name
# confirm font exists
if self.name not in tk.splitlist(tk.call("font", "names")):
name = getattr(name, 'string', name) # can be a Tcl object
if name not in tk.splitlist(tk.call("font", "names")):
raise tkinter._tkinter.TclError(
"named font %s does not already exist" % (self.name,))
# if font config info supplied, apply it
Expand All @@ -123,11 +124,11 @@ def __init__(self, root=None, font=None, name=None, exists=False,
self._call = tk.call

def __str__(self):
# A wrapped description is a list or tuple, not a string; format it as
# a Tcl word so it can be used as an option value (as ttk does).
if isinstance(self.name, str):
return self.name
return tkinter._join(self.name)
# A wrapped description can be a list or tuple; format it as a Tcl
# word so it can be used as an option value (as ttk does).
if isinstance(self.name, (list, tuple)):
return tkinter._join(self.name)
return str(self.name)

def __repr__(self):
return f"<{self.__class__.__module__}.{self.__class__.__qualname__}" \
Expand All @@ -136,7 +137,13 @@ def __repr__(self):
def __eq__(self, other):
if not isinstance(other, Font):
return NotImplemented
return self.name == other.name and self._tk == other._tk
name = self.name
other_name = other.name
if type(name) is not type(other_name):
# A Tcl object does not compare equal to a string.
name = getattr(name, 'string', name)
other_name = getattr(other_name, 'string', other_name)
return name == other_name and self._tk == other._tk

def __getitem__(self, key):
return self.cget(key)
Expand Down
43 changes: 10 additions & 33 deletions Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -1591,10 +1591,10 @@ def __init__(self, *, target=None, encoding=None):
parser.CommentHandler = target.comment
if hasattr(target, 'pi'):
parser.ProcessingInstructionHandler = target.pi
parser.StartDoctypeDeclHandler = self._start_doctype
# Configure pyexpat: buffering, new-style attribute handling.
parser.buffer_text = 1
parser.ordered_attributes = 1
self._doctype = None
self.entity = {}
try:
self.version = "Expat %d.%d.%d" % expat.version_info
Expand Down Expand Up @@ -1713,38 +1713,15 @@ def _default(self, text):
err.lineno = self.parser.ErrorLineNumber
err.offset = self.parser.ErrorColumnNumber
raise err
elif prefix == "<" and text[:9] == "<!DOCTYPE":
self._doctype = [] # inside a doctype declaration
elif self._doctype is not None:
# parse doctype contents
if prefix == ">":
self._doctype = None
return
text = text.strip(_XML_WHITESPACE)
if not text:
return
self._doctype.append(text)
n = len(self._doctype)
if n > 2:
type = self._doctype[1]
if type == "PUBLIC" and n == 4:
name, type, pubid, system = self._doctype
if pubid:
pubid = pubid[1:-1]
elif type == "SYSTEM" and n == 3:
name, type, system = self._doctype
pubid = None
else:
return
if hasattr(self.target, "doctype"):
self.target.doctype(name, pubid, system[1:-1])
elif hasattr(self, "doctype"):
warnings.warn(
"The doctype() method of XMLParser is ignored. "
"Define doctype() method on the TreeBuilder target.",
RuntimeWarning)

self._doctype = None

def _start_doctype(self, name, system, pubid, has_internal_subset):
if hasattr(self.target, "doctype"):
self.target.doctype(name, pubid, system)
elif hasattr(self, "doctype"):
warnings.warn(
"The doctype() method of XMLParser is ignored. "
"Define doctype() method on the TreeBuilder target.",
RuntimeWarning)

def feed(self, data):
"""Feed encoded data to parser."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Rename Doc/library/dialog.rst to tkinter.dialogs.rst.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :func:`tkinter.font.nametofont` and the :class:`tkinter.font.Font`
constructor for a font name or description returned by Tk as a Tcl object,
for example by :meth:`ttk.Style.lookup() <tkinter.ttk.Style.lookup>`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Speed up :func:`csv.writer` by caching the set of characters that need
quoting or escaping in the dialect. Writing long fields is now up to 5 times
faster.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix the Python implementation of :class:`xml.etree.ElementTree.XMLParser`:
the ``doctype()`` method of the target is now called for a document type
declaration without an external identifier, like ``<!DOCTYPE html>``,
as in the C implementation.
65 changes: 56 additions & 9 deletions Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ typedef struct {
Py_UCS4 quotechar; /* quote character */
Py_UCS4 escapechar; /* escape character */
PyObject *lineterminator; /* string to write between records */

/* Cache for the writer: bit c is set if the ASCII character c needs
quoting or escaping (delimiter, quotechar, escapechar, '\r', '\n'
and the characters of lineterminator). */
uint64_t special_chars[2];
/* Whether any of the special characters is non-ASCII. */
bool nonascii_special;
} DialectObj;

typedef struct {
Expand Down Expand Up @@ -332,6 +337,54 @@ _set_str(const char *name, PyObject **target, PyObject *src, const char *dflt)
return 0;
}

static void
dialect_add_special_char(DialectObj *self, Py_UCS4 c)
{
if (c == NOT_SET) {
return;
}
if (c < 128) {
self->special_chars[c / 64] |= (uint64_t)1 << (c % 64);
}
else {
self->nonascii_special = true;
}
}

static void
dialect_init_special_chars_cache(DialectObj *self)
{
self->special_chars[0] = self->special_chars[1] = 0;
self->nonascii_special = false;
dialect_add_special_char(self, self->delimiter);
dialect_add_special_char(self, self->quotechar);
dialect_add_special_char(self, self->escapechar);
dialect_add_special_char(self, '\r');
dialect_add_special_char(self, '\n');
PyObject *lt = self->lineterminator;
for (Py_ssize_t i = 0; i < PyUnicode_GET_LENGTH(lt); i++) {
dialect_add_special_char(self, PyUnicode_READ_CHAR(lt, i));
}
}

/* Whether the character needs quoting or escaping by the writer. */
static inline bool
dialect_is_special_char(DialectObj *self, Py_UCS4 c)
{
if (c < 128) {
return (self->special_chars[c / 64] >> (c % 64)) & 1;
}
if (!self->nonascii_special) {
return false;
}
return (c == self->delimiter ||
c == self->quotechar ||
c == self->escapechar ||
PyUnicode_FindChar(self->lineterminator, c, 0,
PyUnicode_GET_LENGTH(self->lineterminator),
1) >= 0);
}

static int
dialect_check_quoting(int quoting)
{
Expand Down Expand Up @@ -558,6 +611,7 @@ dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
goto err;
}
dialect_init_special_chars_cache(self);

ret = Py_NewRef(self);
err:
Expand Down Expand Up @@ -1208,14 +1262,7 @@ join_append_data(WriterObj *self, int field_kind, const void *field_data,
Py_UCS4 c = PyUnicode_READ(field_kind, field_data, i);
int want_escape = 0;

if (c == dialect->delimiter ||
c == dialect->escapechar ||
c == dialect->quotechar ||
c == '\n' ||
c == '\r' ||
PyUnicode_FindChar(
dialect->lineterminator, c, 0,
PyUnicode_GET_LENGTH(dialect->lineterminator), 1) >= 0) {
if (dialect_is_special_char(dialect, c)) {
if (dialect->quoting == QUOTE_NONE)
want_escape = 1;
else {
Expand Down
Loading