Skip to content

Commit

Permalink
Script (Python) objects now have a _filepath attribute, also used as …
Browse files Browse the repository at this point in the history
…the '__file__' global at runtime. This prevents an import problem caused by the fix to #1074.
  • Loading branch information
Evan Simpson committed Mar 8, 2004
1 parent 1fc5a0e commit c7a0571
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
19 changes: 16 additions & 3 deletions PythonScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
Python code.
"""

__version__='$Revision: 1.54 $'[11:-2]
__version__='$Revision: 1.55 $'[11:-2]

import sys, os, traceback, re, marshal, new
from Globals import DTMLFile, MessageDialog, package_home
import AccessControl, OFS, RestrictedPython
from Acquisition import aq_parent
from OFS.SimpleItem import SimpleItem
from DateTime.DateTime import DateTime
from urllib import quote
Expand Down Expand Up @@ -193,7 +194,7 @@ def ZScriptHTML_tryParams(self):

name = name.strip()
if name and name[0] != '*' and re.match('\w',name):
param_names.append(name.split('=', 1)[0])
param_names.append(name.split('=', 1)[0].strip())
return param_names

def manage_historyCompare(self, rev1, rev2, REQUEST,
Expand Down Expand Up @@ -270,6 +271,9 @@ def _newfun(self, code):
def _makeFunction(self, dummy=0): # CMFCore.FSPythonScript uses dummy arg.
self.ZCacheable_invalidate()
self._compile()
if not (aq_parent(self) is None or hasattr(self, '_filepath')):
# It needs a _filepath, and has an acquisition wrapper.
self._filepath = self.get_filepath()

def _editedBindings(self):
if getattr(self, '_v_ft', None) is not None:
Expand All @@ -288,7 +292,7 @@ def _exec(self, bound_names, args, kw):
asgns = self.getBindingAssignments()
name_context = asgns.getAssignedName('name_context', None)
if name_context:
keyset[name_context] = self.aq_parent.getPhysicalPath()
keyset[name_context] = aq_parent(self).getPhysicalPath()
name_subpath = asgns.getAssignedName('name_subpath', None)
if name_subpath:
keyset[name_subpath] = self._getTraverseSubpath()
Expand All @@ -313,6 +317,7 @@ def _exec(self, bound_names, args, kw):
g.update(bound_names)
g['__traceback_supplement__'] = (
PythonScriptTracebackSupplement, self, -1)
g['__file__'] = getattr(self, '_filepath', None) or self.get_filepath()
f = new.function(fcode, g, None, fadefs)

result = f(*args, **kw)
Expand All @@ -321,6 +326,14 @@ def _exec(self, bound_names, args, kw):
self.ZCacheable_set(result, keywords=keyset)
return result

def manage_afterAdd(self, item, container):
if item is self:
self._filepath = self.get_filepath()

get_filepath=None # Public
def get_filepath(self):
return self.meta_type + ':' + '/'.join(self.getPhysicalPath())

def manage_haveProxy(self,r): return r in self._proxy_roles

def _validateProxy(self, roles=None):
Expand Down
18 changes: 9 additions & 9 deletions tests/testPythonScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# FOR A PARTICULAR PURPOSE
#
##############################################################################
import os, unittest
import os, unittest, warnings

from Products.PythonScripts.PythonScript import PythonScript
from AccessControl.SecurityManagement import newSecurityManager
Expand Down Expand Up @@ -55,19 +55,16 @@ def _newPS(self, txt, bind=None):
return ps

def _filePS(self, fname, bind=None):
ps = PythonScript(fname)
ps = VerifiedPythonScript(fname)
ps.ZBindings_edit(bind or {})
ps.write(readf(fname))
ps._makeFunction()
if ps.errors:
raise SyntaxError, ps.errors[0]
return ps


class TestPythonScriptNoAq(PythonScriptTestBase):

def fail(self):
'Fail if called'
assert 0, 'Fail called'

def testEmpty(self):
empty = self._newPS('')()
self.failUnless(empty is None)
Expand Down Expand Up @@ -238,10 +235,13 @@ def testGlobalIsDeclaration(self):
self.assertEqual(results, 8)

def test__name__(self):
fname = 'class.__name__'
f = self._filePS(fname)
f = self._filePS('class.__name__')
self.assertEqual(f(), ('?.foo', "'string'"))

def test_filepath(self):
f = self._filePS('filepath')
self.assertEqual(f(), [0])

def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestPythonScriptNoAq))
Expand Down
Binary file added tests/tscripts/filepath.ps
Binary file not shown.

0 comments on commit c7a0571

Please sign in to comment.