Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pyodide
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
pyodide
Commits
8babceb7
Commit
8babceb7
authored
Nov 14, 2018
by
Michael Droettboom
Committed by
GitHub
Nov 14, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #257 from mdboom/python2js-refactor
Refactor python2js
parents
f6b9a35d
ea75b225
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
189 additions
and
160 deletions
+189
-160
src/python2js.c
src/python2js.c
+189
-160
No files found.
src/python2js.c
View file @
8babceb7
...
@@ -6,6 +6,11 @@
...
@@ -6,6 +6,11 @@
#include "jsproxy.h"
#include "jsproxy.h"
#include "pyproxy.h"
#include "pyproxy.h"
static
PyObject
*
tbmod
=
NULL
;
static
int
_python2js_unicode
(
PyObject
*
x
);
int
int
pythonexc2js
()
pythonexc2js
()
{
{
...
@@ -27,65 +32,61 @@ pythonexc2js()
...
@@ -27,65 +32,61 @@ pythonexc2js()
goto
exit
;
goto
exit
;
}
}
PyObject
*
tbmod
=
PyImport_ImportModule
(
"traceback"
);
if
(
tbmod
==
NULL
)
{
if
(
tbmod
==
NULL
)
{
PyObject
*
repr
=
PyObject_Repr
(
value
);
tbmod
=
PyImport_ImportModule
(
"traceback"
);
if
(
repr
==
NULL
)
{
if
(
tbmod
==
NULL
)
{
excval
=
hiwire_string_ascii
((
int
)
"Could not get repr for exception"
);
PyObject
*
repr
=
PyObject_Repr
(
value
);
}
else
{
if
(
repr
==
NULL
)
{
excval
=
python2js
(
repr
);
excval
=
hiwire_string_ascii
((
int
)
"Could not get repr for exception"
);
Py_DECREF
(
repr
);
}
else
{
excval
=
_python2js_unicode
(
repr
);
Py_DECREF
(
repr
);
}
goto
exit
;
}
}
}
PyObject
*
format_exception
;
if
(
traceback
==
NULL
||
traceback
==
Py_None
)
{
no_traceback
=
1
;
format_exception
=
PyObject_GetAttrString
(
tbmod
,
"format_exception_only"
);
}
else
{
format_exception
=
PyObject_GetAttrString
(
tbmod
,
"format_exception"
);
}
if
(
format_exception
==
NULL
)
{
excval
=
hiwire_string_ascii
((
int
)
"Could not get format_exception function"
);
}
else
{
}
else
{
PyObject
*
format_exception
;
PyObject
*
pylines
;
if
(
traceback
==
NULL
||
traceback
==
Py_None
)
{
if
(
no_traceback
)
{
no_traceback
=
1
;
pylines
=
format_exception
=
PyObject_GetAttrString
(
tbmod
,
"format_exception_only"
);
PyObject_CallFunctionObjArgs
(
format_exception
,
type
,
value
,
NULL
);
}
else
{
}
else
{
format_exception
=
PyObject_GetAttrString
(
tbmod
,
"format_exception"
);
pylines
=
PyObject_CallFunctionObjArgs
(
format_exception
,
type
,
value
,
traceback
,
NULL
);
}
}
if
(
format_exception
==
NULL
)
{
if
(
pylines
==
NULL
)
{
excval
=
excval
=
hiwire_string_ascii
((
int
)
"Could not get format_exception function"
);
hiwire_string_ascii
((
int
)
"Error calling traceback.format_exception"
);
PyErr_Print
();
PyErr_Clear
();
goto
exit
;
}
else
{
}
else
{
PyObject
*
pylines
;
PyObject
*
empty
=
PyUnicode_FromString
(
""
);
if
(
no_traceback
)
{
PyObject
*
pystr
=
PyUnicode_Join
(
empty
,
pylines
);
pylines
=
printf
(
"Python exception:
\n
"
);
PyObject_CallFunctionObjArgs
(
format_exception
,
type
,
value
,
NULL
);
printf
(
"%s
\n
"
,
PyUnicode_AsUTF8
(
pystr
));
}
else
{
excval
=
_python2js_unicode
(
pystr
);
pylines
=
PyObject_CallFunctionObjArgs
(
Py_DECREF
(
pystr
);
format_exception
,
type
,
value
,
traceback
,
NULL
);
Py_DECREF
(
empty
);
}
Py_DECREF
(
pylines
);
if
(
pylines
==
NULL
)
{
excval
=
hiwire_string_ascii
((
int
)
"Error calling traceback.format_exception"
);
PyErr_Print
();
PyErr_Clear
();
goto
exit
;
}
else
{
PyObject
*
newline
=
PyUnicode_FromString
(
""
);
PyObject
*
pystr
=
PyUnicode_Join
(
newline
,
pylines
);
printf
(
"Python exception:
\n
"
);
printf
(
"%s
\n
"
,
PyUnicode_AsUTF8
(
pystr
));
excval
=
python2js
(
pystr
);
Py_DECREF
(
pystr
);
Py_DECREF
(
newline
);
Py_DECREF
(
pylines
);
}
Py_DECREF
(
format_exception
);
}
}
Py_DECREF
(
tbmod
);
Py_DECREF
(
format_exception
);
}
}
exit:
exit:
Py_XDECREF
(
type
);
Py_XDECREF
(
value
);
Py_XDECREF
(
traceback
);
PyErr_Clear
();
PyErr_Clear
();
hiwire_throw_error
(
excval
);
hiwire_throw_error
(
excval
);
return
HW_ERROR
;
return
HW_ERROR
;
}
}
...
@@ -118,129 +119,161 @@ int
...
@@ -118,129 +119,161 @@ int
_python2js_cache
(
PyObject
*
x
,
PyObject
*
map
);
_python2js_cache
(
PyObject
*
x
,
PyObject
*
map
);
static
int
static
int
_python2js
(
PyObject
*
x
,
PyObject
*
map
)
_python2js
_float
(
PyObject
*
x
)
{
{
if
(
x
==
Py_None
)
{
double
x_double
=
PyFloat_AsDouble
(
x
);
return
hiwire_undefined
();
if
(
x_double
==
-
1
.
0
&&
PyErr_Occurred
())
{
}
else
if
(
x
==
Py_True
)
{
return
HW_ERROR
;
return
hiwire_true
();
}
}
else
if
(
x
==
Py_False
)
{
return
hiwire_double
(
x_double
);
return
hiwire_false
();
}
}
else
if
(
PyLong_Check
(
x
))
{
int
overflow
;
static
int
long
x_long
=
PyLong_AsLongAndOverflow
(
x
,
&
overflow
);
_python2js_long
(
PyObject
*
x
)
if
(
x_long
==
-
1
)
{
{
if
(
overflow
)
{
int
overflow
;
PyObject
*
py_float
=
PyNumber_Float
(
x
);
long
x_long
=
PyLong_AsLongAndOverflow
(
x
,
&
overflow
);
if
(
py_float
==
NULL
)
{
if
(
x_long
==
-
1
)
{
return
HW_ERROR
;
if
(
overflow
)
{
}
PyObject
*
py_float
=
PyNumber_Float
(
x
);
double
x_double
=
PyFloat_AsDouble
(
py_float
);
if
(
py_float
==
NULL
)
{
Py_DECREF
(
py_float
);
if
(
x_double
==
-
1
.
0
&&
PyErr_Occurred
())
{
return
HW_ERROR
;
}
return
hiwire_double
(
x_double
);
}
else
if
(
PyErr_Occurred
())
{
return
HW_ERROR
;
return
HW_ERROR
;
}
}
}
return
_python2js_float
(
py_float
);
return
hiwire_int
(
x_long
);
}
else
if
(
PyErr_Occurred
())
{
}
else
if
(
PyFloat_Check
(
x
))
{
double
x_double
=
PyFloat_AsDouble
(
x
);
if
(
x_double
==
-
1
.
0
&&
PyErr_Occurred
())
{
return
HW_ERROR
;
return
HW_ERROR
;
}
}
return
hiwire_double
(
x_double
);
}
}
else
if
(
PyUnicode_Check
(
x
))
{
return
hiwire_int
(
x_long
);
int
kind
=
PyUnicode_KIND
(
x
);
}
int
data
=
(
int
)
PyUnicode_DATA
(
x
);
int
length
=
(
int
)
PyUnicode_GET_LENGTH
(
x
);
static
int
switch
(
kind
)
{
_python2js_unicode
(
PyObject
*
x
)
case
PyUnicode_1BYTE_KIND
:
{
return
hiwire_string_ucs1
(
data
,
length
);
int
kind
=
PyUnicode_KIND
(
x
);
case
PyUnicode_2BYTE_KIND
:
int
data
=
(
int
)
PyUnicode_DATA
(
x
);
return
hiwire_string_ucs2
(
data
,
length
);
int
length
=
(
int
)
PyUnicode_GET_LENGTH
(
x
);
case
PyUnicode_4BYTE_KIND
:
switch
(
kind
)
{
return
hiwire_string_ucs4
(
data
,
length
);
case
PyUnicode_1BYTE_KIND
:
default:
return
hiwire_string_ucs1
(
data
,
length
);
PyErr_SetString
(
PyExc_ValueError
,
"Unknown Unicode KIND"
);
case
PyUnicode_2BYTE_KIND
:
return
HW_ERROR
;
return
hiwire_string_ucs2
(
data
,
length
);
}
case
PyUnicode_4BYTE_KIND
:
}
else
if
(
PyBytes_Check
(
x
))
{
return
hiwire_string_ucs4
(
data
,
length
);
char
*
x_buff
;
default:
Py_ssize_t
length
;
PyErr_SetString
(
PyExc_ValueError
,
"Unknown Unicode KIND"
);
if
(
PyBytes_AsStringAndSize
(
x
,
&
x_buff
,
&
length
))
{
return
HW_ERROR
;
return
HW_ERROR
;
}
}
return
hiwire_bytes
((
int
)(
void
*
)
x_buff
,
length
);
}
}
else
if
(
JsProxy_Check
(
x
))
{
return
JsProxy_AsJs
(
x
);
static
int
}
else
if
(
PyList_Check
(
x
)
||
is_type_name
(
x
,
"<class 'numpy.ndarray'>"
))
{
_python2js_bytes
(
PyObject
*
x
)
int
jsarray
=
hiwire_array
();
{
if
(
_python2js_add_to_cache
(
map
,
x
,
jsarray
))
{
char
*
x_buff
;
Py_ssize_t
length
;
if
(
PyBytes_AsStringAndSize
(
x
,
&
x_buff
,
&
length
))
{
return
HW_ERROR
;
}
return
hiwire_bytes
((
int
)(
void
*
)
x_buff
,
length
);
}
static
int
_python2js_sequence
(
PyObject
*
x
,
PyObject
*
map
)
{
int
jsarray
=
hiwire_array
();
if
(
_python2js_add_to_cache
(
map
,
x
,
jsarray
))
{
hiwire_decref
(
jsarray
);
return
HW_ERROR
;
}
size_t
length
=
PySequence_Size
(
x
);
for
(
size_t
i
=
0
;
i
<
length
;
++
i
)
{
PyObject
*
pyitem
=
PySequence_GetItem
(
x
,
i
);
if
(
pyitem
==
NULL
)
{
// If something goes wrong converting the sequence (as is the case with
// Pandas data frames), fallback to the Python object proxy
_python2js_remove_from_cache
(
map
,
x
);
hiwire_decref
(
jsarray
);
hiwire_decref
(
jsarray
);
return
HW_ERROR
;
PyErr_Clear
();
Py_INCREF
(
x
);
return
pyproxy_new
((
int
)
x
);
}
}
size_t
length
=
PySequence_Size
(
x
);
int
jsitem
=
_python2js_cache
(
pyitem
,
map
);
for
(
size_t
i
=
0
;
i
<
length
;
++
i
)
{
if
(
jsitem
==
HW_ERROR
)
{
PyObject
*
pyitem
=
PySequence_GetItem
(
x
,
i
);
_python2js_remove_from_cache
(
map
,
x
);
if
(
pyitem
==
NULL
)
{
// If something goes wrong converting the sequence (as is the case with
// Pandas data frames), fallback to the Python object proxy
_python2js_remove_from_cache
(
map
,
x
);
hiwire_decref
(
jsarray
);
PyErr_Clear
();
Py_INCREF
(
x
);
return
pyproxy_new
((
int
)
x
);
}
int
jsitem
=
_python2js_cache
(
pyitem
,
map
);
if
(
jsitem
==
HW_ERROR
)
{
_python2js_remove_from_cache
(
map
,
x
);
Py_DECREF
(
pyitem
);
hiwire_decref
(
jsarray
);
return
HW_ERROR
;
}
Py_DECREF
(
pyitem
);
Py_DECREF
(
pyitem
);
hiwire_push_array
(
jsarray
,
jsitem
);
hiwire_decref
(
jsitem
);
}
if
(
_python2js_remove_from_cache
(
map
,
x
))
{
hiwire_decref
(
jsarray
);
hiwire_decref
(
jsarray
);
return
HW_ERROR
;
return
HW_ERROR
;
}
}
return
jsarray
;
Py_DECREF
(
pyitem
);
}
else
if
(
PyDict_Check
(
x
))
{
hiwire_push_array
(
jsarray
,
jsitem
);
int
jsdict
=
hiwire_object
();
hiwire_decref
(
jsitem
);
if
(
_python2js_add_to_cache
(
map
,
x
,
jsdict
))
{
}
if
(
_python2js_remove_from_cache
(
map
,
x
))
{
hiwire_decref
(
jsarray
);
return
HW_ERROR
;
}
return
jsarray
;
}
static
int
_python2js_dict
(
PyObject
*
x
,
PyObject
*
map
)
{
int
jsdict
=
hiwire_object
();
if
(
_python2js_add_to_cache
(
map
,
x
,
jsdict
))
{
hiwire_decref
(
jsdict
);
return
HW_ERROR
;
}
PyObject
*
pykey
,
*
pyval
;
Py_ssize_t
pos
=
0
;
while
(
PyDict_Next
(
x
,
&
pos
,
&
pykey
,
&
pyval
))
{
int
jskey
=
_python2js_cache
(
pykey
,
map
);
if
(
jskey
==
HW_ERROR
)
{
_python2js_remove_from_cache
(
map
,
x
);
hiwire_decref
(
jsdict
);
hiwire_decref
(
jsdict
);
return
HW_ERROR
;
return
HW_ERROR
;
}
}
PyObject
*
pykey
,
*
pyval
;
int
jsval
=
_python2js_cache
(
pyval
,
map
);
Py_ssize_t
pos
=
0
;
if
(
jsval
==
HW_ERROR
)
{
while
(
PyDict_Next
(
x
,
&
pos
,
&
pykey
,
&
pyval
))
{
_python2js_remove_from_cache
(
map
,
x
);
int
jskey
=
_python2js_cache
(
pykey
,
map
);
if
(
jskey
==
HW_ERROR
)
{
_python2js_remove_from_cache
(
map
,
x
);
hiwire_decref
(
jsdict
);
return
HW_ERROR
;
}
int
jsval
=
_python2js_cache
(
pyval
,
map
);
if
(
jsval
==
HW_ERROR
)
{
_python2js_remove_from_cache
(
map
,
x
);
hiwire_decref
(
jskey
);
hiwire_decref
(
jsdict
);
return
HW_ERROR
;
}
hiwire_push_object_pair
(
jsdict
,
jskey
,
jsval
);
hiwire_decref
(
jskey
);
hiwire_decref
(
jskey
);
hiwire_decref
(
jsval
);
}
if
(
_python2js_remove_from_cache
(
map
,
x
))
{
hiwire_decref
(
jsdict
);
hiwire_decref
(
jsdict
);
return
HW_ERROR
;
return
HW_ERROR
;
}
}
return
jsdict
;
hiwire_push_object_pair
(
jsdict
,
jskey
,
jsval
);
hiwire_decref
(
jskey
);
hiwire_decref
(
jsval
);
}
if
(
_python2js_remove_from_cache
(
map
,
x
))
{
hiwire_decref
(
jsdict
);
return
HW_ERROR
;
}
return
jsdict
;
}
static
int
_python2js
(
PyObject
*
x
,
PyObject
*
map
)
{
if
(
x
==
Py_None
)
{
return
hiwire_undefined
();
}
else
if
(
x
==
Py_True
)
{
return
hiwire_true
();
}
else
if
(
x
==
Py_False
)
{
return
hiwire_false
();
}
else
if
(
PyLong_Check
(
x
))
{
return
_python2js_long
(
x
);
}
else
if
(
PyFloat_Check
(
x
))
{
return
_python2js_float
(
x
);
}
else
if
(
PyUnicode_Check
(
x
))
{
return
_python2js_unicode
(
x
);
}
else
if
(
PyBytes_Check
(
x
))
{
return
_python2js_bytes
(
x
);
}
else
if
(
JsProxy_Check
(
x
))
{
return
JsProxy_AsJs
(
x
);
}
else
if
(
PyList_Check
(
x
)
||
PyTuple_Check
(
x
)
||
is_type_name
(
x
,
"<class 'numpy.ndarray'>"
))
{
return
_python2js_sequence
(
x
,
map
);
}
else
if
(
PyDict_Check
(
x
))
{
return
_python2js_dict
(
x
,
map
);
}
else
{
}
else
{
Py_INCREF
(
x
);
Py_INCREF
(
x
);
return
pyproxy_new
((
int
)
x
);
return
pyproxy_new
((
int
)
x
);
...
@@ -265,25 +298,21 @@ _python2js_add_to_cache(PyObject* map, PyObject* pyparent, int jsparent)
...
@@ -265,25 +298,21 @@ _python2js_add_to_cache(PyObject* map, PyObject* pyparent, int jsparent)
/* Use the pointer converted to an int so cache is by identity, not hash */
/* Use the pointer converted to an int so cache is by identity, not hash */
PyObject
*
pyparentid
=
PyLong_FromSize_t
((
size_t
)
pyparent
);
PyObject
*
pyparentid
=
PyLong_FromSize_t
((
size_t
)
pyparent
);
PyObject
*
jsparentid
=
PyLong_FromLong
(
jsparent
);
PyObject
*
jsparentid
=
PyLong_FromLong
(
jsparent
);
if
(
PyDict_SetItem
(
map
,
pyparentid
,
jsparentid
))
{
int
result
=
PyDict_SetItem
(
map
,
pyparentid
,
jsparentid
);
Py_DECREF
(
pyparentid
);
Py_DECREF
(
jsparentid
);
return
HW_ERROR
;
}
Py_DECREF
(
pyparentid
);
Py_DECREF
(
pyparentid
);
Py_DECREF
(
jsparentid
);
Py_DECREF
(
jsparentid
);
return
0
;
return
result
?
HW_ERROR
:
0
;
}
}
int
int
_python2js_remove_from_cache
(
PyObject
*
map
,
PyObject
*
pyparent
)
_python2js_remove_from_cache
(
PyObject
*
map
,
PyObject
*
pyparent
)
{
{
PyObject
*
pyparentid
=
PyLong_FromSize_t
((
size_t
)
pyparent
);
PyObject
*
pyparentid
=
PyLong_FromSize_t
((
size_t
)
pyparent
);
if
(
PyDict_DelItem
(
map
,
pyparentid
))
{
int
result
=
PyDict_DelItem
(
map
,
pyparentid
);
return
HW_ERROR
;
}
Py_DECREF
(
pyparentid
);
Py_DECREF
(
pyparentid
);
return
0
;
return
result
?
HW_ERROR
:
0
;
}
}
int
int
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment