aboutsummaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--[-rwxr-xr-x]python/ez_setup.py536
-rwxr-xr-xpython/google/protobuf/__init__.py2
-rwxr-xr-xpython/google/protobuf/descriptor.py4
-rw-r--r--python/google/protobuf/descriptor_pool.py12
-rw-r--r--python/google/protobuf/internal/api_implementation_default_test.py6
-rwxr-xr-xpython/google/protobuf/internal/cpp_message.py24
-rwxr-xr-xpython/google/protobuf/internal/decoder.py51
-rw-r--r--python/google/protobuf/internal/descriptor_database_test.py19
-rw-r--r--python/google/protobuf/internal/descriptor_pool_test.py107
-rw-r--r--python/google/protobuf/internal/descriptor_python_test.py6
-rwxr-xr-xpython/google/protobuf/internal/descriptor_test.py13
-rwxr-xr-xpython/google/protobuf/internal/encoder.py52
-rwxr-xr-xpython/google/protobuf/internal/generator_test.py27
-rw-r--r--python/google/protobuf/internal/message_factory_python_test.py6
-rw-r--r--python/google/protobuf/internal/message_factory_test.py38
-rw-r--r--python/google/protobuf/internal/message_python_test.py6
-rwxr-xr-xpython/google/protobuf/internal/message_test.py26
-rw-r--r--python/google/protobuf/internal/proto_builder_test.py6
-rwxr-xr-xpython/google/protobuf/internal/python_message.py31
-rwxr-xr-xpython/google/protobuf/internal/reflection_test.py159
-rwxr-xr-xpython/google/protobuf/internal/service_reflection_test.py7
-rw-r--r--python/google/protobuf/internal/symbol_database_test.py29
-rwxr-xr-xpython/google/protobuf/internal/text_encoding_test.py15
-rwxr-xr-xpython/google/protobuf/internal/text_format_test.py31
-rwxr-xr-xpython/google/protobuf/internal/type_checkers.py27
-rwxr-xr-xpython/google/protobuf/internal/unknown_fields_test.py24
-rwxr-xr-xpython/google/protobuf/internal/wire_format_test.py7
-rw-r--r--python/google/protobuf/message_factory.py10
-rw-r--r--python/google/protobuf/pyext/descriptor_cpp2_test.py6
-rw-r--r--python/google/protobuf/pyext/message_factory_cpp2_test.py6
-rwxr-xr-xpython/google/protobuf/pyext/reflection_cpp2_generated_test.py6
-rw-r--r--python/google/protobuf/text_encoding.py20
-rwxr-xr-xpython/google/protobuf/text_format.py37
-rwxr-xr-xpython/setup.py14
-rw-r--r--python/tox.ini13
35 files changed, 704 insertions, 679 deletions
diff --git a/python/ez_setup.py b/python/ez_setup.py
index 3aec98e4..955a8008 100755..100644
--- a/python/ez_setup.py
+++ b/python/ez_setup.py
@@ -1,284 +1,332 @@
-#!python
-
-# This file was obtained from:
-# http://peak.telecommunity.com/dist/ez_setup.py
-# on 2011/1/21.
-
+#!/usr/bin/env python
"""Bootstrap setuptools installation
-If you want to use setuptools in your package's setup.py, just include this
-file in the same directory with it, and add this to the top of your setup.py::
+To use setuptools in your package's setup.py, include this
+file in the same directory and add this to the top of your setup.py::
from ez_setup import use_setuptools
use_setuptools()
-If you want to require a specific version of setuptools, set a download
-mirror, or use an alternate download directory, you can do so by supplying
+To require a specific version of setuptools, set a download
+mirror, or use an alternate download directory, simply supply
the appropriate options to ``use_setuptools()``.
This file can also be run as a script to install or upgrade setuptools.
"""
+import os
+import shutil
import sys
-DEFAULT_VERSION = "0.6c11"
-DEFAULT_URL = "http://pypi.python.org/packages/%s/s/setuptools/" % sys.version[:3]
-
-md5_data = {
- 'setuptools-0.6b1-py2.3.egg': '8822caf901250d848b996b7f25c6e6ca',
- 'setuptools-0.6b1-py2.4.egg': 'b79a8a403e4502fbb85ee3f1941735cb',
- 'setuptools-0.6b2-py2.3.egg': '5657759d8a6d8fc44070a9d07272d99b',
- 'setuptools-0.6b2-py2.4.egg': '4996a8d169d2be661fa32a6e52e4f82a',
- 'setuptools-0.6b3-py2.3.egg': 'bb31c0fc7399a63579975cad9f5a0618',
- 'setuptools-0.6b3-py2.4.egg': '38a8c6b3d6ecd22247f179f7da669fac',
- 'setuptools-0.6b4-py2.3.egg': '62045a24ed4e1ebc77fe039aa4e6f7e5',
- 'setuptools-0.6b4-py2.4.egg': '4cb2a185d228dacffb2d17f103b3b1c4',
- 'setuptools-0.6c1-py2.3.egg': 'b3f2b5539d65cb7f74ad79127f1a908c',
- 'setuptools-0.6c1-py2.4.egg': 'b45adeda0667d2d2ffe14009364f2a4b',
- 'setuptools-0.6c10-py2.3.egg': 'ce1e2ab5d3a0256456d9fc13800a7090',
- 'setuptools-0.6c10-py2.4.egg': '57d6d9d6e9b80772c59a53a8433a5dd4',
- 'setuptools-0.6c10-py2.5.egg': 'de46ac8b1c97c895572e5e8596aeb8c7',
- 'setuptools-0.6c10-py2.6.egg': '58ea40aef06da02ce641495523a0b7f5',
- 'setuptools-0.6c11-py2.3.egg': '2baeac6e13d414a9d28e7ba5b5a596de',
- 'setuptools-0.6c11-py2.4.egg': 'bd639f9b0eac4c42497034dec2ec0c2b',
- 'setuptools-0.6c11-py2.5.egg': '64c94f3bf7a72a13ec83e0b24f2749b2',
- 'setuptools-0.6c11-py2.6.egg': 'bfa92100bd772d5a213eedd356d64086',
- 'setuptools-0.6c2-py2.3.egg': 'f0064bf6aa2b7d0f3ba0b43f20817c27',
- 'setuptools-0.6c2-py2.4.egg': '616192eec35f47e8ea16cd6a122b7277',
- 'setuptools-0.6c3-py2.3.egg': 'f181fa125dfe85a259c9cd6f1d7b78fa',
- 'setuptools-0.6c3-py2.4.egg': 'e0ed74682c998bfb73bf803a50e7b71e',
- 'setuptools-0.6c3-py2.5.egg': 'abef16fdd61955514841c7c6bd98965e',
- 'setuptools-0.6c4-py2.3.egg': 'b0b9131acab32022bfac7f44c5d7971f',
- 'setuptools-0.6c4-py2.4.egg': '2a1f9656d4fbf3c97bf946c0a124e6e2',
- 'setuptools-0.6c4-py2.5.egg': '8f5a052e32cdb9c72bcf4b5526f28afc',
- 'setuptools-0.6c5-py2.3.egg': 'ee9fd80965da04f2f3e6b3576e9d8167',
- 'setuptools-0.6c5-py2.4.egg': 'afe2adf1c01701ee841761f5bcd8aa64',
- 'setuptools-0.6c5-py2.5.egg': 'a8d3f61494ccaa8714dfed37bccd3d5d',
- 'setuptools-0.6c6-py2.3.egg': '35686b78116a668847237b69d549ec20',
- 'setuptools-0.6c6-py2.4.egg': '3c56af57be3225019260a644430065ab',
- 'setuptools-0.6c6-py2.5.egg': 'b2f8a7520709a5b34f80946de5f02f53',
- 'setuptools-0.6c7-py2.3.egg': '209fdf9adc3a615e5115b725658e13e2',
- 'setuptools-0.6c7-py2.4.egg': '5a8f954807d46a0fb67cf1f26c55a82e',
- 'setuptools-0.6c7-py2.5.egg': '45d2ad28f9750e7434111fde831e8372',
- 'setuptools-0.6c8-py2.3.egg': '50759d29b349db8cfd807ba8303f1902',
- 'setuptools-0.6c8-py2.4.egg': 'cba38d74f7d483c06e9daa6070cce6de',
- 'setuptools-0.6c8-py2.5.egg': '1721747ee329dc150590a58b3e1ac95b',
- 'setuptools-0.6c9-py2.3.egg': 'a83c4020414807b496e4cfbe08507c03',
- 'setuptools-0.6c9-py2.4.egg': '260a2be2e5388d66bdaee06abec6342a',
- 'setuptools-0.6c9-py2.5.egg': 'fe67c3e5a17b12c0e7c541b7ea43a8e6',
- 'setuptools-0.6c9-py2.6.egg': 'ca37b1ff16fa2ede6e19383e7b59245a',
-}
-
-import sys, os
-try: from hashlib import md5
-except ImportError: from md5 import md5
-
-def _validate_md5(egg_name, data):
- if egg_name in md5_data:
- digest = md5(data).hexdigest()
- if digest != md5_data[egg_name]:
- print >>sys.stderr, (
- "md5 validation of %s failed! (Possible download problem?)"
- % egg_name
- )
- sys.exit(2)
- return data
-
-def use_setuptools(
- version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
- download_delay=15
-):
- """Automatically find/download setuptools and make it available on sys.path
-
- `version` should be a valid setuptools version number that is available
- as an egg for download under the `download_base` URL (which should end with
- a '/'). `to_dir` is the directory where setuptools will be downloaded, if
- it is not already available. If `download_delay` is specified, it should
- be the number of seconds that will be paused before initiating a download,
- should one be required. If an older version of setuptools is installed,
- this routine will print a message to ``sys.stderr`` and raise SystemExit in
- an attempt to abort the calling script.
+import tempfile
+import zipfile
+import optparse
+import subprocess
+import platform
+import textwrap
+import contextlib
+
+from distutils import log
+
+try:
+ from urllib.request import urlopen
+except ImportError:
+ from urllib2 import urlopen
+
+try:
+ from site import USER_SITE
+except ImportError:
+ USER_SITE = None
+
+DEFAULT_VERSION = "11.3.1"
+DEFAULT_URL = "https://pypi.python.org/packages/source/s/setuptools/"
+
+def _python_cmd(*args):
"""
- was_imported = 'pkg_resources' in sys.modules or 'setuptools' in sys.modules
- def do_download():
- egg = download_setuptools(version, download_base, to_dir, download_delay)
- sys.path.insert(0, egg)
- import setuptools; setuptools.bootstrap_install_from = egg
- try:
- import pkg_resources
- except ImportError:
- return do_download()
- try:
- return do_download()
- pkg_resources.require("setuptools>="+version); return
- except pkg_resources.VersionConflict, e:
- if was_imported:
- print >>sys.stderr, (
- "The required version of setuptools (>=%s) is not available, and\n"
- "can't be installed while this script is running. Please install\n"
- " a more recent version first, using 'easy_install -U setuptools'."
- "\n\n(Currently using %r)"
- ) % (version, e.args[0])
- sys.exit(2)
- except pkg_resources.DistributionNotFound:
- pass
-
- del pkg_resources, sys.modules['pkg_resources'] # reload ok
- return do_download()
-
-def download_setuptools(
- version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
- delay = 15
-):
- """Download setuptools from a specified location and return its filename
-
- `version` should be a valid setuptools version number that is available
- as an egg for download under the `download_base` URL (which should end
- with a '/'). `to_dir` is the directory where the egg will be downloaded.
- `delay` is the number of seconds to pause before an actual download attempt.
+ Return True if the command succeeded.
"""
- import urllib2, shutil
- egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3])
- url = download_base + egg_name
- saveto = os.path.join(to_dir, egg_name)
- src = dst = None
- if not os.path.exists(saveto): # Avoid repeated downloads
- try:
- from distutils import log
- if delay:
- log.warn("""
----------------------------------------------------------------------------
-This script requires setuptools version %s to run (even to display
-help). I will attempt to download it for you (from
-%s), but
-you may need to enable firewall access for this script first.
-I will start the download in %d seconds.
-
-(Note: if this machine does not have network access, please obtain the file
-
- %s
-
-and place it in this directory before rerunning this script.)
----------------------------------------------------------------------------""",
- version, download_base, delay, url
- ); from time import sleep; sleep(delay)
- log.warn("Downloading %s", url)
- src = urllib2.urlopen(url)
- # Read/write all in one block, so we don't create a corrupt file
- # if the download is interrupted.
- data = _validate_md5(egg_name, src.read())
- dst = open(saveto,"wb"); dst.write(data)
- finally:
- if src: src.close()
- if dst: dst.close()
- return os.path.realpath(saveto)
-
-
-
-
-
-
-
-
-
-
+ args = (sys.executable,) + args
+ return subprocess.call(args) == 0
+def _install(archive_filename, install_args=()):
+ with archive_context(archive_filename):
+ # installing
+ log.warn('Installing Setuptools')
+ if not _python_cmd('setup.py', 'install', *install_args):
+ log.warn('Something went wrong during the installation.')
+ log.warn('See the error message above.')
+ # exitcode will be 2
+ return 2
+def _build_egg(egg, archive_filename, to_dir):
+ with archive_context(archive_filename):
+ # building an egg
+ log.warn('Building a Setuptools egg in %s', to_dir)
+ _python_cmd('setup.py', '-q', 'bdist_egg', '--dist-dir', to_dir)
+ # returning the result
+ log.warn(egg)
+ if not os.path.exists(egg):
+ raise IOError('Could not build the egg.')
+class ContextualZipFile(zipfile.ZipFile):
+ """
+ Supplement ZipFile class to support context manager for Python 2.6
+ """
+ def __enter__(self):
+ return self
+ def __exit__(self, type, value, traceback):
+ self.close()
+ def __new__(cls, *args, **kwargs):
+ """
+ Construct a ZipFile or ContextualZipFile as appropriate
+ """
+ if hasattr(zipfile.ZipFile, '__exit__'):
+ return zipfile.ZipFile(*args, **kwargs)
+ return super(ContextualZipFile, cls).__new__(cls)
+@contextlib.contextmanager
+def archive_context(filename):
+ # extracting the archive
+ tmpdir = tempfile.mkdtemp()
+ log.warn('Extracting in %s', tmpdir)
+ old_wd = os.getcwd()
+ try:
+ os.chdir(tmpdir)
+ with ContextualZipFile(filename) as archive:
+ archive.extractall()
+
+ # going in the directory
+ subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
+ os.chdir(subdir)
+ log.warn('Now working in %s', subdir)
+ yield
+
+ finally:
+ os.chdir(old_wd)
+ shutil.rmtree(tmpdir)
+
+
+def _do_download(version, download_base, to_dir, download_delay):
+ egg = os.path.join(to_dir, 'setuptools-%s-py%d.%d.egg'
+ % (version, sys.version_info[0], sys.version_info[1]))
+ if not os.path.exists(egg):
+ archive = download_setuptools(version, download_base,
+ to_dir, download_delay)
+ _build_egg(egg, archive, to_dir)
+ sys.path.insert(0, egg)
+
+ # Remove previously-imported pkg_resources if present (see
+ # https://bitbucket.org/pypa/setuptools/pull-request/7/ for details).
+ if 'pkg_resources' in sys.modules:
+ del sys.modules['pkg_resources']
+
+ import setuptools
+ setuptools.bootstrap_install_from = egg
+
+
+def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
+ to_dir=os.curdir, download_delay=15):
+ to_dir = os.path.abspath(to_dir)
+ rep_modules = 'pkg_resources', 'setuptools'
+ imported = set(sys.modules).intersection(rep_modules)
+ try:
+ import pkg_resources
+ except ImportError:
+ return _do_download(version, download_base, to_dir, download_delay)
+ try:
+ pkg_resources.require("setuptools>=" + version)
+ return
+ except pkg_resources.DistributionNotFound:
+ return _do_download(version, download_base, to_dir, download_delay)
+ except pkg_resources.VersionConflict as VC_err:
+ if imported:
+ msg = textwrap.dedent("""
+ The required version of setuptools (>={version}) is not available,
+ and can't be installed while this script is running. Please
+ install a more recent version first, using
+ 'easy_install -U setuptools'.
+
+ (Currently using {VC_err.args[0]!r})
+ """).format(VC_err=VC_err, version=version)
+ sys.stderr.write(msg)
+ sys.exit(2)
+ # otherwise, reload ok
+ del pkg_resources, sys.modules['pkg_resources']
+ return _do_download(version, download_base, to_dir, download_delay)
+def _clean_check(cmd, target):
+ """
+ Run the command to download target. If the command fails, clean up before
+ re-raising the error.
+ """
+ try:
+ subprocess.check_call(cmd)
+ except subprocess.CalledProcessError:
+ if os.access(target, os.F_OK):
+ os.unlink(target)
+ raise
+def download_file_powershell(url, target):
+ """
+ Download the file at url to target using Powershell (which will validate
+ trust). Raise an exception if the command cannot complete.
+ """
+ target = os.path.abspath(target)
+ ps_cmd = (
+ "[System.Net.WebRequest]::DefaultWebProxy.Credentials = "
+ "[System.Net.CredentialCache]::DefaultCredentials; "
+ "(new-object System.Net.WebClient).DownloadFile(%(url)r, %(target)r)"
+ % vars()
+ )
+ cmd = [
+ 'powershell',
+ '-Command',
+ ps_cmd,
+ ]
+ _clean_check(cmd, target)
+
+def has_powershell():
+ if platform.system() != 'Windows':
+ return False
+ cmd = ['powershell', '-Command', 'echo test']
+ with open(os.path.devnull, 'wb') as devnull:
+ try:
+ subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
+ except Exception:
+ return False
+ return True
+download_file_powershell.viable = has_powershell
+def download_file_curl(url, target):
+ cmd = ['curl', url, '--silent', '--output', target]
+ _clean_check(cmd, target)
+def has_curl():
+ cmd = ['curl', '--version']
+ with open(os.path.devnull, 'wb') as devnull:
+ try:
+ subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
+ except Exception:
+ return False
+ return True
+download_file_curl.viable = has_curl
+def download_file_wget(url, target):
+ cmd = ['wget', url, '--quiet', '--output-document', target]
+ _clean_check(cmd, target)
+def has_wget():
+ cmd = ['wget', '--version']
+ with open(os.path.devnull, 'wb') as devnull:
+ try:
+ subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
+ except Exception:
+ return False
+ return True
+download_file_wget.viable = has_wget
+def download_file_insecure(url, target):
+ """
+ Use Python to download the file, even though it cannot authenticate the
+ connection.
+ """
+ src = urlopen(url)
+ try:
+ # Read all the data in one block.
+ data = src.read()
+ finally:
+ src.close()
+
+ # Write all the data in one block to avoid creating a partial file.
+ with open(target, "wb") as dst:
+ dst.write(data)
+
+download_file_insecure.viable = lambda: True
+
+def get_best_downloader():
+ downloaders = (
+ download_file_powershell,
+ download_file_curl,
+ download_file_wget,
+ download_file_insecure,
+ )
+ viable_downloaders = (dl for dl in downloaders if dl.viable())
+ return next(viable_downloaders, None)
+
+def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
+ to_dir=os.curdir, delay=15, downloader_factory=get_best_downloader):
+ """
+ Download setuptools from a specified location and return its filename
+ `version` should be a valid setuptools version number that is available
+ as an sdist for download under the `download_base` URL (which should end
+ with a '/'). `to_dir` is the directory where the egg will be downloaded.
+ `delay` is the number of seconds to pause before an actual download
+ attempt.
+ ``downloader_factory`` should be a function taking no arguments and
+ returning a function for downloading a URL to a target.
+ """
+ # making sure we use the absolute path
+ to_dir = os.path.abspath(to_dir)
+ zip_name = "setuptools-%s.zip" % version
+ url = download_base + zip_name
+ saveto = os.path.join(to_dir, zip_name)
+ if not os.path.exists(saveto): # Avoid repeated downloads
+ log.warn("Downloading %s", url)
+ downloader = downloader_factory()
+ downloader(url, saveto)
+ return os.path.realpath(saveto)
+def _build_install_args(options):
+ """
+ Build the arguments to 'python setup.py install' on the setuptools package
+ """
+ return ['--user'] if options.user_install else []
-def main(argv, version=DEFAULT_VERSION):
+def _parse_args():
+ """
+ Parse the command line for options
+ """
+ parser = optparse.OptionParser()
+ parser.add_option(
+ '--user', dest='user_install', action='store_true', default=False,
+ help='install in user site package (requires Python 2.6 or later)')
+ parser.add_option(
+ '--download-base', dest='download_base', metavar="URL",
+ default=DEFAULT_URL,
+ help='alternative URL from where to download the setuptools package')
+ parser.add_option(
+ '--insecure', dest='downloader_factory', action='store_const',
+ const=lambda: download_file_insecure, default=get_best_downloader,
+ help='Use internal, non-validating downloader'
+ )
+ parser.add_option(
+ '--version', help="Specify which version to download",
+ default=DEFAULT_VERSION,
+ )
+ options, args = parser.parse_args()
+ # positional arguments are ignored
+ return options
+
+def main():
"""Install or upgrade setuptools and EasyInstall"""
- try:
- import setuptools
- except ImportError:
- egg = None
- try:
- egg = download_setuptools(version, delay=0)
- sys.path.insert(0,egg)
- from setuptools.command.easy_install import main
- return main(list(argv)+[egg]) # we're done here
- finally:
- if egg and os.path.exists(egg):
- os.unlink(egg)
- else:
- if setuptools.__version__ == '0.0.1':
- print >>sys.stderr, (
- "You have an obsolete version of setuptools installed. Please\n"
- "remove it from your system entirely before rerunning this script."
- )
- sys.exit(2)
-
- req = "setuptools>="+version
- import pkg_resources
- try:
- pkg_resources.require(req)
- except pkg_resources.VersionConflict:
- try:
- from setuptools.command.easy_install import main
- except ImportError:
- from easy_install import main
- main(list(argv)+[download_setuptools(delay=0)])
- sys.exit(0) # try to force an exit
- else:
- if argv:
- from setuptools.command.easy_install import main
- main(argv)
- else:
- print "Setuptools version",version,"or greater has been installed."
- print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)'
-
-def update_md5(filenames):
- """Update our built-in md5 registry"""
-
- import re
-
- for name in filenames:
- base = os.path.basename(name)
- f = open(name,'rb')
- md5_data[base] = md5(f.read()).hexdigest()
- f.close()
-
- data = [" %r: %r,\n" % it for it in md5_data.items()]
- data.sort()
- repl = "".join(data)
-
- import inspect
- srcfile = inspect.getsourcefile(sys.modules[__name__])
- f = open(srcfile, 'rb'); src = f.read(); f.close()
-
- match = re.search("\nmd5_data = {\n([^}]+)}", src)
- if not match:
- print >>sys.stderr, "Internal error!"
- sys.exit(2)
-
- src = src[:match.start(1)] + repl + src[match.end(1):]
- f = open(srcfile,'w')
- f.write(src)
- f.close()
-
-
-if __name__=='__main__':
- if len(sys.argv)>2 and sys.argv[1]=='--md5update':
- update_md5(sys.argv[2:])
- else:
- main(sys.argv[1:])
+ options = _parse_args()
+ archive = download_setuptools(
+ version=options.version,
+ download_base=options.download_base,
+ downloader_factory=options.downloader_factory,
+ )
+ return _install(archive, _build_install_args(options))
+
+if __name__ == '__main__':
+ sys.exit(main())
diff --git a/python/google/protobuf/__init__.py b/python/google/protobuf/__init__.py
index ec3b0934..03eb74ef 100755
--- a/python/google/protobuf/__init__.py
+++ b/python/google/protobuf/__init__.py
@@ -28,8 +28,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-# Needs to stay compatible with Python 2.5 due to GAE.
-#
# Copyright 2007 Google Inc. All Rights Reserved.
__version__ = '3.0.0-pre'
diff --git a/python/google/protobuf/descriptor.py b/python/google/protobuf/descriptor.py
index af571b7c..e7acdacd 100755
--- a/python/google/protobuf/descriptor.py
+++ b/python/google/protobuf/descriptor.py
@@ -28,8 +28,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-# Needs to stay compatible with Python 2.5 due to GAE.
-#
# Copyright 2007 Google Inc. All Rights Reserved.
"""Descriptors essentially contain exactly the information found in a .proto
@@ -846,5 +844,5 @@ def MakeDescriptor(desc_proto, package='', build_file_if_cpp=True):
desc_name = '.'.join(full_message_name)
return Descriptor(desc_proto.name, desc_name, None, None, fields,
- nested_types.values(), enum_types.values(), [],
+ list(nested_types.values()), list(enum_types.values()), [],
options=desc_proto.options)
diff --git a/python/google/protobuf/descriptor_pool.py b/python/google/protobuf/descriptor_pool.py
index bcac513a..9a751bac 100644
--- a/python/google/protobuf/descriptor_pool.py
+++ b/python/google/protobuf/descriptor_pool.py
@@ -57,8 +57,6 @@ directly instead of this class.
__author__ = 'matthewtoia@google.com (Matt Toia)'
-import sys
-
from google.protobuf import descriptor
from google.protobuf import descriptor_database
from google.protobuf import text_encoding
@@ -175,8 +173,7 @@ class DescriptorPool(object):
try:
file_proto = self._internal_db.FindFileByName(file_name)
- except KeyError:
- _, error, _ = sys.exc_info() #PY25 compatible for GAE.
+ except KeyError as error:
if self._descriptor_db:
file_proto = self._descriptor_db.FindFileByName(file_name)
else:
@@ -211,8 +208,7 @@ class DescriptorPool(object):
try:
file_proto = self._internal_db.FindFileContainingSymbol(symbol)
- except KeyError:
- _, error, _ = sys.exc_info() #PY25 compatible for GAE.
+ except KeyError as error:
if self._descriptor_db:
file_proto = self._descriptor_db.FindFileContainingSymbol(symbol)
else:
@@ -282,9 +278,9 @@ class DescriptorPool(object):
# file proto.
for dependency in built_deps:
scope.update(self._ExtractSymbols(
- dependency.message_types_by_name.values()))
+ list(dependency.message_types_by_name.values())))
scope.update((_PrefixWithDot(enum.full_name), enum)
- for enum in dependency.enum_types_by_name.values())
+ for enum in list(dependency.enum_types_by_name.values()))
for message_type in file_proto.message_type:
message_desc = self._ConvertMessageDescriptor(
diff --git a/python/google/protobuf/internal/api_implementation_default_test.py b/python/google/protobuf/internal/api_implementation_default_test.py
index 78d5cf23..cb29e443 100644
--- a/python/google/protobuf/internal/api_implementation_default_test.py
+++ b/python/google/protobuf/internal/api_implementation_default_test.py
@@ -34,16 +34,16 @@
import os
import sys
+import unittest
# Clear environment implementation settings before the google3 imports.
os.environ.pop('PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION', None)
os.environ.pop('PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION', None)
# pylint: disable=g-import-not-at-top
-from google.apputils import basetest
from google.protobuf.internal import api_implementation
-class ApiImplementationDefaultTest(basetest.TestCase):
+class ApiImplementationDefaultTest(unittest.TestCase):
if sys.version_info.major <= 2:
@@ -60,4 +60,4 @@ class ApiImplementationDefaultTest(basetest.TestCase):
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/internal/cpp_message.py b/python/google/protobuf/internal/cpp_message.py
index 0313cb0b..20457375 100755
--- a/python/google/protobuf/internal/cpp_message.py
+++ b/python/google/protobuf/internal/cpp_message.py
@@ -34,8 +34,12 @@ Descriptor objects at runtime backed by the protocol buffer C++ API.
__author__ = 'petar@google.com (Petar Petrov)'
-import copy_reg
+import collections
import operator
+
+import six
+import six.moves.copyreg
+
from google.protobuf.internal import _net_proto2___python
from google.protobuf.internal import enum_type_wrapper
from google.protobuf import message
@@ -146,7 +150,7 @@ class RepeatedScalarContainer(object):
def __eq__(self, other):
if self is other:
return True
- if not operator.isSequenceType(other):
+ if not isinstance(other, collections.Sequence):
raise TypeError(
'Can only compare repeated scalar fields against sequences.')
# We are presumably comparing against some other sequence type.
@@ -259,7 +263,7 @@ class RepeatedCompositeContainer(object):
index_key = lambda i: key(self[i])
# Sort the list of current indexes by the underlying object.
- indexes = range(len(self))
+ indexes = list(range(len(self)))
indexes.sort(cmp=cmp, key=index_key, reverse=reverse)
# Apply the transposition.
@@ -385,7 +389,7 @@ def InitMessage(message_descriptor, cls):
_AddInitMethod(message_descriptor, cls)
_AddMessageMethods(message_descriptor, cls)
_AddPropertiesForExtensions(message_descriptor, cls)
- copy_reg.pickle(cls, lambda obj: (cls, (), obj.__getstate__()))
+ six.moves.copyreg.pickle(cls, lambda obj: (cls, (), obj.__getstate__()))
def _AddDescriptors(message_descriptor, dictionary):
@@ -400,7 +404,7 @@ def _AddDescriptors(message_descriptor, dictionary):
dictionary['__descriptors'][field.name] = GetFieldDescriptor(
field.full_name)
- dictionary['__slots__'] = list(dictionary['__descriptors'].iterkeys()) + [
+ dictionary['__slots__'] = list(dictionary['__descriptors'].keys()) + [
'_cmsg', '_owner', '_composite_fields', 'Extensions', '_HACK_REFCOUNTS']
@@ -420,7 +424,7 @@ def _AddEnumValues(message_descriptor, dictionary):
def _AddClassAttributesForNestedExtensions(message_descriptor, dictionary):
"""Adds class attributes for the nested extensions."""
extension_dict = message_descriptor.extensions_by_name
- for extension_name, extension_field in extension_dict.iteritems():
+ for extension_name, extension_field in extension_dict.items():
assert extension_name not in dictionary
dictionary[extension_name] = extension_field
@@ -474,7 +478,7 @@ def _AddInitMethod(message_descriptor, cls):
self._HACK_REFCOUNTS = self
self._composite_fields = {}
- for field_name, field_value in kwargs.iteritems():
+ for field_name, field_value in kwargs.items():
field_cdescriptor = self.__descriptors.get(field_name, None)
if not field_cdescriptor:
raise ValueError('Protocol message has no "%s" field.' % field_name)
@@ -538,7 +542,7 @@ def _AddMessageMethods(message_descriptor, cls):
def Clear(self):
cmessages_to_release = []
- for field_name, child_field in self._composite_fields.iteritems():
+ for field_name, child_field in self._composite_fields.items():
child_cdescriptor = self.__descriptors[field_name]
# TODO(anuraag): Support clearing repeated message fields as well.
if (child_cdescriptor.label != _LABEL_REPEATED and
@@ -631,7 +635,7 @@ def _AddMessageMethods(message_descriptor, cls):
return text_format.MessageToString(self, as_utf8=True).decode('utf-8')
# Attach the local methods to the message class.
- for key, value in locals().copy().iteritems():
+ for key, value in locals().copy().items():
if key not in ('key', 'value', '__builtins__', '__name__', '__doc__'):
setattr(cls, key, value)
@@ -658,6 +662,6 @@ def _AddMessageMethods(message_descriptor, cls):
def _AddPropertiesForExtensions(message_descriptor, cls):
"""Adds properties for all fields in this protocol message type."""
extension_dict = message_descriptor.extensions_by_name
- for extension_name, extension_field in extension_dict.iteritems():
+ for extension_name, extension_field in extension_dict.items():
constant_name = extension_name.upper() + '_FIELD_NUMBER'
setattr(cls, constant_name, extension_field.number)
diff --git a/python/google/protobuf/internal/decoder.py b/python/google/protobuf/internal/decoder.py
index a4b90608..6b72adef 100755
--- a/python/google/protobuf/internal/decoder.py
+++ b/python/google/protobuf/internal/decoder.py
@@ -28,8 +28,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#PY25 compatible for GAE.
-#
# Copyright 2009 Google Inc. All Rights Reserved.
"""Code for decoding protocol buffer primitives.
@@ -85,8 +83,9 @@ we repeatedly read a tag, look up the corresponding decoder, and invoke it.
__author__ = 'kenton@google.com (Kenton Varda)'
import struct
-import sys ##PY25
-_PY2 = sys.version_info[0] < 3 ##PY25
+
+import six
+
from google.protobuf.internal import encoder
from google.protobuf.internal import wire_format
from google.protobuf import message
@@ -114,14 +113,11 @@ def _VarintDecoder(mask, result_type):
decoder returns a (value, new_pos) pair.
"""
- local_ord = ord
- py2 = _PY2 ##PY25
-##!PY25 py2 = str is bytes
def DecodeVarint(buffer, pos):
result = 0
shift = 0
while 1:
- b = local_ord(buffer[pos]) if py2 else buffer[pos]
+ b = six.indexbytes(buffer, pos)
result |= ((b & 0x7f) << shift)
pos += 1
if not (b & 0x80):
@@ -137,14 +133,11 @@ def _VarintDecoder(mask, result_type):
def _SignedVarintDecoder(mask, result_type):
"""Like _VarintDecoder() but decodes signed values."""
- local_ord = ord
- py2 = _PY2 ##PY25
-##!PY25 py2 = str is bytes
def DecodeVarint(buffer, pos):
result = 0
shift = 0
while 1:
- b = local_ord(buffer[pos]) if py2 else buffer[pos]
+ b = six.indexbytes(buffer, pos)
result |= ((b & 0x7f) << shift)
pos += 1
if not (b & 0x80):
@@ -164,8 +157,8 @@ def _SignedVarintDecoder(mask, result_type):
# alternate implementations where the distinction is more significant
# (e.g. the C++ implementation) simpler.
-_DecodeVarint = _VarintDecoder((1 << 64) - 1, long)
-_DecodeSignedVarint = _SignedVarintDecoder((1 << 64) - 1, long)
+_DecodeVarint = _VarintDecoder((1 << 64) - 1, int)
+_DecodeSignedVarint = _SignedVarintDecoder((1 << 64) - 1, int)
# Use these versions for values which must be limited to 32 bits.
_DecodeVarint32 = _VarintDecoder((1 << 32) - 1, int)
@@ -183,10 +176,8 @@ def ReadTag(buffer, pos):
use that, but not in Python.
"""
- py2 = _PY2 ##PY25
-##!PY25 py2 = str is bytes
start = pos
- while (ord(buffer[pos]) if py2 else buffer[pos]) & 0x80:
+ while six.indexbytes(buffer, pos) & 0x80:
pos += 1
pos += 1
return (buffer[start:pos], pos)
@@ -301,7 +292,6 @@ def _FloatDecoder():
"""
local_unpack = struct.unpack
- b = (lambda x:x) if _PY2 else lambda x:x.encode('latin1') ##PY25
def InnerDecode(buffer, pos):
# We expect a 32-bit value in little-endian byte order. Bit 1 is the sign
@@ -312,17 +302,12 @@ def _FloatDecoder():
# If this value has all its exponent bits set, then it's non-finite.
# In Python 2.4, struct.unpack will convert it to a finite 64-bit value.
# To avoid that, we parse it specially.
- if ((float_bytes[3:4] in b('\x7F\xFF')) ##PY25
-##!PY25 if ((float_bytes[3:4] in b'\x7F\xFF')
- and (float_bytes[2:3] >= b('\x80'))): ##PY25
-##!PY25 and (float_bytes[2:3] >= b'\x80')):
+ if (float_bytes[3:4] in b'\x7F\xFF' and float_bytes[2:3] >= b'\x80'):
# If at least one significand bit is set...
- if float_bytes[0:3] != b('\x00\x00\x80'): ##PY25
-##!PY25 if float_bytes[0:3] != b'\x00\x00\x80':
+ if float_bytes[0:3] != b'\x00\x00\x80':
return (_NAN, new_pos)
# If sign bit is set...
- if float_bytes[3:4] == b('\xFF'): ##PY25
-##!PY25 if float_bytes[3:4] == b'\xFF':
+ if float_bytes[3:4] == b'\xFF':
return (_NEG_INF, new_pos)
return (_POS_INF, new_pos)
@@ -341,7 +326,6 @@ def _DoubleDecoder():
"""
local_unpack = struct.unpack
- b = (lambda x:x) if _PY2 else lambda x:x.encode('latin1') ##PY25
def InnerDecode(buffer, pos):
# We expect a 64-bit value in little-endian byte order. Bit 1 is the sign
@@ -352,12 +336,9 @@ def _DoubleDecoder():
# If this value has all its exponent bits set and at least one significand
# bit set, it's not a number. In Python 2.4, struct.unpack will treat it
# as inf or -inf. To avoid that, we treat it specially.
-##!PY25 if ((double_bytes[7:8] in b'\x7F\xFF')
-##!PY25 and (double_bytes[6:7] >= b'\xF0')
-##!PY25 and (double_bytes[0:7] != b'\x00\x00\x00\x00\x00\x00\xF0')):
- if ((double_bytes[7:8] in b('\x7F\xFF')) ##PY25
- and (double_bytes[6:7] >= b('\xF0')) ##PY25
- and (double_bytes[0:7] != b('\x00\x00\x00\x00\x00\x00\xF0'))): ##PY25
+ if ((double_bytes[7:8] in b'\x7F\xFF')
+ and (double_bytes[6:7] >= b'\xF0')
+ and (double_bytes[0:7] != b'\x00\x00\x00\x00\x00\x00\xF0')):
return (_NAN, new_pos)
# Note that we expect someone up-stack to catch struct.error and convert
@@ -480,12 +461,12 @@ def StringDecoder(field_number, is_repeated, is_packed, key, new_default):
"""Returns a decoder for a string field."""
local_DecodeVarint = _DecodeVarint
- local_unicode = unicode
+ local_unicode = six.text_type
def _ConvertToUnicode(byte_str):
try:
return local_unicode(byte_str, 'utf-8')
- except UnicodeDecodeError, e:
+ except UnicodeDecodeError as e:
# add more information to the error message and re-raise it.
e.reason = '%s in field: %s' % (e, key.full_name)
raise
diff --git a/python/google/protobuf/internal/descriptor_database_test.py b/python/google/protobuf/internal/descriptor_database_test.py
index 8970f5c2..ad13f889 100644
--- a/python/google/protobuf/internal/descriptor_database_test.py
+++ b/python/google/protobuf/internal/descriptor_database_test.py
@@ -34,13 +34,14 @@
__author__ = 'matthewtoia@google.com (Matt Toia)'
-from google.apputils import basetest
+import unittest
+
from google.protobuf import descriptor_pb2
from google.protobuf.internal import factory_test2_pb2
from google.protobuf import descriptor_database
-class DescriptorDatabaseTest(basetest.TestCase):
+class DescriptorDatabaseTest(unittest.TestCase):
def testAdd(self):
db = descriptor_database.DescriptorDatabase()
@@ -48,18 +49,18 @@ class DescriptorDatabaseTest(basetest.TestCase):
factory_test2_pb2.DESCRIPTOR.serialized_pb)
db.Add(file_desc_proto)
- self.assertEquals(file_desc_proto, db.FindFileByName(
+ self.assertEqual(file_desc_proto, db.FindFileByName(
'google/protobuf/internal/factory_test2.proto'))
- self.assertEquals(file_desc_proto, db.FindFileContainingSymbol(
+ self.assertEqual(file_desc_proto, db.FindFileContainingSymbol(
'google.protobuf.python.internal.Factory2Message'))
- self.assertEquals(file_desc_proto, db.FindFileContainingSymbol(
+ self.assertEqual(file_desc_proto, db.FindFileContainingSymbol(
'google.protobuf.python.internal.Factory2Message.NestedFactory2Message'))
- self.assertEquals(file_desc_proto, db.FindFileContainingSymbol(
+ self.assertEqual(file_desc_proto, db.FindFileContainingSymbol(
'google.protobuf.python.internal.Factory2Enum'))
- self.assertEquals(file_desc_proto, db.FindFileContainingSymbol(
+ self.assertEqual(file_desc_proto, db.FindFileContainingSymbol(
'google.protobuf.python.internal.Factory2Message.NestedFactory2Enum'))
- self.assertEquals(file_desc_proto, db.FindFileContainingSymbol(
+ self.assertEqual(file_desc_proto, db.FindFileContainingSymbol(
'google.protobuf.python.internal.MessageWithNestedEnumOnly.NestedEnum'))
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/internal/descriptor_pool_test.py b/python/google/protobuf/internal/descriptor_pool_test.py
index 11ef61c5..fa1a511a 100644
--- a/python/google/protobuf/internal/descriptor_pool_test.py
+++ b/python/google/protobuf/internal/descriptor_pool_test.py
@@ -37,7 +37,6 @@ __author__ = 'matthewtoia@google.com (Matt Toia)'
import os
import unittest
-from google.apputils import basetest
from google.protobuf import unittest_pb2
from google.protobuf import descriptor_pb2
from google.protobuf.internal import api_implementation
@@ -51,7 +50,7 @@ from google.protobuf import descriptor_pool
from google.protobuf import symbol_database
-class DescriptorPoolTest(basetest.TestCase):
+class DescriptorPoolTest(unittest.TestCase):
def setUp(self):
self.pool = descriptor_pool.DescriptorPool()
@@ -66,15 +65,15 @@ class DescriptorPoolTest(basetest.TestCase):
name1 = 'google/protobuf/internal/factory_test1.proto'
file_desc1 = self.pool.FindFileByName(name1)
self.assertIsInstance(file_desc1, descriptor.FileDescriptor)
- self.assertEquals(name1, file_desc1.name)
- self.assertEquals('google.protobuf.python.internal', file_desc1.package)
+ self.assertEqual(name1, file_desc1.name)
+ self.assertEqual('google.protobuf.python.internal', file_desc1.package)
self.assertIn('Factory1Message', file_desc1.message_types_by_name)
name2 = 'google/protobuf/internal/factory_test2.proto'
file_desc2 = self.pool.FindFileByName(name2)
self.assertIsInstance(file_desc2, descriptor.FileDescriptor)
- self.assertEquals(name2, file_desc2.name)
- self.assertEquals('google.protobuf.python.internal', file_desc2.package)
+ self.assertEqual(name2, file_desc2.name)
+ self.assertEqual('google.protobuf.python.internal', file_desc2.package)
self.assertIn('Factory2Message', file_desc2.message_types_by_name)
def testFindFileByNameFailure(self):
@@ -85,17 +84,17 @@ class DescriptorPoolTest(basetest.TestCase):
file_desc1 = self.pool.FindFileContainingSymbol(
'google.protobuf.python.internal.Factory1Message')
self.assertIsInstance(file_desc1, descriptor.FileDescriptor)
- self.assertEquals('google/protobuf/internal/factory_test1.proto',
+ self.assertEqual('google/protobuf/internal/factory_test1.proto',
file_desc1.name)
- self.assertEquals('google.protobuf.python.internal', file_desc1.package)
+ self.assertEqual('google.protobuf.python.internal', file_desc1.package)
self.assertIn('Factory1Message', file_desc1.message_types_by_name)
file_desc2 = self.pool.FindFileContainingSymbol(
'google.protobuf.python.internal.Factory2Message')
self.assertIsInstance(file_desc2, descriptor.FileDescriptor)
- self.assertEquals('google/protobuf/internal/factory_test2.proto',
+ self.assertEqual('google/protobuf/internal/factory_test2.proto',
file_desc2.name)
- self.assertEquals('google.protobuf.python.internal', file_desc2.package)
+ self.assertEqual('google.protobuf.python.internal', file_desc2.package)
self.assertIn('Factory2Message', file_desc2.message_types_by_name)
def testFindFileContainingSymbolFailure(self):
@@ -106,72 +105,72 @@ class DescriptorPoolTest(basetest.TestCase):
msg1 = self.pool.FindMessageTypeByName(
'google.protobuf.python.internal.Factory1Message')
self.assertIsInstance(msg1, descriptor.Descriptor)
- self.assertEquals('Factory1Message', msg1.name)
- self.assertEquals('google.protobuf.python.internal.Factory1Message',
+ self.assertEqual('Factory1Message', msg1.name)
+ self.assertEqual('google.protobuf.python.internal.Factory1Message',
msg1.full_name)
- self.assertEquals(None, msg1.containing_type)
+ self.assertEqual(None, msg1.containing_type)
nested_msg1 = msg1.nested_types[0]
- self.assertEquals('NestedFactory1Message', nested_msg1.name)
- self.assertEquals(msg1, nested_msg1.containing_type)
+ self.assertEqual('NestedFactory1Message', nested_msg1.name)
+ self.assertEqual(msg1, nested_msg1.containing_type)
nested_enum1 = msg1.enum_types[0]
- self.assertEquals('NestedFactory1Enum', nested_enum1.name)
- self.assertEquals(msg1, nested_enum1.containing_type)
+ self.assertEqual('NestedFactory1Enum', nested_enum1.name)
+ self.assertEqual(msg1, nested_enum1.containing_type)
- self.assertEquals(nested_msg1, msg1.fields_by_name[
+ self.assertEqual(nested_msg1, msg1.fields_by_name[
'nested_factory_1_message'].message_type)
- self.assertEquals(nested_enum1, msg1.fields_by_name[
+ self.assertEqual(nested_enum1, msg1.fields_by_name[
'nested_factory_1_enum'].enum_type)
msg2 = self.pool.FindMessageTypeByName(
'google.protobuf.python.internal.Factory2Message')
self.assertIsInstance(msg2, descriptor.Descriptor)
- self.assertEquals('Factory2Message', msg2.name)
- self.assertEquals('google.protobuf.python.internal.Factory2Message',
+ self.assertEqual('Factory2Message', msg2.name)
+ self.assertEqual('google.protobuf.python.internal.Factory2Message',
msg2.full_name)
self.assertIsNone(msg2.containing_type)
nested_msg2 = msg2.nested_types[0]
- self.assertEquals('NestedFactory2Message', nested_msg2.name)
- self.assertEquals(msg2, nested_msg2.containing_type)
+ self.assertEqual('NestedFactory2Message', nested_msg2.name)
+ self.assertEqual(msg2, nested_msg2.containing_type)
nested_enum2 = msg2.enum_types[0]
- self.assertEquals('NestedFactory2Enum', nested_enum2.name)
- self.assertEquals(msg2, nested_enum2.containing_type)
+ self.assertEqual('NestedFactory2Enum', nested_enum2.name)
+ self.assertEqual(msg2, nested_enum2.containing_type)
- self.assertEquals(nested_msg2, msg2.fields_by_name[
+ self.assertEqual(nested_msg2, msg2.fields_by_name[
'nested_factory_2_message'].message_type)
- self.assertEquals(nested_enum2, msg2.fields_by_name[
+ self.assertEqual(nested_enum2, msg2.fields_by_name[
'nested_factory_2_enum'].enum_type)
self.assertTrue(msg2.fields_by_name['int_with_default'].has_default_value)
- self.assertEquals(
+ self.assertEqual(
1776, msg2.fields_by_name['int_with_default'].default_value)
self.assertTrue(
msg2.fields_by_name['double_with_default'].has_default_value)
- self.assertEquals(
+ self.assertEqual(
9.99, msg2.fields_by_name['double_with_default'].default_value)
self.assertTrue(
msg2.fields_by_name['string_with_default'].has_default_value)
- self.assertEquals(
+ self.assertEqual(
'hello world', msg2.fields_by_name['string_with_default'].default_value)
self.assertTrue(msg2.fields_by_name['bool_with_default'].has_default_value)
self.assertFalse(msg2.fields_by_name['bool_with_default'].default_value)
self.assertTrue(msg2.fields_by_name['enum_with_default'].has_default_value)
- self.assertEquals(
+ self.assertEqual(
1, msg2.fields_by_name['enum_with_default'].default_value)
msg3 = self.pool.FindMessageTypeByName(
'google.protobuf.python.internal.Factory2Message.NestedFactory2Message')
- self.assertEquals(nested_msg2, msg3)
+ self.assertEqual(nested_msg2, msg3)
self.assertTrue(msg2.fields_by_name['bytes_with_default'].has_default_value)
- self.assertEquals(
+ self.assertEqual(
b'a\xfb\x00c',
msg2.fields_by_name['bytes_with_default'].default_value)
@@ -191,29 +190,29 @@ class DescriptorPoolTest(basetest.TestCase):
enum1 = self.pool.FindEnumTypeByName(
'google.protobuf.python.internal.Factory1Enum')
self.assertIsInstance(enum1, descriptor.EnumDescriptor)
- self.assertEquals(0, enum1.values_by_name['FACTORY_1_VALUE_0'].number)
- self.assertEquals(1, enum1.values_by_name['FACTORY_1_VALUE_1'].number)
+ self.assertEqual(0, enum1.values_by_name['FACTORY_1_VALUE_0'].number)
+ self.assertEqual(1, enum1.values_by_name['FACTORY_1_VALUE_1'].number)
nested_enum1 = self.pool.FindEnumTypeByName(
'google.protobuf.python.internal.Factory1Message.NestedFactory1Enum')
self.assertIsInstance(nested_enum1, descriptor.EnumDescriptor)
- self.assertEquals(
+ self.assertEqual(
0, nested_enum1.values_by_name['NESTED_FACTORY_1_VALUE_0'].number)
- self.assertEquals(
+ self.assertEqual(
1, nested_enum1.values_by_name['NESTED_FACTORY_1_VALUE_1'].number)
enum2 = self.pool.FindEnumTypeByName(
'google.protobuf.python.internal.Factory2Enum')
self.assertIsInstance(enum2, descriptor.EnumDescriptor)
- self.assertEquals(0, enum2.values_by_name['FACTORY_2_VALUE_0'].number)
- self.assertEquals(1, enum2.values_by_name['FACTORY_2_VALUE_1'].number)
+ self.assertEqual(0, enum2.values_by_name['FACTORY_2_VALUE_0'].number)
+ self.assertEqual(1, enum2.values_by_name['FACTORY_2_VALUE_1'].number)
nested_enum2 = self.pool.FindEnumTypeByName(
'google.protobuf.python.internal.Factory2Message.NestedFactory2Enum')
self.assertIsInstance(nested_enum2, descriptor.EnumDescriptor)
- self.assertEquals(
+ self.assertEqual(
0, nested_enum2.values_by_name['NESTED_FACTORY_2_VALUE_0'].number)
- self.assertEquals(
+ self.assertEqual(
1, nested_enum2.values_by_name['NESTED_FACTORY_2_VALUE_1'].number)
def testFindEnumTypeByNameFailure(self):
@@ -275,8 +274,8 @@ class ProtoFile(object):
def CheckFile(self, test, pool):
file_desc = pool.FindFileByName(self.name)
- test.assertEquals(self.name, file_desc.name)
- test.assertEquals(self.package, file_desc.package)
+ test.assertEqual(self.name, file_desc.name)
+ test.assertEqual(self.package, file_desc.package)
dependencies_names = [f.name for f in file_desc.dependencies]
test.assertEqual(self.dependencies, dependencies_names)
for name, msg_type in self.messages.items():
@@ -426,12 +425,12 @@ class ExtensionField(object):
test.assertEqual(self.extended_type, field_desc.containing_type.name)
-class AddDescriptorTest(basetest.TestCase):
+class AddDescriptorTest(unittest.TestCase):
def _TestMessage(self, prefix):
pool = descriptor_pool.DescriptorPool()
pool.AddDescriptor(unittest_pb2.TestAllTypes.DESCRIPTOR)
- self.assertEquals(
+ self.assertEqual(
'protobuf_unittest.TestAllTypes',
pool.FindMessageTypeByName(
prefix + 'protobuf_unittest.TestAllTypes').full_name)
@@ -442,18 +441,18 @@ class AddDescriptorTest(basetest.TestCase):
prefix + 'protobuf_unittest.TestAllTypes.NestedMessage')
pool.AddDescriptor(unittest_pb2.TestAllTypes.NestedMessage.DESCRIPTOR)
- self.assertEquals(
+ self.assertEqual(
'protobuf_unittest.TestAllTypes.NestedMessage',
pool.FindMessageTypeByName(
prefix + 'protobuf_unittest.TestAllTypes.NestedMessage').full_name)
# Files are implicitly also indexed when messages are added.
- self.assertEquals(
+ self.assertEqual(
'google/protobuf/unittest.proto',
pool.FindFileByName(
'google/protobuf/unittest.proto').name)
- self.assertEquals(
+ self.assertEqual(
'google/protobuf/unittest.proto',
pool.FindFileContainingSymbol(
prefix + 'protobuf_unittest.TestAllTypes.NestedMessage').name)
@@ -465,7 +464,7 @@ class AddDescriptorTest(basetest.TestCase):
def _TestEnum(self, prefix):
pool = descriptor_pool.DescriptorPool()
pool.AddEnumDescriptor(unittest_pb2.ForeignEnum.DESCRIPTOR)
- self.assertEquals(
+ self.assertEqual(
'protobuf_unittest.ForeignEnum',
pool.FindEnumTypeByName(
prefix + 'protobuf_unittest.ForeignEnum').full_name)
@@ -476,18 +475,18 @@ class AddDescriptorTest(basetest.TestCase):
prefix + 'protobuf_unittest.ForeignEnum.NestedEnum')
pool.AddEnumDescriptor(unittest_pb2.TestAllTypes.NestedEnum.DESCRIPTOR)
- self.assertEquals(
+ self.assertEqual(
'protobuf_unittest.TestAllTypes.NestedEnum',
pool.FindEnumTypeByName(
prefix + 'protobuf_unittest.TestAllTypes.NestedEnum').full_name)
# Files are implicitly also indexed when enums are added.
- self.assertEquals(
+ self.assertEqual(
'google/protobuf/unittest.proto',
pool.FindFileByName(
'google/protobuf/unittest.proto').name)
- self.assertEquals(
+ self.assertEqual(
'google/protobuf/unittest.proto',
pool.FindFileContainingSymbol(
prefix + 'protobuf_unittest.TestAllTypes.NestedEnum').name)
@@ -499,7 +498,7 @@ class AddDescriptorTest(basetest.TestCase):
def testFile(self):
pool = descriptor_pool.DescriptorPool()
pool.AddFileDescriptor(unittest_pb2.DESCRIPTOR)
- self.assertEquals(
+ self.assertEqual(
'google/protobuf/unittest.proto',
pool.FindFileByName(
'google/protobuf/unittest.proto').name)
@@ -588,4 +587,4 @@ TEST2_FILE = ProtoFile(
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/internal/descriptor_python_test.py b/python/google/protobuf/internal/descriptor_python_test.py
index 5471ae02..573c1b9d 100644
--- a/python/google/protobuf/internal/descriptor_python_test.py
+++ b/python/google/protobuf/internal/descriptor_python_test.py
@@ -33,22 +33,22 @@
"""Unittest for descriptor.py for the pure Python implementation."""
import os
+import unittest
os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python'
# We must set the implementation version above before the google3 imports.
# pylint: disable=g-import-not-at-top
-from google.apputils import basetest
from google.protobuf.internal import api_implementation
# Run all tests from the original module by putting them in our namespace.
# pylint: disable=wildcard-import
from google.protobuf.internal.descriptor_test import *
-class ConfirmPurePythonTest(basetest.TestCase):
+class ConfirmPurePythonTest(unittest.TestCase):
def testImplementationSetting(self):
self.assertEqual('python', api_implementation.Type())
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/internal/descriptor_test.py b/python/google/protobuf/internal/descriptor_test.py
index 3924f21a..e1506fa4 100755
--- a/python/google/protobuf/internal/descriptor_test.py
+++ b/python/google/protobuf/internal/descriptor_test.py
@@ -34,7 +34,8 @@
__author__ = 'robinson@google.com (Will Robinson)'
-from google.apputils import basetest
+import unittest
+
from google.protobuf import unittest_custom_options_pb2
from google.protobuf import unittest_import_pb2
from google.protobuf import unittest_pb2
@@ -48,7 +49,7 @@ name: 'TestEmptyMessage'
"""
-class DescriptorTest(basetest.TestCase):
+class DescriptorTest(unittest.TestCase):
def setUp(self):
self.my_file = descriptor.FileDescriptor(
@@ -395,7 +396,7 @@ class DescriptorTest(basetest.TestCase):
self.assertEqual(self.my_file.package, 'protobuf_unittest')
-class DescriptorCopyToProtoTest(basetest.TestCase):
+class DescriptorCopyToProtoTest(unittest.TestCase):
"""Tests for CopyTo functions of Descriptor."""
def _AssertProtoEqual(self, actual_proto, expected_class, expected_ascii):
@@ -594,7 +595,7 @@ class DescriptorCopyToProtoTest(basetest.TestCase):
TEST_SERVICE_ASCII)
-class MakeDescriptorTest(basetest.TestCase):
+class MakeDescriptorTest(unittest.TestCase):
def testMakeDescriptorWithNestedFields(self):
file_descriptor_proto = descriptor_pb2.FileDescriptorProto()
@@ -672,8 +673,8 @@ class MakeDescriptorTest(basetest.TestCase):
reformed_descriptor = descriptor.MakeDescriptor(descriptor_proto)
options = reformed_descriptor.GetOptions()
- self.assertEquals(101,
+ self.assertEqual(101,
options.Extensions[unittest_custom_options_pb2.msgopt].i)
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/internal/encoder.py b/python/google/protobuf/internal/encoder.py
index 38a5138a..fa22a9dd 100755
--- a/python/google/protobuf/internal/encoder.py
+++ b/python/google/protobuf/internal/encoder.py
@@ -28,8 +28,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#PY25 compatible for GAE.
-#
# Copyright 2009 Google Inc. All Rights Reserved.
"""Code for encoding protocol message primitives.
@@ -45,7 +43,7 @@ FieldDescriptor) we construct two functions: a "sizer" and an "encoder". The
sizer takes a value of this field's type and computes its byte size. The
encoder takes a writer function and a value. It encodes the value into byte
strings and invokes the writer function to write those strings. Typically the
-writer function is the write() method of a cStringIO.
+writer function is the write() method of a BytesIO.
We try to do as much work as possible when constructing the writer and the
sizer rather than when calling them. In particular:
@@ -71,8 +69,9 @@ sizer rather than when calling them. In particular:
__author__ = 'kenton@google.com (Kenton Varda)'
import struct
-import sys ##PY25
-_PY2 = sys.version_info[0] < 3 ##PY25
+
+import six
+
from google.protobuf.internal import wire_format
@@ -346,16 +345,14 @@ def MessageSetItemSizer(field_number):
def _VarintEncoder():
"""Return an encoder for a basic varint value (does not include tag)."""
- local_chr = _PY2 and chr or (lambda x: bytes((x,))) ##PY25
-##!PY25 local_chr = chr if bytes is str else lambda x: bytes((x,))
def EncodeVarint(write, value):
bits = value & 0x7f
value >>= 7
while value:
- write(local_chr(0x80|bits))
+ write(six.int2byte(0x80|bits))
bits = value & 0x7f
value >>= 7
- return write(local_chr(bits))
+ return write(six.int2byte(bits))
return EncodeVarint
@@ -364,18 +361,16 @@ def _SignedVarintEncoder():
"""Return an encoder for a basic signed varint value (does not include
tag)."""
- local_chr = _PY2 and chr or (lambda x: bytes((x,))) ##PY25
-##!PY25 local_chr = chr if bytes is str else lambda x: bytes((x,))
def EncodeSignedVarint(write, value):
if value < 0:
value += (1 << 64)
bits = value & 0x7f
value >>= 7
while value:
- write(local_chr(0x80|bits))
+ write(six.int2byte(0x80|bits))
bits = value & 0x7f
value >>= 7
- return write(local_chr(bits))
+ return write(six.int2byte(bits))
return EncodeSignedVarint
@@ -390,8 +385,7 @@ def _VarintBytes(value):
pieces = []
_EncodeVarint(pieces.append, value)
- return "".encode("latin1").join(pieces) ##PY25
-##!PY25 return b"".join(pieces)
+ return b"".join(pieces)
def TagBytes(field_number, wire_type):
@@ -529,33 +523,26 @@ def _FloatingPointEncoder(wire_type, format):
format: The format string to pass to struct.pack().
"""
- b = _PY2 and (lambda x:x) or (lambda x:x.encode('latin1')) ##PY25
value_size = struct.calcsize(format)
if value_size == 4:
def EncodeNonFiniteOrRaise(write, value):
# Remember that the serialized form uses little-endian byte order.
if value == _POS_INF:
- write(b('\x00\x00\x80\x7F')) ##PY25
-##!PY25 write(b'\x00\x00\x80\x7F')
+ write(b'\x00\x00\x80\x7F')
elif value == _NEG_INF:
- write(b('\x00\x00\x80\xFF')) ##PY25
-##!PY25 write(b'\x00\x00\x80\xFF')
+ write(b'\x00\x00\x80\xFF')
elif value != value: # NaN
- write(b('\x00\x00\xC0\x7F')) ##PY25
-##!PY25 write(b'\x00\x00\xC0\x7F')
+ write(b'\x00\x00\xC0\x7F')
else:
raise
elif value_size == 8:
def EncodeNonFiniteOrRaise(write, value):
if value == _POS_INF:
- write(b('\x00\x00\x00\x00\x00\x00\xF0\x7F')) ##PY25
-##!PY25 write(b'\x00\x00\x00\x00\x00\x00\xF0\x7F')
+ write(b'\x00\x00\x00\x00\x00\x00\xF0\x7F')
elif value == _NEG_INF:
- write(b('\x00\x00\x00\x00\x00\x00\xF0\xFF')) ##PY25
-##!PY25 write(b'\x00\x00\x00\x00\x00\x00\xF0\xFF')
+ write(b'\x00\x00\x00\x00\x00\x00\xF0\xFF')
elif value != value: # NaN
- write(b('\x00\x00\x00\x00\x00\x00\xF8\x7F')) ##PY25
-##!PY25 write(b'\x00\x00\x00\x00\x00\x00\xF8\x7F')
+ write(b'\x00\x00\x00\x00\x00\x00\xF8\x7F')
else:
raise
else:
@@ -631,10 +618,8 @@ DoubleEncoder = _FloatingPointEncoder(wire_format.WIRETYPE_FIXED64, '<d')
def BoolEncoder(field_number, is_repeated, is_packed):
"""Returns an encoder for a boolean field."""
-##!PY25 false_byte = b'\x00'
-##!PY25 true_byte = b'\x01'
- false_byte = '\x00'.encode('latin1') ##PY25
- true_byte = '\x01'.encode('latin1') ##PY25
+ false_byte = b'\x00'
+ true_byte = b'\x01'
if is_packed:
tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
local_EncodeVarint = _EncodeVarint
@@ -770,8 +755,7 @@ def MessageSetItemEncoder(field_number):
}
}
"""
- start_bytes = "".encode("latin1").join([ ##PY25
-##!PY25 start_bytes = b"".join([
+ start_bytes = b"".join([
TagBytes(1, wire_format.WIRETYPE_START_GROUP),
TagBytes(2, wire_format.WIRETYPE_VARINT),
_VarintBytes(field_number),
diff --git a/python/google/protobuf/internal/generator_test.py b/python/google/protobuf/internal/generator_test.py
index 03361e66..afcf6227 100755
--- a/python/google/protobuf/internal/generator_test.py
+++ b/python/google/protobuf/internal/generator_test.py
@@ -41,7 +41,8 @@ further ensures that we can use Python protocol message objects as we expect.
__author__ = 'robinson@google.com (Will Robinson)'
-from google.apputils import basetest
+import unittest
+
from google.protobuf.internal import test_bad_identifiers_pb2
from google.protobuf import unittest_custom_options_pb2
from google.protobuf import unittest_import_pb2
@@ -55,7 +56,7 @@ from google.protobuf import symbol_database
MAX_EXTENSION = 536870912
-class GeneratorTest(basetest.TestCase):
+class GeneratorTest(unittest.TestCase):
def testNestedMessageDescriptor(self):
field_name = 'optional_nested_message'
@@ -153,7 +154,7 @@ class GeneratorTest(basetest.TestCase):
# extension and for its value to be set to -789.
def testNestedTypes(self):
- self.assertEquals(
+ self.assertEqual(
set(unittest_pb2.TestAllTypes.DESCRIPTOR.nested_types),
set([
unittest_pb2.TestAllTypes.NestedMessage.DESCRIPTOR,
@@ -294,50 +295,50 @@ class GeneratorTest(basetest.TestCase):
self.assertSameElements(
nested_names,
[field.name for field in desc.oneofs[0].fields])
- for field_name, field_desc in desc.fields_by_name.iteritems():
+ for field_name, field_desc in desc.fields_by_name.items():
if field_name in nested_names:
self.assertIs(desc.oneofs[0], field_desc.containing_oneof)
else:
self.assertIsNone(field_desc.containing_oneof)
-class SymbolDatabaseRegistrationTest(basetest.TestCase):
+class SymbolDatabaseRegistrationTest(unittest.TestCase):
"""Checks that messages, enums and files are correctly registered."""
def testGetSymbol(self):
- self.assertEquals(
+ self.assertEqual(
unittest_pb2.TestAllTypes, symbol_database.Default().GetSymbol(
'protobuf_unittest.TestAllTypes'))
- self.assertEquals(
+ self.assertEqual(
unittest_pb2.TestAllTypes.NestedMessage,
symbol_database.Default().GetSymbol(
'protobuf_unittest.TestAllTypes.NestedMessage'))
with self.assertRaises(KeyError):
symbol_database.Default().GetSymbol('protobuf_unittest.NestedMessage')
- self.assertEquals(
+ self.assertEqual(
unittest_pb2.TestAllTypes.OptionalGroup,
symbol_database.Default().GetSymbol(
'protobuf_unittest.TestAllTypes.OptionalGroup'))
- self.assertEquals(
+ self.assertEqual(
unittest_pb2.TestAllTypes.RepeatedGroup,
symbol_database.Default().GetSymbol(
'protobuf_unittest.TestAllTypes.RepeatedGroup'))
def testEnums(self):
- self.assertEquals(
+ self.assertEqual(
'protobuf_unittest.ForeignEnum',
symbol_database.Default().pool.FindEnumTypeByName(
'protobuf_unittest.ForeignEnum').full_name)
- self.assertEquals(
+ self.assertEqual(
'protobuf_unittest.TestAllTypes.NestedEnum',
symbol_database.Default().pool.FindEnumTypeByName(
'protobuf_unittest.TestAllTypes.NestedEnum').full_name)
def testFindFileByName(self):
- self.assertEquals(
+ self.assertEqual(
'google/protobuf/unittest.proto',
symbol_database.Default().pool.FindFileByName(
'google/protobuf/unittest.proto').name)
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/internal/message_factory_python_test.py b/python/google/protobuf/internal/message_factory_python_test.py
index 85e02b25..eeb164b1 100644
--- a/python/google/protobuf/internal/message_factory_python_test.py
+++ b/python/google/protobuf/internal/message_factory_python_test.py
@@ -33,22 +33,22 @@
"""Tests for ..public.message_factory for the pure Python implementation."""
import os
+import unittest
os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python'
# We must set the implementation version above before the google3 imports.
# pylint: disable=g-import-not-at-top
-from google.apputils import basetest
from google.protobuf.internal import api_implementation
# Run all tests from the original module by putting them in our namespace.
# pylint: disable=wildcard-import
from google.protobuf.internal.message_factory_test import *
-class ConfirmPurePythonTest(basetest.TestCase):
+class ConfirmPurePythonTest(unittest.TestCase):
def testImplementationSetting(self):
self.assertEqual('python', api_implementation.Type())
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/internal/message_factory_test.py b/python/google/protobuf/internal/message_factory_test.py
index fcf13410..b33539a0 100644
--- a/python/google/protobuf/internal/message_factory_test.py
+++ b/python/google/protobuf/internal/message_factory_test.py
@@ -34,7 +34,8 @@
__author__ = 'matthewtoia@google.com (Matt Toia)'
-from google.apputils import basetest
+import unittest
+
from google.protobuf import descriptor_pb2
from google.protobuf.internal import factory_test1_pb2
from google.protobuf.internal import factory_test2_pb2
@@ -43,7 +44,7 @@ from google.protobuf import descriptor_pool
from google.protobuf import message_factory
-class MessageFactoryTest(basetest.TestCase):
+class MessageFactoryTest(unittest.TestCase):
def setUp(self):
self.factory_test1_fd = descriptor_pb2.FileDescriptorProto.FromString(
@@ -81,9 +82,9 @@ class MessageFactoryTest(basetest.TestCase):
serialized = msg.SerializeToString()
converted = factory_test2_pb2.Factory2Message.FromString(serialized)
reserialized = converted.SerializeToString()
- self.assertEquals(serialized, reserialized)
+ self.assertEqual(serialized, reserialized)
result = cls.FromString(reserialized)
- self.assertEquals(msg, result)
+ self.assertEqual(msg, result)
def testGetPrototype(self):
db = descriptor_database.DescriptorDatabase()
@@ -93,28 +94,29 @@ class MessageFactoryTest(basetest.TestCase):
factory = message_factory.MessageFactory()
cls = factory.GetPrototype(pool.FindMessageTypeByName(
'google.protobuf.python.internal.Factory2Message'))
- self.assertIsNot(cls, factory_test2_pb2.Factory2Message)
+ self.assertFalse(cls is factory_test2_pb2.Factory2Message)
self._ExerciseDynamicClass(cls)
cls2 = factory.GetPrototype(pool.FindMessageTypeByName(
'google.protobuf.python.internal.Factory2Message'))
- self.assertIs(cls, cls2)
+ self.assertTrue(cls is cls2)
def testGetMessages(self):
# performed twice because multiple calls with the same input must be allowed
for _ in range(2):
messages = message_factory.GetMessages([self.factory_test2_fd,
self.factory_test1_fd])
- self.assertContainsSubset(
- ['google.protobuf.python.internal.Factory2Message',
- 'google.protobuf.python.internal.Factory1Message'],
- messages.keys())
+ self.assertTrue(
+ set(['google.protobuf.python.internal.Factory2Message',
+ 'google.protobuf.python.internal.Factory1Message'],
+ ).issubset(set(messages.keys())))
self._ExerciseDynamicClass(
messages['google.protobuf.python.internal.Factory2Message'])
- self.assertContainsSubset(
- ['google.protobuf.python.internal.Factory2Message.one_more_field',
- 'google.protobuf.python.internal.another_field'],
- (messages['google.protobuf.python.internal.Factory1Message']
- ._extensions_by_name.keys()))
+ self.assertTrue(
+ set(['google.protobuf.python.internal.Factory2Message.one_more_field',
+ 'google.protobuf.python.internal.another_field'],
+ ).issubset(
+ set(messages['google.protobuf.python.internal.Factory1Message']
+ ._extensions_by_name.keys())))
factory_msg1 = messages['google.protobuf.python.internal.Factory1Message']
msg1 = messages['google.protobuf.python.internal.Factory1Message']()
ext1 = factory_msg1._extensions_by_name[
@@ -123,9 +125,9 @@ class MessageFactoryTest(basetest.TestCase):
'google.protobuf.python.internal.another_field']
msg1.Extensions[ext1] = 'test1'
msg1.Extensions[ext2] = 'test2'
- self.assertEquals('test1', msg1.Extensions[ext1])
- self.assertEquals('test2', msg1.Extensions[ext2])
+ self.assertEqual('test1', msg1.Extensions[ext1])
+ self.assertEqual('test2', msg1.Extensions[ext2])
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/internal/message_python_test.py b/python/google/protobuf/internal/message_python_test.py
index c40623a8..ef57967b 100644
--- a/python/google/protobuf/internal/message_python_test.py
+++ b/python/google/protobuf/internal/message_python_test.py
@@ -33,22 +33,22 @@
"""Tests for ..public.message for the pure Python implementation."""
import os
+import unittest
os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python'
# We must set the implementation version above before the google3 imports.
# pylint: disable=g-import-not-at-top
-from google.apputils import basetest
from google.protobuf.internal import api_implementation
# Run all tests from the original module by putting them in our namespace.
# pylint: disable=wildcard-import
from google.protobuf.internal.message_test import *
-class ConfirmPurePythonTest(basetest.TestCase):
+class ConfirmPurePythonTest(unittest.TestCase):
def testImplementationSetting(self):
self.assertEqual('python', api_implementation.Type())
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/internal/message_test.py b/python/google/protobuf/internal/message_test.py
index 42e2ad7e..e69c49b6 100755
--- a/python/google/protobuf/internal/message_test.py
+++ b/python/google/protobuf/internal/message_test.py
@@ -48,8 +48,8 @@ import math
import operator
import pickle
import sys
+import unittest
-from google.apputils import basetest
from google.protobuf import unittest_pb2
from google.protobuf.internal import api_implementation
from google.protobuf.internal import test_util
@@ -69,7 +69,7 @@ def IsNegInf(val):
return isinf(val) and (val < 0)
-class MessageTest(basetest.TestCase):
+class MessageTest(unittest.TestCase):
def testBadUtf8String(self):
if api_implementation.Type() != 'python':
@@ -98,7 +98,7 @@ class MessageTest(basetest.TestCase):
golden_message.ParseFromString(golden_data)
all_set = unittest_pb2.TestAllExtensions()
test_util.SetAllExtensions(all_set)
- self.assertEquals(all_set, golden_message)
+ self.assertEqual(all_set, golden_message)
self.assertEqual(golden_data, golden_message.SerializeToString())
golden_copy = copy.deepcopy(golden_message)
self.assertEqual(golden_data, golden_copy.SerializeToString())
@@ -109,7 +109,7 @@ class MessageTest(basetest.TestCase):
golden_message.ParseFromString(golden_data)
all_set = unittest_pb2.TestPackedTypes()
test_util.SetAllPackedFields(all_set)
- self.assertEquals(all_set, golden_message)
+ self.assertEqual(all_set, golden_message)
self.assertEqual(golden_data, all_set.SerializeToString())
golden_copy = copy.deepcopy(golden_message)
self.assertEqual(golden_data, golden_copy.SerializeToString())
@@ -120,7 +120,7 @@ class MessageTest(basetest.TestCase):
golden_message.ParseFromString(golden_data)
all_set = unittest_pb2.TestPackedExtensions()
test_util.SetAllPackedExtensions(all_set)
- self.assertEquals(all_set, golden_message)
+ self.assertEqual(all_set, golden_message)
self.assertEqual(golden_data, all_set.SerializeToString())
golden_copy = copy.deepcopy(golden_message)
self.assertEqual(golden_data, golden_copy.SerializeToString())
@@ -132,7 +132,7 @@ class MessageTest(basetest.TestCase):
pickled_message = pickle.dumps(golden_message)
unpickled_message = pickle.loads(pickled_message)
- self.assertEquals(unpickled_message, golden_message)
+ self.assertEqual(unpickled_message, golden_message)
def testPickleIncompleteProto(self):
@@ -140,8 +140,8 @@ class MessageTest(basetest.TestCase):
pickled_message = pickle.dumps(golden_message)
unpickled_message = pickle.loads(pickled_message)
- self.assertEquals(unpickled_message, golden_message)
- self.assertEquals(unpickled_message.a, 1)
+ self.assertEqual(unpickled_message, golden_message)
+ self.assertEqual(unpickled_message.a, 1)
# This is still an incomplete proto - so serializing should fail
self.assertRaises(message.EncodeError, unpickled_message.SerializeToString)
@@ -344,11 +344,11 @@ class MessageTest(basetest.TestCase):
msg.repeated_nested_message.add(bb=3)
msg.repeated_nested_message.add(bb=4)
- self.assertEquals([1, 2, 3, 4],
+ self.assertEqual([1, 2, 3, 4],
[m.bb for m in msg.repeated_nested_message])
- self.assertEquals([4, 3, 2, 1],
+ self.assertEqual([4, 3, 2, 1],
[m.bb for m in reversed(msg.repeated_nested_message)])
- self.assertEquals([4, 3, 2, 1],
+ self.assertEqual([4, 3, 2, 1],
[m.bb for m in msg.repeated_nested_message[::-1]])
def testSortingRepeatedScalarFieldsDefaultComparator(self):
@@ -695,7 +695,7 @@ class MessageTest(basetest.TestCase):
m.HasField('repeated_int32')
-class ValidTypeNamesTest(basetest.TestCase):
+class ValidTypeNamesTest(unittest.TestCase):
def assertImportFromName(self, msg, base_name):
# Parse <type 'module.class_name'> to extra 'some.name' as a string.
@@ -718,4 +718,4 @@ class ValidTypeNamesTest(basetest.TestCase):
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/internal/proto_builder_test.py b/python/google/protobuf/internal/proto_builder_test.py
index c74db7e7..9229205a 100644
--- a/python/google/protobuf/internal/proto_builder_test.py
+++ b/python/google/protobuf/internal/proto_builder_test.py
@@ -32,7 +32,7 @@
"""Tests for google.protobuf.proto_builder."""
-from google.apputils import basetest
+import unittest
from google.protobuf import descriptor_pb2
from google.protobuf import descriptor_pool
@@ -40,7 +40,7 @@ from google.protobuf import proto_builder
from google.protobuf import text_format
-class ProtoBuilderTest(basetest.TestCase):
+class ProtoBuilderTest(unittest.TestCase):
def setUp(self):
self._fields = {
@@ -74,4 +74,4 @@ class ProtoBuilderTest(basetest.TestCase):
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/internal/python_message.py b/python/google/protobuf/internal/python_message.py
index 6fda6ae0..58c65db9 100755
--- a/python/google/protobuf/internal/python_message.py
+++ b/python/google/protobuf/internal/python_message.py
@@ -28,8 +28,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-# Keep it Python2.5 compatible for GAE.
-#
# Copyright 2007 Google Inc. All Rights Reserved.
#
# This code is meant to work on Python 2.4 and above only.
@@ -54,19 +52,14 @@ this file*.
__author__ = 'robinson@google.com (Will Robinson)'
+from io import BytesIO
import sys
-if sys.version_info[0] < 3:
- try:
- from cStringIO import StringIO as BytesIO
- except ImportError:
- from StringIO import StringIO as BytesIO
- import copy_reg as copyreg
-else:
- from io import BytesIO
- import copyreg
import struct
import weakref
+import six
+import six.moves.copyreg as copyreg
+
# We use "as" to avoid name collisions with variables.
from google.protobuf.internal import containers
from google.protobuf.internal import decoder
@@ -237,7 +230,7 @@ def _AttachFieldHelpers(cls, field_descriptor):
def _AddClassAttributesForNestedExtensions(descriptor, dictionary):
extension_dict = descriptor.extensions_by_name
- for extension_name, extension_field in extension_dict.iteritems():
+ for extension_name, extension_field in extension_dict.items():
assert extension_name not in dictionary
dictionary[extension_name] = extension_field
@@ -314,7 +307,7 @@ def _ReraiseTypeErrorWithFieldName(message_name, field_name):
exc = TypeError('%s for field %s.%s' % (str(exc), message_name, field_name))
# re-raise possibly-amended exception with original traceback:
- raise type(exc), exc, sys.exc_info()[2]
+ six.reraise(type(exc), exc, sys.exc_info()[2])
def _AddInitMethod(message_descriptor, cls):
@@ -334,7 +327,7 @@ def _AddInitMethod(message_descriptor, cls):
self._is_present_in_parent = False
self._listener = message_listener_mod.NullMessageListener()
self._listener_for_children = _Listener(self)
- for field_name, field_value in kwargs.iteritems():
+ for field_name, field_value in kwargs.items():
field = _GetFieldByName(message_descriptor, field_name)
if field is None:
raise TypeError("%s() got an unexpected keyword argument '%s'" %
@@ -563,7 +556,7 @@ def _AddPropertiesForNonRepeatedCompositeField(field, cls):
def _AddPropertiesForExtensions(descriptor, cls):
"""Adds properties for all fields in this protocol message type."""
extension_dict = descriptor.extensions_by_name
- for extension_name, extension_field in extension_dict.iteritems():
+ for extension_name, extension_field in extension_dict.items():
constant_name = extension_name.upper() + "_FIELD_NUMBER"
setattr(cls, constant_name, extension_field.number)
@@ -618,7 +611,7 @@ def _AddListFieldsMethod(message_descriptor, cls):
"""Helper for _AddMessageMethods()."""
def ListFields(self):
- all_fields = [item for item in self._fields.iteritems() if _IsPresent(item)]
+ all_fields = [item for item in self._fields.items() if _IsPresent(item)]
all_fields.sort(key = lambda item: item[0].number)
return all_fields
@@ -863,7 +856,7 @@ def _AddMergeFromStringMethod(message_descriptor, cls):
except (IndexError, TypeError):
# Now ord(buf[p:p+1]) == ord('') gets TypeError.
raise message_mod.DecodeError('Truncated message.')
- except struct.error, e:
+ except struct.error as e:
raise message_mod.DecodeError(e)
return length # Return this for legacy reasons.
cls.MergeFromString = MergeFromString
@@ -963,7 +956,7 @@ def _AddIsInitializedMethod(message_descriptor, cls):
name = field.name
if field.label == _FieldDescriptor.LABEL_REPEATED:
- for i in xrange(len(value)):
+ for i in range(len(value)):
element = value[i]
prefix = "%s[%d]." % (name, i)
sub_errors = element.FindInitializationErrors()
@@ -993,7 +986,7 @@ def _AddMergeFromMethod(cls):
fields = self._fields
- for field, value in msg._fields.iteritems():
+ for field, value in msg._fields.items():
if field.label == LABEL_REPEATED:
field_value = fields.get(field)
if field_value is None:
diff --git a/python/google/protobuf/internal/reflection_test.py b/python/google/protobuf/internal/reflection_test.py
index 6b24b092..a3757992 100755
--- a/python/google/protobuf/internal/reflection_test.py
+++ b/python/google/protobuf/internal/reflection_test.py
@@ -39,8 +39,21 @@ import copy
import gc
import operator
import struct
+import unittest
+try:
+ from unittest import skipIf
+except ImportError:
+ def skipIf(predicate, message):
+ def decorator(wrapped):
+ if predicate:
+ def _noop(*args, **kw):
+ pass
+ return _noop
+ return wrapped
+ return decorator
+
+import six
-from google.apputils import basetest
from google.protobuf import unittest_import_pb2
from google.protobuf import unittest_mset_pb2
from google.protobuf import unittest_pb2
@@ -102,7 +115,7 @@ class _MiniDecoder(object):
return self._pos == len(self._bytes)
-class ReflectionTest(basetest.TestCase):
+class ReflectionTest(unittest.TestCase):
def assertListsEqual(self, values, others):
self.assertEqual(len(values), len(others))
@@ -128,10 +141,10 @@ class ReflectionTest(basetest.TestCase):
repeated_bool=[True, False, False],
repeated_string=["optional_string"])
- self.assertEquals([1, 2, 3, 4], list(proto.repeated_int32))
- self.assertEquals([1.23, 54.321], list(proto.repeated_double))
- self.assertEquals([True, False, False], list(proto.repeated_bool))
- self.assertEquals(["optional_string"], list(proto.repeated_string))
+ self.assertEqual([1, 2, 3, 4], list(proto.repeated_int32))
+ self.assertEqual([1.23, 54.321], list(proto.repeated_double))
+ self.assertEqual([True, False, False], list(proto.repeated_bool))
+ self.assertEqual(["optional_string"], list(proto.repeated_string))
def testRepeatedCompositeConstructor(self):
# Constructor with only repeated composite types should succeed.
@@ -150,18 +163,18 @@ class ReflectionTest(basetest.TestCase):
unittest_pb2.TestAllTypes.RepeatedGroup(a=1),
unittest_pb2.TestAllTypes.RepeatedGroup(a=2)])
- self.assertEquals(
+ self.assertEqual(
[unittest_pb2.TestAllTypes.NestedMessage(
bb=unittest_pb2.TestAllTypes.FOO),
unittest_pb2.TestAllTypes.NestedMessage(
bb=unittest_pb2.TestAllTypes.BAR)],
list(proto.repeated_nested_message))
- self.assertEquals(
+ self.assertEqual(
[unittest_pb2.ForeignMessage(c=-43),
unittest_pb2.ForeignMessage(c=45324),
unittest_pb2.ForeignMessage(c=12)],
list(proto.repeated_foreign_message))
- self.assertEquals(
+ self.assertEqual(
[unittest_pb2.TestAllTypes.RepeatedGroup(),
unittest_pb2.TestAllTypes.RepeatedGroup(a=1),
unittest_pb2.TestAllTypes.RepeatedGroup(a=2)],
@@ -186,15 +199,15 @@ class ReflectionTest(basetest.TestCase):
self.assertEqual(24, proto.optional_int32)
self.assertEqual('optional_string', proto.optional_string)
- self.assertEquals([1.23, 54.321], list(proto.repeated_double))
- self.assertEquals([True, False, False], list(proto.repeated_bool))
- self.assertEquals(
+ self.assertEqual([1.23, 54.321], list(proto.repeated_double))
+ self.assertEqual([True, False, False], list(proto.repeated_bool))
+ self.assertEqual(
[unittest_pb2.TestAllTypes.NestedMessage(
bb=unittest_pb2.TestAllTypes.FOO),
unittest_pb2.TestAllTypes.NestedMessage(
bb=unittest_pb2.TestAllTypes.BAR)],
list(proto.repeated_nested_message))
- self.assertEquals(
+ self.assertEqual(
[unittest_pb2.ForeignMessage(c=-43),
unittest_pb2.ForeignMessage(c=45324),
unittest_pb2.ForeignMessage(c=12)],
@@ -222,18 +235,18 @@ class ReflectionTest(basetest.TestCase):
def testConstructorInvalidatesCachedByteSize(self):
message = unittest_pb2.TestAllTypes(optional_int32 = 12)
- self.assertEquals(2, message.ByteSize())
+ self.assertEqual(2, message.ByteSize())
message = unittest_pb2.TestAllTypes(
optional_nested_message = unittest_pb2.TestAllTypes.NestedMessage())
- self.assertEquals(3, message.ByteSize())
+ self.assertEqual(3, message.ByteSize())
message = unittest_pb2.TestAllTypes(repeated_int32 = [12])
- self.assertEquals(3, message.ByteSize())
+ self.assertEqual(3, message.ByteSize())
message = unittest_pb2.TestAllTypes(
repeated_nested_message = [unittest_pb2.TestAllTypes.NestedMessage()])
- self.assertEquals(3, message.ByteSize())
+ self.assertEqual(3, message.ByteSize())
def testSimpleHasBits(self):
# Test a scalar.
@@ -467,7 +480,7 @@ class ReflectionTest(basetest.TestCase):
proto.repeated_string.extend(['foo', 'bar'])
proto.repeated_string.extend([])
proto.repeated_string.append('baz')
- proto.repeated_string.extend(str(x) for x in xrange(2))
+ proto.repeated_string.extend(str(x) for x in range(2))
proto.optional_int32 = 21
proto.repeated_bool # Access but don't set anything; should not be listed.
self.assertEqual(
@@ -620,14 +633,18 @@ class ReflectionTest(basetest.TestCase):
if struct.calcsize('L') == 4:
# Python only has signed ints, so 32-bit python can't fit an uint32
# in an int.
- TestGetAndDeserialize('optional_uint32', 1 << 31, long)
+ TestGetAndDeserialize('optional_uint32', 1 << 31, int)
else:
# 64-bit python can fit uint32 inside an int
TestGetAndDeserialize('optional_uint32', 1 << 31, int)
- TestGetAndDeserialize('optional_int64', 1 << 30, long)
- TestGetAndDeserialize('optional_int64', 1 << 60, long)
- TestGetAndDeserialize('optional_uint64', 1 << 30, long)
- TestGetAndDeserialize('optional_uint64', 1 << 60, long)
+ try:
+ integer_64 = long
+ except NameError: # Python3
+ integer_64 = int
+ TestGetAndDeserialize('optional_int64', 1 << 30, integer_64)
+ TestGetAndDeserialize('optional_int64', 1 << 60, integer_64)
+ TestGetAndDeserialize('optional_uint64', 1 << 30, integer_64)
+ TestGetAndDeserialize('optional_uint64', 1 << 60, integer_64)
def testSingleScalarBoundsChecking(self):
def TestMinAndMaxIntegers(field_name, expected_min, expected_max):
@@ -753,18 +770,18 @@ class ReflectionTest(basetest.TestCase):
def testEnum_KeysAndValues(self):
self.assertEqual(['FOREIGN_FOO', 'FOREIGN_BAR', 'FOREIGN_BAZ'],
- unittest_pb2.ForeignEnum.keys())
+ list(unittest_pb2.ForeignEnum.keys()))
self.assertEqual([4, 5, 6],
- unittest_pb2.ForeignEnum.values())
+ list(unittest_pb2.ForeignEnum.values()))
self.assertEqual([('FOREIGN_FOO', 4), ('FOREIGN_BAR', 5),
('FOREIGN_BAZ', 6)],
- unittest_pb2.ForeignEnum.items())
+ list(unittest_pb2.ForeignEnum.items()))
proto = unittest_pb2.TestAllTypes()
- self.assertEqual(['FOO', 'BAR', 'BAZ', 'NEG'], proto.NestedEnum.keys())
- self.assertEqual([1, 2, 3, -1], proto.NestedEnum.values())
+ self.assertEqual(['FOO', 'BAR', 'BAZ', 'NEG'], list(proto.NestedEnum.keys()))
+ self.assertEqual([1, 2, 3, -1], list(proto.NestedEnum.values()))
self.assertEqual([('FOO', 1), ('BAR', 2), ('BAZ', 3), ('NEG', -1)],
- proto.NestedEnum.items())
+ list(proto.NestedEnum.items()))
def testRepeatedScalars(self):
proto = unittest_pb2.TestAllTypes()
@@ -803,7 +820,7 @@ class ReflectionTest(basetest.TestCase):
self.assertEqual([5, 25, 20, 15, 30], proto.repeated_int32[:])
# Test slice assignment with an iterator
- proto.repeated_int32[1:4] = (i for i in xrange(3))
+ proto.repeated_int32[1:4] = (i for i in range(3))
self.assertEqual([5, 0, 1, 2, 30], proto.repeated_int32)
# Test slice assignment.
@@ -1006,9 +1023,8 @@ class ReflectionTest(basetest.TestCase):
containing_type=None, nested_types=[], enum_types=[],
fields=[foo_field_descriptor], extensions=[],
options=descriptor_pb2.MessageOptions())
- class MyProtoClass(message.Message):
+ class MyProtoClass(six.with_metaclass(reflection.GeneratedProtocolMessageType, message.Message)):
DESCRIPTOR = mydescriptor
- __metaclass__ = reflection.GeneratedProtocolMessageType
myproto_instance = MyProtoClass()
self.assertEqual(0, myproto_instance.foo_field)
self.assertTrue(not myproto_instance.HasField('foo_field'))
@@ -1048,14 +1064,13 @@ class ReflectionTest(basetest.TestCase):
new_field.label = descriptor_pb2.FieldDescriptorProto.LABEL_REPEATED
desc = descriptor.MakeDescriptor(desc_proto)
- self.assertTrue(desc.fields_by_name.has_key('name'))
- self.assertTrue(desc.fields_by_name.has_key('year'))
- self.assertTrue(desc.fields_by_name.has_key('automatic'))
- self.assertTrue(desc.fields_by_name.has_key('price'))
- self.assertTrue(desc.fields_by_name.has_key('owners'))
-
- class CarMessage(message.Message):
- __metaclass__ = reflection.GeneratedProtocolMessageType
+ self.assertTrue('name' in desc.fields_by_name)
+ self.assertTrue('year' in desc.fields_by_name)
+ self.assertTrue('automatic' in desc.fields_by_name)
+ self.assertTrue('price' in desc.fields_by_name)
+ self.assertTrue('owners' in desc.fields_by_name)
+
+ class CarMessage(six.with_metaclass(reflection.GeneratedProtocolMessageType, message.Message)):
DESCRIPTOR = desc
prius = CarMessage()
@@ -1173,7 +1188,7 @@ class ReflectionTest(basetest.TestCase):
self.assertTrue(1 in unittest_pb2.TestAllExtensions._extensions_by_number)
# Make sure extensions haven't been registered into types that shouldn't
# have any.
- self.assertEquals(0, len(unittest_pb2.TestAllTypes._extensions_by_name))
+ self.assertEqual(0, len(unittest_pb2.TestAllTypes._extensions_by_name))
# If message A directly contains message B, and
# a.HasField('b') is currently False, then mutating any
@@ -1497,18 +1512,18 @@ class ReflectionTest(basetest.TestCase):
test_util.SetAllNonLazyFields(proto)
# Clear the message.
proto.Clear()
- self.assertEquals(proto.ByteSize(), 0)
+ self.assertEqual(proto.ByteSize(), 0)
empty_proto = unittest_pb2.TestAllTypes()
- self.assertEquals(proto, empty_proto)
+ self.assertEqual(proto, empty_proto)
# Test if extensions which were set are cleared.
proto = unittest_pb2.TestAllExtensions()
test_util.SetAllExtensions(proto)
# Clear the message.
proto.Clear()
- self.assertEquals(proto.ByteSize(), 0)
+ self.assertEqual(proto.ByteSize(), 0)
empty_proto = unittest_pb2.TestAllExtensions()
- self.assertEquals(proto, empty_proto)
+ self.assertEqual(proto, empty_proto)
def testDisconnectingBeforeClear(self):
proto = unittest_pb2.TestAllTypes()
@@ -1619,7 +1634,7 @@ class ReflectionTest(basetest.TestCase):
self.assertFalse(proto.IsInitialized(errors))
self.assertEqual(errors, ['a', 'b', 'c'])
- @basetest.unittest.skipIf(
+ @skipIf(
api_implementation.Type() != 'cpp' or api_implementation.Version() != 2,
'Errors are only available from the most recent C++ implementation.')
def testFileDescriptorErrors(self):
@@ -1640,18 +1655,17 @@ class ReflectionTest(basetest.TestCase):
file_descriptor_proto.name = another_file_name
m2 = file_descriptor_proto.message_type.add()
m2.name = 'msg2'
- with self.assertRaises(TypeError) as cm:
+ try:
descriptor.FileDescriptor(
another_file_name,
package_name,
serialized_pb=file_descriptor_proto.SerializeToString())
- self.assertTrue(hasattr(cm, 'exception'), '%s not raised' %
- getattr(cm.expected, '__name__', cm.expected))
- self.assertIn('test_file_descriptor_errors.proto', str(cm.exception))
- # Error message will say something about this definition being a
- # duplicate, though we don't check the message exactly to avoid a
- # dependency on the C++ logging code.
- self.assertIn('test_file_descriptor_errors.msg1', str(cm.exception))
+ except TypeError as e:
+ message = str(e)
+ else:
+ self.fail("Did not raise TypeError")
+
+ self.assertTrue('test_file_descriptor_errors.proto' in message)
def testStringUTF8Encoding(self):
proto = unittest_pb2.TestAllTypes()
@@ -1661,14 +1675,14 @@ class ReflectionTest(basetest.TestCase):
setattr, proto, 'optional_bytes', u'unicode object')
# Check that the default value is of python's 'unicode' type.
- self.assertEqual(type(proto.optional_string), unicode)
+ self.assertEqual(type(proto.optional_string), six.text_type)
- proto.optional_string = unicode('Testing')
+ proto.optional_string = six.text_type('Testing')
self.assertEqual(proto.optional_string, str('Testing'))
# Assign a value of type 'str' which can be encoded in UTF-8.
proto.optional_string = str('Testing')
- self.assertEqual(proto.optional_string, unicode('Testing'))
+ self.assertEqual(proto.optional_string, six.text_type('Testing'))
# Try to assign a 'bytes' object which contains non-UTF-8.
self.assertRaises(ValueError,
@@ -1715,7 +1729,7 @@ class ReflectionTest(basetest.TestCase):
bytes_read = message2.MergeFromString(raw.item[0].message)
self.assertEqual(len(raw.item[0].message), bytes_read)
- self.assertEqual(type(message2.str), unicode)
+ self.assertEqual(type(message2.str), six.text_type)
self.assertEqual(message2.str, test_utf8)
# The pure Python API throws an exception on MergeFromString(),
@@ -1739,7 +1753,7 @@ class ReflectionTest(basetest.TestCase):
def testBytesInTextFormat(self):
proto = unittest_pb2.TestAllTypes(optional_bytes=b'\x00\x7f\x80\xff')
self.assertEqual(u'optional_bytes: "\\000\\177\\200\\377"\n',
- unicode(proto))
+ six.text_type(proto))
def testEmptyNestedMessage(self):
proto = unittest_pb2.TestAllTypes()
@@ -1797,7 +1811,7 @@ class ReflectionTest(basetest.TestCase):
# into separate TestCase classes.
-class TestAllTypesEqualityTest(basetest.TestCase):
+class TestAllTypesEqualityTest(unittest.TestCase):
def setUp(self):
self.first_proto = unittest_pb2.TestAllTypes()
@@ -1813,7 +1827,7 @@ class TestAllTypesEqualityTest(basetest.TestCase):
self.assertEqual(self.first_proto, self.second_proto)
-class FullProtosEqualityTest(basetest.TestCase):
+class FullProtosEqualityTest(unittest.TestCase):
"""Equality tests using completely-full protos as a starting point."""
@@ -1899,7 +1913,7 @@ class FullProtosEqualityTest(basetest.TestCase):
self.assertEqual(self.first_proto, self.second_proto)
-class ExtensionEqualityTest(basetest.TestCase):
+class ExtensionEqualityTest(unittest.TestCase):
def testExtensionEquality(self):
first_proto = unittest_pb2.TestAllExtensions()
@@ -1932,7 +1946,7 @@ class ExtensionEqualityTest(basetest.TestCase):
self.assertEqual(first_proto, second_proto)
-class MutualRecursionEqualityTest(basetest.TestCase):
+class MutualRecursionEqualityTest(unittest.TestCase):
def testEqualityWithMutualRecursion(self):
first_proto = unittest_pb2.TestMutualRecursionA()
@@ -1944,7 +1958,7 @@ class MutualRecursionEqualityTest(basetest.TestCase):
self.assertEqual(first_proto, second_proto)
-class ByteSizeTest(basetest.TestCase):
+class ByteSizeTest(unittest.TestCase):
def setUp(self):
self.proto = unittest_pb2.TestAllTypes()
@@ -2240,7 +2254,7 @@ class ByteSizeTest(basetest.TestCase):
# * Handling of empty submessages (with and without "has"
# bits set).
-class SerializationTest(basetest.TestCase):
+class SerializationTest(unittest.TestCase):
def testSerializeEmtpyMessage(self):
first_proto = unittest_pb2.TestAllTypes()
@@ -2307,7 +2321,7 @@ class SerializationTest(basetest.TestCase):
test_util.SetAllFields(first_proto)
serialized = first_proto.SerializeToString()
- for truncation_point in xrange(len(serialized) + 1):
+ for truncation_point in range(len(serialized) + 1):
try:
second_proto = unittest_pb2.TestAllTypes()
unknown_fields = unittest_pb2.TestEmptyMessage()
@@ -2467,7 +2481,7 @@ class SerializationTest(basetest.TestCase):
# Check that the message parsed well.
extension_message1 = unittest_mset_pb2.TestMessageSetExtension1
extension1 = extension_message1.message_set_extension
- self.assertEquals(12345, proto.Extensions[extension1].i)
+ self.assertEqual(12345, proto.Extensions[extension1].i)
def testUnknownFields(self):
proto = unittest_pb2.TestAllTypes()
@@ -2791,7 +2805,7 @@ class SerializationTest(basetest.TestCase):
self.assertEqual(3, proto.repeated_int32[2])
-class OptionsTest(basetest.TestCase):
+class OptionsTest(unittest.TestCase):
def testMessageOptions(self):
proto = unittest_mset_pb2.TestMessageSet()
@@ -2818,9 +2832,9 @@ class OptionsTest(basetest.TestCase):
-class ClassAPITest(basetest.TestCase):
+class ClassAPITest(unittest.TestCase):
- @basetest.unittest.skipIf(
+ @skipIf(
api_implementation.Type() == 'cpp' and api_implementation.Version() == 2,
'C++ implementation requires a call to MakeDescriptor()')
def testMakeClassWithNestedDescriptor(self):
@@ -2908,8 +2922,7 @@ class ClassAPITest(basetest.TestCase):
msg_descriptor = descriptor.MakeDescriptor(
file_descriptor.message_type[0])
- class MessageClass(message.Message):
- __metaclass__ = reflection.GeneratedProtocolMessageType
+ class MessageClass(six.with_metaclass(reflection.GeneratedProtocolMessageType, message.Message)):
DESCRIPTOR = msg_descriptor
msg = MessageClass()
msg_str = (
@@ -2952,4 +2965,4 @@ class ClassAPITest(basetest.TestCase):
self.assertEqual(msg.bar.baz.deep, 4)
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/internal/service_reflection_test.py b/python/google/protobuf/internal/service_reflection_test.py
index d066ae70..e3f71545 100755
--- a/python/google/protobuf/internal/service_reflection_test.py
+++ b/python/google/protobuf/internal/service_reflection_test.py
@@ -34,13 +34,14 @@
__author__ = 'petar@google.com (Petar Petrov)'
-from google.apputils import basetest
+import unittest
+
from google.protobuf import unittest_pb2
from google.protobuf import service_reflection
from google.protobuf import service
-class FooUnitTest(basetest.TestCase):
+class FooUnitTest(unittest.TestCase):
def testService(self):
class MockRpcChannel(service.RpcChannel):
@@ -133,4 +134,4 @@ class FooUnitTest(basetest.TestCase):
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/internal/symbol_database_test.py b/python/google/protobuf/internal/symbol_database_test.py
index 47572d58..bbe602b3 100644
--- a/python/google/protobuf/internal/symbol_database_test.py
+++ b/python/google/protobuf/internal/symbol_database_test.py
@@ -32,12 +32,13 @@
"""Tests for google.protobuf.symbol_database."""
-from google.apputils import basetest
+import unittest
+
from google.protobuf import unittest_pb2
from google.protobuf import symbol_database
-class SymbolDatabaseTest(basetest.TestCase):
+class SymbolDatabaseTest(unittest.TestCase):
def _Database(self):
db = symbol_database.SymbolDatabase()
@@ -64,57 +65,57 @@ class SymbolDatabaseTest(basetest.TestCase):
messages['protobuf_unittest.TestAllTypes'])
def testGetSymbol(self):
- self.assertEquals(
+ self.assertEqual(
unittest_pb2.TestAllTypes, self._Database().GetSymbol(
'protobuf_unittest.TestAllTypes'))
- self.assertEquals(
+ self.assertEqual(
unittest_pb2.TestAllTypes.NestedMessage, self._Database().GetSymbol(
'protobuf_unittest.TestAllTypes.NestedMessage'))
- self.assertEquals(
+ self.assertEqual(
unittest_pb2.TestAllTypes.OptionalGroup, self._Database().GetSymbol(
'protobuf_unittest.TestAllTypes.OptionalGroup'))
- self.assertEquals(
+ self.assertEqual(
unittest_pb2.TestAllTypes.RepeatedGroup, self._Database().GetSymbol(
'protobuf_unittest.TestAllTypes.RepeatedGroup'))
def testEnums(self):
# Check registration of types in the pool.
- self.assertEquals(
+ self.assertEqual(
'protobuf_unittest.ForeignEnum',
self._Database().pool.FindEnumTypeByName(
'protobuf_unittest.ForeignEnum').full_name)
- self.assertEquals(
+ self.assertEqual(
'protobuf_unittest.TestAllTypes.NestedEnum',
self._Database().pool.FindEnumTypeByName(
'protobuf_unittest.TestAllTypes.NestedEnum').full_name)
def testFindMessageTypeByName(self):
- self.assertEquals(
+ self.assertEqual(
'protobuf_unittest.TestAllTypes',
self._Database().pool.FindMessageTypeByName(
'protobuf_unittest.TestAllTypes').full_name)
- self.assertEquals(
+ self.assertEqual(
'protobuf_unittest.TestAllTypes.NestedMessage',
self._Database().pool.FindMessageTypeByName(
'protobuf_unittest.TestAllTypes.NestedMessage').full_name)
def testFindFindContainingSymbol(self):
# Lookup based on either enum or message.
- self.assertEquals(
+ self.assertEqual(
'google/protobuf/unittest.proto',
self._Database().pool.FindFileContainingSymbol(
'protobuf_unittest.TestAllTypes.NestedEnum').name)
- self.assertEquals(
+ self.assertEqual(
'google/protobuf/unittest.proto',
self._Database().pool.FindFileContainingSymbol(
'protobuf_unittest.TestAllTypes').name)
def testFindFileByName(self):
- self.assertEquals(
+ self.assertEqual(
'google/protobuf/unittest.proto',
self._Database().pool.FindFileByName(
'google/protobuf/unittest.proto').name)
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/internal/text_encoding_test.py b/python/google/protobuf/internal/text_encoding_test.py
index db0222bd..fbd50bb8 100755
--- a/python/google/protobuf/internal/text_encoding_test.py
+++ b/python/google/protobuf/internal/text_encoding_test.py
@@ -32,7 +32,8 @@
"""Tests for google.protobuf.text_encoding."""
-from google.apputils import basetest
+import unittest
+
from google.protobuf import text_encoding
TEST_VALUES = [
@@ -50,19 +51,19 @@ TEST_VALUES = [
b"\010\011\012\013\014\015")]
-class TextEncodingTestCase(basetest.TestCase):
+class TextEncodingTestCase(unittest.TestCase):
def testCEscape(self):
for escaped, escaped_utf8, unescaped in TEST_VALUES:
- self.assertEquals(escaped,
+ self.assertEqual(escaped,
text_encoding.CEscape(unescaped, as_utf8=False))
- self.assertEquals(escaped_utf8,
+ self.assertEqual(escaped_utf8,
text_encoding.CEscape(unescaped, as_utf8=True))
def testCUnescape(self):
for escaped, escaped_utf8, unescaped in TEST_VALUES:
- self.assertEquals(unescaped, text_encoding.CUnescape(escaped))
- self.assertEquals(unescaped, text_encoding.CUnescape(escaped_utf8))
+ self.assertEqual(unescaped, text_encoding.CUnescape(escaped))
+ self.assertEqual(unescaped, text_encoding.CUnescape(escaped_utf8))
if __name__ == "__main__":
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/internal/text_format_test.py b/python/google/protobuf/internal/text_format_test.py
index 55e3c2c8..eda38ae9 100755
--- a/python/google/protobuf/internal/text_format_test.py
+++ b/python/google/protobuf/internal/text_format_test.py
@@ -35,15 +35,16 @@
__author__ = 'kenton@google.com (Kenton Varda)'
import re
+import unittest
+
+import six
-from google.apputils import basetest
from google.protobuf import text_format
-from google.protobuf.internal import api_implementation
from google.protobuf.internal import test_util
from google.protobuf import unittest_pb2
from google.protobuf import unittest_mset_pb2
-class TextFormatTest(basetest.TestCase):
+class TextFormatTest(unittest.TestCase):
def ReadGolden(self, golden_filename):
with test_util.GoldenFile(golden_filename) as f:
@@ -143,7 +144,7 @@ class TextFormatTest(basetest.TestCase):
'repeated_string: "\\303\\274\\352\\234\\237"\n')
def testPrintExoticUnicodeSubclass(self):
- class UnicodeSub(unicode):
+ class UnicodeSub(six.text_type):
pass
message = unittest_pb2.TestAllTypes()
message.repeated_string.append(UnicodeSub(u'\u00fc\ua71f'))
@@ -232,7 +233,7 @@ class TextFormatTest(basetest.TestCase):
parsed_message = unittest_pb2.TestAllTypes()
r = text_format.Parse(wire_text, parsed_message)
self.assertIs(r, parsed_message)
- self.assertEquals(message, parsed_message)
+ self.assertEqual(message, parsed_message)
# Test as_utf8 = True.
wire_text = text_format.MessageToString(
@@ -240,7 +241,7 @@ class TextFormatTest(basetest.TestCase):
parsed_message = unittest_pb2.TestAllTypes()
r = text_format.Parse(wire_text, parsed_message)
self.assertIs(r, parsed_message)
- self.assertEquals(message, parsed_message,
+ self.assertEqual(message, parsed_message,
'\n%s != %s' % (message, parsed_message))
def testPrintRawUtf8String(self):
@@ -250,7 +251,7 @@ class TextFormatTest(basetest.TestCase):
self.CompareToGoldenText(text, 'repeated_string: "\303\274\352\234\237"\n')
parsed_message = unittest_pb2.TestAllTypes()
text_format.Parse(text, parsed_message)
- self.assertEquals(message, parsed_message,
+ self.assertEqual(message, parsed_message,
'\n%s != %s' % (message, parsed_message))
def testPrintFloatFormat(self):
@@ -308,7 +309,7 @@ class TextFormatTest(basetest.TestCase):
message = unittest_pb2.TestAllTypes()
test_util.SetAllFields(message)
- self.assertEquals(message, parsed_message)
+ self.assertEqual(message, parsed_message)
def testParseGoldenExtensions(self):
golden_text = '\n'.join(self.ReadGolden(
@@ -318,7 +319,7 @@ class TextFormatTest(basetest.TestCase):
message = unittest_pb2.TestAllExtensions()
test_util.SetAllExtensions(message)
- self.assertEquals(message, parsed_message)
+ self.assertEqual(message, parsed_message)
def testParseAllFields(self):
message = unittest_pb2.TestAllTypes()
@@ -359,8 +360,8 @@ class TextFormatTest(basetest.TestCase):
text_format.Parse(text, message)
ext1 = unittest_mset_pb2.TestMessageSetExtension1.message_set_extension
ext2 = unittest_mset_pb2.TestMessageSetExtension2.message_set_extension
- self.assertEquals(23, message.message_set.Extensions[ext1].i)
- self.assertEquals('foo', message.message_set.Extensions[ext2].str)
+ self.assertEqual(23, message.message_set.Extensions[ext1].i)
+ self.assertEqual('foo', message.message_set.Extensions[ext2].str)
def testParseExotic(self):
message = unittest_pb2.TestAllTypes()
@@ -407,7 +408,7 @@ class TextFormatTest(basetest.TestCase):
message = unittest_pb2.TestAllTypes()
text = ''
text_format.Parse(text, message)
- self.assertEquals(unittest_pb2.TestAllTypes(), message)
+ self.assertEqual(unittest_pb2.TestAllTypes(), message)
def testParseInvalidUtf8(self):
message = unittest_pb2.TestAllTypes()
@@ -580,7 +581,7 @@ class TextFormatTest(basetest.TestCase):
message = unittest_pb2.TestAllTypes()
test_util.SetAllFields(message)
- self.assertEquals(message, parsed_message)
+ self.assertEqual(message, parsed_message)
def testMergeLinesGolden(self):
opened = self.ReadGolden('text_format_unittest_data.txt')
@@ -600,7 +601,7 @@ class TextFormatTest(basetest.TestCase):
self.assertEqual('oneof_uint32', m2.WhichOneof('oneof_field'))
-class TokenizerTest(basetest.TestCase):
+class TokenizerTest(unittest.TestCase):
def testSimpleTokenCases(self):
text = ('identifier1:"string1"\n \n\n'
@@ -745,4 +746,4 @@ class TokenizerTest(basetest.TestCase):
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/internal/type_checkers.py b/python/google/protobuf/internal/type_checkers.py
index 118725da..8d10fbe0 100755
--- a/python/google/protobuf/internal/type_checkers.py
+++ b/python/google/protobuf/internal/type_checkers.py
@@ -28,8 +28,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#PY25 compatible for GAE.
-#
# Copyright 2008 Google Inc. All Rights Reserved.
"""Provides type checking routines.
@@ -49,9 +47,8 @@ TYPE_TO_DESERIALIZE_METHOD: A dictionary with field types and deserialization
__author__ = 'robinson@google.com (Will Robinson)'
-import sys ##PY25
-if sys.version < '2.6': bytes = str ##PY25
-from google.protobuf.internal import api_implementation
+import six
+
from google.protobuf.internal import decoder
from google.protobuf.internal import encoder
from google.protobuf.internal import wire_format
@@ -111,9 +108,9 @@ class IntValueChecker(object):
"""Checker used for integer fields. Performs type-check and range check."""
def CheckValue(self, proposed_value):
- if not isinstance(proposed_value, (int, long)):
+ if not isinstance(proposed_value, six.integer_types):
message = ('%.1024r has type %s, but expected one of: %s' %
- (proposed_value, type(proposed_value), (int, long)))
+ (proposed_value, type(proposed_value), six.integer_types))
raise TypeError(message)
if not self._MIN <= proposed_value <= self._MAX:
raise ValueError('Value out of range: %d' % proposed_value)
@@ -132,9 +129,9 @@ class EnumValueChecker(object):
self._enum_type = enum_type
def CheckValue(self, proposed_value):
- if not isinstance(proposed_value, (int, long)):
+ if not isinstance(proposed_value, six.integer_types):
message = ('%.1024r has type %s, but expected one of: %s' %
- (proposed_value, type(proposed_value), (int, long)))
+ (proposed_value, type(proposed_value), six.integer_types))
raise TypeError(message)
if proposed_value not in self._enum_type.values_by_number:
raise ValueError('Unknown enum value: %d' % proposed_value)
@@ -149,9 +146,9 @@ class UnicodeValueChecker(object):
"""
def CheckValue(self, proposed_value):
- if not isinstance(proposed_value, (bytes, unicode)):
+ if not isinstance(proposed_value, (bytes, six.text_type)):
message = ('%.1024r has type %s, but expected one of: %s' %
- (proposed_value, type(proposed_value), (bytes, unicode)))
+ (proposed_value, type(proposed_value), (bytes, six.text_type)))
raise TypeError(message)
# If the value is of type 'bytes' make sure that it is valid UTF-8 data.
@@ -183,13 +180,13 @@ class Uint32ValueChecker(IntValueChecker):
class Int64ValueChecker(IntValueChecker):
_MIN = -(1 << 63)
_MAX = (1 << 63) - 1
- _TYPE = long
+ _TYPE = int
class Uint64ValueChecker(IntValueChecker):
_MIN = 0
_MAX = (1 << 64) - 1
- _TYPE = long
+ _TYPE = int
# Type-checkers for all scalar CPPTYPEs.
@@ -199,9 +196,9 @@ _VALUE_CHECKERS = {
_FieldDescriptor.CPPTYPE_UINT32: Uint32ValueChecker(),
_FieldDescriptor.CPPTYPE_UINT64: Uint64ValueChecker(),
_FieldDescriptor.CPPTYPE_DOUBLE: TypeChecker(
- float, int, long),
+ float, int, int),
_FieldDescriptor.CPPTYPE_FLOAT: TypeChecker(
- float, int, long),
+ float, int, int),
_FieldDescriptor.CPPTYPE_BOOL: TypeChecker(bool, int),
_FieldDescriptor.CPPTYPE_STRING: TypeChecker(bytes),
}
diff --git a/python/google/protobuf/internal/unknown_fields_test.py b/python/google/protobuf/internal/unknown_fields_test.py
index a4dc1f7c..e405f113 100755
--- a/python/google/protobuf/internal/unknown_fields_test.py
+++ b/python/google/protobuf/internal/unknown_fields_test.py
@@ -35,7 +35,19 @@
__author__ = 'bohdank@google.com (Bohdan Koval)'
-from google.apputils import basetest
+import unittest
+try:
+ from unittest import skipIf
+except ImportError:
+ def skipIf(predicate, message):
+ def decorator(wrapped):
+ if predicate:
+ def _noop(*args, **kw):
+ pass
+ return _noop
+ return wrapped
+ return decorator
+
from google.protobuf import unittest_mset_pb2
from google.protobuf import unittest_pb2
from google.protobuf.internal import api_implementation
@@ -45,10 +57,10 @@ from google.protobuf.internal import test_util
from google.protobuf.internal import type_checkers
-@basetest.unittest.skipIf(
+@skipIf(
api_implementation.Type() == 'cpp' and api_implementation.Version() == 2,
'C++ implementation does not expose unknown fields to Python')
-class UnknownFieldsTest(basetest.TestCase):
+class UnknownFieldsTest(unittest.TestCase):
def setUp(self):
self.descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR
@@ -179,10 +191,10 @@ class UnknownFieldsTest(basetest.TestCase):
self.assertNotEqual(self.empty_message, message)
-@basetest.unittest.skipIf(
+@skipIf(
api_implementation.Type() == 'cpp' and api_implementation.Version() == 2,
'C++ implementation does not expose unknown fields to Python')
-class UnknownEnumValuesTest(basetest.TestCase):
+class UnknownEnumValuesTest(unittest.TestCase):
def setUp(self):
self.descriptor = missing_enum_values_pb2.TestEnumValues.DESCRIPTOR
@@ -235,4 +247,4 @@ class UnknownEnumValuesTest(basetest.TestCase):
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/internal/wire_format_test.py b/python/google/protobuf/internal/wire_format_test.py
index f39035ca..e40a40cc 100755
--- a/python/google/protobuf/internal/wire_format_test.py
+++ b/python/google/protobuf/internal/wire_format_test.py
@@ -34,12 +34,13 @@
__author__ = 'robinson@google.com (Will Robinson)'
-from google.apputils import basetest
+import unittest
+
from google.protobuf import message
from google.protobuf.internal import wire_format
-class WireFormatTest(basetest.TestCase):
+class WireFormatTest(unittest.TestCase):
def testPackTag(self):
field_number = 0xabc
@@ -250,4 +251,4 @@ class WireFormatTest(basetest.TestCase):
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/message_factory.py b/python/google/protobuf/message_factory.py
index 7fd7bec0..36062a56 100644
--- a/python/google/protobuf/message_factory.py
+++ b/python/google/protobuf/message_factory.py
@@ -28,8 +28,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#PY25 compatible for GAE.
-#
# Copyright 2012 Google Inc. All Rights Reserved.
"""Provides a factory class for generating dynamic messages.
@@ -43,7 +41,6 @@ my_proto_instance = message_classes['some.proto.package.MessageName']()
__author__ = 'matthewtoia@google.com (Matt Toia)'
-import sys ##PY25
from google.protobuf import descriptor_database
from google.protobuf import descriptor_pool
from google.protobuf import message
@@ -75,8 +72,7 @@ class MessageFactory(object):
"""
if descriptor.full_name not in self._classes:
descriptor_name = descriptor.name
- if sys.version_info[0] < 3: ##PY25
-##!PY25 if str is bytes: # PY2
+ if str is bytes: # PY2
descriptor_name = descriptor.name.encode('ascii', 'ignore')
result_class = reflection.GeneratedProtocolMessageType(
descriptor_name,
@@ -111,7 +107,7 @@ class MessageFactory(object):
result = {}
for file_name in files:
file_desc = self.pool.FindFileByName(file_name)
- for name, msg in file_desc.message_types_by_name.iteritems():
+ for name, msg in file_desc.message_types_by_name.items():
if file_desc.package:
full_name = '.'.join([file_desc.package, name])
else:
@@ -128,7 +124,7 @@ class MessageFactory(object):
# ignore the registration if the original was the same, or raise
# an error if they were different.
- for name, extension in file_desc.extensions_by_name.iteritems():
+ for name, extension in file_desc.extensions_by_name.items():
if extension.containing_type.full_name not in self._classes:
self.GetPrototype(extension.containing_type)
extended_class = self._classes[extension.containing_type.full_name]
diff --git a/python/google/protobuf/pyext/descriptor_cpp2_test.py b/python/google/protobuf/pyext/descriptor_cpp2_test.py
index 3cf45a22..1eb3663b 100644
--- a/python/google/protobuf/pyext/descriptor_cpp2_test.py
+++ b/python/google/protobuf/pyext/descriptor_cpp2_test.py
@@ -35,19 +35,19 @@
__author__ = 'anuraag@google.com (Anuraag Agrawal)'
import os
+import unittest
os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp'
os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION'] = '2'
# We must set the implementation version above before the google3 imports.
# pylint: disable=g-import-not-at-top
-from google.apputils import basetest
from google.protobuf.internal import api_implementation
# Run all tests from the original module by putting them in our namespace.
# pylint: disable=wildcard-import
from google.protobuf.internal.descriptor_test import *
-class ConfirmCppApi2Test(basetest.TestCase):
+class ConfirmCppApi2Test(unittest.TestCase):
def testImplementationSetting(self):
self.assertEqual('cpp', api_implementation.Type())
@@ -55,4 +55,4 @@ class ConfirmCppApi2Test(basetest.TestCase):
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/pyext/message_factory_cpp2_test.py b/python/google/protobuf/pyext/message_factory_cpp2_test.py
index 32ab4f85..ce4ae861 100644
--- a/python/google/protobuf/pyext/message_factory_cpp2_test.py
+++ b/python/google/protobuf/pyext/message_factory_cpp2_test.py
@@ -33,19 +33,19 @@
"""Tests for google.protobuf.message_factory."""
import os
+import unittest
os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp'
os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION'] = '2'
# We must set the implementation version above before the google3 imports.
# pylint: disable=g-import-not-at-top
-from google.apputils import basetest
from google.protobuf.internal import api_implementation
# Run all tests from the original module by putting them in our namespace.
# pylint: disable=wildcard-import
from google.protobuf.internal.message_factory_test import *
-class ConfirmCppApi2Test(basetest.TestCase):
+class ConfirmCppApi2Test(unittest.TestCase):
def testImplementationSetting(self):
self.assertEqual('cpp', api_implementation.Type())
@@ -53,4 +53,4 @@ class ConfirmCppApi2Test(basetest.TestCase):
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/pyext/reflection_cpp2_generated_test.py b/python/google/protobuf/pyext/reflection_cpp2_generated_test.py
index 552efd48..b1a23051 100755
--- a/python/google/protobuf/pyext/reflection_cpp2_generated_test.py
+++ b/python/google/protobuf/pyext/reflection_cpp2_generated_test.py
@@ -36,17 +36,17 @@
__author__ = 'jasonh@google.com (Jason Hsueh)'
import os
+import unittest
os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp'
os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION'] = '2'
-from google.apputils import basetest
from google.protobuf.internal import api_implementation
from google.protobuf.internal import more_extensions_dynamic_pb2
from google.protobuf.internal import more_extensions_pb2
from google.protobuf.internal.reflection_test import *
-class ReflectionCppTest(basetest.TestCase):
+class ReflectionCppTest(unittest.TestCase):
def testImplementationSetting(self):
self.assertEqual('cpp', api_implementation.Type())
self.assertEqual(2, api_implementation.Version())
@@ -91,4 +91,4 @@ class ReflectionCppTest(basetest.TestCase):
if __name__ == '__main__':
- basetest.main()
+ unittest.main()
diff --git a/python/google/protobuf/text_encoding.py b/python/google/protobuf/text_encoding.py
index 2d86a67c..a0728e3c 100644
--- a/python/google/protobuf/text_encoding.py
+++ b/python/google/protobuf/text_encoding.py
@@ -27,16 +27,13 @@
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-#PY25 compatible for GAE.
-#
"""Encoding related utilities."""
-
import re
-import sys ##PY25
+
+import six
# Lookup table for utf8
-_cescape_utf8_to_str = [chr(i) for i in xrange(0, 256)]
+_cescape_utf8_to_str = [chr(i) for i in range(0, 256)]
_cescape_utf8_to_str[9] = r'\t' # optional escape
_cescape_utf8_to_str[10] = r'\n' # optional escape
_cescape_utf8_to_str[13] = r'\r' # optional escape
@@ -46,9 +43,9 @@ _cescape_utf8_to_str[34] = r'\"' # necessary escape
_cescape_utf8_to_str[92] = r'\\' # necessary escape
# Lookup table for non-utf8, with necessary escapes at (o >= 127 or o < 32)
-_cescape_byte_to_str = ([r'\%03o' % i for i in xrange(0, 32)] +
- [chr(i) for i in xrange(32, 127)] +
- [r'\%03o' % i for i in xrange(127, 256)])
+_cescape_byte_to_str = ([r'\%03o' % i for i in range(0, 32)] +
+ [chr(i) for i in range(32, 127)] +
+ [r'\%03o' % i for i in range(127, 256)])
_cescape_byte_to_str[9] = r'\t' # optional escape
_cescape_byte_to_str[10] = r'\n' # optional escape
_cescape_byte_to_str[13] = r'\r' # optional escape
@@ -75,7 +72,7 @@ def CEscape(text, as_utf8):
"""
# PY3 hack: make Ord work for str and bytes:
# //platforms/networking/data uses unicode here, hence basestring.
- Ord = ord if isinstance(text, basestring) else lambda x: x
+ Ord = ord if isinstance(text, six.string_types) else lambda x: x
if as_utf8:
return ''.join(_cescape_utf8_to_str[Ord(c)] for c in text)
return ''.join(_cescape_byte_to_str[Ord(c)] for c in text)
@@ -100,8 +97,7 @@ def CUnescape(text):
# allow single-digit hex escapes (like '\xf').
result = _CUNESCAPE_HEX.sub(ReplaceHex, text)
- if sys.version_info[0] < 3: ##PY25
-##!PY25 if str is bytes: # PY2
+ if str is bytes: # PY2
return result.decode('string_escape')
result = ''.join(_cescape_highbit_to_str[ord(c)] for c in result)
return (result.encode('ascii') # Make it bytes to allow decode.
diff --git a/python/google/protobuf/text_format.py b/python/google/protobuf/text_format.py
index fb54c50c..c50930ef 100755
--- a/python/google/protobuf/text_format.py
+++ b/python/google/protobuf/text_format.py
@@ -28,17 +28,17 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#PY25 compatible for GAE.
-#
# Copyright 2007 Google Inc. All Rights Reserved.
"""Contains routines for printing protocol messages in text format."""
__author__ = 'kenton@google.com (Kenton Varda)'
-import cStringIO
+import io
import re
+import six
+
from google.protobuf.internal import type_checkers
from google.protobuf import descriptor
from google.protobuf import text_encoding
@@ -89,7 +89,7 @@ def MessageToString(message, as_utf8=False, as_one_line=False,
Returns:
A string of the text formatted protocol buffer message.
"""
- out = cStringIO.StringIO()
+ out = io.BytesIO()
PrintMessage(message, out, as_utf8=as_utf8, as_one_line=as_one_line,
pointy_brackets=pointy_brackets,
use_index_order=use_index_order,
@@ -195,7 +195,7 @@ def PrintFieldValue(field, value, out, indent=0, as_utf8=False,
out.write(str(value))
elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING:
out.write('\"')
- if isinstance(value, unicode):
+ if isinstance(value, six.text_type):
out_value = value.encode('utf-8')
else:
out_value = value
@@ -505,7 +505,7 @@ class _Tokenizer(object):
def _PopLine(self):
while len(self._current_line) <= self._column:
try:
- self._current_line = self._lines.next()
+ self._current_line = next(self._lines)
except StopIteration:
self._current_line = ''
self._more_lines = False
@@ -575,7 +575,7 @@ class _Tokenizer(object):
"""
try:
result = ParseInteger(self.token, is_signed=True, is_long=False)
- except ValueError, e:
+ except ValueError as e:
raise self._ParseError(str(e))
self.NextToken()
return result
@@ -591,7 +591,7 @@ class _Tokenizer(object):
"""
try:
result = ParseInteger(self.token, is_signed=False, is_long=False)
- except ValueError, e:
+ except ValueError as e:
raise self._ParseError(str(e))
self.NextToken()
return result
@@ -607,7 +607,7 @@ class _Tokenizer(object):
"""
try:
result = ParseInteger(self.token, is_signed=True, is_long=True)
- except ValueError, e:
+ except ValueError as e:
raise self._ParseError(str(e))
self.NextToken()
return result
@@ -623,7 +623,7 @@ class _Tokenizer(object):
"""
try:
result = ParseInteger(self.token, is_signed=False, is_long=True)
- except ValueError, e:
+ except ValueError as e:
raise self._ParseError(str(e))
self.NextToken()
return result
@@ -639,7 +639,7 @@ class _Tokenizer(object):
"""
try:
result = ParseFloat(self.token)
- except ValueError, e:
+ except ValueError as e:
raise self._ParseError(str(e))
self.NextToken()
return result
@@ -655,7 +655,7 @@ class _Tokenizer(object):
"""
try:
result = ParseBool(self.token)
- except ValueError, e:
+ except ValueError as e:
raise self._ParseError(str(e))
self.NextToken()
return result
@@ -671,8 +671,8 @@ class _Tokenizer(object):
"""
the_bytes = self.ConsumeByteString()
try:
- return unicode(the_bytes, 'utf-8')
- except UnicodeDecodeError, e:
+ return six.text_type(the_bytes, 'utf-8')
+ except UnicodeDecodeError as e:
raise self._StringParseError(e)
def ConsumeByteString(self):
@@ -687,8 +687,7 @@ class _Tokenizer(object):
the_list = [self._ConsumeSingleByteString()]
while self.token and self.token[0] in ('\'', '"'):
the_list.append(self._ConsumeSingleByteString())
- return ''.encode('latin1').join(the_list) ##PY25
-##!PY25 return b''.join(the_list)
+ return b''.join(the_list)
def _ConsumeSingleByteString(self):
"""Consume one token of a string literal.
@@ -706,7 +705,7 @@ class _Tokenizer(object):
try:
result = text_encoding.CUnescape(text[1:-1])
- except ValueError, e:
+ except ValueError as e:
raise self._ParseError(str(e))
self.NextToken()
return result
@@ -714,7 +713,7 @@ class _Tokenizer(object):
def ConsumeEnum(self, field):
try:
result = ParseEnum(field, self.token)
- except ValueError, e:
+ except ValueError as e:
raise self._ParseError(str(e))
self.NextToken()
return result
@@ -779,7 +778,7 @@ def ParseInteger(text, is_signed=False, is_long=False):
# alternate implementations where the distinction is more significant
# (e.g. the C++ implementation) simpler.
if is_long:
- result = long(text, 0)
+ result = int(text, 0)
else:
result = int(text, 0)
except ValueError:
diff --git a/python/setup.py b/python/setup.py
index 61ae8eac..3739dc45 100755
--- a/python/setup.py
+++ b/python/setup.py
@@ -135,13 +135,6 @@ class build_py(_build_py):
pass
# _build_py is an old-style class, so super() doesn't work.
_build_py.run(self)
- # TODO(mrovner): Subclass to run 2to3 on some files only.
- # Tracing what https://wiki.python.org/moin/PortingPythonToPy3k's "Approach 2"
- # section on how to get 2to3 to run on source files during install under
- # Python 3. This class seems like a good place to put logic that calls
- # python3's distutils.util.run_2to3 on the subset of the files we have in our
- # release that are subject to conversion.
- # See code reference in previous code review.
if __name__ == '__main__':
ext_module_list = []
@@ -184,11 +177,6 @@ if __name__ == '__main__':
test_suite = 'setup.MakeTestSuite',
google_test_dir = "google/protobuf/internal",
cmdclass = { 'clean': clean, 'build_py': build_py },
- install_requires = ['setuptools'],
- # TODO: Restore dependency once a Python 3 compatible google-apputils
- # is released.
- setup_requires = (['google-apputils']
- if sys.version_info[0] < 3 else
- []),
+ install_requires = ['setuptools', 'six'],
ext_modules = ext_module_list,
)
diff --git a/python/tox.ini b/python/tox.ini
new file mode 100644
index 00000000..92e7b105
--- /dev/null
+++ b/python/tox.ini
@@ -0,0 +1,13 @@
+[tox]
+envlist =
+ py26,py27,py33,py34
+
+[testenv]
+usedevelop = true
+setenv =
+ LD_LIBRARY_PATH={toxinidir}/../src/.libs
+commands =
+ python setup.py -q --cpp_implementation develop
+ python setup.py -q test -q
+deps =
+ six