Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50640 views
1
# -*- coding: utf-8 -*-
2
# test_sagews.py
3
# basic tests of sage worksheet using TCP protocol with sage_server
4
import socket
5
import conftest
6
import os
7
import re
8
9
class TestBadContinuation:
10
r"""
11
String with badly-formed utf8 would hang worksheet process #1866
12
"""
13
def test_bad_utf8(self, exec2):
14
code = r"""print('u"\xe1"')"""
15
outp = u"�"
16
exec2(code, outp)
17
18
class TestUnicode:
19
r"""
20
To pass unicode in a simulated input cell, quote it.
21
That will send the same message to sage_server
22
as a real input cell without the outer quotes.
23
"""
24
def test_unicode_1(self, exec2):
25
r"""
26
test for cell with input u"äöüß"
27
"""
28
ustr = 'u"äöüß"'
29
uout = ustr[2:-1].decode('utf8').__repr__().decode('utf8')
30
exec2(ustr, uout)
31
def test_unicode_2(self, exec2):
32
r"""
33
Test for cell with input u"ááá".
34
Input u"ááá" in an actual cell causes latin1 encoding to appear
35
enclosed by u"...", inside a unicode string in the message to sage_server.
36
(So there are two u's in the displayed message in the log.)
37
Code part of logged input message to sage_server:
38
u'code': u'u"\xe1\xe1\xe1"\n'
39
Stdout part of logged output message from sage_server:
40
"stdout": "u\'\\\\xe1\\\\xe1\\\\xe1\'\\n"
41
"""
42
ustr = 'u"ááá"'
43
# same as below: uout = u"u'\\xe1\\xe1\\xe1'\n"
44
uout = ustr[2:-1].decode('utf8').__repr__().decode('utf8')
45
exec2(ustr, uout)
46
def test_unicode_3(self, exec2):
47
r"""
48
Test for cell with input "ááá".
49
Input "ááá" in an actual cell causes utf8 encoding to appear
50
inside a unicode string in the message to sage_server.
51
Code part of logged input message to sage_server:
52
u'code': u'"\xe1\xe1\xe1"\n'
53
Stdout part of logged output message from sage_server:
54
"stdout": "\'\\\\xc3\\\\xa1\\\\xc3\\\\xa1\\\\xc3\\\\xa1\'\\n"
55
"""
56
ustr = '"ááá"'
57
uout = ustr[1:-1].decode('utf8').encode('utf8').__repr__().decode('utf8')
58
exec2(ustr, uout)
59
def test_unicode_4(self, exec2):
60
r"""
61
test for cell with input "öäß"
62
"""
63
ustr = '"öäß"'
64
uout = ustr[1:-1].decode('utf8').encode('utf8').__repr__().decode('utf8')
65
exec2(ustr, uout)
66
67
class TestOutputReplace:
68
def test_1865(self,exec2):
69
code = 'for x in [u"ááá", "ááá"]: print(x)'
70
xout = u'ááá\nááá\n'
71
exec2(code, xout)
72
73
class TestErr:
74
def test_non_ascii(self, test_id, sagews):
75
# assign x to hbar to trigger non-ascii warning
76
code = ("x = " + unichr(295) + "\nx").encode('utf-8')
77
m = conftest.message.execute_code(code = code, id = test_id)
78
sagews.send_json(m)
79
# expect 3 messages from worksheet client, including final done:true
80
# 1 stderr Error in lines 1-1
81
typ, mesg = sagews.recv()
82
assert typ == 'json'
83
assert mesg['id'] == test_id
84
assert 'stderr' in mesg
85
assert 'Error in lines 1-1' in mesg['stderr']
86
# 2 stderr WARNING: Code contains non-ascii characters
87
typ, mesg = sagews.recv()
88
assert typ == 'json'
89
assert mesg['id'] == test_id
90
assert 'stderr' in mesg
91
assert 'WARNING: Code contains non-ascii characters' in mesg['stderr']
92
# 3 done
93
conftest.recv_til_done(sagews, test_id)
94
95