Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r59045:3e5417bea513
Date: 2012-11-22 22:46 +0100
http://bitbucket.org/pypy/pypy/changeset/3e5417bea513/

Log:    Ensure that Pycode.co_filename is a string without NUL bytes. It
        will make life easier for cpyext, which can then use path
        manipulation functions from the imp module.

diff --git a/pypy/interpreter/pycode.py b/pypy/interpreter/pycode.py
--- a/pypy/interpreter/pycode.py
+++ b/pypy/interpreter/pycode.py
@@ -15,7 +15,7 @@
     CO_GENERATOR, CO_CONTAINSGLOBALS)
 from pypy.rlib.rarithmetic import intmask
 from pypy.rlib.debug import make_sure_not_resized
-from pypy.rlib import jit
+from pypy.rlib import jit, rstring
 from pypy.rlib.objectmodel import compute_hash, we_are_translated
 from pypy.tool.stdlib_opcode import opcodedesc, HAVE_ARGUMENT
 
@@ -93,6 +93,7 @@
         self.co_varnames = varnames
         self.co_freevars = freevars
         self.co_cellvars = cellvars
+        rstring.check_str0(filename)
         self.co_filename = filename
         self.co_name = name
         self.co_firstlineno = firstlineno
@@ -360,7 +361,7 @@
 
     @unwrap_spec(argcount=int, kwonlyargcount=int, nlocals=int, stacksize=int, 
flags=int,
                  codestring=str,
-                 filename=str, name=str, firstlineno=int,
+                 filename='str0', name=str, firstlineno=int,
                  lnotab=str, magic=int)
     def descr_code__new__(space, w_subtype,
                           argcount, kwonlyargcount, nlocals, stacksize, flags,
diff --git a/pypy/interpreter/pyparser/pyparse.py 
b/pypy/interpreter/pyparser/pyparse.py
--- a/pypy/interpreter/pyparser/pyparse.py
+++ b/pypy/interpreter/pyparser/pyparse.py
@@ -2,6 +2,7 @@
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.pyparser import future, parser, pytokenizer, pygram, 
error
 from pypy.interpreter.astcompiler import consts
+from pypy.rlib import rstring
 
 
 def recode_to_utf8(space, bytes, encoding=None):
@@ -72,6 +73,7 @@
 
     def __init__(self, filename, mode="exec", flags=0, future_pos=(0, 0),
                  hidden_applevel=False):
+        rstring.check_str0(filename)
         self.filename = filename
         self.mode = mode
         self.encoding = None
diff --git a/pypy/module/__builtin__/compiling.py 
b/pypy/module/__builtin__/compiling.py
--- a/pypy/module/__builtin__/compiling.py
+++ b/pypy/module/__builtin__/compiling.py
@@ -9,7 +9,8 @@
 from pypy.interpreter.argument import Arguments
 from pypy.interpreter.nestedscope import Cell
 
-@unwrap_spec(filename=str, mode=str, flags=int, dont_inherit=int, optimize=int)
+@unwrap_spec(filename='str0', mode=str, flags=int, dont_inherit=int,
+             optimize=int)
 def compile(space, w_source, filename, mode, flags=0, dont_inherit=0,
             optimize=0):
     """Compile the source string (a Python module, statement or expression)
diff --git a/pypy/module/cpyext/funcobject.py b/pypy/module/cpyext/funcobject.py
--- a/pypy/module/cpyext/funcobject.py
+++ b/pypy/module/cpyext/funcobject.py
@@ -132,7 +132,7 @@
                              consts=space.fixedview(w_consts),
                              names=unwrap_list_of_strings(space, w_names),
                              varnames=unwrap_list_of_strings(space, 
w_varnames),
-                             filename=space.str_w(w_filename),
+                             filename=space.str0_w(w_filename),
                              name=space.str_w(w_funcname),
                              firstlineno=rffi.cast(lltype.Signed, firstlineno),
                              lnotab=space.str_w(w_lnotab),
diff --git a/pypy/module/parser/pyparser.py b/pypy/module/parser/pyparser.py
--- a/pypy/module/parser/pyparser.py
+++ b/pypy/module/parser/pyparser.py
@@ -49,7 +49,7 @@
         return self._build_app_tree(space, self.tree, space.newlist,
                                     line_info, col_info)
 
-    @unwrap_spec(filename=str)
+    @unwrap_spec(filename='str0')
     def descr_compile(self, space, filename="<syntax-tree>"):
         info = pyparse.CompileInfo(filename, self.mode)
         try:
diff --git a/pypy/objspace/std/marshal_impl.py 
b/pypy/objspace/std/marshal_impl.py
--- a/pypy/objspace/std/marshal_impl.py
+++ b/pypy/objspace/std/marshal_impl.py
@@ -345,6 +345,16 @@
         else:
             raise
 
+def unmarshal_str0(u):
+    w_obj = u.get_w_obj()
+    try:
+        return u.space.bytes0_w(w_obj)
+    except OperationError, e:
+        if e.match(u.space, u.space.w_TypeError):
+            u.raise_exc('invalid marshal data for code object')
+        else:
+            raise
+
 def unmarshal_strlist(u, tc):
     lng = u.atom_lng(tc)
     res = [None] * lng
@@ -369,7 +379,7 @@
     varnames    = unmarshal_strlist(u, TYPE_TUPLE)
     freevars    = unmarshal_strlist(u, TYPE_TUPLE)
     cellvars    = unmarshal_strlist(u, TYPE_TUPLE)
-    filename    = unmarshal_str(u)
+    filename    = unmarshal_str0(u)
     name        = unmarshal_str(u)
     firstlineno = u.get_int()
     lnotab      = unmarshal_str(u)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to