[PATCH 2 of 3] py3: condiionalize thread import

Martijn Pieters mj at zopatista.com
Fri Jul 1 09:35:45 EDT 2016


I much rather see keepalive.py updated to not use the `thread` module. The line

  self._lock = thread.allocate_lock()

can safely be replaced by

  self._lock = threading.Lock()

as `threading.Lock` is basically an alias for `thread.allocate_lock`:

  Python 3.5.0 (default, Oct 16 2015, 13:29:53)
  [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import _thread, threading
  >>> threading.Lock is _thread.allocate_lock
  True



On 30 June 2016 at 10:47, Pulkit Goyal <7895pulkit at gmail.com> wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit at gmail.com>
> # Date 1467113601 -19800
> #      Tue Jun 28 17:03:21 2016 +0530
> # Node ID 91320eb3d9ce90433ec843525e5563dbad5193e2
> # Parent  752f11e6e60f6214335dd761e6bb045d63747bbb
> py3: condiionalize thread import
>
> The thread module is renamed to _thread in python 3. Now we import util.thread and
>  hence a test is added in check-code too.
>
> diff -r 752f11e6e60f -r 91320eb3d9ce contrib/check-code.py
> --- a/contrib/check-code.py     Tue Jun 28 16:01:53 2016 +0530
> +++ b/contrib/check-code.py     Tue Jun 28 17:03:21 2016 +0530
> @@ -331,6 +331,7 @@
>      (r'^import cPickle', "don't use cPickle, use util.pickle"),
>      (r'^import pickle', "don't use pickle, use util.pickle"),
>      (r'^import httplib', "don't use httplib, use util.httplib"),
> +    (r'^import thread\n', "don't use thread, use util.thread"),
>      (r'\.next\(\)', "don't use .next(), use next(...)"),
>
>      # rules depending on implementation of repquote()
> diff -r 752f11e6e60f -r 91320eb3d9ce mercurial/keepalive.py
> --- a/mercurial/keepalive.py    Tue Jun 28 16:01:53 2016 +0530
> +++ b/mercurial/keepalive.py    Tue Jun 28 17:03:21 2016 +0530
> @@ -113,12 +113,12 @@
>  import hashlib
>  import socket
>  import sys
> -import thread
>
>  from . import (
>      util,
>  )
>
> +thread = util.thread
>  httplib = util.httplib
>  urlerr = util.urlerr
>  urlreq = util.urlreq
> diff -r 752f11e6e60f -r 91320eb3d9ce mercurial/pycompat.py
> --- a/mercurial/pycompat.py     Tue Jun 28 16:01:53 2016 +0530
> +++ b/mercurial/pycompat.py     Tue Jun 28 17:03:21 2016 +0530
> @@ -18,6 +18,13 @@
>      pickle.dumps # silence pyflakes
>
>  try:
> +    import thread
> +    thread.LockType
> +except ImportError:
> +    import _thread as thread
> +    thread.LockType
> +
> +try:
>      import httplib
>      httplib.HTTPException
>  except ImportError:
> diff -r 752f11e6e60f -r 91320eb3d9ce mercurial/util.py
> --- a/mercurial/util.py Tue Jun 28 16:01:53 2016 +0530
> +++ b/mercurial/util.py Tue Jun 28 17:03:21 2016 +0530
> @@ -56,6 +56,7 @@
>      #'urlreq',
>      'stringio',
>      'socketserver',
> +    'thread',
>      'xmlrpclib',
>  ):
>      globals()[attr] = getattr(pycompat, attr)
> diff -r 752f11e6e60f -r 91320eb3d9ce tests/test-check-py3-compat.t
> --- a/tests/test-check-py3-compat.t     Tue Jun 28 16:01:53 2016 +0530
> +++ b/tests/test-check-py3-compat.t     Tue Jun 28 17:03:21 2016 +0530
> @@ -115,7 +115,6 @@
>    mercurial/hook.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
>    mercurial/httpconnection.py: error importing: <ImportError> No module named 'rfc822' (error at __init__.py:*) (glob)
>    mercurial/httppeer.py: error importing module: <SyntaxError> invalid syntax (bundle2.py, line *) (line *) (glob)
> -  mercurial/keepalive.py: error importing module: <ImportError> No module named 'thread' (line *) (glob)
>    mercurial/localrepo.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
>    mercurial/mail.py: error importing module: <AttributeError> module 'email' has no attribute 'Header' (line *) (glob)
>    mercurial/manifest.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel



-- 
Martijn Pieters


More information about the Mercurial-devel mailing list