Coverage for adhoc-cicd-odoo-odoo / odoo / _monkeypatches / site.py: 47%
37 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-09 18:15 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-09 18:15 +0000
1"""Patcher for any change not strictly related to an stdlib module
3"""
5import codecs
6import encodings.aliases
7import re
8import sys
10import babel.core
12import odoo
15def patch_module():
16 patch_evented()
17 patch_codecs()
20odoo.evented = False
23def patch_evented():
24 """Running mode flags (gevent, prefork)
26 This should be executed early. It will initialize the `odoo.evented` variable.
27 """
28 if odoo.evented or not (len(sys.argv) > 1 and sys.argv[1] == 'gevent'): 28 ↛ 30line 28 didn't jump to line 30 because the condition on line 28 was always true
29 return
30 sys.argv.remove('gevent')
31 import gevent.monkey # noqa: PLC0415
32 import psycopg2 # noqa: PLC0415
33 from gevent.socket import wait_read, wait_write # noqa: PLC0415
34 gevent.monkey.patch_all()
36 def gevent_wait_callback(conn, timeout=None):
37 """A wait callback useful to allow gevent to work with Psycopg."""
38 # Copyright (C) 2010-2012 Daniele Varrazzo <daniele.varrazzo@gmail.com>
39 # This function is borrowed from psycogreen module which is licensed
40 # under the BSD license (see in odoo/debian/copyright)
41 while 1:
42 state = conn.poll()
43 if state == psycopg2.extensions.POLL_OK:
44 break
45 elif state == psycopg2.extensions.POLL_READ:
46 wait_read(conn.fileno(), timeout=timeout)
47 elif state == psycopg2.extensions.POLL_WRITE:
48 wait_write(conn.fileno(), timeout=timeout)
49 else:
50 raise psycopg2.OperationalError(
51 "Bad result from poll: %r" % state)
53 psycopg2.extensions.set_wait_callback(gevent_wait_callback)
54 odoo.evented = True
57def patch_codecs():
58 # ---------------------------------------------------------
59 # some charset are known by Python under a different name
60 # ---------------------------------------------------------
62 encodings.aliases.aliases['874'] = 'cp874'
63 encodings.aliases.aliases['windows_874'] = 'cp874'
65 # ---------------------------------------------------------
66 # alias hebrew iso-8859-8-i and iso-8859-8-e on iso-8859-8
67 # https://bugs.python.org/issue18624
68 # ---------------------------------------------------------
70 iso8859_8 = codecs.lookup('iso8859_8')
71 iso8859_8ie_re = re.compile(r'iso[-_]?8859[-_]8[-_]?[ei]', re.IGNORECASE)
72 codecs.register(lambda charset: iso8859_8 if iso8859_8ie_re.match(charset) else None)
74 # To remove when corrected in Babel
75 babel.core.LOCALE_ALIASES['nb'] = 'nb_NO'