[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

In general, programs can avoid Unicode encoding errors by either catching exceptions
or opening files in binary mode; since Grep might not be able to interpret some of the
files it visits as text at all, it takes the former approach. Really, opening even text files
in binary mode to read raw byte strings in 3.X mimics the behavior of text files in 2.X,
and underscores why forcing programs to deal with Unicode is sometimes a good
thing—binary mode avoids decoding exceptions, but probably shouldn’t, because the
still-encoded text might not work as expected. In this case, it might yield invalid com-
parison results.


For more details on Grep’s Unicode support, and a set of open issues and options
surrounding it, see the source code listed ahead. For a suggested enhancement, see also
the re pattern matching module in Chapter 19—a tool we could use to search for
patterns instead of specific strings.


Update for initial positioning


Also in this version, text editor updates itself before inserting text into its text widget
at construction time when it is passed an initial file name in its loadFirst argument.
Sometime after the third edition and Python 2.5, either Tk or tkinter changed such that
inserting text before an update call caused the scroll position to be off by one—the text
editor started with line 2 at its top in this mode instead of line 1. This also occurs in
the third edition’s version of this example under Python 2.6, but not 2.5; adding an
update correctly positions at line 1 initially. Obscure but true in the real world of library
dependencies!†


Clients of the classes here should also update before manually inserting text into a newly
created (or packed) text editor object for accurate positioning; PyView later in this
chapter as well as PyMailGUI in Chapter 14 now do. PyEdit doesn’t update itself on
every construction, because it may be created early by, or even hidden in, an enclosing
GUI (for instance, this would show a half-complete window in PyView). Moreover,
PyEdit could automatically update itself at the start of setAllText instead of requiring
this step of clients, but forced update is required only once initially after being packed
(not before each text insertion), and this too might be an undesirable side effect in some
conceivable use cases. As a rule of thumb, adding unrelated operations to methods this
way tends to limit their scope.


Improvements for running code


The Run Code option in the Tools menu was fixed in three ways that make it more
useful for running code being edited from its external file, rather than in-process:


† Interestingly, Python’s own IDLE text editor in Python 3.1 suffers from two of the same bugs described here
and resolved in this edition’s PyEdit—in 3.1, IDLE positions at line 2 instead of line 1 on file opens, and its
external files search (similar to PyEdit’s Grep) crashes on 3.X Unicode decoding errors when scanning the
Python standard library, causing IDLE to exit altogether. Insert snarky comment about the shoemaker’s
children having no shoes here...


PyEdit: A Text Editor Program/Object | 687
Free download pdf