Skip to content

Commit 58a4ce6

Browse files
rom1504extremeheat
andauthored
Add support for node 14 (#33)
* test support in node 14 * Update version to 14 in index.js * fix test, allow for node 14 * Update node version in readme Co-authored-by: extremeheat <[email protected]>
1 parent 046152d commit 58a4ce6

File tree

9 files changed

+120
-120
lines changed

9 files changed

+120
-120
lines changed

.github/workflows/node.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
fail-fast: false
1616
matrix:
1717
os: [ubuntu-latest, windows-latest, macos-latest]
18-
node-version: [16.x]
18+
node-version: [14.x, 16.x]
1919
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
2020
runs-on: ${{ matrix.os }}
2121

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Interoperate Node.js and Python. You can run Python from Node.js, *or* run Node.js from Python. **Work in progress.**
1010

11-
Requires Node.js 16 and Python 3.8 or newer.
11+
Requires Node.js 14 and Python 3.8 or newer.
1212

1313
## Key Features
1414

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
"devDependencies": {
1818
"babel-eslint": "^10.1.0",
1919
"browserify": "^17.0.0",
20-
"mocha": "latest",
20+
"mocha": "^9.1.3",
2121
"pythonia": "file:.",
22-
"standard": "^16.0.3"
22+
"standard": "^16.0.4"
2323
},
2424
"dependencies": {
2525
"caller": "^1.0.1",

src/javascript/__main__.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os, sys, argparse, shutil
22

3+
PACKAGEJSON = '{\n\t"name": "js-modules",\n\t"description": "This folder holds the installed JS deps",\n\t"dependencies": {}\n}'
4+
35
parser = argparse.ArgumentParser(
46
description="javascript (JSPyBridge) package manager. Use this to clear or update the internal package store."
57
)
@@ -32,6 +34,9 @@
3234
os.system("npm update")
3335
elif args.install:
3436
os.chdir(os.path.dirname(__file__) + "/js")
37+
if not os.path.exists("package.json"):
38+
with open("package.json", "w") as f:
39+
f.write(PACKAGEJSON)
3540
os.system(f"npm install {args.install}")
3641
else:
3742
parser.print_help(sys.stderr)

src/javascript/js/errors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function processJSStacktrace (stack, allowInternal) {
7474
const path = absPath || filePath || barePath
7575
if (path) {
7676
const [fpath, errline, char] = path.slice(1)
77-
if (fpath.startsWith('node:')) continue
77+
if (fpath.startsWith('node:') || fpath.startsWith('internal/')) continue
7878
const file = fs.readFileSync(fpath.startsWith('file:') ? new URL(fpath) : fpath, 'utf-8')
7979
const flines = file.split('\n')
8080
jsErrorline = flines[errline - 1]

src/pythonia/errors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function processJSStacktrace (stack, allowInternal) {
6464
const path = absPath || filePath || barePath
6565
if (path) {
6666
const [fpath, errline, char] = path.slice(1)
67-
if (fpath.startsWith('node:')) continue
67+
if (fpath.startsWith('node:') || fpath.startsWith('internal/')) continue
6868
const file = fs.readFileSync(fpath.startsWith('file:') ? new URL(fpath) : fpath, 'utf-8')
6969
const flines = file.split('\n')
7070
jsErrorline = flines[errline - 1]

src/pythonia/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
if (typeof process !== 'undefined' && parseInt(process.versions.node.split('.')[0]) < 16) {
1+
if (typeof process !== 'undefined' && parseInt(process.versions.node.split('.')[0]) < 14) {
22
console.error('Your node version is currently', process.versions.node)
3-
console.error('Please update it to a version >= 16.x.x from https://nodejs.org/')
3+
console.error('Please update it to a version >= 14.x.x from https://nodejs.org/')
44
process.exit(1)
55
}
66

test/javascript.test.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ const cp = require('child_process')
44
const fs = require('fs')
55
const d = __dirname
66

7-
it('runs test', function () {
8-
this.timeout(5000)
9-
const ret = cp.execSync(`python3 ${d}/javascript/test.py`)
10-
console.warn(JSON.stringify(String(ret)), '\n')
11-
const actual = String(ret).replace(/\r/g, '')
12-
const expect = "Hello world!\n5 [ [], 5, 6, 7, 8, { a: 3 } ] []\ncallback from JS (Some Python object) 3\nwrapped fn 3\n6\nwow\n123!\nHello world\ni []\ni 5\ni 6\ni 7\ni 8\ni { a: 3 }\nCallback called with ('It works !',)\nHandler caled [Function (anonymous)] 4 {'a': {'y': 5}}\nHey, I'm only called once !\nHandler caled [Function (anonymous)] 5 {'a': {'y': 6}}\nHandler caled [Function (anonymous)] 6 {'a': {'y': 7}}\nHandler caled [Function (anonymous)] 7 {'a': {'y': 8}}\nDemo array and object [ 1, 5, 3 ] { '1': 5, '2': (Some Python object), a: 'ay', b: 'be', c: 'ce' }\nOK, captured error\nArray [1, 5, 3]\n2\nMy var {'var': 5, 0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}\nOK, we can now exit\n".replace(/\r/g, '')
13-
assert.strictEqual(actual, expect)
14-
assert(fs.existsSync('./HelloWorld.txt'))
15-
fs.unlinkSync('./HelloWorld.txt')
16-
})
7+
describe('Bridge from Python', function () {
8+
it('runs test', function () {
9+
this.timeout(5000)
10+
const ret = cp.execSync(`python3 ${d}/javascript/test.py`)
11+
console.warn(JSON.stringify(String(ret)), '\n')
12+
const actual = String(ret).replace(/\r/g, '')
13+
const expect = "Hello world!\n5 [ [], 5, 6, 7, 8, { a: 3 } ] []\ncallback from JS (Some Python object) 3\nwrapped fn 3\n6\nwow\n123!\nHello world\ni []\ni 5\ni 6\ni 7\ni 8\ni { a: 3 }\nCallback called with ('It works !',)\nHandler caled [Function (anonymous)] 4 {'a': {'y': 5}}\nHey, I'm only called once !\nHandler caled [Function (anonymous)] 5 {'a': {'y': 6}}\nHandler caled [Function (anonymous)] 6 {'a': {'y': 7}}\nHandler caled [Function (anonymous)] 7 {'a': {'y': 8}}\nDemo array and object [ 1, 5, 3 ] { '1': 5, '2': (Some Python object), a: 'ay', b: 'be', c: 'ce' }\nOK, captured error\nArray [1, 5, 3]\n2\nMy var {'var': 5, 0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}\nOK, we can now exit\n".replace(/\r/g, '')
14+
assert.strictEqual(actual, expect)
15+
assert(fs.existsSync('./HelloWorld.txt'))
16+
fs.unlinkSync('./HelloWorld.txt')
17+
})
1718

18-
it('runs general test', function () {
19-
// Just make sure it passes without a bad exit code
20-
cp.execSync(`python3 ${d}/javascript/test_general.py`, { stdio: 'inherit' })
19+
it('runs general test', function () {
20+
// Just make sure it passes without a bad exit code
21+
cp.execSync(`python3 ${d}/javascript/test_general.py`, { stdio: 'inherit' })
22+
})
2123
})

test/pythonia.test.mjs

+92-99
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,101 @@
1-
// process.env.DEBUG = 1
1+
/* eslint-env mocha */
22
import { py, python, PyClass } from 'pythonia'
33
import assert from 'assert'
44
const f = await python('./pythonia/pyImp.py')
55
const demo = await python('./pythonia/demo.py')
66

7-
// async function it (what, fn) {
8-
// console.log('it', what)
9-
// await fn()
10-
// }
11-
12-
await it('does function calls', async function () {
13-
console.log('ok', await f.add_inverse)
14-
assert.strictEqual(await f.add_inverse(3, 2), -5)
15-
const complex = await f.complex_num()
16-
console.log('complex', complex)
17-
console.log('real & complex', await complex.real, await complex.imag)
18-
console.log('FABC - this will fail', f.a.b.c)
19-
})
20-
21-
await it('declares classes', async function () {
22-
class MyClas extends PyClass {
23-
constructor () {
24-
super(demo.DemoClass, [4])
7+
describe('Bridge from Node.js', function () {
8+
it('does function calls', async function () {
9+
console.log('ok', await f.add_inverse)
10+
assert.strictEqual(await f.add_inverse(3, 2), -5)
11+
const complex = await f.complex_num()
12+
console.log('complex', complex)
13+
console.log('real & complex', await complex.real, await complex.imag)
14+
console.log('FABC - this will fail', f.a.b.c)
15+
})
16+
17+
it('declares classes', async function () {
18+
class MyClas extends PyClass {
19+
constructor () {
20+
super(demo.DemoClass, [4])
21+
}
22+
23+
someMethod () {
24+
return 3
25+
}
2526
}
2627

27-
someMethod () {
28-
return 3
28+
await f.some_event(async (message, method) => {
29+
// Call a Python function passed as a paramater
30+
assert.strictEqual(message, 'from python')
31+
assert.strictEqual(await method(), 3)
32+
}, await MyClas.init())
33+
})
34+
35+
it('consumes classes', async function () {
36+
const { DemoClass, add } = demo
37+
const demoInst = await DemoClass(3)
38+
assert.ok(demoInst)
39+
console.log(demoInst)
40+
})
41+
42+
it('catches errors', async function () {
43+
try {
44+
await demo.throw()
45+
} catch (e) {
46+
console.log('OK!')
2947
}
30-
}
31-
32-
await f.some_event(async (message, method) => {
33-
// Call a Python function passed as a paramater
34-
assert.strictEqual(message, 'from python')
35-
assert.strictEqual(await method(), 3)
36-
// console.log('Message', message, await method())
37-
}, await MyClas.init())
38-
})
39-
40-
await it('consumes classes', async function () {
41-
const { DemoClass, add } = demo
42-
const demoInst = await DemoClass(3)
43-
assert.ok(demoInst)
44-
console.log(demoInst)
45-
})
46-
47-
await it('catches errors', async function () {
48-
try {
49-
await demo.throw()
50-
} catch (e) {
51-
console.log('OK!')
52-
}
53-
})
54-
55-
await it('calls functions with special args', async function () {
56-
await demo.special$(1, 2, { kwarg1: 3, extra: 77, xx: Math.random() })
57-
})
58-
59-
await it('can add Python numbers', async function () {
60-
const num = py`3j`
61-
const num2 = py`2j`
62-
console.log('3 + 3', await py`3+3 + ${num} + ${num2}`)
63-
})
64-
65-
await it('can set variables', async function () {
66-
f.x[2] = 4
67-
console.log(await f.x)
68-
f.y.b = 'meow'
69-
console.log(await f.y)
70-
f.a.prop = 44
71-
assert.strictEqual(await f.a.prop, 44)
72-
// console.log(await f.a.prop)
73-
})
74-
75-
await it('can return primitive values', async function () {
76-
const arr = await f.x.valueOf()
77-
console.log(arr, typeof arr)
78-
assert.strictEqual(arr.toString(), '1,2,4')
79-
})
80-
81-
await it('can iterate asynchronously', async function () {
82-
const array = await f.x
83-
let v = 0
84-
for await (const entry of array) {
85-
console.log(entry)
86-
v += entry
87-
}
88-
assert.strictEqual(v, 7)
89-
})
90-
91-
await it('can iterate from Python', async function () {
92-
const a = await f.iter({ x: 1, y: 2, z: 3 })
93-
const b = await f.iter([1, 2, 3])
94-
assert.deepEqual(await a.valueOf(), ['x', 'y', 'z'])
95-
assert.deepEqual(await b.valueOf(), [1, 2, 3])
96-
})
97-
98-
await it('can recieve big numbers', async function () {
99-
const bigNumber = await py`2**63`
100-
console.log(bigNumber)
101-
assert.ok(bigNumber > 2 ** 60)
102-
})
103-
104-
// process.exit(0)
105-
106-
after(() => {
107-
python.exit()
48+
})
49+
50+
it('calls functions with special args', async function () {
51+
await demo.special$(1, 2, { kwarg1: 3, extra: 77, xx: Math.random() })
52+
})
53+
54+
it('can add Python numbers', async function () {
55+
const num = py`3j`
56+
const num2 = py`2j`
57+
console.log('3 + 3', await py`3+3 + ${num} + ${num2}`)
58+
})
59+
60+
it('can set variables', async function () {
61+
f.x[2] = 4
62+
console.log(await f.x)
63+
f.y.b = 'meow'
64+
console.log(await f.y)
65+
f.a.prop = 44
66+
assert.strictEqual(await f.a.prop, 44)
67+
})
68+
69+
it('can return primitive values', async function () {
70+
const arr = await f.x.valueOf()
71+
console.log(arr, typeof arr)
72+
assert.strictEqual(arr.toString(), '1,2,4')
73+
})
74+
75+
it('can iterate asynchronously', async function () {
76+
const array = await f.x
77+
let v = 0
78+
for await (const entry of array) {
79+
console.log(entry)
80+
v += entry
81+
}
82+
assert.strictEqual(v, 7)
83+
})
84+
85+
it('can iterate from Python', async function () {
86+
const a = await f.iter({ x: 1, y: 2, z: 3 })
87+
const b = await f.iter([1, 2, 3])
88+
assert.deepEqual(await a.valueOf(), ['x', 'y', 'z'])
89+
assert.deepEqual(await b.valueOf(), [1, 2, 3])
90+
})
91+
92+
it('can recieve big numbers', async function () {
93+
const bigNumber = await py`2**63`
94+
console.log(bigNumber)
95+
assert.ok(bigNumber > 2 ** 60)
96+
})
97+
98+
after(() => {
99+
python.exit()
100+
})
108101
})

0 commit comments

Comments
 (0)