Mercurial > public > mercurial-scm > hg
comparison mercurial/hgweb/common.py @ 34643:f42dec9c976e
hgweb: do not import uuid immediately to avoid its side effect
With hgdemandimport disabled (chg's case), `import uuid` has an immediate
side effect calling `ctypes.util.find_library` trying to locate the
`libuuid` library. This happens at `import` time before `dispatch.run()`.
The call trace is like:
File "hg/hg", line 54, in <module>
from mercurial import (
File "hg/mercurial/dispatch.py", line 24, in <module>
from . import (
File "hg/mercurial/commands.py", line 23, in <module>
from . import (
File "hg/mercurial/help.py", line 33, in <module>
from .hgweb import (
File "hg/mercurial/hgweb/__init__.py", line 20, in <module>
from . import (
File "hg/mercurial/hgweb/hgweb_mod.py", line 14, in <module>
from .common import (
File "hg/mercurial/hgweb/common.py", line 15, in <module>
import uuid
File "/usr/lib64/python2.7/uuid.py", line 404, in <module>
lib = ctypes.CDLL(ctypes.util.find_library(libname))
The problem is, `ctypes.util.find_library` will execute
`sh -c '/sbin/ldconfig -p 2>/dev/null'` on Python <= 2.7.12. The output of
`sh` may pollute the terminal:
shell-init: error retrieving current directory: getcwd: cannot access
parent directories: No such file or directory
This patch moves `import uuid` so its side-effect can only happen after the
cwd check in `dispatch._getlocal`. Therefore the terminal won't be
polluted by importing `uuid`.
Differential Revision: https://phab.mercurial-scm.org/D1024
author | Jun Wu <quark@fb.com> |
---|---|
date | Wed, 11 Oct 2017 21:24:32 -0700 |
parents | f28c85e29afc |
children | baee5512f262 |
comparison
equal
deleted
inserted
replaced
34642:a679aa582d8d | 34643:f42dec9c976e |
---|---|
10 | 10 |
11 import base64 | 11 import base64 |
12 import errno | 12 import errno |
13 import mimetypes | 13 import mimetypes |
14 import os | 14 import os |
15 import uuid | |
16 | 15 |
17 from .. import ( | 16 from .. import ( |
18 encoding, | 17 encoding, |
19 pycompat, | 18 pycompat, |
20 util, | 19 util, |
219 Returns a 2-tuple of the CSP header value and the nonce value. | 218 Returns a 2-tuple of the CSP header value and the nonce value. |
220 | 219 |
221 First value is ``None`` if CSP isn't enabled. Second value is ``None`` | 220 First value is ``None`` if CSP isn't enabled. Second value is ``None`` |
222 if CSP isn't enabled or if the CSP header doesn't need a nonce. | 221 if CSP isn't enabled or if the CSP header doesn't need a nonce. |
223 """ | 222 """ |
223 # Without demandimport, "import uuid" could have an immediate side-effect | |
224 # running "ldconfig" on Linux trying to find libuuid. | |
225 # With Python <= 2.7.12, that "ldconfig" is run via a shell and the shell | |
226 # may pollute the terminal with: | |
227 # | |
228 # shell-init: error retrieving current directory: getcwd: cannot access | |
229 # parent directories: No such file or directory | |
230 # | |
231 # Python >= 2.7.13 has fixed it by running "ldconfig" directly without a | |
232 # shell (hg changeset a09ae70f3489). | |
233 # | |
234 # Moved "import uuid" from here so it's executed after we know we have | |
235 # a sane cwd (i.e. after dispatch.py cwd check). | |
236 # | |
237 # We can move it back once we no longer need Python <= 2.7.12 support. | |
238 import uuid | |
239 | |
224 # Don't allow untrusted CSP setting since it be disable protections | 240 # Don't allow untrusted CSP setting since it be disable protections |
225 # from a trusted/global source. | 241 # from a trusted/global source. |
226 csp = ui.config('web', 'csp', untrusted=False) | 242 csp = ui.config('web', 'csp', untrusted=False) |
227 nonce = None | 243 nonce = None |
228 | 244 |