Python3 use unicode and Python2 use string

This commit is contained in:
virusdefender 2017-04-30 13:17:38 +08:00
parent e6c6e7c121
commit 0b1c8ad5fc
7 changed files with 39 additions and 12 deletions

View File

@ -1,6 +1,8 @@
language: python language: python
dist: trusty
python: python:
- "2.7" - "2.7"
- "3.5"
sudo: required sudo: required
before_install: before_install:
- sudo apt-get -qq update - sudo apt-get -qq update
@ -8,8 +10,8 @@ before_install:
install: install:
- mkdir build && cd build - mkdir build && cd build
- cmake .. && make && sudo make install - cmake .. && make && sudo make install
- cd ../bindings/Python && sudo python setup.py install - cd ../bindings/Python && sudo `which python` setup.py install
script: script:
- cd ../../tests/Python_and_core && sudo python test.py - cd ../../tests/Python_and_core && sudo `which python` test.py
notifications: notifications:
slack: onlinejudgeteam:BzBz8UFgmS5crpiblof17K2W slack: onlinejudgeteam:BzBz8UFgmS5crpiblof17K2W

View File

@ -1,7 +1,7 @@
#include <unistd.h> #include <unistd.h>
#include <dlfcn.h> #include <dlfcn.h>
#include <sys/types.h> #include <sys/types.h>
#include <python2.7/Python.h> #include <Python.h>
#include "../../src/runner.h" #include "../../src/runner.h"
@ -13,7 +13,7 @@
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
#define PyString_Check PyUnicode_Check #define PyString_Check PyUnicode_Check
#define PyString_AsString(str) str #define PyString_AsString PyUnicode_AsUTF8
#endif #endif
static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) { static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {

View File

@ -1,4 +1,6 @@
# coding=utf-8 # coding=utf-8
from __future__ import print_function
import sys
import _judger import _judger
from unittest import TestCase, main from unittest import TestCase, main
@ -6,5 +8,6 @@ from testcase.integration.test import IntegrationTest
from testcase.seccomp.test import SeccompTest from testcase.seccomp.test import SeccompTest
ver = _judger.VERSION ver = _judger.VERSION
print "Judger version %d.%d.%d" % ((ver >> 16) & 0xff, (ver >> 8) & 0xff, ver & 0xff) print("Judger version %d.%d.%d" % ((ver >> 16) & 0xff, (ver >> 8) & 0xff, ver & 0xff))
main() print(sys.version)
main()

9
tests/Python_and_core/test.sh Executable file
View File

@ -0,0 +1,9 @@
cd ../../bindings/Python
rm -r build
python setup.py install
rm -r build
python3 setup.py install
cd ../../tests/Python_and_core
python test.py
slepp 3
python3 test.py

View File

@ -1,5 +1,7 @@
# coding=utf-8 # coding=utf-8
from __future__ import print_function
import os import os
import random
import shutil import shutil
from unittest import TestCase from unittest import TestCase
@ -21,7 +23,7 @@ class BaseTestCase(TestCase):
return workspace return workspace
def rand_str(self): def rand_str(self):
return ''.join(map(lambda xx:(hex(ord(xx))[2:]), os.urandom(16))) return "".join([random.choice("123456789abcdef") for _ in range(12)])
def _compile_c(self, src_name, extra_flags=None): def _compile_c(self, src_name, extra_flags=None):
path = os.path.dirname(os.path.abspath(__file__)) path = os.path.dirname(os.path.abspath(__file__))
@ -53,4 +55,4 @@ class BaseTestCase(TestCase):
def output_content(self, path): def output_content(self, path):
with open(path, "r") as f: with open(path, "r") as f:
return f.read() return f.read()

View File

@ -1,4 +1,6 @@
# coding=utf-8 # coding=utf-8
from __future__ import print_function
import sys
import _judger import _judger
import signal import signal
import os import os
@ -8,7 +10,7 @@ from .. import base
class IntegrationTest(base.BaseTestCase): class IntegrationTest(base.BaseTestCase):
def setUp(self): def setUp(self):
print "Running", self._testMethodName print("Running", self._testMethodName)
self.config = {"max_cpu_time": 1000, self.config = {"max_cpu_time": 1000,
"max_real_time": 3000, "max_real_time": 3000,
"max_memory": 1024 * 1024 * 128, "max_memory": 1024 * 1024 * 128,
@ -67,11 +69,15 @@ class IntegrationTest(base.BaseTestCase):
args=["1234", None], env=["a=b"], log_path="1.log", args=["1234", None], env=["a=b"], log_path="1.log",
seccomp_rule_name="1.so", uid=0, gid=0) seccomp_rule_name="1.so", uid=0, gid=0)
if sys.version_info >= (3, 5):
args = ["哈哈哈".encode("utf-8")]
else:
args = [u"哈哈哈"]
with self.assertRaisesRegexp(ValueError, "arg item must be a string"): with self.assertRaisesRegexp(ValueError, "arg item must be a string"):
_judger.run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000, _judger.run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000,
max_process_number=200, max_output_size=10000, exe_path="1.out", max_process_number=200, max_output_size=10000, exe_path="1.out",
input_path="1.in", output_path="1.out", error_path="1.out", input_path="1.in", output_path="1.out", error_path="1.out",
args=[u"哈哈哈"], env=["a=b"], log_path="1.log", args=args, env=["a="], log_path="1.log",
seccomp_rule_name="1.so", uid=0, gid=0) seccomp_rule_name="1.so", uid=0, gid=0)
def test_env_must_be_list(self): def test_env_must_be_list(self):
@ -104,11 +110,15 @@ class IntegrationTest(base.BaseTestCase):
args=["1234"], env=["a=b", None], log_path="1.log", args=["1234"], env=["a=b", None], log_path="1.log",
seccomp_rule_name="1.so", uid=0, gid=0) seccomp_rule_name="1.so", uid=0, gid=0)
if sys.version_info >= (3, 5):
env = ["哈哈哈".encode("utf-8")]
else:
env = [u"哈哈哈"]
with self.assertRaisesRegexp(ValueError, "env item must be a string"): with self.assertRaisesRegexp(ValueError, "env item must be a string"):
_judger.run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000, _judger.run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000,
max_process_number=200, max_output_size=10000, exe_path="1.out", max_process_number=200, max_output_size=10000, exe_path="1.out",
input_path="1.in", output_path="1.out", error_path="1.out", input_path="1.in", output_path="1.out", error_path="1.out",
args=["1234"], env=[u"哈哈哈"], log_path="1.log", args=["1234"], env=env, log_path="1.log",
seccomp_rule_name="1.so", uid=0, gid=0) seccomp_rule_name="1.so", uid=0, gid=0)
def test_seccomp_rule_can_be_none(self): def test_seccomp_rule_can_be_none(self):

View File

@ -1,4 +1,5 @@
# coding=utf-8 # coding=utf-8
from __future__ import print_function
import _judger import _judger
import signal import signal
import os import os
@ -8,7 +9,7 @@ from .. import base
class SeccompTest(base.BaseTestCase): class SeccompTest(base.BaseTestCase):
def setUp(self): def setUp(self):
print "Running", self._testMethodName print("Running", self._testMethodName)
self.config = {"max_cpu_time": 1000, self.config = {"max_cpu_time": 1000,
"max_real_time": 3000, "max_real_time": 3000,
"max_memory": 1024 * 1024 * 128, "max_memory": 1024 * 1024 * 128,