Commits

Amaury Forgeot d'Arc committed 3e5417b

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.

Comments (0)

Files changed (6)

pypy/interpreter/pycode.py

     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
 
         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
 
     @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,

pypy/interpreter/pyparser/pyparse.py

 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):
 
     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

pypy/module/__builtin__/compiling.py

 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)

pypy/module/cpyext/funcobject.py

                              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),

pypy/module/parser/pyparser.py

         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:

pypy/objspace/std/marshal_impl.py

         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
     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)