Ori.livneh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/74925


Change subject: Work around circular ref in Python stdlib's 'urlopen'
......................................................................

Work around circular ref in Python stdlib's 'urlopen'

The Python standard library's `urlopen` function (`urllib2.urlopen` in Python
2, and `urllib.request.urlopen` in Python 3) has a circular reference problem.
This patch provides a simple helper method, 'http_get', which fetches text from
a URI and which works around this issue. Verified with gc.DEBUG_LEAK.

Change-Id: I9961c7a3769e0827ad5d83eaa10b1911182b2904
---
M server/eventlogging/compat.py
M server/eventlogging/schema.py
2 files changed, 17 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/EventLogging 
refs/changes/25/74925/1

diff --git a/server/eventlogging/compat.py b/server/eventlogging/compat.py
index a0b772d..84ee6ed 100644
--- a/server/eventlogging/compat.py
+++ b/server/eventlogging/compat.py
@@ -49,6 +49,21 @@
     return unquote(bytes).decode('utf-8')
 
 
+def http_get(url):
+    """Simple wrapper around the standard library's `urlopen` function which
+    works around a circular ref. See <http://bugs.python.org/issue1208304>.
+    """
+    req = None
+    try:
+        req = urlopen(url)
+        return req.read().decode('utf-8')
+    finally:
+        if req is not None:
+            if hasattr(req, 'fp') and hasattr(req.fp, '_sock'):
+                req.fp._sock.recv = None
+            req.close()
+
+
 @functools.wraps(uuid.uuid5)
 def uuid5(namespace, name):
     """Generate UUID5 for `name` in `namespace`."""
diff --git a/server/eventlogging/schema.py b/server/eventlogging/schema.py
index 8436fb5..7b414c4 100644
--- a/server/eventlogging/schema.py
+++ b/server/eventlogging/schema.py
@@ -13,7 +13,7 @@
 
 import jsonschema
 
-from .compat import json, urlopen
+from .compat import json, http_get
 
 __all__ = ('CAPSULE_SCID', 'get_schema', 'SCHEMA_URL_FORMAT', 'validate')
 
@@ -53,8 +53,7 @@
     """Retrieve schema via HTTP."""
     url = SCHEMA_URL_FORMAT % scid
     try:
-        content = urlopen(url).read().decode('utf-8')
-        schema = json.loads(content)
+        schema = json.loads(http_get(url))
     except (ValueError, EnvironmentError) as ex:
         raise jsonschema.SchemaError('Schema fetch failure: %s' % ex)
     jsonschema.Draft3Validator.check_schema(schema)

-- 
To view, visit https://gerrit.wikimedia.org/r/74925
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9961c7a3769e0827ad5d83eaa10b1911182b2904
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/EventLogging
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <o...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to