Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
1.0 (unreleased)
================

- Move 'substanced.locking.now' to 'substanced.util.now', and rework to avoid
warning spew for 'datetime.datetime.utcnow'.

- Suppress other warning spew from upstreams in newer Python versions
('chameleon' / AST, 'pkg_resources', etc.)


1.0b1 (2024-11-27)
==================

Expand Down Expand Up @@ -26,7 +36,7 @@
- Override ``serialize`` for ``ReferenceIdSchemaNode``.
See https://github.com/Pylons/substanced/pull/311

- Get all tests running under ``py.test`` against head of Pyramid's
- Get all tests running under ``py.test`` against head of Pyramid's
``1.10`` branch. See https://github.com/Pylons/substanced/pull/309

- Add support for Python 3.6 and 3.7; drop support for Python 3.4.
Expand Down Expand Up @@ -111,7 +121,7 @@
the change.

- Removed Python 2.6 compatibility shim ``substanced._compat.total_ordering``.

1.0a1 (2015-04-17)
==================

Expand Down
12 changes: 5 additions & 7 deletions demos/blog/blog/views/retail.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
from pyramid.decorator import reify
from pyramid.httpexceptions import HTTPFound
from pyramid.url import resource_url
from substanced.util import find_catalog
from substanced.util import (
find_catalog,
now,
)
from pyramid.view import (
view_config,
view_defaults,
Expand Down Expand Up @@ -122,11 +125,6 @@ def __init__(self, context, request):
self.context = context
self.request = request

def _nowtz(self):
now = datetime.datetime.utcnow() # naive
y, mo, d, h, mi, s = now.timetuple()[:6]
return datetime.datetime(y, mo, d, h, mi, s, tzinfo=pytz.utc)

def _get_feed_info(self):
context = self.context
request = self.request
Expand Down Expand Up @@ -160,7 +158,7 @@ def _add_updated_strings(updated, info):

blogentries.sort(key=lambda x: x[0].isoformat())
blogentries = [entry[1] for entry in reversed(blogentries)][:15]
updated = blogentries and blogentries[0]['pubdate'] or self._nowtz()
updated = blogentries and blogentries[0]['pubdate'] or now()
_add_updated_strings(updated, feed)

return feed, blogentries
Expand Down
4 changes: 4 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ addopts = -l --strict
norecursedirs = lib include .tox .git
python_files = test_*.py tests.py
filterwarnings =
ignore::DeprecationWarning:chameleon
ignore::DeprecationWarning:pkg_resources
ignore:.*pkg_resources:DeprecationWarning
ignore:.*Attribute . is deprecated:DeprecationWarning
ignore::DeprecationWarning:webob
ignore::DeprecationWarning:zodburi
5 changes: 3 additions & 2 deletions substanced/audit/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import datetime
import unittest

from pyramid import testing

class Test_AuditLogEventStreamView(unittest.TestCase):
Expand Down Expand Up @@ -93,11 +95,10 @@ def test_auditstream_sse_with_last_event_id_and_oids(self):
self.assertEqual(list(auditlog.oids), [3])

def test_auditing(self):
import pytz
context = testing.DummyResource()
request = testing.DummyRequest()
request.user = testing.DummyResource()
request.user.timezone = pytz.timezone('UTC')
request.user.timezone = datetime.timezone.utc
inst = self._makeOne(context, request)
inst.get_auditlog = lambda c: DummyAuditLog()
result = inst.auditing()
Expand Down
2 changes: 0 additions & 2 deletions substanced/content/test_it.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def test_add_with_meta(self):
def test_create(self):
registry = DummyRegistry()
inst = self._makeOne(registry)
inst._utcnow = lambda *a: 1
content = testing.DummyResource()
inst.content_types['dummy'] = lambda a: content
inst.meta['dummy'] = {}
Expand All @@ -34,7 +33,6 @@ def test_create(self):
def test_create_with_oid(self):
registry = DummyRegistry()
inst = self._makeOne(registry)
inst._utcnow = lambda *a: 1
content = testing.DummyResource()
inst.content_types['dummy'] = lambda a: content
inst.meta['dummy'] = {}
Expand Down
5 changes: 1 addition & 4 deletions substanced/locking/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import datetime
import uuid
import pytz

from zope.interface import implementer

Expand Down Expand Up @@ -43,6 +42,7 @@
find_objectmap,
find_service,
get_oid,
now,
)
from substanced.schema import Schema

Expand All @@ -69,9 +69,6 @@ class UnlockError(LockingError):
or ``None`` if there was no lock to unlock.
"""

def now():
return datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)

class LockOwnerSchema(colander.SchemaNode):
title = 'Owner'
schema_type = colander.Int
Expand Down
58 changes: 26 additions & 32 deletions substanced/locking/tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@ def test_ctor(self):
inst = self._makeOne('lock')
self.assertEqual(inst.lock, 'lock')

class Test_now(unittest.TestCase):
def _callFUT(self):
from .. import now
return now()

def test_it(self):
from pytz import UTC
result = self._callFUT()
self.assertEqual(result.tzinfo, UTC)

class TestLockOwnerSchema(unittest.TestCase):
def _makeOne(self):
from .. import LockOwnerSchema
Expand Down Expand Up @@ -210,18 +200,18 @@ def test_ctor(self):
self.assertEqual(inst.comment, 'comment')

def test_refresh(self):
import datetime
from .. import now
inst = self._makeOne()
now = datetime.datetime.utcnow()
inst.refresh(when=now)
self.assertEqual(inst.last_refresh, now)
NOW = now()
inst.refresh(when=NOW)
self.assertEqual(inst.last_refresh, NOW)

def test_refresh_with_timeout(self):
import datetime
from .. import now
inst = self._makeOne()
now = datetime.datetime.utcnow()
inst.refresh(timeout=30, when=now)
self.assertEqual(inst.last_refresh, now)
NOW = now()
inst.refresh(timeout=30, when=NOW)
self.assertEqual(inst.last_refresh, NOW)
self.assertEqual(inst.timeout, 30)

def test_expires_timeout_is_None(self):
Expand All @@ -231,11 +221,12 @@ def test_expires_timeout_is_None(self):

def test_expires_timeout_is_int(self):
import datetime
from .. import now
inst = self._makeOne()
inst.timeout = 30
now = datetime.datetime.utcnow()
inst.last_refresh = now
self.assertEqual(inst.expires(), now + datetime.timedelta(seconds=30))
NOW = now()
inst.last_refresh = NOW
self.assertEqual(inst.expires(), NOW + datetime.timedelta(seconds=30))

def test_is_valid_expires_timeout_is_None(self):
inst = self._makeOne()
Expand All @@ -244,31 +235,34 @@ def test_is_valid_expires_timeout_is_None(self):

def test_is_valid_expires_timeout_is_int(self):
import datetime
from .. import now
inst = self._makeOne()
inst.timeout = 30
now = datetime.datetime.utcnow()
future = now + datetime.timedelta(seconds=60)
inst.last_refresh = now
self.assertTrue(inst.is_valid(now))
self.assertFalse(inst.is_valid(future))
NOW = now()
FUTURE = NOW + datetime.timedelta(seconds=60)
inst.last_refresh = NOW
self.assertTrue(inst.is_valid(NOW))
self.assertFalse(inst.is_valid(FUTURE))

def test_is_valid_expires_resource_id_exists(self):
import datetime
from .. import now
inst = self._makeOne()
inst.timeout = 30
now = datetime.datetime.utcnow()
inst.last_refresh = now
NOW = now()
inst.last_refresh = NOW
inst.__objectmap__ = DummyObjectMap([1])
self.assertTrue(inst.is_valid(now))
self.assertTrue(inst.is_valid(NOW))

def test_is_valid_expires_resource_id_notexist(self):
import datetime
from .. import now
inst = self._makeOne()
inst.timeout = 30
now = datetime.datetime.utcnow()
inst.last_refresh = now
NOW = now()
inst.last_refresh = NOW
inst.__objectmap__ = DummyObjectMap([])
self.assertFalse(inst.is_valid(now))
self.assertFalse(inst.is_valid(NOW))

def test_depth_wo_infinite(self):
inst = self._makeOne(infinite=False)
Expand Down
4 changes: 4 additions & 0 deletions substanced/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cProfile as _profile
except ImportError: # pragma: no cover (pypy)
import profile as _profile
import datetime
import itertools
import json
import math
Expand Down Expand Up @@ -30,6 +31,9 @@

_marker = object()

def now():
return datetime.datetime.now(datetime.timezone.utc)

class JsonDict(dict):
def __str__(self):
return json.dumps(self)
Expand Down
15 changes: 13 additions & 2 deletions substanced/util/test_it.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

from . import _marker

class Test_now(unittest.TestCase):
def _callFUT(self):
from . import now
return now()

def test_it(self):
import datetime

result = self._callFUT()
self.assertIs(result.tzinfo, datetime.timezone.utc)

class Test__postorder(unittest.TestCase):
def setUp(self):
testing.setUp()
Expand Down Expand Up @@ -391,8 +402,8 @@ def _callFUT(self, d):

def test_it(self):
import calendar
import datetime
d = datetime.datetime.utcnow()
from . import now
d = now()
result = self._callFUT(d)
timetime = calendar.timegm(d.timetuple())
val = int(timetime) // 100
Expand Down