summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/bdb.py33
-rw-r--r--Lib/distutils/command/register.py95
-rw-r--r--Lib/distutils/command/upload.py41
-rw-r--r--Lib/distutils/config.py4
-rw-r--r--Lib/distutils/core.py1
-rw-r--r--Lib/distutils/dist.py7
-rw-r--r--Lib/distutils/tests/test_config.py103
-rw-r--r--Lib/distutils/tests/test_dist.py46
-rw-r--r--Lib/distutils/tests/test_upload.py34
-rw-r--r--Lib/io.py12
-rw-r--r--Lib/pdb.doc4
-rwxr-xr-xLib/pdb.py13
-rw-r--r--Lib/test/test_marshal.py8
-rw-r--r--Lib/test/test_support.py33
-rw-r--r--Lib/test/test_textwrap.py8
-rw-r--r--Lib/textwrap.py28
16 files changed, 363 insertions, 107 deletions
diff --git a/Lib/bdb.py b/Lib/bdb.py
index e55031e..b8a20ec 100644
--- a/Lib/bdb.py
+++ b/Lib/bdb.py
@@ -37,9 +37,7 @@ class Bdb:
import linecache
linecache.checkcache()
self.botframe = None
- self.stopframe = None
- self.returnframe = None
- self.quitting = 0
+ self._set_stopinfo(None, None)
def trace_dispatch(self, frame, event, arg):
if self.quitting:
@@ -100,7 +98,7 @@ class Bdb:
# (CT) stopframe may now also be None, see dispatch_call.
# (CT) the former test for None is therefore removed from here.
if frame is self.stopframe:
- return True
+ return frame.f_lineno >= self.stoplineno
while frame is not None and frame is not self.stopframe:
if frame is self.botframe:
return True
@@ -156,26 +154,31 @@ class Bdb:
but only if we are to stop at or just below this level."""
pass
+ def _set_stopinfo(self, stopframe, returnframe, stoplineno=-1):
+ self.stopframe = stopframe
+ self.returnframe = returnframe
+ self.quitting = 0
+ self.stoplineno = stoplineno
+
# Derived classes and clients can call the following methods
# to affect the stepping state.
+ def set_until(self, frame): #the name "until" is borrowed from gdb
+ """Stop when the line with the line no greater than the current one is
+ reached or when returning from current frame"""
+ self._set_stopinfo(frame, frame, frame.f_lineno+1)
+
def set_step(self):
"""Stop after one line of code."""
- self.stopframe = None
- self.returnframe = None
- self.quitting = 0
+ self._set_stopinfo(None,None)
def set_next(self, frame):
"""Stop on the next line in or below the given frame."""
- self.stopframe = frame
- self.returnframe = None
- self.quitting = 0
+ self._set_stopinfo(frame, None)
def set_return(self, frame):
"""Stop when returning from the given frame."""
- self.stopframe = frame.f_back
- self.returnframe = frame
- self.quitting = 0
+ self._set_stopinfo(frame.f_back, frame)
def set_trace(self, frame=None):
"""Start debugging from `frame`.
@@ -194,9 +197,7 @@ class Bdb:
def set_continue(self):
# Don't stop except at breakpoints or when finished
- self.stopframe = self.botframe
- self.returnframe = None
- self.quitting = 0
+ self._set_stopinfo(self.botframe, None)
if not self.breaks:
# no breakpoints; run without debugger overhead
sys.settrace(None)
diff --git a/Lib/distutils/command/register.py b/Lib/distutils/command/register.py
index b6a36f5..89cb2d4 100644
--- a/Lib/distutils/command/register.py
+++ b/Lib/distutils/command/register.py
@@ -8,42 +8,34 @@ Implements the Distutils 'register' command (register with the repository).
__revision__ = "$Id$"
import os, string, urllib2, getpass, urlparse
-import io, configparser
+import io
-from distutils.core import Command
+from distutils.core import PyPIRCCommand
from distutils.errors import *
+from distutils import log
def raw_input(prompt):
sys.stdout.write(prompt)
sys.stdout.flush()
return sys.stdin.readline()
-class register(Command):
+class register(PyPIRCCommand):
description = ("register the distribution with the Python package index")
-
- DEFAULT_REPOSITORY = 'http://pypi.python.org/pypi'
-
- user_options = [
- ('repository=', 'r',
- "url of repository [default: %s]"%DEFAULT_REPOSITORY),
+ user_options = PyPIRCCommand.user_options + [
('list-classifiers', None,
'list the valid Trove classifiers'),
- ('show-response', None,
- 'display full response text from server'),
]
- boolean_options = ['verify', 'show-response', 'list-classifiers']
+ boolean_options = PyPIRCCommand.boolean_options + [
+ 'verify', 'list-classifiers']
def initialize_options(self):
- self.repository = None
- self.show_response = 0
+ PyPIRCCommand.initialize_options(self)
self.list_classifiers = 0
- def finalize_options(self):
- if self.repository is None:
- self.repository = self.DEFAULT_REPOSITORY
-
def run(self):
+ self.finalize_options()
+ self._set_config()
self.check_metadata()
if self.dry_run:
self.verify_metadata()
@@ -82,6 +74,23 @@ class register(Command):
"or (maintainer and maintainer_email) " +
"must be supplied")
+ def _set_config(self):
+ ''' Reads the configuration file and set attributes.
+ '''
+ config = self._read_pypirc()
+ if config != {}:
+ self.username = config['username']
+ self.password = config['password']
+ self.repository = config['repository']
+ self.realm = config['realm']
+ self.has_config = True
+ else:
+ if self.repository not in ('pypi', self.DEFAULT_REPOSITORY):
+ raise ValueError('%s not found in .pypirc' % self.repository)
+ if self.repository == 'pypi':
+ self.repository = self.DEFAULT_REPOSITORY
+ self.has_config = False
+
def classifiers(self):
''' Fetch the list of classifiers from the server.
'''
@@ -95,6 +104,7 @@ class register(Command):
(code, result) = self.post_to_server(self.build_post_data('verify'))
print('Server response (%s): %s'%(code, result))
+
def send_metadata(self):
''' Send the metadata to the package index server.
@@ -104,10 +114,14 @@ class register(Command):
First we try to read the username/password from $HOME/.pypirc,
which is a ConfigParser-formatted file with a section
- [server-login] containing username and password entries (both
+ [distutils] containing username and password entries (both
in clear text). Eg:
- [server-login]
+ [distutils]
+ index-servers =
+ pypi
+
+ [pypi]
username: fred
password: sekrit
@@ -119,21 +133,15 @@ class register(Command):
3. set the password to a random string and email the user.
'''
- choice = 'x'
- username = password = ''
-
# see if we can short-cut and get the username/password from the
# config
- config = None
- if 'HOME' in os.environ:
- rc = os.path.join(os.environ['HOME'], '.pypirc')
- if os.path.exists(rc):
- print('Using PyPI login from %s'%rc)
- config = ConfigParser.ConfigParser()
- config.read(rc)
- username = config.get('server-login', 'username')
- password = config.get('server-login', 'password')
- choice = '1'
+ if self.has_config:
+ choice = '1'
+ username = self.username
+ password = self.password
+ else:
+ choice = 'x'
+ username = password = ''
# get the user's login info
choices = '1 2 3 4'.split()
@@ -160,32 +168,24 @@ Your selection [default 1]: ''', end=' ')
# set up the authentication
auth = urllib2.HTTPPasswordMgr()
host = urlparse.urlparse(self.repository)[1]
- auth.add_password('pypi', host, username, password)
-
+ auth.add_password(self.realm, host, username, password)
# send the info to the server and report the result
code, result = self.post_to_server(self.build_post_data('submit'),
auth)
print('Server response (%s): %s'%(code, result))
# possibly save the login
- if 'HOME' in os.environ and config is None and code == 200:
- rc = os.path.join(os.environ['HOME'], '.pypirc')
+ if not self.has_config and code == 200:
print('I can store your PyPI login so future submissions will be faster.')
- print('(the login will be stored in %s)'%rc)
+ print('(the login will be stored in %s)' % self._get_rc_file())
choice = 'X'
while choice.lower() not in 'yn':
choice = raw_input('Save your login (y/N)?')
if not choice:
choice = 'n'
if choice.lower() == 'y':
- f = open(rc, 'w')
- f.write('[server-login]\nusername:%s\npassword:%s\n'%(
- username, password))
- f.close()
- try:
- os.chmod(rc, 0o600)
- except:
- pass
+ self._store_pypirc(username, password)
+
elif choice == '2':
data = {':action': 'user'}
data['name'] = data['password'] = data['email'] = ''
@@ -248,7 +248,8 @@ Your selection [default 1]: ''', end=' ')
def post_to_server(self, data, auth=None):
''' Post a query to the server, and return a string response.
'''
-
+ self.announce('Registering %s to %s' % (data['name'],
+ self.repository), log.INFO)
# Build up the MIME payload for the urllib2 POST data
boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
sep_boundary = '\n--' + boundary
diff --git a/Lib/distutils/command/upload.py b/Lib/distutils/command/upload.py
index 23999ae..603336c 100644
--- a/Lib/distutils/command/upload.py
+++ b/Lib/distutils/command/upload.py
@@ -3,7 +3,7 @@
Implements the Distutils 'upload' subcommand (upload package to PyPI)."""
from distutils.errors import *
-from distutils.core import Command
+from distutils.core import PyPIRCCommand
from distutils.spawn import spawn
from distutils import log
from hashlib import md5
@@ -15,53 +15,38 @@ import httplib
import base64
import urlparse
-class upload(Command):
+class upload(PyPIRCCommand):
description = "upload binary package to PyPI"
- DEFAULT_REPOSITORY = 'http://pypi.python.org/pypi'
-
- user_options = [
- ('repository=', 'r',
- "url of repository [default: %s]" % DEFAULT_REPOSITORY),
- ('show-response', None,
- 'display full response text from server'),
+ user_options = PyPIRCCommand.user_options + [
('sign', 's',
'sign files to upload using gpg'),
('identity=', 'i', 'GPG identity used to sign files'),
]
- boolean_options = ['show-response', 'sign']
+
+ boolean_options = PyPIRCCommand.boolean_options + ['sign']
def initialize_options(self):
+ PyPIRCCommand.initialize_options(self)
self.username = ''
self.password = ''
- self.repository = ''
self.show_response = 0
self.sign = False
self.identity = None
def finalize_options(self):
+ PyPIRCCommand.finalize_options(self)
if self.identity and not self.sign:
raise DistutilsOptionError(
"Must use --sign for --identity to have meaning"
)
- if 'HOME' in os.environ:
- rc = os.path.join(os.environ['HOME'], '.pypirc')
- if os.path.exists(rc):
- self.announce('Using PyPI login from %s' % rc)
- config = ConfigParser.ConfigParser({
- 'username':'',
- 'password':'',
- 'repository':''})
- config.read(rc)
- if not self.repository:
- self.repository = config.get('server-login', 'repository')
- if not self.username:
- self.username = config.get('server-login', 'username')
- if not self.password:
- self.password = config.get('server-login', 'password')
- if not self.repository:
- self.repository = self.DEFAULT_REPOSITORY
+ config = self._read_pypirc()
+ if config != {}:
+ self.username = config['username']
+ self.password = config['password']
+ self.repository = config['repository']
+ self.realm = config['realm']
def run(self):
if not self.distribution.dist_files:
diff --git a/Lib/distutils/config.py b/Lib/distutils/config.py
index e9ba402..a625aec 100644
--- a/Lib/distutils/config.py
+++ b/Lib/distutils/config.py
@@ -49,7 +49,7 @@ class PyPIRCCommand(Command):
finally:
f.close()
try:
- os.chmod(rc, 0600)
+ os.chmod(rc, 0o600)
except OSError:
# should do something better here
pass
@@ -58,7 +58,7 @@ class PyPIRCCommand(Command):
"""Reads the .pypirc file."""
rc = self._get_rc_file()
if os.path.exists(rc):
- print 'Using PyPI login from %s' % rc
+ print('Using PyPI login from %s' % rc)
repository = self.repository or self.DEFAULT_REPOSITORY
realm = self.realm or self.DEFAULT_REALM
diff --git a/Lib/distutils/core.py b/Lib/distutils/core.py
index a4c5e18..6e48920 100644
--- a/Lib/distutils/core.py
+++ b/Lib/distutils/core.py
@@ -17,6 +17,7 @@ from distutils.util import grok_environment_error
# Mainly import these so setup scripts can "from distutils.core import" them.
from distutils.dist import Distribution
from distutils.cmd import Command
+from distutils.config import PyPIRCCommand
from distutils.extension import Extension
# This is a barebones help message generated displayed when the user
diff --git a/Lib/distutils/dist.py b/Lib/distutils/dist.py
index 847eb90..ddde909 100644
--- a/Lib/distutils/dist.py
+++ b/Lib/distutils/dist.py
@@ -334,10 +334,9 @@ Common commands: (see '--help-commands' for more)
user_filename = "pydistutils.cfg"
# And look for the user config file
- if 'HOME' in os.environ:
- user_file = os.path.join(os.environ.get('HOME'), user_filename)
- if os.path.isfile(user_file):
- files.append(user_file)
+ user_file = os.path.join(os.path.expanduser('~'), user_filename)
+ if os.path.isfile(user_file):
+ files.append(user_file)
# All platforms support local setup.cfg
local_file = "setup.cfg"
diff --git a/Lib/distutils/tests/test_config.py b/Lib/distutils/tests/test_config.py
new file mode 100644
index 0000000..016ba4c
--- /dev/null
+++ b/Lib/distutils/tests/test_config.py
@@ -0,0 +1,103 @@
+"""Tests for distutils.pypirc.pypirc."""
+import sys
+import os
+import unittest
+
+from distutils.core import PyPIRCCommand
+from distutils.core import Distribution
+
+from distutils.tests import support
+
+PYPIRC = """\
+[distutils]
+
+index-servers =
+ server1
+ server2
+
+[server1]
+username:me
+password:secret
+
+[server2]
+username:meagain
+password: secret
+realm:acme
+repository:http://another.pypi/
+"""
+
+PYPIRC_OLD = """\
+[server-login]
+username:tarek
+password:secret
+"""
+
+class PyPIRCCommandTestCase(support.TempdirManager, unittest.TestCase):
+
+ def setUp(self):
+ """Patches the environment."""
+ if 'HOME' in os.environ:
+ self._old_home = os.environ['HOME']
+ else:
+ self._old_home = None
+ curdir = os.path.dirname(__file__)
+ os.environ['HOME'] = curdir
+ self.rc = os.path.join(curdir, '.pypirc')
+ self.dist = Distribution()
+
+ class command(PyPIRCCommand):
+ def __init__(self, dist):
+ PyPIRCCommand.__init__(self, dist)
+ def initialize_options(self):
+ pass
+ finalize_options = initialize_options
+
+ self._cmd = command
+
+ def tearDown(self):
+ """Removes the patch."""
+ if self._old_home is None:
+ del os.environ['HOME']
+ else:
+ os.environ['HOME'] = self._old_home
+ if os.path.exists(self.rc):
+ os.remove(self.rc)
+
+ def test_server_registration(self):
+ # This test makes sure PyPIRCCommand knows how to:
+ # 1. handle several sections in .pypirc
+ # 2. handle the old format
+
+ # new format
+ f = open(self.rc, 'w')
+ try:
+ f.write(PYPIRC)
+ finally:
+ f.close()
+
+ cmd = self._cmd(self.dist)
+ config = cmd._read_pypirc()
+
+ config = list(sorted(config.items()))
+ waited = [('password', 'secret'), ('realm', 'pypi'),
+ ('repository', 'http://pypi.python.org/pypi'),
+ ('server', 'server1'), ('username', 'me')]
+ self.assertEquals(config, waited)
+
+ # old format
+ f = open(self.rc, 'w')
+ f.write(PYPIRC_OLD)
+ f.close()
+
+ config = cmd._read_pypirc()
+ config = list(sorted(config.items()))
+ waited = [('password', 'secret'), ('realm', 'pypi'),
+ ('repository', 'http://pypi.python.org/pypi'),
+ ('server', 'server-login'), ('username', 'tarek')]
+ self.assertEquals(config, waited)
+
+def test_suite():
+ return unittest.makeSuite(PyPIRCCommandTestCase)
+
+if __name__ == "__main__":
+ unittest.main(defaultTest="test_suite")
diff --git a/Lib/distutils/tests/test_dist.py b/Lib/distutils/tests/test_dist.py
index 91acf45..81459ac 100644
--- a/Lib/distutils/tests/test_dist.py
+++ b/Lib/distutils/tests/test_dist.py
@@ -55,6 +55,7 @@ class DistributionTestCase(unittest.TestCase):
self.assertEqual(d.get_command_packages(), ["distutils.command"])
def test_command_packages_cmdline(self):
+ from distutils.tests.test_dist import test_dist
sys.argv.extend(["--command-packages",
"foo.bar,distutils.tests",
"test_dist",
@@ -179,9 +180,54 @@ class MetadataTestCase(unittest.TestCase):
dist.metadata.write_pkg_file(sio)
return sio.getvalue()
+ def test_custom_pydistutils(self):
+ # fixes #2166
+ # make sure pydistutils.cfg is found
+ old = {}
+ for env in ('HOME', 'HOMEPATH', 'HOMEDRIVE'):
+ value = os.environ.get(env)
+ old[env] = value
+ if value is not None:
+ del os.environ[env]
+
+ if os.name == 'posix':
+ user_filename = ".pydistutils.cfg"
+ else:
+ user_filename = "pydistutils.cfg"
+
+ curdir = os.path.dirname(__file__)
+ user_filename = os.path.join(curdir, user_filename)
+ f = open(user_filename, 'w')
+ f.write('.')
+ f.close()
+
+ try:
+ dist = distutils.dist.Distribution()
+
+ # linux-style
+ if sys.platform in ('linux', 'darwin'):
+ os.environ['HOME'] = curdir
+ files = dist.find_config_files()
+ self.assert_(user_filename in files)
+
+ # win32-style
+ if sys.platform == 'win32':
+ # home drive should be found
+ os.environ['HOMEPATH'] = curdir
+ files = dist.find_config_files()
+ self.assert_(user_filename in files)
+ finally:
+ for key, value in old.items():
+ if value is None:
+ continue
+ os.environ[key] = value
+ os.remove(user_filename)
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(DistributionTestCase))
suite.addTest(unittest.makeSuite(MetadataTestCase))
return suite
+
+if __name__ == "__main__":
+ unittest.main(defaultTest="test_suite")
diff --git a/Lib/distutils/tests/test_upload.py b/Lib/distutils/tests/test_upload.py
new file mode 100644
index 0000000..b05ab1f
--- /dev/null
+++ b/Lib/distutils/tests/test_upload.py
@@ -0,0 +1,34 @@
+"""Tests for distutils.command.upload."""
+import sys
+import os
+import unittest
+
+from distutils.command.upload import upload
+from distutils.core import Distribution
+
+from distutils.tests import support
+from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase
+
+class uploadTestCase(PyPIRCCommandTestCase):
+
+ def test_finalize_options(self):
+
+ # new format
+ f = open(self.rc, 'w')
+ f.write(PYPIRC)
+ f.close()
+
+ dist = Distribution()
+ cmd = upload(dist)
+ cmd.finalize_options()
+ for attr, waited in (('username', 'me'), ('password', 'secret'),
+ ('realm', 'pypi'),
+ ('repository', 'http://pypi.python.org/pypi')):
+ self.assertEquals(getattr(cmd, attr), waited)
+
+
+def test_suite():
+ return unittest.makeSuite(uploadTestCase)
+
+if __name__ == "__main__":
+ unittest.main(defaultTest="test_suite")
diff --git a/Lib/io.py b/Lib/io.py
index d5c8dfe..207e428 100644
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -813,14 +813,14 @@ class _BytesIO(BufferedIOBase):
n = len(b)
if n == 0:
return 0
- newpos = self._pos + n
- if newpos > len(self._buffer):
+ pos = self._pos
+ if pos > len(self._buffer):
# Inserts null bytes between the current end of the file
# and the new write position.
- padding = b'\x00' * (newpos - len(self._buffer) - n)
- self._buffer[self._pos:newpos - n] = padding
- self._buffer[self._pos:newpos] = b
- self._pos = newpos
+ padding = b'\x00' * (pos - len(self._buffer))
+ self._buffer += padding
+ self._buffer[pos:pos + n] = b
+ self._pos += n
return n
def seek(self, pos, whence=0):
diff --git a/Lib/pdb.doc b/Lib/pdb.doc
index c513954..0d32800 100644
--- a/Lib/pdb.doc
+++ b/Lib/pdb.doc
@@ -128,6 +128,10 @@ n(ext)
Continue execution until the next line in the current function
is reached or it returns.
+unt(il)
+ Continue execution until the line with a number greater than the
+ current one is reached or until the current frame returns.
+
r(eturn)
Continue execution until the current function returns.
diff --git a/Lib/pdb.py b/Lib/pdb.py
index d77ea28..7e1edb5 100755
--- a/Lib/pdb.py
+++ b/Lib/pdb.py
@@ -611,6 +611,11 @@ class Pdb(bdb.Bdb, cmd.Cmd):
self.lineno = None
do_d = do_down
+ def do_until(self, arg):
+ self.set_until(self.curframe)
+ return 1
+ do_unt = do_until
+
def do_step(self, arg):
self.set_step()
return 1
@@ -958,6 +963,14 @@ i.e., the breakpoint is made unconditional.""", file=self.stdout)
Execute the current line, stop at the first possible occasion
(either in a function that is called or in the current function).""", file=self.stdout)
+ def help_until(self):
+ self.help_unt()
+
+ def help_unt(self):
+ print("""unt(il)
+Continue execution until the line with a number greater than the current
+one is reached or until the current frame returns""")
+
def help_next(self):
self.help_n()
diff --git a/Lib/test/test_marshal.py b/Lib/test/test_marshal.py
index 66545e0..838207a 100644
--- a/Lib/test/test_marshal.py
+++ b/Lib/test/test_marshal.py
@@ -199,6 +199,14 @@ class BugsTestCase(unittest.TestCase):
subtyp = type('subtyp', (typ,), {})
self.assertRaises(ValueError, marshal.dumps, subtyp())
+ # Issue #1792 introduced a change in how marshal increases the size of its
+ # internal buffer; this test ensures that the new code is exercised.
+ def test_large_marshal(self):
+ size = int(1e6)
+ testString = 'abc' * size
+ marshal.dumps(testString)
+
+
def test_main():
test_support.run_unittest(IntTestCase,
FloatTestCase,
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
index 27a01ea..c4084bb 100644
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -409,6 +409,39 @@ def catch_warning(module=warnings, record=True):
module.showwarning = original_showwarning
module.filters = original_filters
+
+class CleanImport(object):
+ """Context manager to force import to return a new module reference.
+
+ This is useful for testing module-level behaviours, such as
+ the emission of a DepreciationWarning on import.
+
+ Use like this:
+
+ with CleanImport("foo"):
+ __import__("foo") # new reference
+ """
+
+ def __init__(self, *module_names):
+ self.original_modules = sys.modules.copy()
+ for module_name in module_names:
+ if module_name in sys.modules:
+ module = sys.modules[module_name]
+ # It is possible that module_name is just an alias for
+ # another module (e.g. stub for modules renamed in 3.x).
+ # In that case, we also need delete the real module to clear
+ # the import cache.
+ if module.__name__ != module_name:
+ del sys.modules[module.__name__]
+ del sys.modules[module_name]
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *ignore_exc):
+ sys.modules.update(self.original_modules)
+
+
class EnvironmentVarGuard(object):
"""Class to help protect the environment variable properly. Can be used as
diff --git a/Lib/test/test_textwrap.py b/Lib/test/test_textwrap.py
index dc97d40..1a9761a 100644
--- a/Lib/test/test_textwrap.py
+++ b/Lib/test/test_textwrap.py
@@ -351,6 +351,14 @@ What a mess!
["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-",
"ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"])
+ def test_break_on_hyphens(self):
+ # Ensure that the break_on_hyphens attributes work
+ text = "yaba daba-doo"
+ self.check_wrap(text, 10, ["yaba daba-", "doo"],
+ break_on_hyphens=True)
+ self.check_wrap(text, 10, ["yaba", "daba-doo"],
+ break_on_hyphens=False)
+
def test_bad_width(self):
# Ensure that width <= 0 is caught.
text = "Whatever, it doesn't matter."
diff --git a/Lib/textwrap.py b/Lib/textwrap.py
index b5f87efc..6a2021d 100644
--- a/Lib/textwrap.py
+++ b/Lib/textwrap.py
@@ -55,6 +55,10 @@ class TextWrapper:
break_long_words (default: true)
Break words longer than 'width'. If false, those words will not
be broken, and some lines might be longer than 'width'.
+ break_on_hyphens (default: true)
+ Allow breaking hyphenated words. If true, wrapping will occur
+ preferably on whitespaces and right after hyphens part of
+ compound words.
drop_whitespace (default: true)
Drop leading and trailing whitespace from lines.
"""
@@ -75,11 +79,18 @@ class TextWrapper:
r'[^\s\w]*\w+[a-zA-Z]-(?=\w+[a-zA-Z])|' # hyphenated words
r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))') # em-dash
- # XXX this is not locale-aware
+ # This less funky little regex just split on recognized spaces. E.g.
+ # "Hello there -- you goof-ball, use the -b option!"
+ # splits into
+ # Hello/ /there/ /--/ /you/ /goof-ball,/ /use/ /the/ /-b/ /option!/
+ wordsep_simple_re = re.compile(r'(\s+)')
+
+ # XXX this is not locale- or charset-aware -- string.lowercase
+ # is US-ASCII only (and therefore English-only)
sentence_end_re = re.compile(r'[a-z]' # lowercase letter
r'[\.\!\?]' # sentence-ending punct.
r'[\"\']?' # optional end-of-quote
- r'\Z') # end of chunk
+ r'\Z') # end of chunk
def __init__(self,
@@ -90,7 +101,8 @@ class TextWrapper:
replace_whitespace=True,
fix_sentence_endings=False,
break_long_words=True,
- drop_whitespace=True):
+ drop_whitespace=True,
+ break_on_hyphens=True):
self.width = width
self.initial_indent = initial_indent
self.subsequent_indent = subsequent_indent
@@ -99,6 +111,7 @@ class TextWrapper:
self.fix_sentence_endings = fix_sentence_endings
self.break_long_words = break_long_words
self.drop_whitespace = drop_whitespace
+ self.break_on_hyphens = break_on_hyphens
# -- Private methods -----------------------------------------------
@@ -128,8 +141,15 @@ class TextWrapper:
breaks into the following chunks:
'Look,', ' ', 'goof-', 'ball', ' ', '--', ' ',
'use', ' ', 'the', ' ', '-b', ' ', 'option!'
+ if break_on_hyphens is True, or in:
+ 'Look,', ' ', 'goof-ball', ' ', '--', ' ',
+ 'use', ' ', 'the', ' ', '-b', ' ', option!'
+ otherwise.
"""
- chunks = self.wordsep_re.split(text)
+ if self.break_on_hyphens is True:
+ chunks = self.wordsep_re.split(text)
+ else:
+ chunks = self.wordsep_simple_re.split(text)
chunks = [c for c in chunks if c]
return chunks