Skip to content
This repository has been archived by the owner on Jul 10, 2020. It is now read-only.

Commit

Permalink
Fixed issue with threading
Browse files Browse the repository at this point in the history
  • Loading branch information
pzinovkin committed Apr 5, 2012
1 parent 9de4f44 commit a2f82e7
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Installation

::
export V8_SVN_URL="http://v8.googlecode.com/svn/trunk/@10412"
export V8_SVN_URL="http://v8.googlecode.com/svn/trunk/@10875"
pip install -r requirements.txt


Expand Down
2 changes: 1 addition & 1 deletion example/example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

PROJECT_ROOT = os.path.dirname(__file__)

DEBUG = False
DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
Expand Down
6 changes: 6 additions & 0 deletions fabfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from fabric.api import local


def test():
local('django-admin.py test --settings=fest.tests.settings')
74 changes: 62 additions & 12 deletions fest/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,55 @@ def fest_error(message):
raise TemplateSyntaxError(message)


class JSLocker(PyV8.JSLocker):

def __enter__(self):
self.enter()

if JSContext.entered:
self.leave()
raise RuntimeError('Lock should be acquired before enter'
' the context')

return self

def __exit__(self, exc_type, exc_value, traceback):
if JSContext.entered:
self.leave()
raise RuntimeError('Lock should be released after leave'
' the context')

self.leave()

def __nonzero__(self):
return self.entered()


class JSContext(PyV8.JSContext):

def __init__(self, obj=None, extensions=None, ctxt=None):
if JSLocker.active:
self.lock = JSLocker()
self.lock.enter()

if ctxt:
PyV8.JSContext.__init__(self, ctxt)
else:
PyV8.JSContext.__init__(self, obj, extensions or [])

def __enter__(self):
self.enter()
return self

def __exit__(self, exc_type, exc_value, traceback):
self.leave()
if hasattr(JSLocker, 'lock'):
self.lock.leave()
self.lock = None

del self


class TemplateGlobal(PyV8.JSClass):

def __init__(self, template):
Expand Down Expand Up @@ -63,7 +112,7 @@ def __init__(self, template_string, origin=None,
@property
def context(self):
if not hasattr(self, '_context'):
self._context = PyV8.JSContext(TemplateGlobal(self))
self._context = JSContext(TemplateGlobal(self))
return self._context

def compile(self):
Expand All @@ -74,16 +123,17 @@ def compile(self):

def render(self, context):

with self.context as env:
if not settings.DEBUG:
template = self.template_string
else:
template = self.compile()
with JSLocker():
with self.context as env:
if not settings.DEBUG:
template = self.template_string
else:
template = self.compile()

# maybe i'm just stupid
template = """(function(json_string, fest_error) {
return %s(JSON.parse(json_string), fest_error);
})""" % template
# maybe i'm just stupid
template = """(function(json_string, fest_error) {
return %s(JSON.parse(json_string), fest_error);
})""" % template

func = env.eval(template)
return func(json.dumps(list(context).pop()), fest_error)
func = env.eval(template)
return func(json.dumps(list(context).pop()), fest_error)
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
git+git://github.com/django/django.git@996f702#egg=django
Django==1.4

svn+http://pyv8.googlecode.com/svn/trunk/@429#egg=PyV8
svn+http://pyv8.googlecode.com/svn/trunk/@433#egg=PyV8
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
'PyV8==1.0',
],
dependency_links=[
'svn+http://pyv8.googlecode.com/svn/trunk/@429#egg=PyV8-1.0',
'svn+http://pyv8.googlecode.com/svn/trunk/@433#egg=PyV8-1.0',
]
)

0 comments on commit a2f82e7

Please sign in to comment.