Skip to content

Commit

Permalink
Merge pull request #326 from topazproject/tim/windows
Browse files Browse the repository at this point in the history
Fix translation issues on windows
  • Loading branch information
alex committed Mar 13, 2013
2 parents 9904663 + 7973809 commit 7f80877
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 59 deletions.
6 changes: 4 additions & 2 deletions tests/modules/test_process.py
Expand Up @@ -2,6 +2,8 @@

import pytest

from topaz.modules import process

from ..base import BaseTopazTest


Expand Down Expand Up @@ -35,7 +37,7 @@ def test_exit(self, space):
assert self.unwrap(space, w_res) == [False, 1]

def test_fork(self, space, monkeypatch, capfd):
monkeypatch.setattr(os, "fork", lambda: 0)
monkeypatch.setattr(process, "fork", lambda: 0)
with self.raises(space, "SystemExit"):
space.execute("""
Process.fork do
Expand All @@ -45,7 +47,7 @@ def test_fork(self, space, monkeypatch, capfd):
out, err = capfd.readouterr()
assert err == ""
assert out == "child\n"
monkeypatch.setattr(os, "fork", lambda: 200)
monkeypatch.setattr(process, "fork", lambda: 200)
w_res = space.execute("""
return Process.fork do
puts "child"
Expand Down
16 changes: 12 additions & 4 deletions topaz/main.py
Expand Up @@ -8,6 +8,7 @@
from topaz.error import RubyError, print_traceback
from topaz.objects.exceptionobject import W_SystemExit
from topaz.objspace import ObjectSpace
from topaz.system import IS_WINDOWS, IS_64BIT


USAGE = "\n".join([
Expand Down Expand Up @@ -175,7 +176,11 @@ def _parse_argv(space, argv):


def _entry_point(space, argv):
system, _, _, _, cpu = os.uname()
if IS_WINDOWS:
system = "Windows"
cpu = "x86_64" if IS_64BIT else "i686"
else:
system, _, _, _, cpu = os.uname()
platform = "%s-%s" % (cpu, system.lower())
engine = "topaz"
version = "1.9.3"
Expand Down Expand Up @@ -237,7 +242,7 @@ def _entry_point(space, argv):
path = candidate_path
break
try:
f = open_file_as_stream(path)
f = open_file_as_stream(path, buffering=0)
except OSError as e:
os.write(2, "%s -- %s (LoadError)\n" % (os.strerror(e.errno), path))
return 1
Expand All @@ -248,8 +253,11 @@ def _entry_point(space, argv):
elif explicitly_verbose:
return 0
else:
source = fdopen_as_stream(0, "r").readall()
path = "-"
if IS_WINDOWS:
raise NotImplementedError("executing from stdin on Windows")
else:
source = fdopen_as_stream(0, "r").readall()
path = "-"

for globalized_switch in globalized_switches:
value = None
Expand Down
10 changes: 10 additions & 0 deletions topaz/module.py
Expand Up @@ -57,6 +57,16 @@ def method_allocate(self, space):
raise space.error(space.w_TypeError, "allocator undefined for %s" % self.name)
return method_allocate

def notimplemented(self, name):
@self.method(name)
def method(self, space):
raise space.error(space.w_NotImplementedError)

def singleton_notimplemented(self, name):
@self.singleton_method(name)
def method(self, space):
raise space.error(space.w_NotImplementedError)


class Module(object):
pass
Expand Down
2 changes: 1 addition & 1 deletion topaz/modules/kernel.py
Expand Up @@ -67,7 +67,7 @@ def load_feature(space, path, orig_path):
raise space.error(space.w_LoadError, orig_path)

try:
f = open_file_as_stream(path)
f = open_file_as_stream(path, buffering=0)
try:
contents = f.readall()
finally:
Expand Down
21 changes: 18 additions & 3 deletions topaz/modules/process.py
@@ -1,16 +1,31 @@
from __future__ import absolute_import

import os
import sys

from topaz.module import Module, ModuleDef
from topaz.system import IS_WINDOWS


if IS_WINDOWS:
def geteuid():
return 0 # MRI behaviour on windows
def fork():
raise NotImplementedError("fork on windows")
def WEXITSTATUS(status):
return status
else:
geteuid = os.geteuid
fork = os.fork
WEXITSTATUS = os.WEXITSTATUS


class Process(Module):
moduledef = ModuleDef("Process", filepath=__file__)

@moduledef.function("euid")
def method_euid(self, space):
return space.newint(os.geteuid())
return space.newint(geteuid())

@moduledef.function("pid")
def method_pid(self, space):
Expand All @@ -19,7 +34,7 @@ def method_pid(self, space):
@moduledef.function("waitpid", pid="int")
def method_waitpid(self, space, pid=-1):
pid, status = os.waitpid(pid, 0)
status = os.WEXITSTATUS(status)
status = WEXITSTATUS(status)
w_status = space.send(
space.find_const(self, "Status"),
space.newsymbol("new"),
Expand All @@ -38,7 +53,7 @@ def method_exit_bang(self, space, status=0):

@moduledef.function("fork")
def method_fork(self, space, block):
pid = os.fork()
pid = fork()
if pid == 0:
if block is not None:
space.invoke_block(block, [])
Expand Down
5 changes: 5 additions & 0 deletions topaz/objects/dirobject.py
Expand Up @@ -13,6 +13,11 @@ class W_DirObject(W_Object):
classdef = ClassDef("Dir", W_Object.classdef, filepath=__file__)
classdef.include_module(Enumerable)

def __init__(self, space, klass=None):
W_Object.__init__(self, space, klass)
self.open = False
self.path = None

def __del__(self):
if self.open:
closedir(self.dirp)
Expand Down
86 changes: 52 additions & 34 deletions topaz/objects/fileobject.py
Expand Up @@ -9,7 +9,10 @@
from topaz.objects.hashobject import W_HashObject
from topaz.objects.objectobject import W_Object
from topaz.objects.ioobject import W_IOObject
from topaz.utils.filemode import map_filemode, O_BINARY
from topaz.objects.stringobject import W_StringObject
from topaz.system import IS_WINDOWS
from topaz.utils.ll_file import O_BINARY, ftruncate, isdir, fchmod
from topaz.utils.filemode import map_filemode


FNM_NOESCAPE = 0x01
Expand All @@ -22,7 +25,7 @@ class W_FileObject(W_IOObject):

@classdef.setup_class
def setup_class(cls, space, w_cls):
if sys.platform == "win32":
if IS_WINDOWS:
w_alt_seperator = space.newstr_fromstr("\\")
w_fnm_syscase = space.newint(0x08)
else:
Expand Down Expand Up @@ -56,7 +59,7 @@ def singleton_method_size_p(self, space, name):
stat_val = os.stat(name)
except OSError:
return space.w_nil
return space.w_nil if stat_val.st_size == 0 else space.newint(stat_val.st_size)
return space.w_nil if stat_val.st_size == 0 else space.newint_or_bigint(stat_val.st_size)

@classdef.singleton_method("unlink")
@classdef.singleton_method("delete")
Expand Down Expand Up @@ -175,7 +178,7 @@ def method_filep(self, space, filename):

@classdef.singleton_method("directory?", filename="path")
def method_directoryp(self, space, filename):
return space.newbool(os.path.isdir(filename))
return space.newbool(isdir(filename))

@classdef.singleton_method("symlink?", filename="path")
def method_symlinkp(self, space, filename):
Expand Down Expand Up @@ -213,13 +216,13 @@ def method_umask(self, space, mask=-1):
@classdef.method("truncate", length="int")
def method_truncate(self, space, length):
self.ensure_not_closed(space)
os.ftruncate(self.fd, length)
ftruncate(self.fd, length)
return space.newint(0)

@classdef.method("chmod", mode="int")
def method_chmod(self, space, mode):
try:
os.fchmod(self.fd, mode)
fchmod(self.fd, mode)
except OSError as e:
raise error_for_oserror(space, e)
return space.newint(0)
Expand Down Expand Up @@ -254,21 +257,25 @@ def singleton_method_lstat(self, space, filename):
stat_obj.set_stat(stat_val)
return stat_obj

@classdef.singleton_method("symlink", old_name="path", new_name="path")
def singleton_method_symlink(self, space, old_name, new_name):
try:
os.symlink(old_name, new_name)
except OSError as e:
raise error_for_oserror(space, e)
return space.newint(0)
if IS_WINDOWS:
classdef.s_notimplemented("symlink")
classdef.s_notimplemented("link")
else:
@classdef.singleton_method("symlink", old_name="path", new_name="path")
def singleton_method_symlink(self, space, old_name, new_name):
try:
os.symlink(old_name, new_name)
except OSError as e:
raise error_for_oserror(space, e)
return space.newint(0)

@classdef.singleton_method("link", old_name="path", new_name="path")
def singleton_method_link(self, space, old_name, new_name):
try:
os.link(old_name, new_name)
except OSError as e:
raise error_for_oserror(space, e)
return space.newint(0)
@classdef.singleton_method("link", old_name="path", new_name="path")
def singleton_method_link(self, space, old_name, new_name):
try:
os.link(old_name, new_name)
except OSError as e:
raise error_for_oserror(space, e)
return space.newint(0)


class W_FileStatObject(W_Object):
Expand Down Expand Up @@ -298,25 +305,40 @@ def method_initialize(self, space, filename):
except OSError as e:
raise error_for_oserror(space, e)

@classdef.method("blksize")
def method_blksize(self, space):
return space.newint(self.get_stat(space).st_blksize)

@classdef.method("blockdev?")
def method_blockdevp(self, space):
return space.newbool(stat.S_ISBLK(self.get_stat(space).st_mode))

@classdef.method("blocks")
def method_blocks(self, space):
return space.newint(self.get_stat(space).st_blocks)
if IS_WINDOWS:
def unsupported_attr(name, classdef):
@classdef.method(name)
def method(self, space):
return space.w_nil
method.__name__ = name
return method
method_blksize = unsupported_attr("blksize", classdef)
method_blocks = unsupported_attr("blocks", classdef)
method_rdev = unsupported_attr("rdev", classdef)
else:
@classdef.method("blksize")
def method_blksize(self, space):
return space.newint(self.get_stat(space).st_blksize)

@classdef.method("rdev")
def method_rdev(self, space):
return space.newint(self.get_stat(space).st_rdev)

@classdef.method("blocks")
def method_blocks(self, space):
return space.newint(self.get_stat(space).st_blocks)

@classdef.method("chardev?")
def method_chardevp(self, space):
return space.newbool(stat.S_ISCHR(self.get_stat(space).st_mode))

@classdef.method("dev")
def method_dev(self, space):
return space.newint(self.get_stat(space).st_dev)
return space.newint_or_bigint(self.get_stat(space).st_dev)

@classdef.method("directory?")
def method_directoryp(self, space):
Expand Down Expand Up @@ -352,7 +374,7 @@ def method_gid(self, space):

@classdef.method("ino")
def method_ino(self, space):
return space.newint(self.get_stat(space).st_ino)
return space.newint_or_bigint(self.get_stat(space).st_ino)

def get_w_mode(self, space):
return space.newint(self.get_stat(space).st_mode)
Expand All @@ -365,10 +387,6 @@ def method_mode(self, space):
def method_nlink(self, space):
return space.newint(self.get_stat(space).st_nlink)

@classdef.method("rdev")
def method_rdev(self, space):
return space.newint(self.get_stat(space).st_rdev)

@classdef.method("setgid?")
def method_setgidp(self, space):
return space.newbool(stat.S_IMODE(self.get_stat(space).st_mode) & stat.S_ISGID)
Expand All @@ -379,7 +397,7 @@ def method_setuidp(self, space):

@classdef.method("size")
def method_size(self, space):
return space.newint(self.get_stat(space).st_size)
return space.newint_or_bigint(self.get_stat(space).st_size)

@classdef.method("socket?")
def method_socketp(self, space):
Expand Down
13 changes: 10 additions & 3 deletions topaz/objects/intobject.py
Expand Up @@ -13,6 +13,7 @@
from topaz.objects.integerobject import W_IntegerObject
from topaz.objects.numericobject import W_NumericObject
from topaz.objects.objectobject import W_RootObject
from topaz.system import IS_WINDOWS


class FixnumStorage(object):
Expand Down Expand Up @@ -271,9 +272,15 @@ def comparator(self, space, other):
def method_hash(self, space):
return self

@classdef.method("size")
def method_size(self, space):
return space.newint(rffi.sizeof(lltype.typeOf(self.intvalue)))
if IS_WINDOWS:
@classdef.method("size")
def method_size(self, space):
# RPython translation is always 32bit on Windows
return space.newint(4)
else:
@classdef.method("size")
def method_size(self, space):
return space.newint(rffi.sizeof(lltype.typeOf(self.intvalue)))

@classdef.method("coerce")
def method_coerce(self, space, w_other):
Expand Down

0 comments on commit 7f80877

Please sign in to comment.