Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Unicode characters in paths #2254

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion gyp/pylib/gyp/easy_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ def WriteXmlIfChanged(content, path, encoding="utf-8", pretty=False,

default_encoding = locale.getdefaultlocale()[1]
if default_encoding and default_encoding.upper() != encoding.upper():
xml_string = xml_string.encode(encoding)
if sys.platform == "win32":
if isinstance(xml_string, str):
xml_string = xml_string.decode("cp1251") # str --> bytes
xml_string = xml_string.encode(encoding) # bytes --> str

# Get the old content
try:
Expand Down
4 changes: 4 additions & 0 deletions lib/find-python-script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import sys
if sys.stdout.encoding != "utf-8" and sys.platform == "win32":
sys.stdout.reconfigure(encoding='utf-8')
print(sys.executable)
5 changes: 3 additions & 2 deletions lib/find-python.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const path = require('path')
const log = require('npmlog')
const semver = require('semver')
const cp = require('child_process')
Expand Down Expand Up @@ -47,7 +48,7 @@ function PythonFinder (configPython, callback) {

PythonFinder.prototype = {
log: logWithPrefix(log, 'find Python'),
argsExecutable: ['-c', 'import sys; print(sys.executable);'],
argsExecutable: [path.resolve(__dirname, 'find-python-script.py')],
argsVersion: ['-c', 'import sys; print("%s.%s.%s" % sys.version_info[:3]);'],
semverRange: '>=3.6.0',

Expand Down Expand Up @@ -274,7 +275,7 @@ PythonFinder.prototype = {
run: function run (exec, args, shell, callback) {
var env = extend({}, this.env)
env.TERM = 'dumb'
const opts = { env: env, shell: shell }
const opts = { env: env, shell: shell, encoding: 'utf8' }

this.log.silly('execFile: exec = %j', exec)
this.log.silly('execFile: args = %j', args)
Expand Down
20 changes: 20 additions & 0 deletions test/rm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const fs = require('fs')
const path = require('path')

/** recursively delete files, symlinks (without following them) and dirs */
module.exports = function rmRecSync (pth) {
pth = path.normalize(pth)

rm(pth)

function rm (pth) {
const pathStat = fs.statSync(pth)
// trick with lstat is used to avoid following symlinks (especially junctions on windows)
if (pathStat.isDirectory() && !fs.lstatSync(pth).isSymbolicLink()) {
fs.readdirSync(pth).forEach((nextPath) => rm(path.join(pth, nextPath)))
fs.rmdirSync(pth)
} else {
fs.unlinkSync(pth)
}
}
}
84 changes: 84 additions & 0 deletions test/test-find-python-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// @ts-check
'use strict'
/** @typedef {import("tap")} Tap */

const test = require('tap').test
const execFile = require('child_process').execFile
const path = require('path')

require('npmlog').level = 'warn'

//* name can be used as short descriptions

/**
* @typedef Check
* @property {string} path - path to executable or command
* @property {string} name - very little description
*/

// TODO: add symlinks to python which will contain utf-8 chars
/**
* @type {Check[]}
*/
const checks = [
{ path: process.env.PYTHON, name: 'env var PYTHON' },
{ path: 'python3', name: 'python3 in PATH' },
{ path: 'python', name: 'python in PATH' }
]
const args = [path.resolve('./lib/find-python-script.py')]
const options = {
windowsHide: true
}

/**
Getting output from find-python-script.py,
compare it to path provided to terminal.
If equals - test pass

runs for all checks

@private
@argument {Error} err - exec error
@argument {string} stdout - stdout buffer of child process
@argument {string} stderr
@this {{t: Tap, exec: Check}}
*/
function check (err, stdout, stderr) {
const { t, exec } = this
if (!err && !stderr) {
t.ok(
stdout.trim(),
`${exec.name}: check path ${exec.path} equals ${stdout.trim()}`
)
} else {
// @ts-ignore
if (err.code === 9009 || err.code === 'ENOENT') {
t.skip(`skipped: ${exec.name} file not found`)
} else {
t.skip(`error: ${err}\n\nstderr: ${stderr}`)
}
}
}

test('find-python-script', { buffered: false }, (t) => {
t.plan(checks.length)

// ? may be more elegant way to pass context
// context for check functions
const ctx = {
t: t,
exec: {}
}

for (const exec of checks) {
// checking if env var exist
if (!(exec.path === undefined || exec.path === null)) {
ctx.exec = exec
// passing ctx as copied object to make properties immutable from here
const boundedCheck = check.bind(Object.assign({}, ctx))
execFile(exec.path, args, options, boundedCheck)
} else {
t.skip(`skipped: ${exec.name} doesn't exist or unavailable`)
}
}
})