Skip to content

Commit

Permalink
Enable Boto workaround only on Python 3.x (issue paylogic#68)
Browse files Browse the repository at this point in the history
Refer to issue 68 on GitHub for details:
  paylogic#68
  • Loading branch information
xolox committed Mar 4, 2016
1 parent 99dd661 commit 4a7cbfa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
19 changes: 13 additions & 6 deletions pip_accel/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Accelerator for pip, the Python package manager.
#
# Author: Peter Odding <[email protected]>
# Last Change: February 19, 2016
# Last Change: March 4, 2016
# URL: https://github.com/paylogic/pip-accel
#
# TODO Permanently store logs in the pip-accel directory (think about log rotation).
Expand Down Expand Up @@ -77,7 +77,7 @@
from pip.req import InstallRequirement

# Semi-standard module versioning.
__version__ = '0.42.4'
__version__ = '0.42.5'

# Initialize a logger for this module.
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -781,27 +781,34 @@ class PatchedAttribute(object):
exited.
"""

def __init__(self, object, attribute, value):
def __init__(self, object, attribute, value, enabled=True):
"""
Initialize a :class:`PatchedAttribute` object.
:param object: The object whose attribute should be patched.
:param attribute: The name of the attribute to be patched (a string).
:param value: The temporary value for the attribute.
:param enabled: :data:`True` to patch the attribute, :data:`False` to
do nothing instead. This enables conditional attribute
patching while unconditionally using the
:keyword:`with` statement.
"""
self.object = object
self.attribute = attribute
self.patched_value = value
self.original_value = None
self.enabled = enabled

def __enter__(self):
"""Change the object attribute when entering the context."""
self.original_value = getattr(self.object, self.attribute)
setattr(self.object, self.attribute, self.patched_value)
if self.enabled:
self.original_value = getattr(self.object, self.attribute)
setattr(self.object, self.attribute, self.patched_value)

def __exit__(self, exc_type=None, exc_value=None, traceback=None):
"""Restore the object attribute when leaving the context."""
setattr(self.object, self.attribute, self.original_value)
if self.enabled:
setattr(self.object, self.attribute, self.original_value)


class AttributeOverrides(object):
Expand Down
5 changes: 3 additions & 2 deletions pip_accel/caches/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Authors:
# - Adam Feuer <[email protected]>
# - Peter Odding <[email protected]>
# Last Change: November 8, 2015
# Last Change: March 4, 2016
# URL: https://github.com/paylogic/pip-accel
#
# A word of warning: Do *not* use the cached_property decorator here, because
Expand Down Expand Up @@ -99,7 +99,7 @@
# Modules included in our package.
from pip_accel import PatchedAttribute
from pip_accel.caches import AbstractCacheBackend
from pip_accel.compat import urlparse
from pip_accel.compat import PY3, urlparse
from pip_accel.exceptions import CacheBackendDisabledError, CacheBackendError
from pip_accel.utils import AtomicReplace, makedirs

Expand Down Expand Up @@ -361,6 +361,7 @@ def __init__(self):
object=Config,
attribute='get',
value=self.get,
enabled=PY3,
)

def get(self, section, name, default=None, **kw):
Expand Down
4 changes: 3 additions & 1 deletion pip_accel/compat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Accelerator for pip, the Python package manager.
#
# Author: Peter Odding <[email protected]>
# Last Change: November 8, 2015
# Last Change: March 4, 2016
# URL: https://github.com/paylogic/pip-accel

"""Operating system detection and Python version compatibility."""
Expand Down Expand Up @@ -31,10 +31,12 @@
from StringIO import StringIO
from urllib import pathname2url
from urlparse import urljoin, urlparse
PY3 = False
except (ImportError, NameError):
# Python 3.
basestring = str
import configparser
from io import StringIO
from urllib.parse import urljoin, urlparse
from urllib.request import pathname2url
PY3 = True

0 comments on commit 4a7cbfa

Please sign in to comment.